From a6ddc6540fa79db9f01df28b1bb005ea4bdc34f2 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 6 Jan 2025 10:31:41 -0800 Subject: [PATCH] fix: Paste blocks copied from a mutator into the mutator. --- core/clipboard.ts | 4 +++- core/shortcut_items.ts | 7 +++++-- tests/mocha/clipboard_test.js | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/core/clipboard.ts b/core/clipboard.ts index 62e23fd24a..ba6f44e6f4 100644 --- a/core/clipboard.ts +++ b/core/clipboard.ts @@ -83,7 +83,9 @@ function pasteFromData( workspace: WorkspaceSvg, coordinate?: Coordinate, ): ICopyable | null { - workspace = workspace.getRootWorkspace() ?? workspace; + workspace = workspace.isMutator + ? workspace + : (workspace.getRootWorkspace() ?? workspace); return (globalRegistry .getObject(globalRegistry.Type.PASTER, copyData.paster, false) ?.paste(copyData, workspace, coordinate) ?? null) as ICopyable | null; diff --git a/core/shortcut_items.ts b/core/shortcut_items.ts index 3a32b687fe..0db28a51a4 100644 --- a/core/shortcut_items.ts +++ b/core/shortcut_items.ts @@ -18,7 +18,7 @@ import {KeyboardShortcut, ShortcutRegistry} from './shortcut_registry.js'; import {Coordinate} from './utils/coordinate.js'; import {KeyCodes} from './utils/keycodes.js'; import {Rect} from './utils/rect.js'; -import type {WorkspaceSvg} from './workspace_svg.js'; +import {WorkspaceSvg} from './workspace_svg.js'; /** * Object holding the names of the default shortcut items. @@ -131,7 +131,10 @@ export function registerCopy() { const selected = common.getSelected(); if (!selected || !isCopyable(selected)) return false; copyData = selected.toCopyData(); - copyWorkspace = workspace; + copyWorkspace = + selected.workspace instanceof WorkspaceSvg + ? selected.workspace + : workspace; copyCoords = isDraggable(selected) ? selected.getRelativeToSurfaceXY() : null; diff --git a/tests/mocha/clipboard_test.js b/tests/mocha/clipboard_test.js index 6526984213..0f2d067708 100644 --- a/tests/mocha/clipboard_test.js +++ b/tests/mocha/clipboard_test.js @@ -61,6 +61,30 @@ suite('Clipboard', function () { ); }); + test('copied from a mutator pastes them into the mutator', async function () { + const block = Blockly.serialization.blocks.append( + { + 'type': 'controls_if', + 'id': 'blockId', + 'extraState': { + 'elseIfCount': 1, + }, + }, + this.workspace, + ); + const mutatorIcon = block.getIcon(Blockly.icons.IconType.MUTATOR); + await mutatorIcon.setBubbleVisible(true); + const mutatorWorkspace = mutatorIcon.getWorkspace(); + const elseIf = mutatorWorkspace.getBlocksByType('controls_if_elseif')[0]; + assert.notEqual(elseIf, undefined); + assert.lengthOf(mutatorWorkspace.getAllBlocks(), 2); + assert.lengthOf(this.workspace.getAllBlocks(), 1); + const data = elseIf.toCopyData(); + Blockly.clipboard.paste(data, mutatorWorkspace); + assert.lengthOf(mutatorWorkspace.getAllBlocks(), 3); + assert.lengthOf(this.workspace.getAllBlocks(), 1); + }); + suite('pasted blocks are placed in unambiguous locations', function () { test('pasted blocks are bumped to not overlap', function () { const block = Blockly.serialization.blocks.append(