Merge pull request #914 from kchadha/bugfix-909

Keep block xml up-to-date after renaming a variable.
This commit is contained in:
kchadha 2018-01-29 09:30:10 -05:00 committed by GitHub
commit 490248f652
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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