mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-03-08 13:37:55 -05:00
Add variables from var_create events to appropriate target based on isLocal flag.
This commit is contained in:
parent
aa400ea15b
commit
92dfebdae6
1 changed files with 22 additions and 10 deletions
|
@ -262,6 +262,7 @@ class Blocks {
|
|||
return;
|
||||
}
|
||||
const stage = optRuntime.getTargetForStage();
|
||||
const editingTarget = optRuntime.getEditingTarget();
|
||||
|
||||
// UI event: clicked scripts toggle in the runtime.
|
||||
if (e.element === 'stackclick') {
|
||||
|
@ -331,11 +332,12 @@ class Blocks {
|
|||
case 'var_create':
|
||||
// New variables being created by the user are all global.
|
||||
// Check if this variable exists on the current target or stage.
|
||||
// If not, create it on the stage.
|
||||
// TODO create global and local variables when UI provides a way.
|
||||
if (optRuntime.getEditingTarget()) {
|
||||
if (!optRuntime.getEditingTarget().lookupVariableById(e.varId)) {
|
||||
stage.createVariable(e.varId, e.varName, e.varType);
|
||||
// If not, create it on the appropriate target based on the event's
|
||||
// 'isLocal' flag.
|
||||
if (editingTarget) {
|
||||
if (!editingTarget.lookupVariableById(e.varId)) {
|
||||
const varTarget = e.isLocal ? editingTarget : stage;
|
||||
varTarget.createVariable(e.varId, e.varName, e.varType);
|
||||
}
|
||||
} else if (!stage.lookupVariableById(e.varId)) {
|
||||
// Since getEditingTarget returned null, we now need to
|
||||
|
@ -345,9 +347,16 @@ class Blocks {
|
|||
}
|
||||
break;
|
||||
case 'var_rename':
|
||||
stage.renameVariable(e.varId, e.newName);
|
||||
// Update all the blocks that use the renamed variable.
|
||||
if (optRuntime) {
|
||||
if (editingTarget && editingTarget.hasOwnProperty(e.varId)) {
|
||||
// This is a local variable, rename on the current target
|
||||
editingTarget.renameVariable(e.varId, e.newName);
|
||||
// Update all the blocks on the current target that use
|
||||
// this variable
|
||||
editingTarget.blocks.updateBlocksAfterVarRename(e.varId, e.newName);
|
||||
} else {
|
||||
// This is a global variable
|
||||
stage.renameVariable(e.varId, e.newName);
|
||||
// Update all blocks on all targets that use the renamed variable
|
||||
const targets = optRuntime.targets;
|
||||
for (let i = 0; i < targets.length; i++) {
|
||||
const currTarget = targets[i];
|
||||
|
@ -355,9 +364,12 @@ class Blocks {
|
|||
}
|
||||
}
|
||||
break;
|
||||
case 'var_delete':
|
||||
stage.deleteVariable(e.varId);
|
||||
case 'var_delete': {
|
||||
const target = (editingTarget && editingTarget.hasOwnProperty(e.varId)) ?
|
||||
editingTarget : stage;
|
||||
target.deleteVariable(e.varId);
|
||||
break;
|
||||
}
|
||||
case 'comment_create':
|
||||
if (optRuntime && optRuntime.getEditingTarget()) {
|
||||
const currTarget = optRuntime.getEditingTarget();
|
||||
|
|
Loading…
Reference in a new issue