From a8767ee6c95e6e1a3f3edfcacaa8a601a071ff50 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Wed, 25 Apr 2018 15:15:05 -0700 Subject: [PATCH 1/2] Add a referencesVariables function to field --- core/block.js | 8 ++++---- core/field.js | 11 +++++++++++ core/field_variable.js | 11 +++++++++++ core/xml.js | 4 ++-- 4 files changed, 28 insertions(+), 6 deletions(-) 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 a494ac411..cd6d6ddc5 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -353,4 +353,15 @@ Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) { this.setValue(id); }; +/** + * 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.FieldVariable.prototype.referencesVariables = function() { + return true; +}; + Blockly.Field.register('field_variable', Blockly.FieldVariable); diff --git a/core/xml.js b/core/xml.js index 9af982ca2..b1099e2fa 100644 --- a/core/xml.js +++ b/core/xml.js @@ -136,7 +136,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()); @@ -796,7 +796,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); From 66fde72ab6897159a489af5db1bccda831176334 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Fri, 27 Apr 2018 13:25:06 -0700 Subject: [PATCH 2/2] Change comment on function --- core/field_variable.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/field_variable.js b/core/field_variable.js index cd6d6ddc5..c036a0b83 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -354,11 +354,10 @@ Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) { }; /** - * 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. + * Overrides referencesVariables(), indicating this field refers to a variable. + * @return {boolean} True. * @package + * @override */ Blockly.FieldVariable.prototype.referencesVariables = function() { return true;