Allow inheriting of fromJson in fields

This commit is contained in:
Neil Fraser
2021-06-30 12:00:33 -07:00
committed by Neil Fraser
parent 6ccff4431d
commit cba7a68207
12 changed files with 42 additions and 13 deletions

View File

@@ -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;
};

View File

@@ -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);
};
/**

View File

@@ -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);
};
/**

View File

@@ -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);
};
/**

View File

@@ -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);
};

View File

@@ -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);
};

View File

@@ -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);
};
/**

View File

@@ -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);
};
/**

View File

@@ -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);
};
/**

View File

@@ -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);
};

View File

@@ -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);
};
/**

View File

@@ -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);
};
/**