Update block xml for all blocks that use a variable that was just renamed.

This commit is contained in:
Karishma Chadha 2018-01-22 15:14:43 -05:00
parent 90e9e1c0b0
commit 784341909b
2 changed files with 31 additions and 0 deletions

View file

@ -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);

View file

@ -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;
}
}
}
}
}
}
/**