mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Merge pull request #914 from kchadha/bugfix-909
Keep block xml up-to-date after renaming a variable.
This commit is contained in:
commit
490248f652
1 changed files with 31 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue