@@ -262,6 +262,7 @@ class Blocks {
262
262
return ;
263
263
}
264
264
const stage = optRuntime . getTargetForStage ( ) ;
265
+ const editingTarget = optRuntime . getEditingTarget ( ) ;
265
266
266
267
// UI event: clicked scripts toggle in the runtime.
267
268
if ( e . element === 'stackclick' ) {
@@ -331,11 +332,12 @@ class Blocks {
331
332
case 'var_create' :
332
333
// New variables being created by the user are all global.
333
334
// 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 ) ;
339
341
}
340
342
} else if ( ! stage . lookupVariableById ( e . varId ) ) {
341
343
// Since getEditingTarget returned null, we now need to
@@ -345,19 +347,29 @@ class Blocks {
345
347
}
346
348
break ;
347
349
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
351
360
const targets = optRuntime . targets ;
352
361
for ( let i = 0 ; i < targets . length ; i ++ ) {
353
362
const currTarget = targets [ i ] ;
354
363
currTarget . blocks . updateBlocksAfterVarRename ( e . varId , e . newName ) ;
355
364
}
356
365
}
357
366
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 ) ;
360
371
break ;
372
+ }
361
373
case 'comment_create' :
362
374
if ( optRuntime && optRuntime . getEditingTarget ( ) ) {
363
375
const currTarget = optRuntime . getEditingTarget ( ) ;
0 commit comments