mirror of
https://github.com/google/blockly.git
synced 2026-01-04 23:50:12 +01:00
* feat(j2ts): Add support for migrating renaming imports
Convert
const {foo: bar} = require(/*...*/);
into
import {foo as bar} from /*...*/;
^^^^^^^^^^
Also fix a bug that caused relative paths to ESM in the same
directory to be missing a leading "./".
* fix(build): Fix trivial error exports for generators
The UMD wrapper was inadvertently exporting the contents of (e.g.)
the Blockly.JavaScript closure module rather than the intended
export of Blockly.JavaScript.all module - which went unnoticed
because the latter just reexported the former - but we are
about to convert the former to ESM.
* chore(generators): Migrate language generators to ESM
Migrate the main language generators in generators/*.js to ESM.
This was done by running js2ts on the files, renaming them back
to .js, and commenting out "import type" statements, which are
legal TS but not needed in JS (at least if you are not actually
letting Closure Compiler do type checking, which we are not.)
* chore(generators): Migrate block generators to ESM
Migrate generators/*/*.js (except all.js) to ESM.
This was done by running js2ts on the files, renaming them back
to .js, and removing now-spurious @suppress {extraRequire}
directives.
* chores(generators): Migrate generator chunk entrypoints to ESM
This was done by running js2ts on the files, renaming them back
to .js, and manually fixing the export statements.
An additional change to the chunk exports configuration in
build_tasks.js was necessary in order for the UMD wrapper to
find the new module object, which is given a different name
than the old exports object.
68 lines
2.4 KiB
JavaScript
68 lines
2.4 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2012 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Generating Python for colour blocks.
|
|
*/
|
|
|
|
import * as goog from '../../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.Python.colour');
|
|
|
|
import {pythonGenerator as Python} from '../python.js';
|
|
|
|
|
|
Python['colour_picker'] = function(block) {
|
|
// Colour picker.
|
|
const code = Python.quote_(block.getFieldValue('COLOUR'));
|
|
return [code, Python.ORDER_ATOMIC];
|
|
};
|
|
|
|
Python['colour_random'] = function(block) {
|
|
// Generate a random colour.
|
|
Python.definitions_['import_random'] = 'import random';
|
|
const code = '\'#%06x\' % random.randint(0, 2**24 - 1)';
|
|
return [code, Python.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Python['colour_rgb'] = function(block) {
|
|
// Compose a colour from RGB components expressed as percentages.
|
|
const functionName = Python.provideFunction_('colour_rgb', `
|
|
def ${Python.FUNCTION_NAME_PLACEHOLDER_}(r, g, b):
|
|
r = round(min(100, max(0, r)) * 2.55)
|
|
g = round(min(100, max(0, g)) * 2.55)
|
|
b = round(min(100, max(0, b)) * 2.55)
|
|
return '#%02x%02x%02x' % (r, g, b)
|
|
`);
|
|
const r = Python.valueToCode(block, 'RED', Python.ORDER_NONE) || 0;
|
|
const g = Python.valueToCode(block, 'GREEN', Python.ORDER_NONE) || 0;
|
|
const b = Python.valueToCode(block, 'BLUE', Python.ORDER_NONE) || 0;
|
|
const code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
|
|
return [code, Python.ORDER_FUNCTION_CALL];
|
|
};
|
|
|
|
Python['colour_blend'] = function(block) {
|
|
// Blend two colours together.
|
|
const functionName = Python.provideFunction_('colour_blend', `
|
|
def ${Python.FUNCTION_NAME_PLACEHOLDER_}(colour1, colour2, ratio):
|
|
r1, r2 = int(colour1[1:3], 16), int(colour2[1:3], 16)
|
|
g1, g2 = int(colour1[3:5], 16), int(colour2[3:5], 16)
|
|
b1, b2 = int(colour1[5:7], 16), int(colour2[5:7], 16)
|
|
ratio = min(1, max(0, ratio))
|
|
r = round(r1 * (1 - ratio) + r2 * ratio)
|
|
g = round(g1 * (1 - ratio) + g2 * ratio)
|
|
b = round(b1 * (1 - ratio) + b2 * ratio)
|
|
return '#%02x%02x%02x' % (r, g, b)
|
|
`);
|
|
const colour1 =
|
|
Python.valueToCode(block, 'COLOUR1', Python.ORDER_NONE) || '\'#000000\'';
|
|
const colour2 =
|
|
Python.valueToCode(block, 'COLOUR2', Python.ORDER_NONE) || '\'#000000\'';
|
|
const ratio = Python.valueToCode(block, 'RATIO', Python.ORDER_NONE) || 0;
|
|
const code =
|
|
functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
|
|
return [code, Python.ORDER_FUNCTION_CALL];
|
|
};
|