mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-25 07:22:33 -05:00
Don't generate new IDs for variables when making a sprite clone.
This commit is contained in:
parent
337c1f464d
commit
d91788406c
1 changed files with 18 additions and 10 deletions
|
@ -317,14 +317,16 @@ class Target extends EventEmitter {
|
||||||
* Create a clone of the variable with the given id from the dictionary of
|
* Create a clone of the variable with the given id from the dictionary of
|
||||||
* this target's variables.
|
* this target's variables.
|
||||||
* @param {string} id Id of variable to duplicate.
|
* @param {string} id Id of variable to duplicate.
|
||||||
|
* @param {boolean=} optKeepOriginalId Optional flag to keep the original variable ID
|
||||||
|
* for the duplicate variable. This is necessary when cloning a sprite, for example.
|
||||||
* @return {?Variable} The duplicated variable, or null if
|
* @return {?Variable} The duplicated variable, or null if
|
||||||
* the original variable was not found.
|
* the original variable was not found.
|
||||||
*/
|
*/
|
||||||
duplicateVariable (id) {
|
duplicateVariable (id, optKeepOriginalId) {
|
||||||
if (this.variables.hasOwnProperty(id)) {
|
if (this.variables.hasOwnProperty(id)) {
|
||||||
const originalVariable = this.variables[id];
|
const originalVariable = this.variables[id];
|
||||||
const newVariable = new Variable(
|
const newVariable = new Variable(
|
||||||
null, // generate a new uid
|
optKeepOriginalId ? id : null, // conditionally keep original id or generate a new one
|
||||||
originalVariable.name,
|
originalVariable.name,
|
||||||
originalVariable.type,
|
originalVariable.type,
|
||||||
originalVariable.isCloud
|
originalVariable.isCloud
|
||||||
|
@ -338,19 +340,25 @@ class Target extends EventEmitter {
|
||||||
/**
|
/**
|
||||||
* Duplicate the dictionary of this target's variables as part of duplicating.
|
* Duplicate the dictionary of this target's variables as part of duplicating.
|
||||||
* this target or making a clone.
|
* this target or making a clone.
|
||||||
* @param {object} blocks Block container for the target being duplicated
|
* @param {object=} optBlocks Optional block container for the target being duplicated.
|
||||||
|
* If provided, new variables will be generated with new UIDs and any variable references
|
||||||
|
* in this blocks container will be updated to refer to the corresponding new IDs.
|
||||||
* @return {object} The duplicated dictionary of variables
|
* @return {object} The duplicated dictionary of variables
|
||||||
*/
|
*/
|
||||||
duplicateVariables (blocks) {
|
duplicateVariables (optBlocks) {
|
||||||
const allVarRefs = blocks.getAllVariableAndListReferences();
|
let allVarRefs;
|
||||||
|
if (optBlocks) {
|
||||||
|
allVarRefs = optBlocks.getAllVariableAndListReferences();
|
||||||
|
}
|
||||||
return Object.keys(this.variables).reduce((accum, varId) => {
|
return Object.keys(this.variables).reduce((accum, varId) => {
|
||||||
const newVariable = this.duplicateVariable(varId);
|
const newVariable = this.duplicateVariable(varId, !optBlocks);
|
||||||
accum[newVariable.id] = newVariable;
|
accum[newVariable.id] = newVariable;
|
||||||
|
if (optBlocks && allVarRefs) {
|
||||||
const currVarRefs = allVarRefs[varId];
|
const currVarRefs = allVarRefs[varId];
|
||||||
if (currVarRefs) {
|
if (currVarRefs) {
|
||||||
this.mergeVariables(varId, newVariable.id, currVarRefs);
|
this.mergeVariables(varId, newVariable.id, currVarRefs);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return accum;
|
return accum;
|
||||||
}, {});
|
}, {});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue