From 784341909b4d7cc8faaca72ad423840c2d218013 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Mon, 22 Jan 2018 15:14:43 -0500 Subject: [PATCH 1/2] Update block xml for all blocks that use a variable that was just renamed. --- src/engine/blocks.js | 4 ++++ src/engine/runtime.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/engine/blocks.js b/src/engine/blocks.js index b889f201..032bb075 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -319,6 +319,10 @@ class Blocks { break; case 'var_rename': stage.renameVariable(e.varId, e.newName); + // Update all the blocks that use the renamed variable. + if (optRuntime) { + optRuntime.updateBlocksAfterVarRename(e.varId, e.newName); + } break; case 'var_delete': stage.deleteVariable(e.varId); diff --git a/src/engine/runtime.js b/src/engine/runtime.js index bf7969a0..d5e79f4d 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -1581,6 +1581,33 @@ class Runtime extends EventEmitter { disableProfiling () { this.profiler = null; } + + /** + * 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 allTargets = this.targets; + for (let i = 0; i < allTargets.length; i++) { + const currTarget = allTargets[i]; + const currBlocks = currTarget.blocks._blocks; + for (const blockId in currBlocks) { + let varOrListField = null; + if (currBlocks[blockId].fields.VARIABLE) { + varOrListField = currBlocks[blockId].fields.VARIABLE; + } else if (currBlocks[blockId].fields.LIST) { + varOrListField = currBlocks[blockId].fields.LIST; + } + if (varOrListField) { + const currFieldId = varOrListField.id; + if (varId === currFieldId) { + varOrListField.value = newName; + } + } + } + } + } } /** From 0b2eaa9e1982804d34105ebff41898c9adf47a11 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Fri, 26 Jan 2018 16:58:45 -0500 Subject: [PATCH 2/2] Moving block updating code into blocks instead of runtime. --- src/engine/blocks.js | 31 +++++++++++++++++++++++++++++-- src/engine/runtime.js | 27 --------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/engine/blocks.js b/src/engine/blocks.js index 032bb075..35b8f03c 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -321,7 +321,11 @@ class Blocks { stage.renameVariable(e.varId, e.newName); // Update all the blocks that use the renamed variable. if (optRuntime) { - optRuntime.updateBlocksAfterVarRename(e.varId, e.newName); + 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': @@ -415,7 +419,7 @@ class Blocks { const isSpriteSpecific = optRuntime.monitorBlockInfo.hasOwnProperty(block.opcode) && optRuntime.monitorBlockInfo[block.opcode].isSpriteSpecific; block.targetId = isSpriteSpecific ? optRuntime.getEditingTarget().id : null; - + if (wasMonitored && !block.isMonitored) { optRuntime.requestRemoveMonitor(block.id); } else if (!wasMonitored && block.isMonitored) { @@ -550,6 +554,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; + } + } + } + } + // --------------------------------------------------------------------- /** diff --git a/src/engine/runtime.js b/src/engine/runtime.js index d5e79f4d..bf7969a0 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -1581,33 +1581,6 @@ class Runtime extends EventEmitter { disableProfiling () { this.profiler = null; } - - /** - * 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 allTargets = this.targets; - for (let i = 0; i < allTargets.length; i++) { - const currTarget = allTargets[i]; - const currBlocks = currTarget.blocks._blocks; - for (const blockId in currBlocks) { - let varOrListField = null; - if (currBlocks[blockId].fields.VARIABLE) { - varOrListField = currBlocks[blockId].fields.VARIABLE; - } else if (currBlocks[blockId].fields.LIST) { - varOrListField = currBlocks[blockId].fields.LIST; - } - if (varOrListField) { - const currFieldId = varOrListField.id; - if (varId === currFieldId) { - varOrListField.value = newName; - } - } - } - } - } } /**