diff --git a/core/field_variable.js b/core/field_variable.js index 9bff99214..0b9238f02 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -167,6 +167,8 @@ Blockly.FieldVariable.prototype.getText = function() { * variable. */ Blockly.FieldVariable.prototype.setValue = function(id) { + // TODO: Handle undo a change to go back to the default value. + // TODO: Handle null ID, which means "use default". var workspace = this.sourceBlock_.workspace; var variable = workspace.getVariableById(id); if (!variable) { @@ -189,6 +191,11 @@ Blockly.FieldVariable.prototype.setValue = function(id) { throw new Error('Variable type doesn\'t match this field! Type was ' + type); } + if (this.sourceBlock_ && Blockly.Events.isEnabled()) { + var oldValue = this.variable_ ? this.variable_.getId() : null; + Blockly.Events.fire(new Blockly.Events.BlockChange( + this.sourceBlock_, 'field', this.name, oldValue, variable.getId())); + } this.variable_ = variable; this.setText(variable.name); }; diff --git a/core/workspace.js b/core/workspace.js index 2c4205158..6fed60405 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -266,7 +266,6 @@ Blockly.Workspace.prototype.renameVariableById = function(id, newName) { Blockly.Events.setGroup(true); var blocks = this.getAllBlocks(); - this.variableMap_.renameVariable(variable, newName, type); // Iterate through every block and update name. for (var i = 0; i < blocks.length; i++) { @@ -275,6 +274,8 @@ Blockly.Workspace.prototype.renameVariableById = function(id, newName) { blocks[i].renameVarById(newId, newId); } } + + this.variableMap_.renameVariable(variable, newName, type); Blockly.Events.setGroup(false); }; diff --git a/tests/jsunit/field_variable_test.js b/tests/jsunit/field_variable_test.js index 0cf1f11f8..6d3ef9d66 100644 --- a/tests/jsunit/field_variable_test.js +++ b/tests/jsunit/field_variable_test.js @@ -68,8 +68,9 @@ function test_fieldVariable_setValueMatchId() { var fieldVariable = fieldVariable_createAndInitField(workspace); + var oldId = fieldVariable.getValue(); var event = new Blockly.Events.BlockChange( - fieldVariable.sourceBlock_, 'field', undefined, 'name1', 'id2'); + fieldVariable.sourceBlock_, 'field', undefined, oldId, 'id2'); setUpMockMethod(mockControl_, Blockly.Events, 'fire', [event], null); fieldVariable.setValue('id2'); @@ -87,10 +88,6 @@ function test_fieldVariable_setValueNoVariable() { return false; }; - var event = new Blockly.Events.BlockChange( - mockBlock, 'field', undefined, 'name1', 'id1'); - setUpMockMethod(mockControl_, Blockly.Events, 'fire', [event], null); - try { fieldVariable.setValue('id1'); // Calling setValue with a variable that doesn't exist throws an error. diff --git a/tests/jsunit/index.html b/tests/jsunit/index.html index 500f2cb37..96424df44 100644 --- a/tests/jsunit/index.html +++ b/tests/jsunit/index.html @@ -8,7 +8,7 @@
- +