mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-22 22:12:28 -05:00
Merge pull request #2065 from picklesrus/duplicate-sprites
Fix #4573 by using an existing utitlity that re-ids blocks when we du…
This commit is contained in:
commit
d182659a57
2 changed files with 26 additions and 1 deletions
|
@ -2,6 +2,7 @@ const RenderedTarget = require('./rendered-target');
|
|||
const Blocks = require('../engine/blocks');
|
||||
const {loadSoundFromAsset} = require('../import/load-sound');
|
||||
const {loadCostumeFromAsset} = require('../import/load-costume');
|
||||
const newBlockIds = require('../util/new-block-ids');
|
||||
const StringUtil = require('../util/string-util');
|
||||
const StageLayering = require('../engine/stage-layering');
|
||||
|
||||
|
@ -136,8 +137,14 @@ class Sprite {
|
|||
|
||||
duplicate () {
|
||||
const newSprite = new Sprite(null, this.runtime);
|
||||
const blocksContainer = this.blocks._blocks;
|
||||
const originalBlocks = Object.keys(blocksContainer).map(key => blocksContainer[key]);
|
||||
const copiedBlocks = JSON.parse(JSON.stringify(originalBlocks));
|
||||
newBlockIds(copiedBlocks);
|
||||
copiedBlocks.forEach(block => {
|
||||
newSprite.blocks.createBlock(block);
|
||||
});
|
||||
|
||||
newSprite.blocks = this.blocks.duplicate();
|
||||
|
||||
const allNames = this.runtime.targets.map(t => t.sprite.name);
|
||||
newSprite.name = StringUtil.unusedName(this.name, allNames);
|
||||
|
|
|
@ -27,6 +27,24 @@ test('setxy', t => {
|
|||
t.end();
|
||||
});
|
||||
|
||||
test('blocks get new id on duplicate', t => {
|
||||
const r = new Runtime();
|
||||
const s = new Sprite(null, r);
|
||||
const rt = new RenderedTarget(s, r);
|
||||
const block = {
|
||||
id: 'id1',
|
||||
topLevel: true,
|
||||
fields: {}
|
||||
};
|
||||
|
||||
rt.blocks.createBlock(block);
|
||||
|
||||
return rt.duplicate().then(duplicate => {
|
||||
t.notOk(duplicate.blocks._blocks.hasOwnProperty(block.id));
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('direction', t => {
|
||||
const r = new Runtime();
|
||||
const s = new Sprite(null, r);
|
||||
|
|
Loading…
Reference in a new issue