Added XML Field (De)Serialization

* Call parent method in FieldDropdown's fromXml
* Added protected helper methods to handle serialization/deserialization of enabled property/attribute of fields
* Minor changes to annotations to account for field disabling and 4 spaces per line break per style guide
This commit is contained in:
jschanker
2021-06-21 03:12:15 -04:00
parent d630d767e2
commit 1964e866b6
4 changed files with 41 additions and 11 deletions

View File

@@ -399,14 +399,39 @@ Blockly.Field.prototype.bindEvents_ = function() {
};
/**
* Sets the field's value based on the given XML element. Should only be
* called by Blockly.Xml.
* Disables field if the given XML element specifies this.
* @param {!Element} fieldElement The element containing info about the
* field's state.
* field's state.
* @protected
*/
Blockly.Field.prototype.setEnabledFromXml_ = function(fieldElement) {
if (fieldElement.getAttribute('enabled') === 'false') {
this.setEnabled(false);
}
};
/**
* Sets the field's value and possibly disables field based on the given XML
* element. Should only be called by Blockly.Xml.
* @param {!Element} fieldElement The element containing info about the
* field's state.
* @package
*/
Blockly.Field.prototype.fromXml = function(fieldElement) {
this.setValue(fieldElement.textContent);
this.setEnabledFromXml_(fieldElement);
};
/**
* Adds false enabled attribute to the given XML element if field is disabled.
* @param {!Element} fieldElement The element containing info about the
* field's state.
* @protected
*/
Blockly.Field.prototype.addDisabledToXml_ = function(fieldElement) {
if (!this.enabled_) {
fieldElement.setAttribute('enabled', 'false');
}
};
/**
@@ -418,6 +443,7 @@ Blockly.Field.prototype.fromXml = function(fieldElement) {
*/
Blockly.Field.prototype.toXml = function(fieldElement) {
fieldElement.textContent = this.getValue();
this.addDisabledToXml_(fieldElement);
return fieldElement;
};
@@ -460,7 +486,7 @@ Blockly.Field.prototype.updateEditable = function() {
/**
* Set whether this field's value can be changed using the editor when the
* source block is editable.
* source block is editable.
* @param {boolean} enabled True if enabled.
*/
Blockly.Field.prototype.setEnabled = function(enabled) {
@@ -470,7 +496,7 @@ Blockly.Field.prototype.setEnabled = function(enabled) {
/**
* Check whether this field's value can be changed using the editor when the
* source block is editable.
* source block is editable.
* @return {boolean} Whether this field is enabled.
*/
Blockly.Field.prototype.isEnabled = function() {

View File

@@ -153,17 +153,17 @@ Blockly.FieldDropdown.fromJson = function(options) {
};
/**
* Sets the field's value based on the given XML element. Should only be
* called by Blockly.Xml.
* Sets the field's value and possibly disables field based on the given XML
* element. Should only be called by Blockly.Xml.
* @param {!Element} fieldElement The element containing info about the
* field's state.
* field's state.
* @package
*/
Blockly.FieldDropdown.prototype.fromXml = function(fieldElement) {
if (this.isOptionListDynamic()) {
this.getOptions(false);
}
this.setValue(fieldElement.textContent);
Blockly.FieldDropdown.superClass_.fromXml.call(this, fieldElement);
};
/**

View File

@@ -105,18 +105,20 @@ Blockly.FieldMultilineInput.prototype.toXml = function(fieldElement) {
// `Blockly.Xml.domToText` will appear on a single line (this is a limitation
// of the plain-text format).
fieldElement.textContent = this.getValue().replace(/\n/g, '
');
this.addDisabledToXml_(fieldElement);
return fieldElement;
};
/**
* Sets the field's value based on the given XML element. Should only be
* called by Blockly.Xml.
* Sets the field's value and possibly disables field based on the given XML
* element. Should only be called by Blockly.Xml.
* @param {!Element} fieldElement The element containing info about the
* field's state.
* @package
*/
Blockly.FieldMultilineInput.prototype.fromXml = function(fieldElement) {
this.setValue(fieldElement.textContent.replace(/
/g, '\n'));
this.setEnabledFromXml_(fieldElement);
};
/**

View File

@@ -173,6 +173,7 @@ Blockly.FieldVariable.prototype.fromXml = function(fieldElement) {
}
this.setValue(variable.getId());
this.setEnabledFromXml_(fieldElement);
};
/**
@@ -190,6 +191,7 @@ Blockly.FieldVariable.prototype.toXml = function(fieldElement) {
if (this.variable_.type) {
fieldElement.setAttribute('variabletype', this.variable_.type);
}
this.addDisabledToXml_(fieldElement);
return fieldElement;
};