Add a serializable attribute to field, distinct from EDITABLE

This commit is contained in:
Rachel Fenichel 2017-10-20 13:58:20 -07:00
parent 514fc04738
commit cb058a69af
5 changed files with 51 additions and 19 deletions

View file

@ -31,7 +31,7 @@ goog.require('Blockly.Colours');
goog.require('Blockly.Comment'); goog.require('Blockly.Comment');
goog.require('Blockly.Connection'); goog.require('Blockly.Connection');
goog.require('Blockly.Extensions'); goog.require('Blockly.Extensions');
goog.require('Blockly.FieldLabelEditable'); goog.require('Blockly.FieldLabelSerializable');
goog.require('Blockly.FieldVariableGetter'); goog.require('Blockly.FieldVariableGetter');
goog.require('Blockly.Input'); goog.require('Blockly.Input');
goog.require('Blockly.Mutator'); goog.require('Blockly.Mutator');
@ -1337,7 +1337,7 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
field = Blockly.Block.newFieldLabelFromJson_(element); field = Blockly.Block.newFieldLabelFromJson_(element);
break; break;
case 'field_label_editable': case 'field_label_editable':
field = Blockly.Block.newFieldLabelEditableFromJson_(element); field = Blockly.Block.newFieldLabelSerializableFromJson_(element);
break; break;
case 'field_input': case 'field_input':
field = Blockly.Block.newFieldTextInputFromJson_(element); field = Blockly.Block.newFieldTextInputFromJson_(element);
@ -1449,15 +1449,15 @@ Blockly.Block.newFieldLabelFromJson_ = function(options) {
}; };
/** /**
* Helper function to construct a FieldLabelEditable from a JSON arg object, * Helper function to construct a FieldLabelSerializable from a JSON arg object,
* dereferencing any string table references. * dereferencing any string table references.
* @param {!Object} options A JSON object with options (text, and class). * @param {!Object} options A JSON object with options (text, and class).
* @returns {!Blockly.FieldLabelEditable} The new label. * @returns {!Blockly.FieldLabelSerializable} The new label.
* @private * @private
*/ */
Blockly.Block.newFieldLabelEditableFromJson_ = function(options) { Blockly.Block.newFieldLabelSerializableFromJson_ = function(options) {
var text = Blockly.utils.replaceMessageReferences(options['text']); var text = Blockly.utils.replaceMessageReferences(options['text']);
return new Blockly.FieldLabelEditable(text, options['class']); return new Blockly.FieldLabelSerializable(text, options['class']);
}; };
/** /**

View file

@ -132,10 +132,20 @@ Blockly.Field.prototype.validator_ = null;
Blockly.Field.NBSP = '\u00A0'; Blockly.Field.NBSP = '\u00A0';
/** /**
* Editable fields are saved by the XML renderer, non-editable fields are not. * Editable fields usually show some sort of UI for the user to change them.
* @type {boolean}
* @public
*/ */
Blockly.Field.prototype.EDITABLE = true; Blockly.Field.prototype.EDITABLE = true;
/**
* Serializable fields are saved by the XML renderer, non-serializable fields
* are not. Editable fields should be serialized.
* @type {boolean}
* @public
*/
Blockly.Field.prototype.SERIALIZABLE = Blockly.Field.prototype.EDITABLE;
/** /**
* Attach this field to a block. * Attach this field to a block.
* @param {!Blockly.Block} block The block containing this field. * @param {!Blockly.Block} block The block containing this field.

View file

@ -20,12 +20,12 @@
/** /**
* @fileoverview Serialized label field. Behaves like a normal label but is * @fileoverview Serialized label field. Behaves like a normal label but is
* always serialized to XML. * always serialized to XML. It may only be edited programmatically.
* @author fenichel@google.com (Rachel Fenichel) * @author fenichel@google.com (Rachel Fenichel)
*/ */
'use strict'; 'use strict';
goog.provide('Blockly.FieldLabelEditable'); goog.provide('Blockly.FieldLabelSerializable');
goog.require('Blockly.FieldLabel'); goog.require('Blockly.FieldLabel');
@ -38,25 +38,36 @@ goog.require('Blockly.FieldLabel');
* @constructor * @constructor
* *
*/ */
Blockly.FieldLabelEditable = function(text, opt_class) { Blockly.FieldLabelSerializable = function(text, opt_class) {
Blockly.FieldLabelEditable.superClass_.constructor.call(this, text, Blockly.FieldLabelSerializable.superClass_.constructor.call(this, text,
opt_class); opt_class);
// Used in base field rendering, but we don't need it. // Used in base field rendering, but we don't need it.
this.arrowWidth_ = 0; this.arrowWidth_ = 0;
}; };
goog.inherits(Blockly.FieldLabelEditable, Blockly.FieldLabel); goog.inherits(Blockly.FieldLabelSerializable, Blockly.FieldLabel);
/** /**
* Editable fields are saved by the XML renderer, non-editable fields are not. * Editable fields usually show some sort of UI for the user to change them.
* This field should be serialized, but only edited programmatically.
* @type {boolean}
* @public
*/ */
Blockly.FieldLabelEditable.prototype.EDITABLE = true; Blockly.FieldLabelSerializable.prototype.EDITABLE = false;
/**
* Serializable fields are saved by the XML renderer, non-serializable fields
* are not. This field should be serialized, but only edited programmatically.
* @type {boolean}
* @public
*/
Blockly.FieldLabelSerializable.prototype.SERIALIZABLE = true;
/** /**
* Updates the width of the field. This calls getCachedWidth which won't cache * Updates the width of the field. This calls getCachedWidth which won't cache
* the approximated width on IE/Edge when `getComputedTextLength` fails. Once * the approximated width on IE/Edge when `getComputedTextLength` fails. Once
* it eventually does succeed, the result will be cached. * it eventually does succeed, the result will be cached.
**/ **/
Blockly.FieldLabelEditable.prototype.updateWidth = function() { Blockly.FieldLabelSerializable.prototype.updateWidth = function() {
// Set width of the field. // Set width of the field.
// Unlike the base Field class, this doesn't add space to editable fields. // Unlike the base Field class, this doesn't add space to editable fields.
this.size_.width = Blockly.Field.getCachedWidth(this.textElement_); this.size_.width = Blockly.Field.getCachedWidth(this.textElement_);
@ -67,7 +78,7 @@ Blockly.FieldLabelEditable.prototype.updateWidth = function() {
* Saves the computed width in a property. * Saves the computed width in a property.
* @private * @private
*/ */
Blockly.FieldLabelEditable.prototype.render_ = function() { Blockly.FieldLabelSerializable.prototype.render_ = function() {
if (this.visible_ && this.textElement_) { if (this.visible_ && this.textElement_) {
// Replace the text. // Replace the text.
goog.dom.removeChildren(/** @type {!Element} */ (this.textElement_)); goog.dom.removeChildren(/** @type {!Element} */ (this.textElement_));

View file

@ -45,9 +45,20 @@ Blockly.FieldVariableGetter = function(text, name) {
goog.inherits(Blockly.FieldVariableGetter, Blockly.Field); goog.inherits(Blockly.FieldVariableGetter, Blockly.Field);
/** /**
* Editable fields are saved by the XML renderer, non-editable fields are not. * Editable fields usually show some sort of UI for the user to change them.
* This field should be serialized, but only edited programmatically.
* @type {boolean}
* @public
*/ */
Blockly.FieldVariableGetter.prototype.EDITABLE = true; Blockly.FieldVariableGetter.prototype.EDITABLE = false;
/**
* Serializable fields are saved by the XML renderer, non-serializable fields
* are not. This field should be serialized, but only edited programmatically.
* @type {boolean}
* @public
*/
Blockly.FieldVariableGetter.prototype.SERIALIZABLE = true;
/** /**
* Install this field on a block. * Install this field on a block.

View file

@ -107,7 +107,7 @@ Blockly.Xml.blockToDom = function(block, opt_noId) {
} }
} }
function fieldToDom(field) { function fieldToDom(field) {
if (field.name && field.EDITABLE) { if (field.name && field.SERIALIZABLE) {
var container = goog.dom.createDom('field', null, field.getValue()); var container = goog.dom.createDom('field', null, field.getValue());
container.setAttribute('name', field.name); container.setAttribute('name', field.name);
if (field instanceof Blockly.FieldVariable || field instanceof if (field instanceof Blockly.FieldVariable || field instanceof