Skip to content

Commit

Permalink
feature(blocks): Export block definitions (#5908)
Browse files Browse the repository at this point in the history
* refactor: Provide a BlockDefinition type

* refactor: Split defineBlocksWithJsonArray

  Split defineBlocksWithJsonArray into:

  - createBlockDefinitionsFromJsonArray, which creates BlockDefinitions
    from a (possibly JSON-originated) POJsO array, having no side-effects
    except possibly issuing warnings to the console.
  - defineBlocks, which add any dictionary of BlockDefinitions to
    the Blocks dictionary.

* feat(blocks): Export block definitions per-module

  - Define all blocks in a local blocks dictionary, often using
    createBlockDefinitionFromJsonArray, without registering them.
  - Separately, use defineBlocks to register the exported
    BlockDefinitions at the end of each Blockly.blocks.*
    module.
  - In Blockly.blocks.all, create a blocks export that combines all
    of the blocks exports from the individual blocks modules.

* chore: have format script run clang-format on blocks/ too
  • Loading branch information
cpcallen authored Jan 31, 2022
1 parent 74ef1cb commit ffb8907
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 69 deletions.
11 changes: 11 additions & 0 deletions blocks/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const procedures = goog.require('Blockly.blocks.procedures');
const texts = goog.require('Blockly.blocks.texts');
const variables = goog.require('Blockly.blocks.variables');
const variablesDynamic = goog.require('Blockly.blocks.variablesDynamic');
/* eslint-disable-next-line no-unused-vars */
const {BlockDefinition} = goog.requireType('Blockly.blocks');


exports.colour = colour;
Expand All @@ -34,3 +36,12 @@ exports.texts = texts;
exports.variables = variables;
exports.variablesDynamic = variablesDynamic;

/**
* A dictionary of the block definitions provided by all the
* Blockly.blocks.* modules.
* @type {!Object<string, !BlockDefinition>}
*/
const blocks = Object.assign(
{}, colour.blocks, lists.blocks, logic.blocks, loops.blocks, math.blocks,
procedures.blocks, variables.blocks, variablesDynamic.blocks);
exports.blocks = blocks;
14 changes: 12 additions & 2 deletions blocks/colour.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@

goog.module('Blockly.blocks.colour');

const {defineBlocksWithJsonArray} = goog.require('Blockly.common');
/* eslint-disable-next-line no-unused-vars */
const {BlockDefinition} = goog.requireType('Blockly.blocks');
const {createBlockDefinitionsFromJsonArray, defineBlocks} = goog.require('Blockly.common');
/** @suppress {extraRequire} */
goog.require('Blockly.FieldColour');


defineBlocksWithJsonArray([
/**
* A dictionary of the block definitions provided by this module.
* @type {!Object<string, !BlockDefinition>}
*/
const blocks = createBlockDefinitionsFromJsonArray([
// Block for colour picker.
{
'type': 'colour_picker',
Expand Down Expand Up @@ -107,3 +113,7 @@ defineBlocksWithJsonArray([
'tooltip': '%{BKY_COLOUR_BLEND_TOOLTIP}',
},
]);
exports.blocks = blocks;

// Register provided blocks.
defineBlocks(blocks);
33 changes: 21 additions & 12 deletions blocks/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ const xmlUtils = goog.require('Blockly.utils.xml');
const {Align} = goog.require('Blockly.Input');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {Blocks} = goog.require('Blockly.blocks');
/* eslint-disable-next-line no-unused-vars */
const {BlockDefinition} = goog.requireType('Blockly.blocks');
const {ConnectionType} = goog.require('Blockly.ConnectionType');
const {FieldDropdown} = goog.require('Blockly.FieldDropdown');
const {Msg} = goog.require('Blockly.Msg');
const {Mutator} = goog.require('Blockly.Mutator');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
const {defineBlocksWithJsonArray} = goog.require('Blockly.common');
const {createBlockDefinitionsFromJsonArray, defineBlocks} = goog.require('Blockly.common');
/** @suppress {extraRequire} */
goog.require('Blockly.FieldDropdown');


defineBlocksWithJsonArray([
/**
* A dictionary of the block definitions provided by this module.
* @type {!Object<string, !BlockDefinition>}
*/
const blocks = createBlockDefinitionsFromJsonArray([
// Block for creating an empty list
// The 'list_create_with' block is preferred as it is more flexible.
// <block type="lists_create_with">
Expand Down Expand Up @@ -112,8 +117,9 @@ defineBlocksWithJsonArray([
'helpUrl': '%{BKY_LISTS_LENGTH_HELPURL}',
},
]);
exports.blocks = blocks;

Blocks['lists_create_with'] = {
blocks['lists_create_with'] = {
/**
* Block for creating a list with any number of elements of any type.
* @this {Block}
Expand Down Expand Up @@ -255,7 +261,7 @@ Blocks['lists_create_with'] = {
},
};

Blocks['lists_create_with_container'] = {
blocks['lists_create_with_container'] = {
/**
* Mutator block for list container.
* @this {Block}
Expand All @@ -270,7 +276,7 @@ Blocks['lists_create_with_container'] = {
},
};

Blocks['lists_create_with_item'] = {
blocks['lists_create_with_item'] = {
/**
* Mutator block for adding items.
* @this {Block}
Expand All @@ -285,7 +291,7 @@ Blocks['lists_create_with_item'] = {
},
};

Blocks['lists_indexOf'] = {
blocks['lists_indexOf'] = {
/**
* Block for finding an item in the list.
* @this {Block}
Expand All @@ -312,7 +318,7 @@ Blocks['lists_indexOf'] = {
},
};

Blocks['lists_getIndex'] = {
blocks['lists_getIndex'] = {
/**
* Block for getting element at index.
* @this {Block}
Expand Down Expand Up @@ -516,7 +522,7 @@ Blocks['lists_getIndex'] = {
},
};

Blocks['lists_setIndex'] = {
blocks['lists_setIndex'] = {
/**
* Block for setting the element at index.
* @this {Block}
Expand Down Expand Up @@ -668,7 +674,7 @@ Blocks['lists_setIndex'] = {
},
};

Blocks['lists_getSublist'] = {
blocks['lists_getSublist'] = {
/**
* Block for getting sublist.
* @this {Block}
Expand Down Expand Up @@ -786,7 +792,7 @@ Blocks['lists_getSublist'] = {
},
};

Blocks['lists_sort'] = {
blocks['lists_sort'] = {
/**
* Block for sorting a list.
* @this {Block}
Expand Down Expand Up @@ -826,7 +832,7 @@ Blocks['lists_sort'] = {
},
};

Blocks['lists_split'] = {
blocks['lists_split'] = {
/**
* Block for splitting text into a list, or joining a list into text.
* @this {Block}
Expand Down Expand Up @@ -913,3 +919,6 @@ Blocks['lists_split'] = {
// dropdown values.
// XML hooks are kept for backwards compatibility.
};

// Register provided blocks.
defineBlocks(blocks);
14 changes: 12 additions & 2 deletions blocks/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@ const Extensions = goog.require('Blockly.Extensions');
const xmlUtils = goog.require('Blockly.utils.xml');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
/* eslint-disable-next-line no-unused-vars */
const {BlockDefinition} = goog.requireType('Blockly.blocks');
const {Msg} = goog.require('Blockly.Msg');
const {Mutator} = goog.require('Blockly.Mutator');
/* eslint-disable-next-line no-unused-vars */
const {RenderedConnection} = goog.requireType('Blockly.RenderedConnection');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
const {defineBlocksWithJsonArray} = goog.require('Blockly.common');
const {createBlockDefinitionsFromJsonArray, defineBlocks} = goog.require('Blockly.common');
/** @suppress {extraRequire} */
goog.require('Blockly.FieldDropdown');
/** @suppress {extraRequire} */
goog.require('Blockly.FieldLabel');


defineBlocksWithJsonArray([
/**
* A dictionary of the block definitions provided by this module.
* @type {!Object<string, !BlockDefinition>}
*/
const blocks = createBlockDefinitionsFromJsonArray([
// Block for boolean data type: true and false.
{
'type': 'logic_boolean',
Expand Down Expand Up @@ -258,6 +264,7 @@ defineBlocksWithJsonArray([
'tooltip': '%{BKY_CONTROLS_IF_ELSE_TOOLTIP}',
},
]);
exports.blocks = blocks;

/**
* Tooltip text, keyed by block OP value. Used by logic_compare and
Expand Down Expand Up @@ -645,3 +652,6 @@ const LOGIC_TERNARY_ONCHANGE_MIXIN = {
};

Extensions.registerMixin('logic_ternary', LOGIC_TERNARY_ONCHANGE_MIXIN);

// Register provided blocks.
defineBlocks(blocks);
14 changes: 12 additions & 2 deletions blocks/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ const ContextMenu = goog.require('Blockly.ContextMenu');
const Events = goog.require('Blockly.Events');
const Extensions = goog.require('Blockly.Extensions');
const Variables = goog.require('Blockly.Variables');
const common = goog.require('Blockly.common');
const xmlUtils = goog.require('Blockly.utils.xml');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
/* eslint-disable-next-line no-unused-vars */
const {BlockDefinition} = goog.requireType('Blockly.blocks');
const {Msg} = goog.require('Blockly.Msg');
const {createBlockDefinitionsFromJsonArray, defineBlocks} = goog.require('Blockly.common');
/** @suppress {extraRequire} */
goog.require('Blockly.FieldDropdown');
/** @suppress {extraRequire} */
Expand All @@ -35,7 +37,11 @@ goog.require('Blockly.FieldVariable');
goog.require('Blockly.Warning');


common.defineBlocksWithJsonArray([
/**
* A dictionary of the block definitions provided by this module.
* @type {!Object<string, !BlockDefinition>}
*/
const blocks = createBlockDefinitionsFromJsonArray([
// Block for repeat n times (external number).
{
'type': 'controls_repeat_ext',
Expand Down Expand Up @@ -205,6 +211,7 @@ common.defineBlocksWithJsonArray([
],
},
]);
exports.blocks = blocks;

/**
* Tooltips for the 'controls_whileUntil' block, keyed by MODE value.
Expand Down Expand Up @@ -358,3 +365,6 @@ const CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = {

Extensions.registerMixin(
'controls_flow_in_loop_check', CONTROL_FLOW_IN_LOOP_CHECK_MIXIN);

// Register provided blocks.
defineBlocks(blocks);
13 changes: 11 additions & 2 deletions blocks/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const xmlUtils = goog.require('Blockly.utils.xml');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
/* eslint-disable-next-line no-unused-vars */
const {defineBlocksWithJsonArray} = goog.require('Blockly.common');
const {BlockDefinition} = goog.requireType('Blockly.blocks');
const {createBlockDefinitionsFromJsonArray, defineBlocks} = goog.require('Blockly.common');
/** @suppress {extraRequire} */
goog.require('Blockly.FieldLabel');
/** @suppress {extraRequire} */
Expand All @@ -29,7 +30,11 @@ goog.require('Blockly.FieldNumber');
goog.require('Blockly.FieldVariable');


defineBlocksWithJsonArray([
/**
* A dictionary of the block definitions provided by this module.
* @type {!Object<string, !BlockDefinition>}
*/
const blocks = createBlockDefinitionsFromJsonArray([
// Block for numeric value.
{
'type': 'math_number',
Expand Down Expand Up @@ -384,6 +389,7 @@ defineBlocksWithJsonArray([
'helpUrl': '%{BKY_MATH_ATAN2_HELPURL}',
},
]);
exports.blocks = blocks;

/**
* Mapping of math block OP value to tooltip message for blocks
Expand Down Expand Up @@ -581,3 +587,6 @@ const LIST_MODES_MUTATOR_EXTENSION = function() {
Extensions.registerMutator(
'math_modes_of_list_mutator', LIST_MODES_MUTATOR_MIXIN,
LIST_MODES_MUTATOR_EXTENSION);

// Register provided blocks.
defineBlocks(blocks);
28 changes: 20 additions & 8 deletions blocks/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const xmlUtils = goog.require('Blockly.utils.xml');
const {Align} = goog.require('Blockly.Input');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {Blocks} = goog.require('Blockly.blocks');
/* eslint-disable-next-line no-unused-vars */
const {BlockDefinition} = goog.requireType('Blockly.blocks');
/* eslint-disable-next-line no-unused-vars */
const {FieldCheckbox} = goog.require('Blockly.FieldCheckbox');
const {FieldLabel} = goog.require('Blockly.FieldLabel');
Expand All @@ -36,12 +37,20 @@ const {Names} = goog.require('Blockly.Names');
const {VariableModel} = goog.requireType('Blockly.VariableModel');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
const {defineBlocks} = goog.require('Blockly.common');
/** @suppress {extraRequire} */
goog.require('Blockly.Comment');
/** @suppress {extraRequire} */
goog.require('Blockly.Warning');


/**
* A dictionary of the block definitions provided by this module.
* @type {!Object<string, !BlockDefinition>}
*/
const blocks = {};
exports.blocks = blocks;

/**
* Common properties for the procedure_defnoreturn and
* procedure_defreturn blocks.
Expand Down Expand Up @@ -437,7 +446,7 @@ const PROCEDURE_DEF_COMMON = {
callType_: 'procedures_callnoreturn',
};

Blocks['procedures_defnoreturn'] = {
blocks['procedures_defnoreturn'] = {
...PROCEDURE_DEF_COMMON,
/**
* Block for defining a procedure with no return value.
Expand Down Expand Up @@ -479,7 +488,7 @@ Blocks['procedures_defnoreturn'] = {
},
};

Blocks['procedures_defreturn'] = {
blocks['procedures_defreturn'] = {
...PROCEDURE_DEF_COMMON,
/**
* Block for defining a procedure with a return value.
Expand Down Expand Up @@ -524,7 +533,7 @@ Blocks['procedures_defreturn'] = {
},
};

Blocks['procedures_mutatorcontainer'] = {
blocks['procedures_mutatorcontainer'] = {
/**
* Mutator block for procedure container.
* @this {Block}
Expand All @@ -542,7 +551,7 @@ Blocks['procedures_mutatorcontainer'] = {
},
};

Blocks['procedures_mutatorarg'] = {
blocks['procedures_mutatorarg'] = {
/**
* Mutator block for procedure argument.
* @this {Block}
Expand Down Expand Up @@ -1033,7 +1042,7 @@ const PROCEDURE_CALL_COMMON = {
},
};

Blocks['procedures_callnoreturn'] = {
blocks['procedures_callnoreturn'] = {
...PROCEDURE_CALL_COMMON,
/**
* Block for calling a procedure with no return value.
Expand All @@ -1056,7 +1065,7 @@ Blocks['procedures_callnoreturn'] = {
defType_: 'procedures_defnoreturn',
};

Blocks['procedures_callreturn'] = {
blocks['procedures_callreturn'] = {
...PROCEDURE_CALL_COMMON,
/**
* Block for calling a procedure with a return value.
Expand All @@ -1078,7 +1087,7 @@ Blocks['procedures_callreturn'] = {
defType_: 'procedures_defreturn',
};

Blocks['procedures_ifreturn'] = {
blocks['procedures_ifreturn'] = {
/**
* Block for conditionally returning a value from a procedure.
* @this {Block}
Expand Down Expand Up @@ -1179,3 +1188,6 @@ Blocks['procedures_ifreturn'] = {
*/
FUNCTION_TYPES: ['procedures_defnoreturn', 'procedures_defreturn'],
};

// Register provided blocks.
defineBlocks(blocks);
Loading

0 comments on commit ffb8907

Please sign in to comment.