From 94bdd1850582ef4332c76f3f9c486fba9c48367b Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Wed, 21 Jul 2021 08:23:17 -0700 Subject: [PATCH 1/4] Migrate core/field_number.js to ES6 const/let --- core/field_number.js | 10 +++++----- tests/deps.js | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/field_number.js b/core/field_number.js index 369a161f8..d45c790cd 100644 --- a/core/field_number.js +++ b/core/field_number.js @@ -225,14 +225,14 @@ Blockly.FieldNumber.prototype.setPrecision = function(precision) { */ Blockly.FieldNumber.prototype.setPrecisionInternal_ = function(precision) { this.precision_ = Number(precision) || 0; - var precisionString = String(this.precision_); + let precisionString = String(this.precision_); if (precisionString.indexOf('e') != -1) { // String() is fast. But it turns .0000001 into '1e-7'. // Use the much slower toLocaleString to access all the digits. precisionString = this.precision_.toLocaleString('en-US', {maximumFractionDigits: 20}); } - var decimalIndex = precisionString.indexOf('.'); + const decimalIndex = precisionString.indexOf('.'); if (decimalIndex == -1) { // If the precision is 0 (float) allow any number of decimals, // otherwise allow none. @@ -265,7 +265,7 @@ Blockly.FieldNumber.prototype.doClassValidation_ = function(opt_newValue) { return null; } // Clean up text. - var newValue = String(opt_newValue); + let newValue = String(opt_newValue); // TODO: Handle cases like 'ten', '1.203,14', etc. // 'O' is sometimes mistaken for '0' by inexperienced users. newValue = newValue.replace(/O/ig, '0'); @@ -275,7 +275,7 @@ Blockly.FieldNumber.prototype.doClassValidation_ = function(opt_newValue) { newValue = newValue.replace(/infinity/i, 'Infinity'); // Clean up number. - var n = Number(newValue || 0); + let n = Number(newValue || 0); if (isNaN(n)) { // Invalid number. return null; @@ -300,7 +300,7 @@ Blockly.FieldNumber.prototype.doClassValidation_ = function(opt_newValue) { * @override */ Blockly.FieldNumber.prototype.widgetCreate_ = function() { - var htmlInput = Blockly.FieldNumber.superClass_.widgetCreate_.call(this); + const htmlInput = Blockly.FieldNumber.superClass_.widgetCreate_.call(this); // Set the accessibility state if (this.min_ > -Infinity) { diff --git a/tests/deps.js b/tests/deps.js index 979c58c74..5485014dd 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -57,7 +57,7 @@ goog.addDependency('../../core/field_image.js', ['Blockly.FieldImage'], ['Blockl goog.addDependency('../../core/field_label.js', ['Blockly.FieldLabel'], ['Blockly.Field', 'Blockly.fieldRegistry', 'Blockly.utils', 'Blockly.utils.dom', 'Blockly.utils.object']); goog.addDependency('../../core/field_label_serializable.js', ['Blockly.FieldLabelSerializable'], ['Blockly.FieldLabel', 'Blockly.fieldRegistry', 'Blockly.utils', 'Blockly.utils.object']); goog.addDependency('../../core/field_multilineinput.js', ['Blockly.FieldMultilineInput'], ['Blockly.Css', 'Blockly.Field', 'Blockly.FieldTextInput', 'Blockly.WidgetDiv', 'Blockly.fieldRegistry', 'Blockly.utils', 'Blockly.utils.KeyCodes', 'Blockly.utils.Svg', 'Blockly.utils.aria', 'Blockly.utils.dom', 'Blockly.utils.object', 'Blockly.utils.userAgent'], {'lang': 'es5'}); -goog.addDependency('../../core/field_number.js', ['Blockly.FieldNumber'], ['Blockly.FieldTextInput', 'Blockly.fieldRegistry', 'Blockly.utils.aria', 'Blockly.utils.object']); +goog.addDependency('../../core/field_number.js', ['Blockly.FieldNumber'], ['Blockly.FieldTextInput', 'Blockly.fieldRegistry', 'Blockly.utils.aria', 'Blockly.utils.object'], {'lang': 'es6'}); 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.constants', 'Blockly.fieldRegistry', 'Blockly.utils', 'Blockly.utils.Size', 'Blockly.utils.object']); From 0a1311c9b5002d8edd568eb24f8e203743b699f2 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Wed, 21 Jul 2021 08:26:41 -0700 Subject: [PATCH 2/4] Migrate core/field_number.js to goog.module --- core/field_number.js | 51 +++++++++++++++++++++++--------------------- tests/deps.js | 2 +- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/core/field_number.js b/core/field_number.js index d45c790cd..67900f344 100644 --- a/core/field_number.js +++ b/core/field_number.js @@ -10,7 +10,8 @@ */ 'use strict'; -goog.provide('Blockly.FieldNumber'); +goog.module('Blockly.FieldNumber'); +goog.module.declareLegacyNamespace(); goog.require('Blockly.fieldRegistry'); goog.require('Blockly.FieldTextInput'); @@ -34,7 +35,7 @@ goog.require('Blockly.utils.object'); * @extends {Blockly.FieldTextInput} * @constructor */ -Blockly.FieldNumber = function(opt_value, opt_min, opt_max, opt_precision, +const FieldNumber = function(opt_value, opt_min, opt_max, opt_precision, opt_validator, opt_config) { /** @@ -66,31 +67,31 @@ Blockly.FieldNumber = function(opt_value, opt_min, opt_max, opt_precision, */ this.decimalPlaces_ = null; - Blockly.FieldNumber.superClass_.constructor.call( + FieldNumber.superClass_.constructor.call( this, opt_value, opt_validator, opt_config); if (!opt_config) { // Only do one kind of configuration or the other. this.setConstraints(opt_min, opt_max, opt_precision); } }; -Blockly.utils.object.inherits(Blockly.FieldNumber, Blockly.FieldTextInput); +Blockly.utils.object.inherits(FieldNumber, Blockly.FieldTextInput); /** * The default value for this field. * @type {*} * @protected */ -Blockly.FieldNumber.prototype.DEFAULT_VALUE = 0; +FieldNumber.prototype.DEFAULT_VALUE = 0; /** * Construct a FieldNumber from a JSON arg object. * @param {!Object} options A JSON object with options (value, min, max, and * precision). - * @return {!Blockly.FieldNumber} The new field instance. + * @return {!FieldNumber} The new field instance. * @package * @nocollapse */ -Blockly.FieldNumber.fromJson = function(options) { +FieldNumber.fromJson = function(options) { // `this` might be a subclass of FieldNumber if that class doesn't override // the static fromJson method. return new this(options['value'], @@ -102,7 +103,7 @@ Blockly.FieldNumber.fromJson = function(options) { * are not. Editable fields should also be serializable. * @type {boolean} */ -Blockly.FieldNumber.prototype.SERIALIZABLE = true; +FieldNumber.prototype.SERIALIZABLE = true; /** * Configure the field based on the given map of options. @@ -110,8 +111,8 @@ Blockly.FieldNumber.prototype.SERIALIZABLE = true; * @protected * @override */ -Blockly.FieldNumber.prototype.configure_ = function(config) { - Blockly.FieldNumber.superClass_.configure_.call(this, config); +FieldNumber.prototype.configure_ = function(config) { + FieldNumber.superClass_.configure_.call(this, config); this.setMinInternal_(config['min']); this.setMaxInternal_(config['max']); this.setPrecisionInternal_(config['precision']); @@ -128,7 +129,7 @@ Blockly.FieldNumber.prototype.configure_ = function(config) { * @param {?(number|string|undefined)} max Maximum value. * @param {?(number|string|undefined)} precision Precision for value. */ -Blockly.FieldNumber.prototype.setConstraints = function(min, max, precision) { +FieldNumber.prototype.setConstraints = function(min, max, precision) { this.setMinInternal_(min); this.setMaxInternal_(max); this.setPrecisionInternal_(precision); @@ -139,7 +140,7 @@ Blockly.FieldNumber.prototype.setConstraints = function(min, max, precision) { * Sets the minimum value this field can contain. Updates the value to reflect. * @param {?(number|string|undefined)} min Minimum value. */ -Blockly.FieldNumber.prototype.setMin = function(min) { +FieldNumber.prototype.setMin = function(min) { this.setMinInternal_(min); this.setValue(this.getValue()); }; @@ -150,7 +151,7 @@ Blockly.FieldNumber.prototype.setMin = function(min) { * @param {?(number|string|undefined)} min Minimum value. * @private */ -Blockly.FieldNumber.prototype.setMinInternal_ = function(min) { +FieldNumber.prototype.setMinInternal_ = function(min) { if (min == null) { this.min_ = -Infinity; } else { @@ -166,7 +167,7 @@ Blockly.FieldNumber.prototype.setMinInternal_ = function(min) { * -Infinity. * @return {number} The current minimum value this field can contain. */ -Blockly.FieldNumber.prototype.getMin = function() { +FieldNumber.prototype.getMin = function() { return this.min_; }; @@ -174,7 +175,7 @@ Blockly.FieldNumber.prototype.getMin = function() { * Sets the maximum value this field can contain. Updates the value to reflect. * @param {?(number|string|undefined)} max Maximum value. */ -Blockly.FieldNumber.prototype.setMax = function(max) { +FieldNumber.prototype.setMax = function(max) { this.setMaxInternal_(max); this.setValue(this.getValue()); }; @@ -185,7 +186,7 @@ Blockly.FieldNumber.prototype.setMax = function(max) { * @param {?(number|string|undefined)} max Maximum value. * @private */ -Blockly.FieldNumber.prototype.setMaxInternal_ = function(max) { +FieldNumber.prototype.setMaxInternal_ = function(max) { if (max == null) { this.max_ = Infinity; } else { @@ -201,7 +202,7 @@ Blockly.FieldNumber.prototype.setMaxInternal_ = function(max) { * Infinity. * @return {number} The current maximum value this field can contain. */ -Blockly.FieldNumber.prototype.getMax = function() { +FieldNumber.prototype.getMax = function() { return this.max_; }; @@ -211,7 +212,7 @@ Blockly.FieldNumber.prototype.getMax = function() { * @param {?(number|string|undefined)} precision The number to which the * field's value is rounded. */ -Blockly.FieldNumber.prototype.setPrecision = function(precision) { +FieldNumber.prototype.setPrecision = function(precision) { this.setPrecisionInternal_(precision); this.setValue(this.getValue()); }; @@ -223,7 +224,7 @@ Blockly.FieldNumber.prototype.setPrecision = function(precision) { * field's value is rounded. * @private */ -Blockly.FieldNumber.prototype.setPrecisionInternal_ = function(precision) { +FieldNumber.prototype.setPrecisionInternal_ = function(precision) { this.precision_ = Number(precision) || 0; let precisionString = String(this.precision_); if (precisionString.indexOf('e') != -1) { @@ -248,7 +249,7 @@ Blockly.FieldNumber.prototype.setPrecisionInternal_ = function(precision) { * the value is not rounded. * @return {number} The number to which this field's value is rounded. */ -Blockly.FieldNumber.prototype.getPrecision = function() { +FieldNumber.prototype.getPrecision = function() { return this.precision_; }; @@ -260,7 +261,7 @@ Blockly.FieldNumber.prototype.getPrecision = function() { * @protected * @override */ -Blockly.FieldNumber.prototype.doClassValidation_ = function(opt_newValue) { +FieldNumber.prototype.doClassValidation_ = function(opt_newValue) { if (opt_newValue === null) { return null; } @@ -299,8 +300,8 @@ Blockly.FieldNumber.prototype.doClassValidation_ = function(opt_newValue) { * @protected * @override */ -Blockly.FieldNumber.prototype.widgetCreate_ = function() { - const htmlInput = Blockly.FieldNumber.superClass_.widgetCreate_.call(this); +FieldNumber.prototype.widgetCreate_ = function() { + const htmlInput = FieldNumber.superClass_.widgetCreate_.call(this); // Set the accessibility state if (this.min_ > -Infinity) { @@ -314,4 +315,6 @@ Blockly.FieldNumber.prototype.widgetCreate_ = function() { return htmlInput; }; -Blockly.fieldRegistry.register('field_number', Blockly.FieldNumber); +Blockly.fieldRegistry.register('field_number', FieldNumber); + +exports = FieldNumber; diff --git a/tests/deps.js b/tests/deps.js index 5485014dd..80ddf87c8 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -57,7 +57,7 @@ goog.addDependency('../../core/field_image.js', ['Blockly.FieldImage'], ['Blockl goog.addDependency('../../core/field_label.js', ['Blockly.FieldLabel'], ['Blockly.Field', 'Blockly.fieldRegistry', 'Blockly.utils', 'Blockly.utils.dom', 'Blockly.utils.object']); goog.addDependency('../../core/field_label_serializable.js', ['Blockly.FieldLabelSerializable'], ['Blockly.FieldLabel', 'Blockly.fieldRegistry', 'Blockly.utils', 'Blockly.utils.object']); goog.addDependency('../../core/field_multilineinput.js', ['Blockly.FieldMultilineInput'], ['Blockly.Css', 'Blockly.Field', 'Blockly.FieldTextInput', 'Blockly.WidgetDiv', 'Blockly.fieldRegistry', 'Blockly.utils', 'Blockly.utils.KeyCodes', 'Blockly.utils.Svg', 'Blockly.utils.aria', 'Blockly.utils.dom', 'Blockly.utils.object', 'Blockly.utils.userAgent'], {'lang': 'es5'}); -goog.addDependency('../../core/field_number.js', ['Blockly.FieldNumber'], ['Blockly.FieldTextInput', 'Blockly.fieldRegistry', 'Blockly.utils.aria', 'Blockly.utils.object'], {'lang': 'es6'}); +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.constants', 'Blockly.fieldRegistry', 'Blockly.utils', 'Blockly.utils.Size', 'Blockly.utils.object']); From 25fc34925f194c3c703cb9b4c7945e34950f1b34 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Wed, 21 Jul 2021 08:31:39 -0700 Subject: [PATCH 3/4] Migrate core/field_number.js to named requires --- core/field_number.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/core/field_number.js b/core/field_number.js index 67900f344..e8d628572 100644 --- a/core/field_number.js +++ b/core/field_number.js @@ -13,10 +13,10 @@ goog.module('Blockly.FieldNumber'); goog.module.declareLegacyNamespace(); -goog.require('Blockly.fieldRegistry'); -goog.require('Blockly.FieldTextInput'); -goog.require('Blockly.utils.aria'); -goog.require('Blockly.utils.object'); +const FieldTextInput = goog.require('Blockly.FieldTextInput'); +const aria = goog.require('Blockly.utils.aria'); +const {inherits} = goog.require('Blockly.utils.object'); +const {register} = goog.require('Blockly.fieldRegistry'); /** @@ -32,7 +32,7 @@ goog.require('Blockly.utils.object'); * @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/number#creation} * for a list of properties this parameter supports. - * @extends {Blockly.FieldTextInput} + * @extends {FieldTextInput} * @constructor */ const FieldNumber = function(opt_value, opt_min, opt_max, opt_precision, @@ -74,7 +74,7 @@ const FieldNumber = function(opt_value, opt_min, opt_max, opt_precision, this.setConstraints(opt_min, opt_max, opt_precision); } }; -Blockly.utils.object.inherits(FieldNumber, Blockly.FieldTextInput); +inherits(FieldNumber, FieldTextInput); /** * The default value for this field. @@ -305,16 +305,16 @@ FieldNumber.prototype.widgetCreate_ = function() { // Set the accessibility state if (this.min_ > -Infinity) { - Blockly.utils.aria.setState(htmlInput, - Blockly.utils.aria.State.VALUEMIN, this.min_); + aria.setState(htmlInput, + aria.State.VALUEMIN, this.min_); } if (this.max_ < Infinity) { - Blockly.utils.aria.setState(htmlInput, - Blockly.utils.aria.State.VALUEMAX, this.max_); + aria.setState(htmlInput, + aria.State.VALUEMAX, this.max_); } return htmlInput; }; -Blockly.fieldRegistry.register('field_number', FieldNumber); +register('field_number', FieldNumber); exports = FieldNumber; From e15470bbd0f7ca74e0452dba8606fe2344e1539b Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Wed, 21 Jul 2021 08:32:09 -0700 Subject: [PATCH 4/4] clang-format core/field_number.js --- core/field_number.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/core/field_number.js b/core/field_number.js index e8d628572..8389f477f 100644 --- a/core/field_number.js +++ b/core/field_number.js @@ -30,14 +30,14 @@ const {register} = goog.require('Blockly.fieldRegistry'); * changes to the field's value. Takes in a number & returns a validated * number, or null to abort the change. * @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/number#creation} + * See the [field creation documentation]{@link + * https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/number#creation} * for a list of properties this parameter supports. * @extends {FieldTextInput} * @constructor */ -const FieldNumber = function(opt_value, opt_min, opt_max, opt_precision, - opt_validator, opt_config) { - +const FieldNumber = function( + opt_value, opt_min, opt_max, opt_precision, opt_validator, opt_config) { /** * The minimum value this number field can contain. * @type {number} @@ -94,8 +94,8 @@ FieldNumber.prototype.DEFAULT_VALUE = 0; FieldNumber.fromJson = function(options) { // `this` might be a subclass of FieldNumber if that class doesn't override // the static fromJson method. - return new this(options['value'], - undefined, undefined, undefined, undefined, options); + return new this( + options['value'], undefined, undefined, undefined, undefined, options); }; /** @@ -305,12 +305,10 @@ FieldNumber.prototype.widgetCreate_ = function() { // Set the accessibility state if (this.min_ > -Infinity) { - aria.setState(htmlInput, - aria.State.VALUEMIN, this.min_); + aria.setState(htmlInput, aria.State.VALUEMIN, this.min_); } if (this.max_ < Infinity) { - aria.setState(htmlInput, - aria.State.VALUEMAX, this.max_); + aria.setState(htmlInput, aria.State.VALUEMAX, this.max_); } return htmlInput; };