Skip to content

Commit 92dfebd

Browse files
committed
Add variables from var_create events to appropriate target based on isLocal flag.
1 parent aa400ea commit 92dfebd

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/engine/blocks.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ class Blocks {
262262
return;
263263
}
264264
const stage = optRuntime.getTargetForStage();
265+
const editingTarget = optRuntime.getEditingTarget();
265266

266267
// UI event: clicked scripts toggle in the runtime.
267268
if (e.element === 'stackclick') {
@@ -331,11 +332,12 @@ class Blocks {
331332
case 'var_create':
332333
// New variables being created by the user are all global.
333334
// Check if this variable exists on the current target or stage.
334-
// If not, create it on the stage.
335-
// TODO create global and local variables when UI provides a way.
336-
if (optRuntime.getEditingTarget()) {
337-
if (!optRuntime.getEditingTarget().lookupVariableById(e.varId)) {
338-
stage.createVariable(e.varId, e.varName, e.varType);
335+
// If not, create it on the appropriate target based on the event's
336+
// 'isLocal' flag.
337+
if (editingTarget) {
338+
if (!editingTarget.lookupVariableById(e.varId)) {
339+
const varTarget = e.isLocal ? editingTarget : stage;
340+
varTarget.createVariable(e.varId, e.varName, e.varType);
339341
}
340342
} else if (!stage.lookupVariableById(e.varId)) {
341343
// Since getEditingTarget returned null, we now need to
@@ -345,19 +347,29 @@ class Blocks {
345347
}
346348
break;
347349
case 'var_rename':
348-
stage.renameVariable(e.varId, e.newName);
349-
// Update all the blocks that use the renamed variable.
350-
if (optRuntime) {
350+
if (editingTarget && editingTarget.hasOwnProperty(e.varId)) {
351+
// This is a local variable, rename on the current target
352+
editingTarget.renameVariable(e.varId, e.newName);
353+
// Update all the blocks on the current target that use
354+
// this variable
355+
editingTarget.blocks.updateBlocksAfterVarRename(e.varId, e.newName);
356+
} else {
357+
// This is a global variable
358+
stage.renameVariable(e.varId, e.newName);
359+
// Update all blocks on all targets that use the renamed variable
351360
const targets = optRuntime.targets;
352361
for (let i = 0; i < targets.length; i++) {
353362
const currTarget = targets[i];
354363
currTarget.blocks.updateBlocksAfterVarRename(e.varId, e.newName);
355364
}
356365
}
357366
break;
358-
case 'var_delete':
359-
stage.deleteVariable(e.varId);
367+
case 'var_delete': {
368+
const target = (editingTarget && editingTarget.hasOwnProperty(e.varId)) ?
369+
editingTarget : stage;
370+
target.deleteVariable(e.varId);
360371
break;
372+
}
361373
case 'comment_create':
362374
if (optRuntime && optRuntime.getEditingTarget()) {
363375
const currTarget = optRuntime.getEditingTarget();

0 commit comments

Comments
 (0)