From 7d7bd75c3a42f98c6974b8ffb22eab386684ea1e Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Thu, 2 Dec 2021 19:06:48 +0000 Subject: [PATCH] refactor: Migrate `blocks/variables.js` to `goog.module` syntax (#5774) * refactor: Migrate blocks/variables.js to goog.module * refactor: Migrate blocks/variables.js named requires * chore: clang-format blocks/variables.js * chore: JSDoc grammar correction --- blocks/variables.js | 130 +++++++++++++++++++++++--------------------- tests/deps.js | 2 +- 2 files changed, 68 insertions(+), 64 deletions(-) diff --git a/blocks/variables.js b/blocks/variables.js index 6dc38eadc..c9e4720fa 100644 --- a/blocks/variables.js +++ b/blocks/variables.js @@ -6,63 +6,65 @@ /** * @fileoverview Variable blocks for Blockly. - * @suppress {extraRequire|missingRequire|checkTypes} + * @suppress {checkTypes} */ 'use strict'; -goog.provide('Blockly.blocks.variables'); -goog.provide('Blockly.Constants.Variables'); +goog.module('Blockly.blocks.variables'); -goog.require('Blockly'); +const ContextMenu = goog.require('Blockly.ContextMenu'); +const Extensions = goog.require('Blockly.Extensions'); +const Variables = goog.require('Blockly.Variables'); +const xmlUtils = goog.require('Blockly.utils.xml'); +/* eslint-disable-next-line no-unused-vars */ +const {Block} = goog.requireType('Blockly.Block'); +const {Msg} = goog.require('Blockly.Msg'); +const {defineBlocksWithJsonArray} = goog.require('Blockly.common'); +/** @suppress {extraRequire} */ goog.require('Blockly.FieldLabel'); +/** @suppress {extraRequire} */ goog.require('Blockly.FieldVariable'); -/** - * Unused constant for the common HSV hue for all blocks in this category. - * @deprecated Use Blockly.Msg['VARIABLES_HUE']. (2018 April 5) - */ -Blockly.Constants.Variables.HUE = 330; - -Blockly.defineBlocksWithJsonArray([ +defineBlocksWithJsonArray([ // Block for variable getter. { - "type": "variables_get", - "message0": "%1", - "args0": [ + 'type': 'variables_get', + 'message0': '%1', + 'args0': [ { - "type": "field_variable", - "name": "VAR", - "variable": "%{BKY_VARIABLES_DEFAULT_NAME}", + 'type': 'field_variable', + 'name': 'VAR', + 'variable': '%{BKY_VARIABLES_DEFAULT_NAME}', }, ], - "output": null, - "style": "variable_blocks", - "helpUrl": "%{BKY_VARIABLES_GET_HELPURL}", - "tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}", - "extensions": ["contextMenu_variableSetterGetter"], + 'output': null, + 'style': 'variable_blocks', + 'helpUrl': '%{BKY_VARIABLES_GET_HELPURL}', + 'tooltip': '%{BKY_VARIABLES_GET_TOOLTIP}', + 'extensions': ['contextMenu_variableSetterGetter'], }, // Block for variable setter. { - "type": "variables_set", - "message0": "%{BKY_VARIABLES_SET}", - "args0": [ + 'type': 'variables_set', + 'message0': '%{BKY_VARIABLES_SET}', + 'args0': [ { - "type": "field_variable", - "name": "VAR", - "variable": "%{BKY_VARIABLES_DEFAULT_NAME}", + 'type': 'field_variable', + 'name': 'VAR', + 'variable': '%{BKY_VARIABLES_DEFAULT_NAME}', }, { - "type": "input_value", - "name": "VALUE", + 'type': 'input_value', + 'name': 'VALUE', }, ], - "previousStatement": null, - "nextStatement": null, - "style": "variable_blocks", - "tooltip": "%{BKY_VARIABLES_SET_TOOLTIP}", - "helpUrl": "%{BKY_VARIABLES_SET_HELPURL}", - "extensions": ["contextMenu_variableSetterGetter"], + 'previousStatement': null, + 'nextStatement': null, + 'style': 'variable_blocks', + 'tooltip': '%{BKY_VARIABLES_SET_TOOLTIP}', + 'helpUrl': '%{BKY_VARIABLES_SET_HELPURL}', + 'extensions': ['contextMenu_variableSetterGetter'], }, ]); @@ -71,15 +73,15 @@ Blockly.defineBlocksWithJsonArray([ * setter/getter. * Used by blocks 'variables_set' and 'variables_get'. * @mixin - * @augments Blockly.Block + * @augments Block * @package * @readonly */ -Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = { +const CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = { /** * Add menu option to create getter/setter block for this setter/getter. * @param {!Array} options List of menu options to add to. - * @this {Blockly.Block} + * @this {Block} */ customContextMenu: function(options) { if (!this.isInFlyout) { @@ -88,36 +90,37 @@ Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = { // Getter blocks have the option to create a setter block, and vice versa. if (this.type === 'variables_get') { oppositeType = 'variables_set'; - contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET']; + contextMenuMsg = Msg['VARIABLES_GET_CREATE_SET']; } else { oppositeType = 'variables_get'; - contextMenuMsg = Blockly.Msg['VARIABLES_SET_CREATE_GET']; + contextMenuMsg = Msg['VARIABLES_SET_CREATE_GET']; } const option = {enabled: this.workspace.remainingCapacity() > 0}; const name = this.getField('VAR').getText(); option.text = contextMenuMsg.replace('%1', name); - const xmlField = Blockly.utils.xml.createElement('field'); + const xmlField = xmlUtils.createElement('field'); xmlField.setAttribute('name', 'VAR'); - xmlField.appendChild(Blockly.utils.xml.createTextNode(name)); - const xmlBlock = Blockly.utils.xml.createElement('block'); + xmlField.appendChild(xmlUtils.createTextNode(name)); + const xmlBlock = xmlUtils.createElement('block'); xmlBlock.setAttribute('type', oppositeType); xmlBlock.appendChild(xmlField); - option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock); + option.callback = ContextMenu.callbackFactory(this, xmlBlock); options.push(option); // Getter blocks have the option to rename or delete that variable. } else { - if (this.type === 'variables_get' || this.type === 'variables_get_reporter') { + if (this.type === 'variables_get' || + this.type === 'variables_get_reporter') { const renameOption = { - text: Blockly.Msg['RENAME_VARIABLE'], + text: Msg['RENAME_VARIABLE'], enabled: true, - callback: Blockly.Constants.Variables.RENAME_OPTION_CALLBACK_FACTORY(this), + callback: renameOptionCallbackFactory(this), }; const name = this.getField('VAR').getText(); const deleteOption = { - text: Blockly.Msg['DELETE_VARIABLE'].replace('%1', name), + text: Msg['DELETE_VARIABLE'].replace('%1', name), enabled: true, - callback: Blockly.Constants.Variables.DELETE_OPTION_CALLBACK_FACTORY(this), + callback: deleteOptionCallbackFactory(this), }; options.unshift(renameOption); options.unshift(deleteOption); @@ -127,26 +130,26 @@ Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = { }; /** - * Callback for rename variable dropdown menu option associated with a - * variable getter block. - * @param {!Blockly.Block} block The block with the variable to rename. - * @return {!function()} A function that renames the variable. - */ -Blockly.Constants.Variables.RENAME_OPTION_CALLBACK_FACTORY = function(block) { + * Factory for callbacks for rename variable dropdown menu option + * associated with a variable getter block. + * @param {!Block} block The block with the variable to rename. + * @return {!function()} A function that renames the variable. + */ +const renameOptionCallbackFactory = function(block) { return function() { const workspace = block.workspace; const variable = block.getField('VAR').getVariable(); - Blockly.Variables.renameVariable(workspace, variable); + Variables.renameVariable(workspace, variable); }; }; /** - * Callback for delete variable dropdown menu option associated with a - * variable getter block. - * @param {!Blockly.Block} block The block with the variable to delete. + * Factory for callbacks for delete variable dropdown menu option + * associated with a variable getter block. + * @param {!Block} block The block with the variable to delete. * @return {!function()} A function that deletes the variable. */ -Blockly.Constants.Variables.DELETE_OPTION_CALLBACK_FACTORY = function(block) { +const deleteOptionCallbackFactory = function(block) { return function() { const workspace = block.workspace; const variable = block.getField('VAR').getVariable(); @@ -155,5 +158,6 @@ Blockly.Constants.Variables.DELETE_OPTION_CALLBACK_FACTORY = function(block) { }; }; -Blockly.Extensions.registerMixin('contextMenu_variableSetterGetter', - Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN); +Extensions.registerMixin( + 'contextMenu_variableSetterGetter', + CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN); diff --git a/tests/deps.js b/tests/deps.js index d2a0763c4..dea14d6a2 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -6,7 +6,7 @@ goog.addDependency('../../blocks/loops.js', ['Blockly.blocks.loops'], ['Blockly. goog.addDependency('../../blocks/math.js', ['Blockly.blocks.math'], ['Blockly.Extensions', 'Blockly.FieldDropdown', 'Blockly.FieldLabel', 'Blockly.FieldNumber', 'Blockly.FieldVariable', 'Blockly.common', 'Blockly.utils.xml'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../blocks/procedures.js', ['Blockly.blocks.procedures'], ['Blockly.Comment', 'Blockly.ContextMenu', 'Blockly.Events', 'Blockly.FieldCheckbox', 'Blockly.FieldLabel', 'Blockly.FieldTextInput', 'Blockly.Input', 'Blockly.Msg', 'Blockly.Mutator', 'Blockly.Names', 'Blockly.Procedures', 'Blockly.Variables', 'Blockly.Warning', 'Blockly.Xml', 'Blockly.blocks', 'Blockly.internalConstants', 'Blockly.utils.xml'], {'lang': 'es9', 'module': 'goog'}); goog.addDependency('../../blocks/text.js', ['Blockly.blocks.texts'], ['Blockly.ConnectionType', 'Blockly.Extensions', 'Blockly.FieldDropdown', 'Blockly.FieldImage', 'Blockly.FieldMultilineInput', 'Blockly.FieldTextInput', 'Blockly.FieldVariable', 'Blockly.Input', 'Blockly.Msg', 'Blockly.Mutator', 'Blockly.blocks', 'Blockly.common', 'Blockly.utils.xml'], {'lang': 'es9', 'module': 'goog'}); -goog.addDependency('../../blocks/variables.js', ['Blockly.Constants.Variables', 'Blockly.blocks.variables'], ['Blockly', 'Blockly.FieldLabel', 'Blockly.FieldVariable'], {'lang': 'es6'}); +goog.addDependency('../../blocks/variables.js', ['Blockly.blocks.variables'], ['Blockly.ContextMenu', 'Blockly.Extensions', 'Blockly.FieldLabel', 'Blockly.FieldVariable', 'Blockly.Msg', 'Blockly.Variables', 'Blockly.common', 'Blockly.utils.xml'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../blocks/variables_dynamic.js', ['Blockly.Constants.VariablesDynamic', 'Blockly.blocks.variablesDynamic'], ['Blockly', 'Blockly.FieldLabel', 'Blockly.FieldVariable'], {'lang': 'es6'}); goog.addDependency('../../core/block.js', ['Blockly.Block'], ['Blockly.ASTNode', 'Blockly.Connection', 'Blockly.ConnectionType', 'Blockly.Events.BlockChange', 'Blockly.Events.BlockCreate', 'Blockly.Events.BlockDelete', 'Blockly.Events.BlockMove', 'Blockly.Events.utils', 'Blockly.Extensions', 'Blockly.IASTNodeLocation', 'Blockly.IDeletable', 'Blockly.Input', 'Blockly.Tooltip', 'Blockly.blocks', 'Blockly.common', 'Blockly.constants', 'Blockly.fieldRegistry', 'Blockly.inputTypes', 'Blockly.utils.Coordinate', 'Blockly.utils.Size', 'Blockly.utils.array', 'Blockly.utils.idGenerator', 'Blockly.utils.object', 'Blockly.utils.parsing'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/block_animations.js', ['Blockly.blockAnimations'], ['Blockly.utils.Svg', 'Blockly.utils.dom'], {'lang': 'es6', 'module': 'goog'});