From cba7a682074f84f7bd17acebe067299f71d119e0 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Wed, 30 Jun 2021 12:00:33 -0700 Subject: [PATCH] Allow inheriting of fromJson in fields --- core/field.js | 11 ++++++++++- core/field_angle.js | 4 +++- core/field_checkbox.js | 4 +++- core/field_colour.js | 4 +++- core/field_dropdown.js | 2 ++ core/field_image.js | 5 +++-- core/field_label.js | 4 +++- core/field_label_serializable.js | 4 +++- core/field_multilineinput.js | 4 +++- core/field_number.js | 4 +++- core/field_textinput.js | 4 +++- core/field_variable.js | 5 +++-- 12 files changed, 42 insertions(+), 13 deletions(-) diff --git a/core/field.js b/core/field.js index 9d52bacbb..fd9e7185b 100644 --- a/core/field.js +++ b/core/field.js @@ -154,6 +154,15 @@ Blockly.Field = function(value, opt_validator, opt_config) { opt_validator && this.setValidator(opt_validator); }; +/** + * Construct a Field from a JSON arg object. + * @param {!Object} options_ A JSON object with options (options). + * @return {!Blockly.Field} The new field instance. + */ +Blockly.Field.fromJson = function(options_) { + throw Error("Field subclass doesn't define fromJson"); +}; + /** * The default value for this field. * @type {*} @@ -276,7 +285,7 @@ Blockly.Field.prototype.configure_ = function(config) { */ Blockly.Field.prototype.setSourceBlock = function(block) { if (this.sourceBlock_) { - throw Error('Field already bound to a block.'); + throw Error('Field already bound to a block'); } this.sourceBlock_ = block; }; diff --git a/core/field_angle.js b/core/field_angle.js index f37d6c9b9..778ebe066 100644 --- a/core/field_angle.js +++ b/core/field_angle.js @@ -135,7 +135,9 @@ Blockly.FieldAngle.prototype.DEFAULT_VALUE = 0; * @nocollapse */ Blockly.FieldAngle.fromJson = function(options) { - return new Blockly.FieldAngle(options['angle'], undefined, options); + // `this` might be a subclass of FieldAngle if that class doesn't override + // the static fromJson method. + return new this(options['angle'], undefined, options); }; /** diff --git a/core/field_checkbox.js b/core/field_checkbox.js index 26927b4d5..207b29f7f 100644 --- a/core/field_checkbox.js +++ b/core/field_checkbox.js @@ -63,7 +63,9 @@ Blockly.FieldCheckbox.prototype.DEFAULT_VALUE = false; * @nocollapse */ Blockly.FieldCheckbox.fromJson = function(options) { - return new Blockly.FieldCheckbox(options['checked'], undefined, options); + // `this` might be a subclass of FieldCheckbox if that class doesn't override + // the static fromJson method. + return new this(options['checked'], undefined, options); }; /** diff --git a/core/field_colour.js b/core/field_colour.js index 1d3d617f2..17d593f0e 100644 --- a/core/field_colour.js +++ b/core/field_colour.js @@ -104,7 +104,9 @@ Blockly.utils.object.inherits(Blockly.FieldColour, Blockly.Field); * @nocollapse */ Blockly.FieldColour.fromJson = function(options) { - return new Blockly.FieldColour(options['colour'], undefined, options); + // `this` might be a subclass of FieldColour if that class doesn't override + // the static fromJson method. + return new this(options['colour'], undefined, options); }; /** diff --git a/core/field_dropdown.js b/core/field_dropdown.js index 66c7d11b1..7b9ff6356 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -149,6 +149,8 @@ Blockly.FieldDropdown.ImageProperties; * @nocollapse */ Blockly.FieldDropdown.fromJson = function(options) { + // `this` might be a subclass of FieldDropdown if that class doesn't override + // the static fromJson method. return new Blockly.FieldDropdown(options['options'], undefined, options); }; diff --git a/core/field_image.js b/core/field_image.js index 9fe31f80f..eed381b63 100644 --- a/core/field_image.js +++ b/core/field_image.js @@ -132,8 +132,9 @@ Blockly.FieldImage.prototype.DEFAULT_VALUE = ''; * @nocollapse */ Blockly.FieldImage.fromJson = function(options) { - return new Blockly.FieldImage( - options['src'], options['width'], options['height'], + // `this` might be a subclass of FieldImage if that class doesn't override + // the static fromJson method. + return new this(options['src'], options['width'], options['height'], undefined, undefined, undefined, options); }; diff --git a/core/field_label.js b/core/field_label.js index c332dde23..5dc434c92 100644 --- a/core/field_label.js +++ b/core/field_label.js @@ -65,7 +65,9 @@ Blockly.FieldLabel.prototype.DEFAULT_VALUE = ''; */ Blockly.FieldLabel.fromJson = function(options) { var text = Blockly.utils.replaceMessageReferences(options['text']); - return new Blockly.FieldLabel(text, undefined, options); + // `this` might be a subclass of FieldLabel if that class doesn't override + // the static fromJson method. + return new this(text, undefined, options); }; /** diff --git a/core/field_label_serializable.js b/core/field_label_serializable.js index eeaac9ffa..0201db130 100644 --- a/core/field_label_serializable.js +++ b/core/field_label_serializable.js @@ -48,7 +48,9 @@ Blockly.utils.object.inherits(Blockly.FieldLabelSerializable, */ Blockly.FieldLabelSerializable.fromJson = function(options) { var text = Blockly.utils.replaceMessageReferences(options['text']); - return new Blockly.FieldLabelSerializable(text, undefined, options); + // `this` might be a subclass of FieldLabelSerializable if that class doesn't + // override the static fromJson method. + return new this(text, undefined, options); }; /** diff --git a/core/field_multilineinput.js b/core/field_multilineinput.js index c6260e4fd..7df835ebe 100644 --- a/core/field_multilineinput.js +++ b/core/field_multilineinput.js @@ -89,7 +89,9 @@ Blockly.FieldMultilineInput.prototype.configure_ = function(config) { */ Blockly.FieldMultilineInput.fromJson = function(options) { var text = Blockly.utils.replaceMessageReferences(options['text']); - return new Blockly.FieldMultilineInput(text, undefined, options); + // `this` might be a subclass of FieldLabel if that class doesn't override + // the static fromJson method. + return new this(text, undefined, options); }; /** diff --git a/core/field_number.js b/core/field_number.js index 2cfa2ca23..369a161f8 100644 --- a/core/field_number.js +++ b/core/field_number.js @@ -91,7 +91,9 @@ Blockly.FieldNumber.prototype.DEFAULT_VALUE = 0; * @nocollapse */ Blockly.FieldNumber.fromJson = function(options) { - return new Blockly.FieldNumber(options['value'], + // `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); }; diff --git a/core/field_textinput.js b/core/field_textinput.js index 9b389c5a8..c66b7a4f4 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -110,7 +110,9 @@ Blockly.FieldTextInput.prototype.DEFAULT_VALUE = ''; */ Blockly.FieldTextInput.fromJson = function(options) { var text = Blockly.utils.replaceMessageReferences(options['text']); - return new Blockly.FieldTextInput(text, undefined, options); + // `this` might be a subclass of FieldTextInput if that class doesn't override + // the static fromJson method. + return new this(text, undefined, options); }; /** diff --git a/core/field_variable.js b/core/field_variable.js index efdf08bfa..18f2d20b4 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -100,8 +100,9 @@ Blockly.utils.object.inherits(Blockly.FieldVariable, Blockly.FieldDropdown); */ Blockly.FieldVariable.fromJson = function(options) { var varName = Blockly.utils.replaceMessageReferences(options['variable']); - return new Blockly.FieldVariable( - varName, undefined, undefined, undefined, options); + // `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); }; /**