diff --git a/src/serialization/sb3.js b/src/serialization/sb3.js index 6b3b47e40..a87d566d5 100644 --- a/src/serialization/sb3.js +++ b/src/serialization/sb3.js @@ -200,15 +200,13 @@ const serializeBlock = function (block) { obj.parent = block.parent; obj.inputs = serializeInputs(block.inputs); obj.fields = serializeFields(block.fields); - obj.topLevel = block.topLevel ? block.topLevel : false; obj.shadow = block.shadow; if (block.topLevel) { - if (block.x) { - obj.x = Math.round(block.x); - } - if (block.y) { - obj.y = Math.round(block.y); - } + obj.topLevel = true; + obj.x = block.x ? Math.round(block.x) : 0; + obj.y = block.y ? Math.round(block.y) : 0; + } else { + obj.topLevel = false; } if (block.mutation) { obj.mutation = block.mutation; diff --git a/test/fixtures/top-level-reporters.sb3 b/test/fixtures/top-level-reporters.sb3 new file mode 100644 index 000000000..7e66deef1 Binary files /dev/null and b/test/fixtures/top-level-reporters.sb3 differ diff --git a/test/unit/serialization_sb3.js b/test/unit/serialization_sb3.js index ec75c80b6..74f146db3 100644 --- a/test/unit/serialization_sb3.js +++ b/test/unit/serialization_sb3.js @@ -8,6 +8,7 @@ const commentsSB2ProjectPath = path.resolve(__dirname, '../fixtures/comments.sb2 const commentsSB3ProjectPath = path.resolve(__dirname, '../fixtures/comments.sb3'); const commentsSB3NoDupeIds = path.resolve(__dirname, '../fixtures/comments_no_duplicate_id_serialization.sb3'); const variableReporterSB2ProjectPath = path.resolve(__dirname, '../fixtures/top-level-variable-reporter.sb2'); +const topLevelReportersProjectPath = path.resolve(__dirname, '../fixtures/top-level-reporters.sb3'); const FakeRenderer = require('../fixtures/fake-renderer'); test('serialize', t => { @@ -198,6 +199,38 @@ test('serializeBlocks', t => { }); }); +test('serializeBlocks serializes x and y for topLevel blocks with x,y of 0,0', t => { + const vm = new VirtualMachine(); + vm.loadProject(readFileToBuffer(topLevelReportersProjectPath)) + .then(() => { + // Verify that there are 2 blocks and they are both top level + const blocks = vm.runtime.targets[1].blocks._blocks; + const blockIds = Object.keys(blocks); + t.equal(blockIds.length, 2); + const blocksArray = blockIds.map(key => blocks[key]); + t.equal(blocksArray.every(b => b.topLevel), true); + // Simulate cleaning up the blocks by resetting x and y positions to 0 + blockIds.forEach(blockId => { + blocks[blockId].x = 0; + blocks[blockId].y = 0; + }); + const result = sb3.serializeBlocks(blocks); + const serializedBlocks = result[0]; + + t.type(serializedBlocks, 'object'); + const serializedBlockIds = Object.keys(serializedBlocks); + t.equal(serializedBlockIds.length, 2); + const firstBlock = serializedBlocks[serializedBlockIds[0]]; + const secondBlock = serializedBlocks[serializedBlockIds[1]]; + t.equal(firstBlock.x, 0); + t.equal(firstBlock.y, 0); + t.equal(secondBlock.x, 0); + t.equal(secondBlock.y, 0); + + t.end(); + }); +}); + test('deserializeBlocks', t => { const vm = new VirtualMachine(); vm.loadProject(readFileToBuffer(commentsSB3ProjectPath))