diff --git a/src/engine/blocks.js b/src/engine/blocks.js index 8854feb2b..c16da7114 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -327,6 +327,14 @@ class Blocks { break; case 'var_rename': stage.renameVariable(e.varId, e.newName); + // Update all the blocks that use the renamed variable. + if (optRuntime) { + const targets = optRuntime.targets; + for (let i = 0; i < targets.length; i++) { + const currTarget = targets[i]; + currTarget.blocks.updateBlocksAfterVarRename(e.varId, e.newName); + } + } break; case 'var_delete': stage.deleteVariable(e.varId); @@ -555,6 +563,29 @@ class Blocks { this.resetCache(); } + /** + * Keep blocks up to date after a variable gets renamed. + * @param {string} varId The id of the variable that was renamed + * @param {string} newName The new name of the variable that was renamed + */ + updateBlocksAfterVarRename (varId, newName) { + const blocks = this._blocks; + for (const blockId in blocks) { + let varOrListField = null; + if (blocks[blockId].fields.VARIABLE) { + varOrListField = blocks[blockId].fields.VARIABLE; + } else if (blocks[blockId].fields.LIST) { + varOrListField = blocks[blockId].fields.LIST; + } + if (varOrListField) { + const currFieldId = varOrListField.id; + if (varId === currFieldId) { + varOrListField.value = newName; + } + } + } + } + // --------------------------------------------------------------------- /**