ChangeBlock uses id's of variable instead of name.

This commit is contained in:
marisaleung 2017-06-27 16:15:07 -07:00
parent b0fb4f0b55
commit 22f87e32f8
3 changed files with 34 additions and 6 deletions

View file

@ -296,7 +296,16 @@ class Blocks {
case 'field':
// Update block value
if (!block.fields[args.name]) return;
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);

View file

@ -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.

View file

@ -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;
}
/**