From fed47362cd7fe77bd1cff3e7a3ddaf1786492ceb Mon Sep 17 00:00:00 2001 From: kozbial Date: Wed, 14 Jul 2021 15:45:22 -0700 Subject: [PATCH] Migrate core/extensions.js to goog.module --- core/extensions.js | 81 ++++++++++++++++++++++++---------------------- tests/deps.js | 2 +- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/core/extensions.js b/core/extensions.js index 4871d4622..8a287e33a 100644 --- a/core/extensions.js +++ b/core/extensions.js @@ -17,7 +17,8 @@ * @name Blockly.Extensions * @namespace */ -goog.provide('Blockly.Extensions'); +goog.module('Blockly.Extensions'); +goog.module.declareLegacyNamespace(); goog.require('Blockly.utils'); @@ -28,7 +29,7 @@ goog.requireType('Blockly.Block'); * The set of all registered extensions, keyed by extension name/id. * @private */ -Blockly.Extensions.ALL_ = Object.create(null); +const ALL = Object.create(null); /** * Registers a new extension function. Extensions are functions that help @@ -40,17 +41,17 @@ Blockly.Extensions.ALL_ = Object.create(null); * @throws {Error} if the extension name is empty, the extension is already * registered, or extensionFn is not a function. */ -Blockly.Extensions.register = function(name, initFn) { +const register = function(name, initFn) { if ((typeof name != 'string') || (name.trim() == '')) { throw Error('Error: Invalid extension name "' + name + '"'); } - if (Blockly.Extensions.ALL_[name]) { + if (ALL[name]) { throw Error('Error: Extension "' + name + '" is already registered.'); } if (typeof initFn != 'function') { throw Error('Error: Extension "' + name + '" must be a function'); } - Blockly.Extensions.ALL_[name] = initFn; + ALL[name] = initFn; }; /** @@ -60,11 +61,11 @@ Blockly.Extensions.register = function(name, initFn) { * @throws {Error} if the extension name is empty or the extension is already * registered. */ -Blockly.Extensions.registerMixin = function(name, mixinObj) { +const registerMixin = function(name, mixinObj) { if (!mixinObj || typeof mixinObj != 'object') { throw Error('Error: Mixin "' + name + '" must be a object'); } - Blockly.Extensions.register(name, function() { + register(name, function() { this.mixin(mixinObj); }); }; @@ -82,25 +83,25 @@ Blockly.Extensions.registerMixin = function(name, mixinObj) { * flyout of the mutator dialog. * @throws {Error} if the mutation is invalid or can't be applied to the block. */ -Blockly.Extensions.registerMutator = function(name, mixinObj, opt_helperFn, +const registerMutator = function(name, mixinObj, opt_helperFn, opt_blockList) { const errorPrefix = 'Error when registering mutator "' + name + '": '; // Sanity check the mixin object before registering it. - Blockly.Extensions.checkHasFunction_( + checkHasFunction( errorPrefix, mixinObj.domToMutation, 'domToMutation'); - Blockly.Extensions.checkHasFunction_( + checkHasFunction( errorPrefix, mixinObj.mutationToDom, 'mutationToDom'); const hasMutatorDialog = - Blockly.Extensions.checkMutatorDialog_(mixinObj, errorPrefix); + checkMutatorDialog(mixinObj, errorPrefix); if (opt_helperFn && (typeof opt_helperFn != 'function')) { throw Error('Extension "' + name + '" is not a function'); } // Sanity checks passed. - Blockly.Extensions.register(name, function() { + register(name, function() { if (hasMutatorDialog) { if (!Blockly.Mutator) { throw Error(errorPrefix + 'Missing require for Blockly.Mutator'); @@ -120,9 +121,9 @@ Blockly.Extensions.registerMutator = function(name, mixinObj, opt_helperFn, * Unregisters the extension registered with the given name. * @param {string} name The name of the extension to unregister. */ -Blockly.Extensions.unregister = function(name) { - if (Blockly.Extensions.ALL_[name]) { - delete Blockly.Extensions.ALL_[name]; +const unregister = function(name) { + if (ALL[name]) { + delete ALL[name]; } else { console.warn('No extension mapping for name "' + name + '" found to unregister'); @@ -137,27 +138,27 @@ Blockly.Extensions.unregister = function(name) { * @param {boolean} isMutator True if this extension defines a mutator. * @throws {Error} if the extension is not found. */ -Blockly.Extensions.apply = function(name, block, isMutator) { - const extensionFn = Blockly.Extensions.ALL_[name]; +const apply = function(name, block, isMutator) { + const extensionFn = ALL[name]; if (typeof extensionFn != 'function') { throw Error('Error: Extension "' + name + '" not found.'); } let mutatorProperties; if (isMutator) { // Fail early if the block already has mutation properties. - Blockly.Extensions.checkNoMutatorProperties_(name, block); + checkNoMutatorProperties(name, block); } else { // Record the old properties so we can make sure they don't change after // applying the extension. - mutatorProperties = Blockly.Extensions.getMutatorProperties_(block); + mutatorProperties = getMutatorProperties(block); } extensionFn.apply(block); if (isMutator) { const errorPrefix = 'Error after applying mutator "' + name + '": '; - Blockly.Extensions.checkBlockHasMutatorProperties_(errorPrefix, block); + checkBlockHasMutatorProperties(errorPrefix, block); } else { - if (!Blockly.Extensions.mutatorPropertiesMatch_( + if (!mutatorPropertiesMatch( /** @type {!Array} */ (mutatorProperties), block)) { throw Error('Error when applying extension "' + name + '": ' + 'mutation properties changed when applying a non-mutator extension.'); @@ -173,7 +174,7 @@ Blockly.Extensions.apply = function(name, block, isMutator) { * @throws {Error} if the property does not exist or is not a function. * @private */ -Blockly.Extensions.checkHasFunction_ = function(errorPrefix, func, +const checkHasFunction = function(errorPrefix, func, propertyName) { if (!func) { throw Error(errorPrefix + @@ -194,8 +195,8 @@ Blockly.Extensions.checkHasFunction_ = function(errorPrefix, func, * @throws {Error} if any of the properties already exist on the block. * @private */ -Blockly.Extensions.checkNoMutatorProperties_ = function(mutationName, block) { - const properties = Blockly.Extensions.getMutatorProperties_(block); +const checkNoMutatorProperties = function(mutationName, block) { + const properties = getMutatorProperties(block); if (properties.length) { throw Error('Error: tried to apply mutation "' + mutationName + '" to a block that already has mutator functions.' + @@ -215,7 +216,7 @@ Blockly.Extensions.checkNoMutatorProperties_ = function(mutationName, block) { * @throws {Error} if the object has only one of the functions. * @private */ -Blockly.Extensions.checkMutatorDialog_ = function(object, errorPrefix) { +const checkMutatorDialog = function(object, errorPrefix) { const hasCompose = object.compose !== undefined; const hasDecompose = object.decompose !== undefined; @@ -240,7 +241,7 @@ Blockly.Extensions.checkMutatorDialog_ = function(object, errorPrefix) { * @param {!Blockly.Block} block The block to inspect. * @private */ -Blockly.Extensions.checkBlockHasMutatorProperties_ = function(errorPrefix, +const checkBlockHasMutatorProperties = function(errorPrefix, block) { if (typeof block.domToMutation != 'function') { throw Error(errorPrefix + 'Applying a mutator didn\'t add "domToMutation"'); @@ -251,7 +252,7 @@ Blockly.Extensions.checkBlockHasMutatorProperties_ = function(errorPrefix, // A block with a mutator isn't required to have a mutation dialog, but // it should still have both or neither of compose and decompose. - Blockly.Extensions.checkMutatorDialog_(block, errorPrefix); + checkMutatorDialog(block, errorPrefix); }; /** @@ -261,7 +262,7 @@ Blockly.Extensions.checkBlockHasMutatorProperties_ = function(errorPrefix, * should be functions, but may be anything other than undefined. * @private */ -Blockly.Extensions.getMutatorProperties_ = function(block) { +const getMutatorProperties = function(block) { const result = []; // List each function explicitly by reference to allow for renaming // during compilation. @@ -289,8 +290,8 @@ Blockly.Extensions.getMutatorProperties_ = function(block) { * @return {boolean} True if the property lists match. * @private */ -Blockly.Extensions.mutatorPropertiesMatch_ = function(oldProperties, block) { - const newProperties = Blockly.Extensions.getMutatorProperties_(block); +const mutatorPropertiesMatch = function(oldProperties, block) { + const newProperties = getMutatorProperties(block); if (newProperties.length != oldProperties.length) { return false; } @@ -321,7 +322,7 @@ Blockly.Extensions.mutatorPropertiesMatch_ = function(oldProperties, block) { * tooltip text. * @return {!Function} The extension function. */ -Blockly.Extensions.buildTooltipForDropdown = function(dropdownName, +const buildTooltipForDropdown = function(dropdownName, lookupTable) { // List of block types already validated, to minimize duplicate warnings. const blockTypesChecked = []; @@ -345,7 +346,7 @@ Blockly.Extensions.buildTooltipForDropdown = function(dropdownName, */ const extensionFn = function () { if (this.type && blockTypesChecked.indexOf(this.type) == -1) { - Blockly.Extensions.checkDropdownOptionsInTable_( + checkDropdownOptionsInTable( this, dropdownName, lookupTable); blockTypesChecked.push(this.type); } @@ -380,7 +381,7 @@ Blockly.Extensions.buildTooltipForDropdown = function(dropdownName, * @param {!Object} lookupTable The string lookup table * @private */ -Blockly.Extensions.checkDropdownOptionsInTable_ = function(block, dropdownName, +const checkDropdownOptionsInTable = function(block, dropdownName, lookupTable) { // Validate all dropdown options have values. const dropdown = block.getField(dropdownName); @@ -405,7 +406,7 @@ Blockly.Extensions.checkDropdownOptionsInTable_ = function(block, dropdownName, * @param {string} fieldName The field with the replacement text. * @return {!Function} The extension function. */ -Blockly.Extensions.buildTooltipWithFieldText = function(msgTemplate, +const buildTooltipWithFieldText = function(msgTemplate, fieldName) { // Check the tooltip string messages for invalid references. // Wait for load, in case Blockly.Msg is not yet populated. @@ -440,13 +441,15 @@ Blockly.Extensions.buildTooltipWithFieldText = function(msgTemplate, * @this {Blockly.Block} * @private */ -Blockly.Extensions.extensionParentTooltip_ = function() { - this.tooltipWhenNotConnected_ = this.tooltip; +const extensionParentTooltip = function() { + this.tooltipWhenNotConnected = this.tooltip; this.setTooltip(function() { const parent = this.getParent(); return (parent && parent.getInputsInline() && parent.tooltip) || - this.tooltipWhenNotConnected_; + this.tooltipWhenNotConnected; }.bind(this)); }; -Blockly.Extensions.register('parent_tooltip_when_inline', - Blockly.Extensions.extensionParentTooltip_); +register('parent_tooltip_when_inline', + extensionParentTooltip); + +exports = {ALL_: ALL, register, registerMixin, registerMutator, unregister, apply, buildTooltipForDropdown, buildTooltipWithFieldText}; diff --git a/tests/deps.js b/tests/deps.js index 89bb3d512..5c46df3f6 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -47,7 +47,7 @@ goog.addDependency('../../core/events/ui_events.js', ['Blockly.Events.Ui', 'Bloc goog.addDependency('../../core/events/variable_events.js', ['Blockly.Events.VarBase', 'Blockly.Events.VarCreate', 'Blockly.Events.VarDelete', 'Blockly.Events.VarRename'], ['Blockly.Events', 'Blockly.Events.Abstract', 'Blockly.registry', 'Blockly.utils.object']); goog.addDependency('../../core/events/workspace_events.js', ['Blockly.Events.FinishedLoading'], ['Blockly.Events', 'Blockly.Events.Abstract', 'Blockly.registry', 'Blockly.utils.object'], {'lang': 'es5'}); goog.addDependency('../../core/events/ws_comment_events.js', ['Blockly.Events.CommentBase', 'Blockly.Events.CommentChange', 'Blockly.Events.CommentCreate', 'Blockly.Events.CommentDelete', 'Blockly.Events.CommentMove'], ['Blockly.Events', 'Blockly.Events.Abstract', 'Blockly.Xml', 'Blockly.registry', 'Blockly.utils.Coordinate', 'Blockly.utils.object', 'Blockly.utils.xml']); -goog.addDependency('../../core/extensions.js', ['Blockly.Extensions'], ['Blockly.utils']); +goog.addDependency('../../core/extensions.js', ['Blockly.Extensions'], ['Blockly.utils'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/field.js', ['Blockly.Field'], ['Blockly.DropDownDiv', 'Blockly.Events', 'Blockly.Events.BlockChange', 'Blockly.Gesture', 'Blockly.IASTNodeLocationSvg', 'Blockly.IASTNodeLocationWithBlock', 'Blockly.IKeyboardAccessible', 'Blockly.IRegistrable', 'Blockly.MarkerManager', 'Blockly.Tooltip', 'Blockly.WidgetDiv', 'Blockly.browserEvents', 'Blockly.utils', 'Blockly.utils.Rect', 'Blockly.utils.Size', 'Blockly.utils.Svg', 'Blockly.utils.dom', 'Blockly.utils.style', 'Blockly.utils.userAgent'], {'lang': 'es5'}); goog.addDependency('../../core/field_angle.js', ['Blockly.FieldAngle'], ['Blockly.Css', 'Blockly.DropDownDiv', 'Blockly.FieldTextInput', 'Blockly.WidgetDiv', 'Blockly.browserEvents', 'Blockly.fieldRegistry', 'Blockly.utils.KeyCodes', 'Blockly.utils.Svg', 'Blockly.utils.dom', 'Blockly.utils.math', 'Blockly.utils.object', 'Blockly.utils.userAgent']); goog.addDependency('../../core/field_checkbox.js', ['Blockly.FieldCheckbox'], ['Blockly.Events.BlockChange', 'Blockly.Field', 'Blockly.fieldRegistry', 'Blockly.utils.dom', 'Blockly.utils.object']);