From 44e78b1456f969c2050099800be82a97e87433d7 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Tue, 5 Aug 2025 11:17:10 -0700 Subject: [PATCH] feat: Add an option to copy subsequent blocks when getting copy data from a block. (#9279) --- core/block_svg.ts | 7 +++++-- tests/mocha/clipboard_test.js | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/core/block_svg.ts b/core/block_svg.ts index 1b85d38ce..c6065282a 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -951,9 +951,12 @@ export class BlockSvg /** * Encode a block for copying. * + * @param addNextBlocks If true, copy subsequent blocks attached to this one + * as well. + * * @returns Copy metadata, or null if the block is an insertion marker. */ - toCopyData(): BlockCopyData | null { + toCopyData(addNextBlocks = false): BlockCopyData | null { if (this.isInsertionMarker_) { return null; } @@ -961,7 +964,7 @@ export class BlockSvg paster: BlockPaster.TYPE, blockState: blocks.save(this, { addCoordinates: true, - addNextBlocks: false, + addNextBlocks, saveIds: false, }) as blocks.State, typeCounts: common.getBlockTypeCounts(this, true), diff --git a/tests/mocha/clipboard_test.js b/tests/mocha/clipboard_test.js index 85cdd2297..d58f78b9b 100644 --- a/tests/mocha/clipboard_test.js +++ b/tests/mocha/clipboard_test.js @@ -61,6 +61,31 @@ suite('Clipboard', function () { ); }); + test('pasting blocks includes next blocks if requested', function () { + const block = Blockly.serialization.blocks.append( + { + 'type': 'controls_if', + 'id': 'blockId', + 'next': { + 'block': { + 'type': 'controls_if', + 'id': 'blockId2', + }, + }, + }, + this.workspace, + ); + assert.equal(this.workspace.getBlocksByType('controls_if').length, 2); + // Both blocks should be copied + const data = block.toCopyData(true); + this.clock.runAll(); + + Blockly.clipboard.paste(data, this.workspace); + this.clock.runAll(); + // After pasting, we should have gone from 2 to 4 blocks. + assert.equal(this.workspace.getBlocksByType('controls_if').length, 4); + }); + test('copied from a mutator pastes them into the mutator', async function () { const block = Blockly.serialization.blocks.append( {