From 22f87e32f81d10e680ea6ecc500a727512442cf3 Mon Sep 17 00:00:00 2001 From: marisaleung Date: Tue, 27 Jun 2017 16:15:07 -0700 Subject: [PATCH] ChangeBlock uses id's of variable instead of name. --- src/engine/blocks.js | 11 ++++++++++- src/engine/runtime.js | 8 ++++++++ src/engine/target.js | 21 ++++++++++++++++----- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/engine/blocks.js b/src/engine/blocks.js index 1acc7774f..93b865770 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -296,7 +296,16 @@ class Blocks { case 'field': // Update block value if (!block.fields[args.name]) return; - block.fields[args.name].value = args.value; + if (args.name === 'VARIABLE') { + // Get variable name using the id in args.value. + const variable = optRuntime.getEditingTarget().lookupVariableById(args.value); + if (variable) { + block.fields[args.name].value = variable.name; + block.fields[args.name].id = args.value; + } + } else { + block.fields[args.name].value = args.value; + } break; case 'mutation': block.mutation = mutationAdapter(args.value); diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 80a404002..928f55e1e 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -981,6 +981,14 @@ class Runtime extends EventEmitter { } } + /** + * Get the editing target. + * @return {?Target} The editing target. + */ + getEditingTarget () { + return this._editingTarget; + } + /** * Tell the runtime to request a redraw. * Use after a clone/sprite has completed some visible operation on the stage. diff --git a/src/engine/target.js b/src/engine/target.js index aac3a2052..92a5817f0 100644 --- a/src/engine/target.js +++ b/src/engine/target.js @@ -71,12 +71,27 @@ class Target extends EventEmitter { /** * Look up a variable object, and create it if one doesn't exist. - * Search begins for local variables; then look for globals. * @param {string} id Id of the variable. * @param {string} name Name of the variable. * @return {!Variable} Variable object. */ lookupOrCreateVariable (id, name) { + const variable = this.lookupVariableById(id); + if (variable) return variable; + // No variable with this name exists - create it locally. + const newVariable = new Variable(id, name, 0, false); + this.variables[id] = newVariable; + return newVariable; + } + + /** + * Look up a variable object. + * Search begins for local variables; then look for globals. + * @param {string} id Id of the variable. + * @param {string} name Name of the variable. + * @return {!Variable} Variable object. + */ + lookupVariableById (id) { // If we have a local copy, return it. if (this.variables.hasOwnProperty(id)) { return this.variables[id]; @@ -88,10 +103,6 @@ class Target extends EventEmitter { return stage.variables[id]; } } - // No variable with this name exists - create it locally. - const newVariable = new Variable(id, name, 0, false); - this.variables[id] = newVariable; - return newVariable; } /**