fix!: Export loopTypes from Blockly.blocks.loops (#5900)

* refactor(blocks): Make loopTypes a Set

  This is likely to slightly improve performance, especially if there
  are many entries.

* refactor(blocks): Re-export individual block modules from Blockly.blocks.all

* fix!(blocks): Have blocks_compressed.js export Blockly.blocks.all

  Previously the value obtained by

      const blocks = require('blockly/blocks');  // Node.js

  or

      import blocks from 'blockly/blocks.js';  // ES Modules

  would be the block definitions dictionary (Blockly.Blocks).

  Change this so that it is instead the export object from
  Blockly.blocks.all.

  This means you can now access loopTypes via:

      import blocks from 'blockly/blocks.js';
      blocks.loops.loopTypes.add('my_loop_blocktype');

  This is a breaking change for any code which depended on the value
  that was exported by blocks_compressed.js.

BREAKING CHANGE: the exports provided by blocks_compressed.js (and
therefore by `import ... from 'blockly/blocks'`) have changed; see above.
This commit is contained in:
Christopher Allen
2022-01-31 18:36:10 +00:00
committed by GitHub
parent 9b06fc8853
commit 74ef1cbf52
4 changed files with 32 additions and 19 deletions

View File

@@ -11,13 +11,26 @@
'use strict';
goog.module('Blockly.blocks.all');
goog.module.declareLegacyNamespace();
const colour = goog.require('Blockly.blocks.colour');
const lists = goog.require('Blockly.blocks.lists');
const logic = goog.require('Blockly.blocks.logic');
const loops = goog.require('Blockly.blocks.loops');
const math = goog.require('Blockly.blocks.math');
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');
exports.colour = colour;
exports.lists = lists;
exports.logic = logic;
exports.loops = loops;
exports.math = math;
exports.procedures = procedures;
exports.texts = texts;
exports.variables = variables;
exports.variablesDynamic = variablesDynamic;
goog.require('Blockly.blocks.colour');
goog.require('Blockly.blocks.lists');
goog.require('Blockly.blocks.logic');
goog.require('Blockly.blocks.loops');
goog.require('Blockly.blocks.math');
goog.require('Blockly.blocks.procedures');
goog.require('Blockly.blocks.texts');
goog.require('Blockly.blocks.variables');
goog.require('Blockly.blocks.variablesDynamic');

View File

@@ -287,21 +287,21 @@ Extensions.register(
*
* // If using the Blockly npm package and es6 import syntax:
* import {loopTypes} from 'blockly/blocks';
* loopTypes.push('custom_loop');
* loopTypes.add('custom_loop');
*
* // Else if using Closure Compiler and goog.modules:
* const {loopTypes} = goog.require('Blockly.blocks.loops');
* loopTypes.push('custom_loop');
* loopTypes.add('custom_loop');
*
* @type {!Array<string>}
* @type {!Set<string>}
*/
const loopTypes = [
const loopTypes = new Set([
'controls_repeat',
'controls_repeat_ext',
'controls_forEach',
'controls_for',
'controls_whileUntil',
];
]);
exports.loopTypes = loopTypes;
/**
@@ -321,7 +321,7 @@ const CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = {
getSurroundLoop: function() {
let block = this;
do {
if (loopTypes.includes(block.type)) {
if (loopTypes.has(block.type)) {
return block;
}
block = block.getSurroundParent();