From 2cf1d21b0920e7069b4d8d9fcaed918ff812df0f Mon Sep 17 00:00:00 2001 From: picklesrus Date: Mon, 25 Mar 2019 10:18:00 -0400 Subject: [PATCH 1/3] Fix #4573 by using an existing utitlity that re-ids blocks when we duplicate a sprite. --- src/sprites/sprite.js | 7 ++++++- test/unit/sprites_rendered-target.js | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/sprites/sprite.js b/src/sprites/sprite.js index 07b94673c..cde064b02 100644 --- a/src/sprites/sprite.js +++ b/src/sprites/sprite.js @@ -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,12 @@ class Sprite { duplicate () { const newSprite = new Sprite(null, this.runtime); + const copiedBlocks = JSON.parse(JSON.stringify(Object.values(this.blocks._blocks))); + 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); diff --git a/test/unit/sprites_rendered-target.js b/test/unit/sprites_rendered-target.js index e768574d9..3045c4b3c 100644 --- a/test/unit/sprites_rendered-target.js +++ b/test/unit/sprites_rendered-target.js @@ -1,4 +1,5 @@ const test = require('tap').test; +const Blocks = require('../../src/engine/blocks'); const RenderedTarget = require('../../src/sprites/rendered-target'); const Sprite = require('../../src/sprites/sprite'); const Runtime = require('../../src/engine/runtime'); @@ -27,6 +28,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); From 45ef63a64854780f411ad9c172fcec816fbff187 Mon Sep 17 00:00:00 2001 From: picklesrus Date: Mon, 25 Mar 2019 11:35:06 -0400 Subject: [PATCH 2/3] Remove unused import. --- test/unit/sprites_rendered-target.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/sprites_rendered-target.js b/test/unit/sprites_rendered-target.js index 3045c4b3c..20430008b 100644 --- a/test/unit/sprites_rendered-target.js +++ b/test/unit/sprites_rendered-target.js @@ -1,5 +1,4 @@ const test = require('tap').test; -const Blocks = require('../../src/engine/blocks'); const RenderedTarget = require('../../src/sprites/rendered-target'); const Sprite = require('../../src/sprites/sprite'); const Runtime = require('../../src/engine/runtime'); From 51ad4185d111e7d2db06a47a4c0cab194b4ca895 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 10 Apr 2019 08:55:54 -0400 Subject: [PATCH 3/3] Update src/sprites/sprite.js Don't use object.values because it doesn't work in old Safaris. Co-Authored-By: picklesrus --- src/sprites/sprite.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sprites/sprite.js b/src/sprites/sprite.js index cde064b02..a38fa129c 100644 --- a/src/sprites/sprite.js +++ b/src/sprites/sprite.js @@ -137,7 +137,9 @@ class Sprite { duplicate () { const newSprite = new Sprite(null, this.runtime); - const copiedBlocks = JSON.parse(JSON.stringify(Object.values(this.blocks._blocks))); + 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);