From b342fb2121ad8a9c6a9426df144bbbdac1ebf851 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Tue, 2 Jan 2018 16:42:28 -0800 Subject: [PATCH] Fix problem with clearing flyout after a variable was deleted. --- core/field_variable.js | 12 ++++++++++++ core/xml.js | 14 +++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/core/field_variable.js b/core/field_variable.js index 5a5ab5def..5e654fde2 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -137,6 +137,18 @@ Blockly.FieldVariable.prototype.getText = function() { return this.variable_ ? this.variable_.name : ''; }; +/** + * Get the variable model for the selected variable. + * Not guaranteed to be in the variable map on the workspace (e.g. if accessed + * after the variable has been deleted). + * @return {?Blockly.VariableModel} the selected variable, or null if none was + * selected. + * @package + */ +Blockly.FieldVariable.prototype.getVariable = function() { + return this.variable_; +}; + /** * Set the variable ID. * @param {string} id New variable ID, which must reference an existing diff --git a/core/xml.js b/core/xml.js index 1faaa7a3d..a3e42bc54 100644 --- a/core/xml.js +++ b/core/xml.js @@ -89,19 +89,20 @@ Blockly.Xml.blockToDomWithXY = function(block, opt_noId) { /** * Encode a variable field as XML. * @param {!Blockly.Field} field The field to encode. - * @param {!Blockly.Workspace} workspace The workspace that the field is in. * @return {?Element} XML element, or null if the field did not need to be * serialized. * @private */ -Blockly.Xml.fieldToDomVariable_ = function(field, workspace) { +Blockly.Xml.fieldToDomVariable_ = function(field) { var id = field.getValue(); // The field had not been initialized fully before being serialized. if (id == null) { field.initModel(); id = field.getValue(); } - var variable = Blockly.Variables.getVariable(workspace, id); + // Get the variable directly from the field, instead of doing a lookup. This + // will work even if the variable has already been deleted. + var variable = field.getVariable(); if (!variable) { throw Error('Tried to serialize a variable field with no variable.'); @@ -121,10 +122,10 @@ Blockly.Xml.fieldToDomVariable_ = function(field, workspace) { * serialized. * @private */ -Blockly.Xml.fieldToDom_ = function(field, workspace) { +Blockly.Xml.fieldToDom_ = function(field) { if (field.name && field.EDITABLE) { if (field instanceof Blockly.FieldVariable) { - return Blockly.Xml.fieldToDomVariable_(field, workspace); + return Blockly.Xml.fieldToDomVariable_(field); } else { var container = goog.dom.createDom('field', null, field.getValue()); container.setAttribute('name', field.name); @@ -143,10 +144,9 @@ Blockly.Xml.fieldToDom_ = function(field, workspace) { * @private */ Blockly.Xml.allFieldsToDom_ = function(block, element) { - var workspace = block.workspace; for (var i = 0, input; input = block.inputList[i]; i++) { for (var j = 0, field; field = input.fieldRow[j]; j++) { - var fieldDom = Blockly.Xml.fieldToDom_(field, workspace); + var fieldDom = Blockly.Xml.fieldToDom_(field); if (fieldDom) { element.appendChild(fieldDom); }