Skip to content

Commit

Permalink
Added XML Field (De)Serialization
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jschanker committed Jun 21, 2021
1 parent d630d76 commit 1964e86
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
36 changes: 31 additions & 5 deletions core/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
};

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

Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions core/field_dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down
6 changes: 4 additions & 2 deletions core/field_multilineinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down
2 changes: 2 additions & 0 deletions core/field_variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Blockly.FieldVariable.prototype.fromXml = function(fieldElement) {
}

this.setValue(variable.getId());
this.setEnabledFromXml_(fieldElement);
};

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

Expand Down

0 comments on commit 1964e86

Please sign in to comment.