Skip to content

Commit

Permalink
refactor: Migrate blocks/variables_dynamic.js to goog.module synt…
Browse files Browse the repository at this point in the history
…ax (#5779)

* refactor: Migrate blocks/variables_dynamic.js to goog.module

* refactor: Migrate blocks/variables_dynamic.js named requires

* chore: clang-format blocks/variables_dynamic.js
  • Loading branch information
cpcallen authored Dec 3, 2021
1 parent c93e1df commit b08187c
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 118 deletions.
145 changes: 75 additions & 70 deletions blocks/variables_dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,65 @@

/**
* @fileoverview Variable blocks for Blockly.
* @suppress {extraRequire|missingRequire|checkTypes}
* @suppress {checkTypes}
*/
'use strict';

goog.provide('Blockly.blocks.variablesDynamic');
goog.provide('Blockly.Constants.VariablesDynamic');
goog.module('Blockly.blocks.variablesDynamic');

goog.require('Blockly');
/* eslint-disable-next-line no-unused-vars */
const AbstractEvent = goog.requireType('Blockly.Events.Abstract');
const ContextMenu = goog.require('Blockly.ContextMenu');
const Extensions = goog.require('Blockly.Extensions');
const Variables = goog.require('Blockly.Variables');
const xml = 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_DYNAMIC_HUE']. (2018 April 5)
*/
Blockly.Constants.VariablesDynamic.HUE = 310;

Blockly.defineBlocksWithJsonArray([
defineBlocksWithJsonArray([
// Block for variable getter.
{
"type": "variables_get_dynamic",
"message0": "%1",
"args0": [{
"type": "field_variable",
"name": "VAR",
"variable": "%{BKY_VARIABLES_DEFAULT_NAME}",
'type': 'variables_get_dynamic',
'message0': '%1',
'args0': [{
'type': 'field_variable',
'name': 'VAR',
'variable': '%{BKY_VARIABLES_DEFAULT_NAME}',
}],
"output": null,
"style": "variable_dynamic_blocks",
"helpUrl": "%{BKY_VARIABLES_GET_HELPURL}",
"tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}",
"extensions": ["contextMenu_variableDynamicSetterGetter"],
'output': null,
'style': 'variable_dynamic_blocks',
'helpUrl': '%{BKY_VARIABLES_GET_HELPURL}',
'tooltip': '%{BKY_VARIABLES_GET_TOOLTIP}',
'extensions': ['contextMenu_variableDynamicSetterGetter'],
},
// Block for variable setter.
{
"type": "variables_set_dynamic",
"message0": "%{BKY_VARIABLES_SET}",
"args0": [{
"type": "field_variable",
"name": "VAR",
"variable": "%{BKY_VARIABLES_DEFAULT_NAME}",
},
{
"type": "input_value",
"name": "VALUE",
},
'type': 'variables_set_dynamic',
'message0': '%{BKY_VARIABLES_SET}',
'args0': [
{
'type': 'field_variable',
'name': 'VAR',
'variable': '%{BKY_VARIABLES_DEFAULT_NAME}',
},
{
'type': 'input_value',
'name': 'VALUE',
},
],
"previousStatement": null,
"nextStatement": null,
"style": "variable_dynamic_blocks",
"tooltip": "%{BKY_VARIABLES_SET_TOOLTIP}",
"helpUrl": "%{BKY_VARIABLES_SET_HELPURL}",
"extensions": ["contextMenu_variableDynamicSetterGetter"],
'previousStatement': null,
'nextStatement': null,
'style': 'variable_dynamic_blocks',
'tooltip': '%{BKY_VARIABLES_SET_TOOLTIP}',
'helpUrl': '%{BKY_VARIABLES_SET_HELPURL}',
'extensions': ['contextMenu_variableDynamicSetterGetter'],
},
]);

Expand All @@ -68,15 +73,14 @@ Blockly.defineBlocksWithJsonArray([
* setter/getter.
* Used by blocks 'variables_set_dynamic' and 'variables_get_dynamic'.
* @mixin
* @augments Blockly.Block
* @package
* @augments Block
* @readonly
*/
Blockly.Constants.VariablesDynamic.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) {
// Getter blocks have the option to create a setter block, and vice versa.
Expand All @@ -88,37 +92,37 @@ Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MI
const varType = variableModel.type;
if (this.type === 'variables_get_dynamic') {
oppositeType = 'variables_set_dynamic';
contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET'];
contextMenuMsg = Msg['VARIABLES_GET_CREATE_SET'];
} else {
oppositeType = 'variables_get_dynamic';
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 = xml.createElement('field');
xmlField.setAttribute('name', 'VAR');
xmlField.setAttribute('variabletype', varType);
xmlField.appendChild(Blockly.utils.xml.createTextNode(name));
const xmlBlock = Blockly.utils.xml.createElement('block');
xmlField.appendChild(xml.createTextNode(name));
const xmlBlock = xml.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);
} else {
if (this.type === 'variables_get_dynamic' ||
this.type === 'variables_get_reporter_dynamic') {
this.type === 'variables_get_reporter_dynamic') {
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);
Expand All @@ -128,12 +132,12 @@ Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MI
/**
* Called whenever anything on the workspace changes.
* Set the connection type for this block.
* @param {!Blockly.Events.Abstract} _e Change event.
* @this {Blockly.Block}
* @param {AbstractEvent} _e Change event.
* @this {Block}
*/
onchange: function(_e) {
const id = this.getFieldValue('VAR');
const variableModel = Blockly.Variables.getVariable(this.workspace, id);
const variableModel = Variables.getVariable(this.workspace, id);
if (this.type === 'variables_get_dynamic') {
this.outputConnection.setCheck(variableModel.type);
} else {
Expand All @@ -143,26 +147,26 @@ Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MI
};

/**
* 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.VariablesDynamic.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.VariablesDynamic.DELETE_OPTION_CALLBACK_FACTORY = function(block) {
const deleteOptionCallbackFactory = function(block) {
return function() {
const workspace = block.workspace;
const variable = block.getField('VAR').getVariable();
Expand All @@ -171,5 +175,6 @@ Blockly.Constants.VariablesDynamic.DELETE_OPTION_CALLBACK_FACTORY = function(blo
};
};

Blockly.Extensions.registerMixin('contextMenu_variableDynamicSetterGetter',
Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN);
Extensions.registerMixin(
'contextMenu_variableDynamicSetterGetter',
CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN);
94 changes: 47 additions & 47 deletions scripts/gulpfiles/chunks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,64 @@
"all5:11:requires"
],
"js": [
"./core/contextmenu_items.js",
"./core/shortcut_items.js",
"./core/theme/zelos.js",
"./core/theme/themes.js",
"./core/renderers/geras/constants.js",
"./core/renderers/geras/highlight_constants.js",
"./core/renderers/geras/measurables/inline_input.js",
"./core/renderers/geras/path_object.js",
"./core/renderers/geras/renderer.js",
"./core/renderers/geras/measurables/statement_input.js",
"./core/renderers/geras/info.js",
"./core/renderers/geras/highlighter.js",
"./core/renderers/geras/drawer.js",
"./core/renderers/geras/geras.js",
"./core/renderers/minimalist/constants.js",
"./core/renderers/minimalist/renderer.js",
"./core/renderers/minimalist/info.js",
"./core/renderers/minimalist/drawer.js",
"./core/renderers/minimalist/minimalist.js",
"./core/serialization/variables.js",
"./core/serialization/workspaces.js",
"./core/renderers/thrasos/renderer.js",
"./core/renderers/thrasos/info.js",
"./core/renderers/thrasos/thrasos.js",
"./core/renderers/zelos/measurables/bottom_row.js",
"./core/renderers/zelos/constants.js",
"./core/renderers/zelos/path_object.js",
"./core/renderers/zelos/marker_svg.js",
"./core/renderers/zelos/renderer.js",
"./core/renderers/zelos/measurables/row_elements.js",
"./core/renderers/zelos/measurables/inputs.js",
"./core/renderers/zelos/measurables/top_row.js",
"./core/renderers/zelos/info.js",
"./core/renderers/zelos/drawer.js",
"./core/renderers/zelos/zelos.js",
"./core/toolbox/collapsible_category.js",
"./core/field_angle.js",
"./core/field_colour.js",
"./core/field_label_serializable.js",
"./core/flyout_metrics_manager.js",
"./core/flyout_base.js",
"./core/generator.js",
"./core/flyout_horizontal.js",
"./core/interfaces/i_styleable.js",
"./core/toolbox/toolbox.js",
"./core/flyout_vertical.js",
"./core/inject.js",
"./core/blockly.js",
"./core/flyout_vertical.js",
"./core/toolbox/toolbox.js",
"./core/interfaces/i_styleable.js",
"./core/flyout_horizontal.js",
"./core/generator.js",
"./core/flyout_base.js",
"./core/flyout_metrics_manager.js",
"./core/field_variable.js",
"./core/field_number.js",
"./core/field_multilineinput.js",
"./core/field_label_serializable.js",
"./core/field_dropdown.js",
"./core/field_colour.js",
"./core/field_checkbox.js",
"./core/field_angle.js",
"./core/toolbox/collapsible_category.js",
"./core/renderers/zelos/measurables/top_row.js",
"./core/renderers/zelos/measurables/inputs.js",
"./core/renderers/zelos/measurables/row_elements.js",
"./core/renderers/zelos/marker_svg.js",
"./core/renderers/zelos/renderer.js",
"./core/field_textinput.js",
"./core/field_image.js",
"./core/renderers/zelos/info.js",
"./core/renderers/zelos/path_object.js",
"./core/renderers/zelos/drawer.js",
"./core/renderers/zelos/constants.js",
"./core/renderers/zelos/measurables/bottom_row.js",
"./core/renderers/zelos/zelos.js",
"./core/renderers/thrasos/renderer.js",
"./core/renderers/thrasos/info.js",
"./core/renderers/thrasos/thrasos.js",
"./core/serialization/workspaces.js",
"./core/serialization/variables.js",
"./core/renderers/minimalist/renderer.js",
"./core/renderers/minimalist/info.js",
"./core/renderers/minimalist/drawer.js",
"./core/renderers/minimalist/constants.js",
"./core/renderers/minimalist/minimalist.js",
"./core/renderers/geras/measurables/statement_input.js",
"./core/renderers/geras/path_object.js",
"./core/renderers/geras/renderer.js",
"./core/renderers/geras/measurables/inline_input.js",
"./core/renderers/geras/info.js",
"./core/renderers/geras/highlight_constants.js",
"./core/renderers/geras/highlighter.js",
"./core/renderers/geras/drawer.js",
"./core/renderers/geras/constants.js",
"./core/renderers/geras/geras.js",
"./core/theme/zelos.js",
"./core/theme/themes.js",
"./core/shortcut_items.js",
"./core/events/workspace_events.js",
"./core/events/events_toolbox_item_select.js",
"./core/events/events_ui.js",
"./core/events/events.js",
"./core/contextmenu_items.js",
"./core/widgetdiv.js",
"./core/clipboard.js",
"./core/menuitem.js",
Expand Down Expand Up @@ -265,6 +264,7 @@
"./core/xml.js",
"./core/connection.js",
"./core/common.js",
"./core/blockly.js",
"./closure/goog/base_minimal.js",
"./core/requires.js",
"./blocks/variables_dynamic.js",
Expand Down
2 changes: 1 addition & 1 deletion tests/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b08187c

Please sign in to comment.