diff --git a/core/block.js b/core/block.js index 50762ea05..5386b4372 100644 --- a/core/block.js +++ b/core/block.js @@ -751,7 +751,7 @@ Blockly.Block.prototype.getVars = function() { var vars = []; for (var i = 0, input; input = this.inputList[i]; i++) { for (var j = 0, field; field = input.fieldRow[j]; j++) { - if (field instanceof Blockly.FieldVariable) { + if (field.referencesVariables()) { vars.push(field.getValue()); } } @@ -768,7 +768,7 @@ Blockly.Block.prototype.getVarModels = function() { var vars = []; for (var i = 0, input; input = this.inputList[i]; i++) { for (var j = 0, field; field = input.fieldRow[j]; j++) { - if (field instanceof Blockly.FieldVariable) { + if (field.referencesVariables()) { var model = this.workspace.getVariableById(field.getValue()); // Check if the variable actually exists (and isn't just a potential // variable). @@ -790,7 +790,7 @@ Blockly.Block.prototype.getVarModels = function() { Blockly.Block.prototype.updateVarName = function(variable) { for (var i = 0, input; input = this.inputList[i]; i++) { for (var j = 0, field; field = input.fieldRow[j]; j++) { - if (field instanceof Blockly.FieldVariable && + if (field.referencesVariables() && variable.getId() == field.getValue()) { field.setText(variable.name); } @@ -808,7 +808,7 @@ Blockly.Block.prototype.updateVarName = function(variable) { Blockly.Block.prototype.renameVarById = function(oldId, newId) { for (var i = 0, input; input = this.inputList[i]; i++) { for (var j = 0, field; field = input.fieldRow[j]; j++) { - if (field instanceof Blockly.FieldVariable && + if (field.referencesVariables() && oldId == field.getValue()) { field.setValue(newId); } diff --git a/core/field.js b/core/field.js index 3f915f2db..e90c2f1bb 100644 --- a/core/field.js +++ b/core/field.js @@ -604,3 +604,14 @@ Blockly.Field.prototype.setTooltip = function(_newTip) { Blockly.Field.prototype.getAbsoluteXY_ = function() { return goog.style.getPageOffset(this.borderRect_); }; + +/** + * Whether this field references any Blockly variables. If true it may need to + * be handled differently during serialization and deserialization. Subclasses + * may override this. + * @return {boolean} True if this field has any variable references. + * @package + */ +Blockly.Field.prototype.referencesVariables = function() { + return false; +}; diff --git a/core/field_variable.js b/core/field_variable.js index 255c67a0c..3a97c4031 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -353,4 +353,14 @@ Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) { this.setValue(id); }; +/** + * Overrides referencesVariables(), indicating this field refers to a variable. + * @return {boolean} True. + * @package + * @override + */ +Blockly.FieldVariable.prototype.referencesVariables = function() { + return true; +}; + Blockly.Field.register('field_variable', Blockly.FieldVariable); diff --git a/core/xml.js b/core/xml.js index 4f1507fff..15199c1ac 100644 --- a/core/xml.js +++ b/core/xml.js @@ -138,7 +138,7 @@ Blockly.Xml.fieldToDomVariable_ = function(field) { */ Blockly.Xml.fieldToDom_ = function(field) { if (field.name && field.EDITABLE) { - if (field instanceof Blockly.FieldVariable) { + if (field.referencesVariables()) { return Blockly.Xml.fieldToDomVariable_(field); } else { var container = goog.dom.createDom('field', null, field.getValue()); @@ -806,7 +806,7 @@ Blockly.Xml.domToField_ = function(block, fieldName, xml) { var workspace = block.workspace; var text = xml.textContent; - if (field instanceof Blockly.FieldVariable) { + if (field.referencesVariables()) { Blockly.Xml.domToFieldVariable_(workspace, xml, text, field); } else { field.setValue(text);