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:
picklesrus 2019-04-10 09:05:53 -04:00 committed by GitHub
commit d182659a57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View file

@ -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);

View file

@ -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);