Fix duplication of variables and re-write variable ids in referencing fields.

This commit is contained in:
Karishma Chadha 2018-08-07 00:30:03 -04:00
parent 6e6e9b49cb
commit 337c1f464d
2 changed files with 46 additions and 4 deletions

View file

@ -269,7 +269,7 @@ class Target extends EventEmitter {
/**
* Renames the variable with the given id to newName.
* @param {string} id Id of renamed variable.
* @param {string} id Id of variable to rename.
* @param {string} newName New name for the variable.
*/
renameVariable (id, newName) {
@ -301,7 +301,7 @@ class Target extends EventEmitter {
/**
* Removes the variable with the given id from the dictionary of variables.
* @param {string} id Id of renamed variable.
* @param {string} id Id of variable to delete.
*/
deleteVariable (id) {
if (this.variables.hasOwnProperty(id)) {
@ -313,6 +313,48 @@ class Target extends EventEmitter {
}
}
/**
* Create a clone of the variable with the given id from the dictionary of
* this target's variables.
* @param {string} id Id of variable to duplicate.
* @return {?Variable} The duplicated variable, or null if
* the original variable was not found.
*/
duplicateVariable (id) {
if (this.variables.hasOwnProperty(id)) {
const originalVariable = this.variables[id];
const newVariable = new Variable(
null, // generate a new uid
originalVariable.name,
originalVariable.type,
originalVariable.isCloud
);
newVariable.value = originalVariable.value;
return newVariable;
}
return null;
}
/**
* Duplicate the dictionary of this target's variables as part of duplicating.
* this target or making a clone.
* @param {object} blocks Block container for the target being duplicated
* @return {object} The duplicated dictionary of variables
*/
duplicateVariables (blocks) {
const allVarRefs = blocks.getAllVariableAndListReferences();
return Object.keys(this.variables).reduce((accum, varId) => {
const newVariable = this.duplicateVariable(varId);
accum[newVariable.id] = newVariable;
const currVarRefs = allVarRefs[varId];
if (currVarRefs) {
this.mergeVariables(varId, newVariable.id, currVarRefs);
}
return accum;
}, {});
}
/**
* Post/edit sprite info.
* @param {object} data An object with sprite info data to set.

View file

@ -1003,7 +1003,7 @@ class RenderedTarget extends Target {
newClone.currentCostume = this.currentCostume;
newClone.rotationStyle = this.rotationStyle;
newClone.effects = JSON.parse(JSON.stringify(this.effects));
newClone.variables = JSON.parse(JSON.stringify(this.variables));
newClone.variables = this.duplicateVariables();
newClone.initDrawable(StageLayering.SPRITE_LAYER);
newClone.updateAllDrawableProperties();
// Place behind the current target.
@ -1029,7 +1029,7 @@ class RenderedTarget extends Target {
newTarget.currentCostume = this.currentCostume;
newTarget.rotationStyle = this.rotationStyle;
newTarget.effects = JSON.parse(JSON.stringify(this.effects));
newTarget.variables = JSON.parse(JSON.stringify(this.variables));
newTarget.variables = this.duplicateVariables(newTarget.blocks);
newTarget.updateAllDrawableProperties();
newTarget.goBehindOther(this);
return newTarget;