From f92968b3023759af5e78a887332cf6e5c7d75abd Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Thu, 22 Jul 2021 10:29:34 -0700 Subject: [PATCH 1/4] Migrate core/field_variable.js to ES6 const/let --- core/field_variable.js | 55 +++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/core/field_variable.js b/core/field_variable.js index e5e379c13..e481c728a 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -97,7 +97,7 @@ Blockly.utils.object.inherits(Blockly.FieldVariable, Blockly.FieldDropdown); * @nocollapse */ Blockly.FieldVariable.fromJson = function(options) { - var varName = Blockly.utils.replaceMessageReferences(options['variable']); + const varName = Blockly.utils.replaceMessageReferences(options['variable']); // `this` might be a subclass of FieldVariable if that class doesn't override // the static fromJson method. return new this(varName, undefined, undefined, undefined, options); @@ -130,7 +130,7 @@ Blockly.FieldVariable.prototype.initModel = function() { if (this.variable_) { return; // Initialization already happened. } - var variable = Blockly.Variables.getOrCreateVariablePackage( + const variable = Blockly.Variables.getOrCreateVariablePackage( this.sourceBlock_.workspace, null, this.defaultVariableName, this.defaultType_); @@ -153,14 +153,14 @@ Blockly.FieldVariable.prototype.shouldAddBorderRect_ = function() { * variable field's state. */ Blockly.FieldVariable.prototype.fromXml = function(fieldElement) { - var id = fieldElement.getAttribute('id'); - var variableName = fieldElement.textContent; + const id = fieldElement.getAttribute('id'); + const variableName = fieldElement.textContent; // 'variabletype' should be lowercase, but until July 2019 it was sometimes // recorded as 'variableType'. Thus we need to check for both. - var variableType = fieldElement.getAttribute('variabletype') || + const variableType = fieldElement.getAttribute('variabletype') || fieldElement.getAttribute('variableType') || ''; - var variable = Blockly.Variables.getOrCreateVariablePackage( + const variable = Blockly.Variables.getOrCreateVariablePackage( this.sourceBlock_.workspace, id, variableName, variableType); // This should never happen :) @@ -259,8 +259,8 @@ Blockly.FieldVariable.prototype.doClassValidation_ = function(opt_newValue) { if (opt_newValue === null) { return null; } - var newId = /** @type {string} */ (opt_newValue); - var variable = Blockly.Variables.getVariable( + const newId = /** @type {string} */ (opt_newValue); + const variable = Blockly.Variables.getVariable( this.sourceBlock_.workspace, newId); if (!variable) { console.warn('Variable id doesn\'t point to a real variable! ' + @@ -268,7 +268,7 @@ Blockly.FieldVariable.prototype.doClassValidation_ = function(opt_newValue) { return null; } // Type Checks. - var type = variable.type; + const type = variable.type; if (!this.typeIsAllowed_(type)) { console.warn('Variable type doesn\'t match this field! Type was ' + type); return null; @@ -297,11 +297,11 @@ Blockly.FieldVariable.prototype.doValueUpdate_ = function(newId) { * @private */ Blockly.FieldVariable.prototype.typeIsAllowed_ = function(type) { - var typeList = this.getVariableTypes_(); + const typeList = this.getVariableTypes_(); if (!typeList) { return true; // If it's null, all types are valid. } - for (var i = 0; i < typeList.length; i++) { + for (let i = 0; i < typeList.length; i++) { if (type == typeList[i]) { return true; } @@ -317,7 +317,7 @@ Blockly.FieldVariable.prototype.typeIsAllowed_ = function(type) { */ Blockly.FieldVariable.prototype.getVariableTypes_ = function() { // TODO (#1513): Try to avoid calling this every time the field is edited. - var variableTypes = this.variableTypes; + let variableTypes = this.variableTypes; if (variableTypes === null) { // If variableTypes is null, return all variable types. if (this.sourceBlock_ && this.sourceBlock_.workspace) { @@ -327,7 +327,7 @@ Blockly.FieldVariable.prototype.getVariableTypes_ = function() { variableTypes = variableTypes || ['']; if (variableTypes.length == 0) { // Throw an error if variableTypes is an empty list. - var name = this.getText(); + const name = this.getText(); throw Error('\'variableTypes\' of field variable ' + name + ' was an empty list'); } @@ -348,15 +348,16 @@ Blockly.FieldVariable.prototype.setTypes_ = function(opt_variableTypes, opt_defaultType) { // If you expected that the default type would be the same as the only entry // in the variable types array, tell the Blockly team by commenting on #1499. - var defaultType = opt_defaultType || ''; + const defaultType = opt_defaultType || ''; + let variableTypes; // Set the allowable variable types. Null means all types on the workspace. if (opt_variableTypes == null || opt_variableTypes == undefined) { - var variableTypes = null; + variableTypes = null; } else if (Array.isArray(opt_variableTypes)) { - var variableTypes = opt_variableTypes; + variableTypes = opt_variableTypes; // Make sure the default type is valid. - var isInArray = false; - for (var i = 0; i < variableTypes.length; i++) { + let isInArray = false; + for (let i = 0; i < variableTypes.length; i++) { if (variableTypes[i] == defaultType) { isInArray = true; } @@ -395,23 +396,23 @@ Blockly.FieldVariable.dropdownCreate = function() { throw Error('Tried to call dropdownCreate on a variable field with no' + ' variable selected.'); } - var name = this.getText(); - var variableModelList = []; + const name = this.getText(); + let variableModelList = []; if (this.sourceBlock_ && this.sourceBlock_.workspace) { - var variableTypes = this.getVariableTypes_(); + const variableTypes = this.getVariableTypes_(); // Get a copy of the list, so that adding rename and new variable options // doesn't modify the workspace's list. - for (var i = 0; i < variableTypes.length; i++) { - var variableType = variableTypes[i]; - var variables = + for (let i = 0; i < variableTypes.length; i++) { + const variableType = variableTypes[i]; + const variables = this.sourceBlock_.workspace.getVariablesOfType(variableType); variableModelList = variableModelList.concat(variables); } } variableModelList.sort(Blockly.VariableModel.compareByName); - var options = []; - for (var i = 0; i < variableModelList.length; i++) { + const options = []; + for (let i = 0; i < variableModelList.length; i++) { // Set the UUID as the internal representation of the variable. options[i] = [variableModelList[i].name, variableModelList[i].getId()]; } @@ -437,7 +438,7 @@ Blockly.FieldVariable.dropdownCreate = function() { * @protected */ Blockly.FieldVariable.prototype.onItemSelected_ = function(menu, menuItem) { - var id = menuItem.getValue(); + const id = menuItem.getValue(); // Handle special cases. if (this.sourceBlock_ && this.sourceBlock_.workspace) { if (id == Blockly.internalConstants.RENAME_VARIABLE_ID) { From f5de84486f8d10bfa55737f816c7fc3b25690ee7 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Thu, 22 Jul 2021 10:32:47 -0700 Subject: [PATCH 2/4] Migrate core/field_variable.js to goog.module --- core/field_variable.js | 67 ++++++++++++++++++++++-------------------- tests/deps.js | 2 +- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/core/field_variable.js b/core/field_variable.js index e481c728a..11d16532e 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -10,7 +10,8 @@ */ 'use strict'; -goog.provide('Blockly.FieldVariable'); +goog.module('Blockly.FieldVariable'); +goog.module.declareLegacyNamespace(); /** @suppress {extraRequire} */ goog.require('Blockly.Events.BlockChange'); @@ -47,7 +48,7 @@ goog.requireType('Blockly.MenuItem'); * @extends {Blockly.FieldDropdown} * @constructor */ -Blockly.FieldVariable = function(varName, opt_validator, opt_variableTypes, +const FieldVariable = function(varName, opt_validator, opt_variableTypes, opt_defaultType, opt_config) { // The FieldDropdown constructor expects the field's initial value to be // the first entry in the menu generator, which it may or may not be. @@ -60,7 +61,7 @@ Blockly.FieldVariable = function(varName, opt_validator, opt_variableTypes, * !function(this:Blockly.FieldDropdown): !Array)} * @protected */ - this.menuGenerator_ = Blockly.FieldVariable.dropdownCreate; + this.menuGenerator_ = FieldVariable.dropdownCreate; /** * The initial variable name passed to this field's constructor, or an @@ -85,18 +86,18 @@ Blockly.FieldVariable = function(varName, opt_validator, opt_variableTypes, this.setTypes_(opt_variableTypes, opt_defaultType); } }; -Blockly.utils.object.inherits(Blockly.FieldVariable, Blockly.FieldDropdown); +Blockly.utils.object.inherits(FieldVariable, Blockly.FieldDropdown); /** * Construct a FieldVariable from a JSON arg object, * dereferencing any string table references. * @param {!Object} options A JSON object with options (variable, * variableTypes, and defaultType). - * @return {!Blockly.FieldVariable} The new field instance. + * @return {!FieldVariable} The new field instance. * @package * @nocollapse */ -Blockly.FieldVariable.fromJson = function(options) { +FieldVariable.fromJson = function(options) { const varName = Blockly.utils.replaceMessageReferences(options['variable']); // `this` might be a subclass of FieldVariable if that class doesn't override // the static fromJson method. @@ -108,15 +109,15 @@ Blockly.FieldVariable.fromJson = function(options) { * are not. Editable fields should also be serializable. * @type {boolean} */ -Blockly.FieldVariable.prototype.SERIALIZABLE = true; +FieldVariable.prototype.SERIALIZABLE = true; /** * Configure the field based on the given map of options. * @param {!Object} config A map of options to configure the field based on. * @protected */ -Blockly.FieldVariable.prototype.configure_ = function(config) { - Blockly.FieldVariable.superClass_.configure_.call(this, config); +FieldVariable.prototype.configure_ = function(config) { + FieldVariable.superClass_.configure_.call(this, config); this.setTypes_(config['variableTypes'], config['defaultType']); }; @@ -126,7 +127,7 @@ Blockly.FieldVariable.prototype.configure_ = function(config) { * variable rather than let the value be invalid. * @package */ -Blockly.FieldVariable.prototype.initModel = function() { +FieldVariable.prototype.initModel = function() { if (this.variable_) { return; // Initialization already happened. } @@ -141,8 +142,8 @@ Blockly.FieldVariable.prototype.initModel = function() { /** * @override */ -Blockly.FieldVariable.prototype.shouldAddBorderRect_ = function() { - return Blockly.FieldVariable.superClass_.shouldAddBorderRect_.call(this) && +FieldVariable.prototype.shouldAddBorderRect_ = function() { + return FieldVariable.superClass_.shouldAddBorderRect_.call(this) && (!this.getConstants().FIELD_DROPDOWN_NO_BORDER_RECT_SHADOW || this.sourceBlock_.type != 'variables_get'); }; @@ -152,7 +153,7 @@ Blockly.FieldVariable.prototype.shouldAddBorderRect_ = function() { * @param {!Element} fieldElement The element containing information about the * variable field's state. */ -Blockly.FieldVariable.prototype.fromXml = function(fieldElement) { +FieldVariable.prototype.fromXml = function(fieldElement) { const id = fieldElement.getAttribute('id'); const variableName = fieldElement.textContent; // 'variabletype' should be lowercase, but until July 2019 it was sometimes @@ -180,7 +181,7 @@ Blockly.FieldVariable.prototype.fromXml = function(fieldElement) { * field's state. * @return {!Element} The element containing info about the field's state. */ -Blockly.FieldVariable.prototype.toXml = function(fieldElement) { +FieldVariable.prototype.toXml = function(fieldElement) { // Make sure the variable is initialized. this.initModel(); @@ -196,18 +197,18 @@ Blockly.FieldVariable.prototype.toXml = function(fieldElement) { * Attach this field to a block. * @param {!Blockly.Block} block The block containing this field. */ -Blockly.FieldVariable.prototype.setSourceBlock = function(block) { +FieldVariable.prototype.setSourceBlock = function(block) { if (block.isShadow()) { throw Error('Variable fields are not allowed to exist on shadow blocks.'); } - Blockly.FieldVariable.superClass_.setSourceBlock.call(this, block); + FieldVariable.superClass_.setSourceBlock.call(this, block); }; /** * Get the variable's ID. * @return {string} Current variable's ID. */ -Blockly.FieldVariable.prototype.getValue = function() { +FieldVariable.prototype.getValue = function() { return this.variable_ ? this.variable_.getId() : null; }; @@ -216,7 +217,7 @@ Blockly.FieldVariable.prototype.getValue = function() { * @return {string} The selected variable's name, or the empty string if no * variable is selected. */ -Blockly.FieldVariable.prototype.getText = function() { +FieldVariable.prototype.getText = function() { return this.variable_ ? this.variable_.name : ''; }; @@ -228,7 +229,7 @@ Blockly.FieldVariable.prototype.getText = function() { * selected. * @package */ -Blockly.FieldVariable.prototype.getVariable = function() { +FieldVariable.prototype.getVariable = function() { return this.variable_; }; @@ -239,7 +240,7 @@ Blockly.FieldVariable.prototype.getVariable = function() { * a block and workspace at that point. * @return {?Function} Validation function, or null. */ -Blockly.FieldVariable.prototype.getValidator = function() { +FieldVariable.prototype.getValidator = function() { // Validators shouldn't operate on the initial setValue call. // Normally this is achieved by calling setValidator after setValue, but // this is not a possibility with variable fields. @@ -255,7 +256,7 @@ Blockly.FieldVariable.prototype.getValidator = function() { * @return {?string} The validated ID, or null if invalid. * @protected */ -Blockly.FieldVariable.prototype.doClassValidation_ = function(opt_newValue) { +FieldVariable.prototype.doClassValidation_ = function(opt_newValue) { if (opt_newValue === null) { return null; } @@ -284,10 +285,10 @@ Blockly.FieldVariable.prototype.doClassValidation_ = function(opt_newValue) { * @param {*} newId The value to be saved. * @protected */ -Blockly.FieldVariable.prototype.doValueUpdate_ = function(newId) { +FieldVariable.prototype.doValueUpdate_ = function(newId) { this.variable_ = Blockly.Variables.getVariable( this.sourceBlock_.workspace, /** @type {string} */ (newId)); - Blockly.FieldVariable.superClass_.doValueUpdate_.call(this, newId); + FieldVariable.superClass_.doValueUpdate_.call(this, newId); }; /** @@ -296,7 +297,7 @@ Blockly.FieldVariable.prototype.doValueUpdate_ = function(newId) { * @return {boolean} True if the type is in the list of allowed types. * @private */ -Blockly.FieldVariable.prototype.typeIsAllowed_ = function(type) { +FieldVariable.prototype.typeIsAllowed_ = function(type) { const typeList = this.getVariableTypes_(); if (!typeList) { return true; // If it's null, all types are valid. @@ -315,7 +316,7 @@ Blockly.FieldVariable.prototype.typeIsAllowed_ = function(type) { * @throws {Error} if variableTypes is an empty array. * @private */ -Blockly.FieldVariable.prototype.getVariableTypes_ = function() { +FieldVariable.prototype.getVariableTypes_ = function() { // TODO (#1513): Try to avoid calling this every time the field is edited. let variableTypes = this.variableTypes; if (variableTypes === null) { @@ -344,7 +345,7 @@ Blockly.FieldVariable.prototype.getVariableTypes_ = function() { * field's value is not explicitly set. Defaults to ''. * @private */ -Blockly.FieldVariable.prototype.setTypes_ = function(opt_variableTypes, +FieldVariable.prototype.setTypes_ = function(opt_variableTypes, opt_defaultType) { // If you expected that the default type would be the same as the only entry // in the variable types array, tell the Blockly team by commenting on #1499. @@ -381,7 +382,7 @@ Blockly.FieldVariable.prototype.setTypes_ = function(opt_variableTypes, * be called by the block. * @package */ -Blockly.FieldVariable.prototype.refreshVariableName = function() { +FieldVariable.prototype.refreshVariableName = function() { this.forceRerender(); }; @@ -389,9 +390,9 @@ Blockly.FieldVariable.prototype.refreshVariableName = function() { * Return a sorted list of variable names for variable dropdown menus. * Include a special option at the end for creating a new variable name. * @return {!Array} Array of variable names/id tuples. - * @this {Blockly.FieldVariable} + * @this {FieldVariable} */ -Blockly.FieldVariable.dropdownCreate = function() { +FieldVariable.dropdownCreate = function() { if (!this.variable_) { throw Error('Tried to call dropdownCreate on a variable field with no' + ' variable selected.'); @@ -437,7 +438,7 @@ Blockly.FieldVariable.dropdownCreate = function() { * @param {!Blockly.MenuItem} menuItem The MenuItem selected within menu. * @protected */ -Blockly.FieldVariable.prototype.onItemSelected_ = function(menu, menuItem) { +FieldVariable.prototype.onItemSelected_ = function(menu, menuItem) { const id = menuItem.getValue(); // Handle special cases. if (this.sourceBlock_ && this.sourceBlock_.workspace) { @@ -462,8 +463,10 @@ Blockly.FieldVariable.prototype.onItemSelected_ = function(menu, menuItem) { * @package * @override */ -Blockly.FieldVariable.prototype.referencesVariables = function() { +FieldVariable.prototype.referencesVariables = function() { return true; }; -Blockly.fieldRegistry.register('field_variable', Blockly.FieldVariable); +Blockly.fieldRegistry.register('field_variable', FieldVariable); + +exports = FieldVariable; diff --git a/tests/deps.js b/tests/deps.js index da56c39f2..5b085e8e6 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -60,7 +60,7 @@ goog.addDependency('../../core/field_multilineinput.js', ['Blockly.FieldMultilin goog.addDependency('../../core/field_number.js', ['Blockly.FieldNumber'], ['Blockly.FieldTextInput', 'Blockly.fieldRegistry', 'Blockly.utils.aria', 'Blockly.utils.object'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/field_registry.js', ['Blockly.fieldRegistry'], ['Blockly.registry']); goog.addDependency('../../core/field_textinput.js', ['Blockly.FieldTextInput'], ['Blockly.DropDownDiv', 'Blockly.Events', 'Blockly.Events.BlockChange', 'Blockly.Field', 'Blockly.Msg', 'Blockly.WidgetDiv', 'Blockly.browserEvents', 'Blockly.fieldRegistry', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.KeyCodes', 'Blockly.utils.aria', 'Blockly.utils.dom', 'Blockly.utils.object', 'Blockly.utils.userAgent']); -goog.addDependency('../../core/field_variable.js', ['Blockly.FieldVariable'], ['Blockly.Events.BlockChange', 'Blockly.FieldDropdown', 'Blockly.Msg', 'Blockly.VariableModel', 'Blockly.Variables', 'Blockly.Xml', 'Blockly.fieldRegistry', 'Blockly.internalConstants', 'Blockly.utils', 'Blockly.utils.Size', 'Blockly.utils.object']); +goog.addDependency('../../core/field_variable.js', ['Blockly.FieldVariable'], ['Blockly.Events.BlockChange', 'Blockly.FieldDropdown', 'Blockly.Msg', 'Blockly.VariableModel', 'Blockly.Variables', 'Blockly.Xml', 'Blockly.fieldRegistry', 'Blockly.internalConstants', 'Blockly.utils', 'Blockly.utils.Size', 'Blockly.utils.object'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/flyout_base.js', ['Blockly.Flyout'], ['Blockly.Block', 'Blockly.ComponentManager', 'Blockly.DeleteArea', 'Blockly.Events', 'Blockly.Events.BlockCreate', 'Blockly.Events.VarCreate', 'Blockly.FlyoutMetricsManager', 'Blockly.Gesture', 'Blockly.IFlyout', 'Blockly.ScrollbarPair', 'Blockly.Tooltip', 'Blockly.Touch', 'Blockly.WorkspaceSvg', 'Blockly.Xml', 'Blockly.blockRendering', 'Blockly.browserEvents', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Svg', 'Blockly.utils.dom', 'Blockly.utils.toolbox', 'Blockly.utils.xml']); goog.addDependency('../../core/flyout_button.js', ['Blockly.FlyoutButton'], ['Blockly.Css', 'Blockly.browserEvents', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Svg', 'Blockly.utils.dom', 'Blockly.utils.style'], {'lang': 'es5'}); goog.addDependency('../../core/flyout_horizontal.js', ['Blockly.HorizontalFlyout'], ['Blockly.Block', 'Blockly.DropDownDiv', 'Blockly.Flyout', 'Blockly.Scrollbar', 'Blockly.WidgetDiv', 'Blockly.registry', 'Blockly.utils', 'Blockly.utils.Rect', 'Blockly.utils.object', 'Blockly.utils.toolbox']); From bd5fd3bb088af2127d717dea81825ba178e86f1e Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Thu, 22 Jul 2021 10:37:24 -0700 Subject: [PATCH 3/4] Migrate core/field_variable.js named requires --- core/field_variable.js | 75 +++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/core/field_variable.js b/core/field_variable.js index 11d16532e..1a50f8c88 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -13,22 +13,21 @@ goog.module('Blockly.FieldVariable'); goog.module.declareLegacyNamespace(); +const Block = goog.requireType('Blockly.Block'); +const FieldDropdown = goog.require('Blockly.FieldDropdown'); +const Menu = goog.requireType('Blockly.Menu'); +const MenuItem = goog.requireType('Blockly.MenuItem'); +const Msg = goog.require('Blockly.Msg'); +const Size = goog.require('Blockly.utils.Size'); +const VariableModel = goog.require('Blockly.VariableModel'); +const Variables = goog.require('Blockly.Variables'); +const Xml = goog.require('Blockly.Xml'); +const fieldRegistry = goog.require('Blockly.fieldRegistry'); +const internalConstants = goog.require('Blockly.internalConstants'); +const {inherits} = goog.require('Blockly.utils.object'); +const {replaceMessageReferences} = goog.require('Blockly.utils'); /** @suppress {extraRequire} */ goog.require('Blockly.Events.BlockChange'); -goog.require('Blockly.FieldDropdown'); -goog.require('Blockly.fieldRegistry'); -goog.require('Blockly.internalConstants'); -goog.require('Blockly.Msg'); -goog.require('Blockly.utils'); -goog.require('Blockly.utils.object'); -goog.require('Blockly.utils.Size'); -goog.require('Blockly.VariableModel'); -goog.require('Blockly.Variables'); -goog.require('Blockly.Xml'); - -goog.requireType('Blockly.Block'); -goog.requireType('Blockly.Menu'); -goog.requireType('Blockly.MenuItem'); /** @@ -45,7 +44,7 @@ goog.requireType('Blockly.MenuItem'); * @param {Object=} opt_config A map of options used to configure the field. * See the [field creation documentation]{@link https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/variable#creation} * for a list of properties this parameter supports. - * @extends {Blockly.FieldDropdown} + * @extends {FieldDropdown} * @constructor */ const FieldVariable = function(varName, opt_validator, opt_variableTypes, @@ -58,7 +57,7 @@ const FieldVariable = function(varName, opt_validator, opt_variableTypes, * An array of options for a dropdown list, * or a function which generates these options. * @type {(!Array| - * !function(this:Blockly.FieldDropdown): !Array)} + * !function(this:FieldDropdown): !Array)} * @protected */ this.menuGenerator_ = FieldVariable.dropdownCreate; @@ -73,11 +72,11 @@ const FieldVariable = function(varName, opt_validator, opt_variableTypes, /** * The size of the area rendered by the field. - * @type {Blockly.utils.Size} + * @type {Size} * @protected * @override */ - this.size_ = new Blockly.utils.Size(0, 0); + this.size_ = new Size(0, 0); opt_config && this.configure_(opt_config); opt_validator && this.setValidator(opt_validator); @@ -86,7 +85,7 @@ const FieldVariable = function(varName, opt_validator, opt_variableTypes, this.setTypes_(opt_variableTypes, opt_defaultType); } }; -Blockly.utils.object.inherits(FieldVariable, Blockly.FieldDropdown); +inherits(FieldVariable, FieldDropdown); /** * Construct a FieldVariable from a JSON arg object, @@ -98,7 +97,7 @@ Blockly.utils.object.inherits(FieldVariable, Blockly.FieldDropdown); * @nocollapse */ FieldVariable.fromJson = function(options) { - const varName = Blockly.utils.replaceMessageReferences(options['variable']); + const varName = replaceMessageReferences(options['variable']); // `this` might be a subclass of FieldVariable if that class doesn't override // the static fromJson method. return new this(varName, undefined, undefined, undefined, options); @@ -131,7 +130,7 @@ FieldVariable.prototype.initModel = function() { if (this.variable_) { return; // Initialization already happened. } - const variable = Blockly.Variables.getOrCreateVariablePackage( + const variable = Variables.getOrCreateVariablePackage( this.sourceBlock_.workspace, null, this.defaultVariableName, this.defaultType_); @@ -161,7 +160,7 @@ FieldVariable.prototype.fromXml = function(fieldElement) { const variableType = fieldElement.getAttribute('variabletype') || fieldElement.getAttribute('variableType') || ''; - const variable = Blockly.Variables.getOrCreateVariablePackage( + const variable = Variables.getOrCreateVariablePackage( this.sourceBlock_.workspace, id, variableName, variableType); // This should never happen :) @@ -169,7 +168,7 @@ FieldVariable.prototype.fromXml = function(fieldElement) { throw Error('Serialized variable type with id \'' + variable.getId() + '\' had type ' + variable.type + ', and ' + 'does not match variable field that references it: ' + - Blockly.Xml.domToText(fieldElement) + '.'); + Xml.domToText(fieldElement) + '.'); } this.setValue(variable.getId()); @@ -195,7 +194,7 @@ FieldVariable.prototype.toXml = function(fieldElement) { /** * Attach this field to a block. - * @param {!Blockly.Block} block The block containing this field. + * @param {!Block} block The block containing this field. */ FieldVariable.prototype.setSourceBlock = function(block) { if (block.isShadow()) { @@ -225,7 +224,7 @@ FieldVariable.prototype.getText = function() { * Get the variable model for the selected variable. * Not guaranteed to be in the variable map on the workspace (e.g. if accessed * after the variable has been deleted). - * @return {?Blockly.VariableModel} The selected variable, or null if none was + * @return {?VariableModel} The selected variable, or null if none was * selected. * @package */ @@ -261,7 +260,7 @@ FieldVariable.prototype.doClassValidation_ = function(opt_newValue) { return null; } const newId = /** @type {string} */ (opt_newValue); - const variable = Blockly.Variables.getVariable( + const variable = Variables.getVariable( this.sourceBlock_.workspace, newId); if (!variable) { console.warn('Variable id doesn\'t point to a real variable! ' + @@ -286,7 +285,7 @@ FieldVariable.prototype.doClassValidation_ = function(opt_newValue) { * @protected */ FieldVariable.prototype.doValueUpdate_ = function(newId) { - this.variable_ = Blockly.Variables.getVariable( + this.variable_ = Variables.getVariable( this.sourceBlock_.workspace, /** @type {string} */ (newId)); FieldVariable.superClass_.doValueUpdate_.call(this, newId); }; @@ -410,7 +409,7 @@ FieldVariable.dropdownCreate = function() { variableModelList = variableModelList.concat(variables); } } - variableModelList.sort(Blockly.VariableModel.compareByName); + variableModelList.sort(VariableModel.compareByName); const options = []; for (let i = 0; i < variableModelList.length; i++) { @@ -418,12 +417,12 @@ FieldVariable.dropdownCreate = function() { options[i] = [variableModelList[i].name, variableModelList[i].getId()]; } options.push([ - Blockly.Msg['RENAME_VARIABLE'], Blockly.internalConstants.RENAME_VARIABLE_ID + Msg['RENAME_VARIABLE'], internalConstants.RENAME_VARIABLE_ID ]); - if (Blockly.Msg['DELETE_VARIABLE']) { + if (Msg['DELETE_VARIABLE']) { options.push([ - Blockly.Msg['DELETE_VARIABLE'].replace('%1', name), - Blockly.internalConstants.DELETE_VARIABLE_ID + Msg['DELETE_VARIABLE'].replace('%1', name), + internalConstants.DELETE_VARIABLE_ID ]); } @@ -434,20 +433,20 @@ FieldVariable.dropdownCreate = function() { * Handle the selection of an item in the variable dropdown menu. * Special case the 'Rename variable...' and 'Delete variable...' options. * In the rename case, prompt the user for a new name. - * @param {!Blockly.Menu} menu The Menu component clicked. - * @param {!Blockly.MenuItem} menuItem The MenuItem selected within menu. + * @param {!Menu} menu The Menu component clicked. + * @param {!MenuItem} menuItem The MenuItem selected within menu. * @protected */ FieldVariable.prototype.onItemSelected_ = function(menu, menuItem) { const id = menuItem.getValue(); // Handle special cases. if (this.sourceBlock_ && this.sourceBlock_.workspace) { - if (id == Blockly.internalConstants.RENAME_VARIABLE_ID) { + if (id == internalConstants.RENAME_VARIABLE_ID) { // Rename variable. - Blockly.Variables.renameVariable( + Variables.renameVariable( this.sourceBlock_.workspace, this.variable_); return; - } else if (id == Blockly.internalConstants.DELETE_VARIABLE_ID) { + } else if (id == internalConstants.DELETE_VARIABLE_ID) { // Delete variable. this.sourceBlock_.workspace.deleteVariableById(this.variable_.getId()); return; @@ -467,6 +466,6 @@ FieldVariable.prototype.referencesVariables = function() { return true; }; -Blockly.fieldRegistry.register('field_variable', FieldVariable); +fieldRegistry.register('field_variable', FieldVariable); exports = FieldVariable; From 88aa32510c8549a68d0e234146535710a8689b83 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Thu, 22 Jul 2021 10:38:10 -0700 Subject: [PATCH 4/4] clang-format core/field_variable.js --- core/field_variable.js | 56 ++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/core/field_variable.js b/core/field_variable.js index 1a50f8c88..f700d4a99 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -42,13 +42,14 @@ goog.require('Blockly.Events.BlockChange'); * @param {string=} opt_defaultType The type of variable to create if this * field's value is not explicitly set. Defaults to ''. * @param {Object=} opt_config A map of options used to configure the field. - * See the [field creation documentation]{@link https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/variable#creation} + * See the [field creation documentation]{@link + * https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/variable#creation} * for a list of properties this parameter supports. * @extends {FieldDropdown} * @constructor */ -const FieldVariable = function(varName, opt_validator, opt_variableTypes, - opt_defaultType, opt_config) { +const FieldVariable = function( + varName, opt_validator, opt_variableTypes, opt_defaultType, opt_config) { // The FieldDropdown constructor expects the field's initial value to be // the first entry in the menu generator, which it may or may not be. // Just do the relevant parts of the constructor. @@ -131,8 +132,8 @@ FieldVariable.prototype.initModel = function() { return; // Initialization already happened. } const variable = Variables.getOrCreateVariablePackage( - this.sourceBlock_.workspace, null, - this.defaultVariableName, this.defaultType_); + this.sourceBlock_.workspace, null, this.defaultVariableName, + this.defaultType_); // Don't call setValue because we don't want to cause a rerender. this.doValueUpdate_(variable.getId()); @@ -143,8 +144,8 @@ FieldVariable.prototype.initModel = function() { */ FieldVariable.prototype.shouldAddBorderRect_ = function() { return FieldVariable.superClass_.shouldAddBorderRect_.call(this) && - (!this.getConstants().FIELD_DROPDOWN_NO_BORDER_RECT_SHADOW || - this.sourceBlock_.type != 'variables_get'); + (!this.getConstants().FIELD_DROPDOWN_NO_BORDER_RECT_SHADOW || + this.sourceBlock_.type != 'variables_get'); }; /** @@ -165,10 +166,11 @@ FieldVariable.prototype.fromXml = function(fieldElement) { // This should never happen :) if (variableType != null && variableType !== variable.type) { - throw Error('Serialized variable type with id \'' + - variable.getId() + '\' had type ' + variable.type + ', and ' + - 'does not match variable field that references it: ' + - Xml.domToText(fieldElement) + '.'); + throw Error( + 'Serialized variable type with id \'' + variable.getId() + + '\' had type ' + variable.type + ', and ' + + 'does not match variable field that references it: ' + + Xml.domToText(fieldElement) + '.'); } this.setValue(variable.getId()); @@ -260,10 +262,10 @@ FieldVariable.prototype.doClassValidation_ = function(opt_newValue) { return null; } const newId = /** @type {string} */ (opt_newValue); - const variable = Variables.getVariable( - this.sourceBlock_.workspace, newId); + const variable = Variables.getVariable(this.sourceBlock_.workspace, newId); if (!variable) { - console.warn('Variable id doesn\'t point to a real variable! ' + + console.warn( + 'Variable id doesn\'t point to a real variable! ' + 'ID was ' + newId); return null; } @@ -328,8 +330,8 @@ FieldVariable.prototype.getVariableTypes_ = function() { if (variableTypes.length == 0) { // Throw an error if variableTypes is an empty list. const name = this.getText(); - throw Error('\'variableTypes\' of field variable ' + - name + ' was an empty list'); + throw Error( + '\'variableTypes\' of field variable ' + name + ' was an empty list'); } return variableTypes; }; @@ -344,8 +346,8 @@ FieldVariable.prototype.getVariableTypes_ = function() { * field's value is not explicitly set. Defaults to ''. * @private */ -FieldVariable.prototype.setTypes_ = function(opt_variableTypes, - opt_defaultType) { +FieldVariable.prototype.setTypes_ = function( + opt_variableTypes, opt_defaultType) { // If you expected that the default type would be the same as the only entry // in the variable types array, tell the Blockly team by commenting on #1499. const defaultType = opt_defaultType || ''; @@ -363,11 +365,13 @@ FieldVariable.prototype.setTypes_ = function(opt_variableTypes, } } if (!isInArray) { - throw Error('Invalid default type \'' + defaultType + '\' in ' + + throw Error( + 'Invalid default type \'' + defaultType + '\' in ' + 'the definition of a FieldVariable'); } } else { - throw Error('\'variableTypes\' was not an array in the definition of ' + + throw Error( + '\'variableTypes\' was not an array in the definition of ' + 'a FieldVariable'); } // Only update the field once all checks pass. @@ -393,7 +397,8 @@ FieldVariable.prototype.refreshVariableName = function() { */ FieldVariable.dropdownCreate = function() { if (!this.variable_) { - throw Error('Tried to call dropdownCreate on a variable field with no' + + throw Error( + 'Tried to call dropdownCreate on a variable field with no' + ' variable selected.'); } const name = this.getText(); @@ -405,7 +410,7 @@ FieldVariable.dropdownCreate = function() { for (let i = 0; i < variableTypes.length; i++) { const variableType = variableTypes[i]; const variables = - this.sourceBlock_.workspace.getVariablesOfType(variableType); + this.sourceBlock_.workspace.getVariablesOfType(variableType); variableModelList = variableModelList.concat(variables); } } @@ -416,9 +421,7 @@ FieldVariable.dropdownCreate = function() { // Set the UUID as the internal representation of the variable. options[i] = [variableModelList[i].name, variableModelList[i].getId()]; } - options.push([ - Msg['RENAME_VARIABLE'], internalConstants.RENAME_VARIABLE_ID - ]); + options.push([Msg['RENAME_VARIABLE'], internalConstants.RENAME_VARIABLE_ID]); if (Msg['DELETE_VARIABLE']) { options.push([ Msg['DELETE_VARIABLE'].replace('%1', name), @@ -443,8 +446,7 @@ FieldVariable.prototype.onItemSelected_ = function(menu, menuItem) { if (this.sourceBlock_ && this.sourceBlock_.workspace) { if (id == internalConstants.RENAME_VARIABLE_ID) { // Rename variable. - Variables.renameVariable( - this.sourceBlock_.workspace, this.variable_); + Variables.renameVariable(this.sourceBlock_.workspace, this.variable_); return; } else if (id == internalConstants.DELETE_VARIABLE_ID) { // Delete variable.