Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add block definitions to colour field plugin #2162

Merged
merged 24 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6a9e007
feat: export functions to register some angle, colour, and multiline …
rachel-fenichel Jan 24, 2024
a5a6b6b
feat: add block definitions to colour and multiline input fields
rachel-fenichel Jan 24, 2024
777ca2d
feat: add block generators for the colour_picker block
rachel-fenichel Jan 30, 2024
932fed7
fix: use Blockly.common.defineBlocksWithJsonArray
rachel-fenichel Jan 30, 2024
9f7f940
feat: add block generators for colour_random block
rachel-fenichel Jan 31, 2024
7dc93ef
feat: add generators type and standardize exports for block files
rachel-fenichel Jan 31, 2024
4c1df53
chore: update to blockly@beta and fix types
rachel-fenichel Jan 31, 2024
9e782d2
chore: move all colour blocks to separate files and add more generato…
rachel-fenichel Jan 31, 2024
b30ef1d
feat: finish adding block code generators for colour blocks
rachel-fenichel Jan 31, 2024
4b4e7e8
fix: PR feedback
rachel-fenichel Feb 5, 2024
218fc20
fix: remove immediate registration of blocks and fields in field_colour
rachel-fenichel Feb 6, 2024
80b0c8c
chore: use named imports and numbered TODOs
rachel-fenichel Feb 6, 2024
34db058
feat: add usage information about blocks to README
rachel-fenichel Feb 6, 2024
fec3c70
chore: revert changes outside of field_colour
rachel-fenichel Feb 6, 2024
e425bc7
chore: clean up tsdoc and exports
rachel-fenichel Feb 7, 2024
5f03921
cgire: clean up README
rachel-fenichel Feb 7, 2024
5e2b57d
chore: respond to PR feedback on names and comments
rachel-fenichel Feb 7, 2024
4f3d42c
feat(tests): add and improve tests
rachel-fenichel Feb 8, 2024
27294c1
chore(format): run formatter
rachel-fenichel Feb 8, 2024
f7c108f
fix: respond to PR feedback about names and file structure
rachel-fenichel Feb 9, 2024
f730619
fix: allow const variables to have UPPER_CASE names
rachel-fenichel Feb 9, 2024
60543e9
fix: respond to PR feedback
rachel-fenichel Feb 9, 2024
d87a677
chore: format
rachel-fenichel Feb 9, 2024
7080ac8
fix: line length
rachel-fenichel Feb 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions plugins/field-angle/src/field_angle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,12 +658,20 @@ export class FieldAngle extends Blockly.FieldNumber {
}
}

// Unregister legacy field_angle that was in core. Delete this once
// core Blockly no longer defines field_angle.
// If field_angle is not defined in core, this generates a console warning.
Blockly.fieldRegistry.unregister('field_angle');
/**
* Register the field and any dependencies.
*/
export function registerAngleField() {
// Unregister legacy field_angle that was in core. Delete this once
// core Blockly no longer defines field_angle.
rachel-fenichel marked this conversation as resolved.
Show resolved Hide resolved
// If field_angle is not defined in core, this generates a console warning.
Blockly.fieldRegistry.unregister('field_angle');

Blockly.fieldRegistry.register('field_angle', FieldAngle);
}

Blockly.fieldRegistry.register('field_angle', FieldAngle);
// Immediately register the field.
registerAngleField();
rachel-fenichel marked this conversation as resolved.
Show resolved Hide resolved

FieldAngle.prototype.DEFAULT_VALUE = 0;

Expand Down
138 changes: 138 additions & 0 deletions plugins/field-colour/src/blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import * as Blockly from 'blockly';
import { registerColourField } from './field_colour';
import { installColourPickerBlock } from './blocks/colourPicker';

// Re-export all parts of the definition.
export * from './blocks/colourPicker';

// TODO: Write correct types for the `generators` parameter for each block's
// `install` function.
const generators: Record<string, Blockly.Generator> {
'javascript': typeof JavaScript.javascriptGenerator
}

// Helper function to define a single block from a JSON definition.
function defineBlockFromJson(blockJsonDef : any) {
Blockly.common.defineBlocks(
Blockly.common.createBlockDefinitionsFromJsonArray([blockJsonDef]));
}

// Block for random colour.
const randomColourDef =
{
'type': 'colour_random',
'message0': '%{BKY_COLOUR_RANDOM_TITLE}',
'output': 'Colour',
'helpUrl': '%{BKY_COLOUR_RANDOM_HELPURL}',
'style': 'colour_blocks',
'tooltip': '%{BKY_COLOUR_RANDOM_TOOLTIP}',
};

// Block for composing a colour from RGB components.
const colourRgbDef =
{
'type': 'colour_rgb',
'message0':
'%{BKY_COLOUR_RGB_TITLE} %{BKY_COLOUR_RGB_RED} %1 %{BKY_COLOUR_RGB_GREEN} %2 %{BKY_COLOUR_RGB_BLUE} %3',
'args0': [
{
'type': 'input_value',
'name': 'RED',
'check': 'Number',
'align': 'RIGHT',
},
{
'type': 'input_value',
'name': 'GREEN',
'check': 'Number',
'align': 'RIGHT',
},
{
'type': 'input_value',
'name': 'BLUE',
'check': 'Number',
'align': 'RIGHT',
},
],
'output': 'Colour',
'helpUrl': '%{BKY_COLOUR_RGB_HELPURL}',
'style': 'colour_blocks',
'tooltip': '%{BKY_COLOUR_RGB_TOOLTIP}',
};

// Block for blending two colours together.
const colourBlendDef =
{
'type': 'colour_blend',
'message0':
'%{BKY_COLOUR_BLEND_TITLE} %{BKY_COLOUR_BLEND_COLOUR1} ' +
'%1 %{BKY_COLOUR_BLEND_COLOUR2} %2 %{BKY_COLOUR_BLEND_RATIO} %3',
'args0': [
{
'type': 'input_value',
'name': 'COLOUR1',
'check': 'Colour',
'align': 'RIGHT',
},
{
'type': 'input_value',
'name': 'COLOUR2',
'check': 'Colour',
'align': 'RIGHT',
},
{
'type': 'input_value',
'name': 'RATIO',
'check': 'Number',
'align': 'RIGHT',
},
],
'output': 'Colour',
'helpUrl': '%{BKY_COLOUR_BLEND_HELPURL}',
'style': 'colour_blocks',
'tooltip': '%{BKY_COLOUR_BLEND_TOOLTIP}',
};

/**
* Install the `colour_rgb` block and all of its dependencies.
*/
export function installColourRgbBlock(generators = {}) {
defineBlockFromJson(colourRgbDef);
registerColourField();
}

/**
* Install the `colour_random` block and all of its dependencies.
*/
export function installColourRandomBlock(generators = {}) {
defineBlockFromJson(randomColourDef);
registerColourField();
}

/**
* Install the `colour_blend` block and all of its dependencies.
*/
export function installColourBlendBlock(generators = {}) {
defineBlockFromJson(colourBlendDef);
registerColourField();
}

/**
* Install all of the blocks defined in this file and all of their
* dependencies.
*/
export function installAllBlocks(generators = {}) {
installColourPickerBlock(generators);
installColourRgbBlock(generators);
installColourRandomBlock(generators);
installColourBlendBlock(generators);
}

// Calling this installs blocks, which means it has side effects.
installAllBlocks();
rachel-fenichel marked this conversation as resolved.
Show resolved Hide resolved
139 changes: 139 additions & 0 deletions plugins/field-colour/src/blocks/colourPicker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import * as Blockly from 'blockly';
import * as JavaScript from 'blockly/javascript';
import * as Dart from 'blockly/dart';
import * as Lua from 'blockly/lua';
import * as PHP from 'blockly/php';
import * as Python from 'blockly/python';
import { registerColourField } from '../field_colour';


// Helper function to define a single block from a JSON definition.
function defineBlockFromJson(blockJsonDef: any) {

Check warning on line 17 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
rachel-fenichel marked this conversation as resolved.
Show resolved Hide resolved
Blockly.common.defineBlocks(
Blockly.common.createBlockDefinitionsFromJsonArray([blockJsonDef]));
}

const blockName = 'colour_picker';

// Block for colour picker.
const colourPickerDef =
{
'type': blockName,
'message0': '%1',
'args0': [
{
'type': 'field_colour',
'name': 'COLOUR',
'colour': '#ff0000',
},
],
'output': 'Colour',
'helpUrl': '%{BKY_COLOUR_PICKER_HELPURL}',
'style': 'colour_blocks',
'tooltip': '%{BKY_COLOUR_PICKER_TOOLTIP}',
'extensions': ['parent_tooltip_when_inline'],
};

/**
* Javascript generator definition.

Check failure on line 44 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Expected 1 lines after block description
* @param block
* @param generator
* @returns

Check warning on line 47 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns description
*/
export function colourPickerGenJs(
block: Blockly.Block,
generator: typeof JavaScript.javascriptGenerator,
): [string, JavaScript.Order] {
// Colour picker.
const code = generator.quote_(block.getFieldValue('COLOUR'));
return [code, JavaScript.Order.ATOMIC];
}

/**
* Dart generator definition.

Check failure on line 59 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Expected 1 lines after block description
* @param block
* @param generator
* @returns

Check warning on line 62 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns description
*/
export function colourPickerGenDart(
block: Blockly.Block,
generator: typeof Dart.dartGenerator,
): [string, Dart.Order] {
// Colour picker.
const code = generator.quote_(block.getFieldValue('COLOUR'));
return [code, Dart.Order.ATOMIC];
}

/**
* Lua generator definition.

Check failure on line 74 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Expected 1 lines after block description
* @param block
* @param generator
* @returns

Check warning on line 77 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns description
*/
export function colourPickerGenLua(
block: Blockly.Block,
generator: typeof Lua.luaGenerator,
): [string, Lua.Order] {
// Colour picker.
const code = generator.quote_(block.getFieldValue('COLOUR'));
return [code, Lua.Order.ATOMIC];
}

/**
* PHP generator definition.

Check failure on line 89 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Expected 1 lines after block description
* @param block
* @param generator
* @returns

Check warning on line 92 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns description
*/
export function colourPickerGenPhp(
block: Blockly.Block,
generator: typeof PHP.phpGenerator,
): [string, PHP.Order] {
// Colour picker.
const code = generator.quote_(block.getFieldValue('COLOUR'));
return [code, PHP.Order.ATOMIC];
}

/**
* Python generator definition.

Check failure on line 104 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Expected 1 lines after block description
* @param block
* @param generator
* @returns

Check warning on line 107 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @returns description
*/
export function colourPickerGenPython(
block: Blockly.Block,
generator: typeof Python.pythonGenerator,
): [string, Python.Order] {
// Colour picker.
const code = generator.quote_(block.getFieldValue('COLOUR'));
return [code, Python.Order.ATOMIC];
}

/**

Check warning on line 118 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "generators" declaration
* Install the `colour_picker` block and all of its dependencies.
*/
export function installColourPickerBlock(generators: any = {}) {

Check warning on line 121 in plugins/field-colour/src/blocks/colourPicker.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
defineBlockFromJson(colourPickerDef);
registerColourField();
if (generators.javascript) {
generators.javascript.forBlock[blockName] = colourPickerGenJs;
}
if (generators.dart) {
generators.dart.forBlock[blockName] = colourPickerGenDart;
}
if (generators.lua) {
generators.lua.forBlock[blockName] = colourPickerGenLua;
}
if (generators.php) {
generators.php.forBlock[blockName] = colourPickerGenPhp;
}
if (generators.python) {
generators.python.forBlock[blockName] = colourPickerGenPython;
}
}
18 changes: 13 additions & 5 deletions plugins/field-colour/src/field_colour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,12 +729,20 @@ export class FieldColour extends Blockly.Field<string> {
/** The default value for this field. */
FieldColour.prototype.DEFAULT_VALUE = '#ffffff';

// Unregister legacy field_colour that was in core. Delete this once
// core Blockly no longer defines field_colour.
// If field_colour is not defined in core, this generates a console warning.
Blockly.fieldRegistry.unregister('field_colour');
/**
* Register the field and any dependencies.
*/
export function registerColourField() {
// Unregister legacy field_colour that was in core. Delete this once
// core Blockly no longer defines field_colour.
// If field_colour is not defined in core, this generates a console warning.
Blockly.fieldRegistry.unregister('field_colour');

Blockly.fieldRegistry.register('field_colour', FieldColour);
};

Blockly.fieldRegistry.register('field_colour', FieldColour);
// Immediately register the field.
registerColourField();

/**
* CSS for colour picker.
Expand Down
2 changes: 2 additions & 0 deletions plugins/field-colour/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
*/

export * from './field_colour';

export * from './blocks';
59 changes: 59 additions & 0 deletions plugins/field-multilineinput/src/blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import * as Blockly from 'blockly';
import { registerMultilineInputField } from './field_multilineinput';

const multilineTextDef = {
'type': 'text_multiline',
'message0': '%1 %2',
'args0': [
{
'type': 'field_image',
'src':
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAARCAYAAADpP' +
'U2iAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAdhgAAHYYBXaITgQAAABh0RVh0' +
'U29mdHdhcmUAcGFpbnQubmV0IDQuMS42/U4J6AAAAP1JREFUOE+Vks0KQUEYhjm' +
'RIja4ABtZ2dm5A3t3Ia6AUm7CylYuQRaUhZSlLZJiQbFAyRnPN33y01HOW08z88' +
'73zpwzM4F3GWOCruvGIE4/rLaV+Nq1hVGMBqzhqlxgCys4wJA65xnogMHsQ5luj' +
'nYHTejBBCK2mE4abjCgMGhNxHgDFWjDSG07kdfVa2pZMf4ZyMAdWmpZMfYOsLiD' +
'MYMjlMB+K613QISRhTnITnsYg5yUd0DETmEoMlkFOeIT/A58iyK5E18BuTBfgYX' +
'fwNJv4P9/oEBerLylOnRhygmGdPpTTBZAPkde61lbQe4moWUvYUZYLfUNftIY4z' +
'wA5X2Z9AYnQrEAAAAASUVORK5CYII=',
'width': 12,
'height': 17,
'alt': '\u00B6',
},
{
'type': 'field_multilinetext',
'name': 'TEXT',
'text': '',
},
],
'output': 'String',
'style': 'text_blocks',
'helpUrl': '%{BKY_TEXT_TEXT_HELPURL}',
'tooltip': '%{BKY_TEXT_TEXT_TOOLTIP}',
'extensions': ['parent_tooltip_when_inline'],
};

// Helper function to define a single block from a JSON definition.
function defineBlockFromJson(blockJsonDef: any) {

Check warning on line 44 in plugins/field-multilineinput/src/blocks.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
Blockly.common.defineBlocks(
Blockly.common.createBlockDefinitionsFromJsonArray([blockJsonDef]));
}

/**
* Install all of the blocks defined in this file and all of their
* dependencies.
*/
export function installAllBlocks() {
defineBlockFromJson(multilineTextDef);
registerMultilineInputField();
}

// Calling this installs blocks, which means it has side effects.
installAllBlocks();
Loading
Loading