Skip to content

Commit 77c695a

Browse files
authored
fix: Paste blocks copied from a mutator into the mutator. (#8719)
1 parent bd7c86a commit 77c695a

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

core/clipboard.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ function pasteFromData<T extends ICopyData>(
8383
workspace: WorkspaceSvg,
8484
coordinate?: Coordinate,
8585
): ICopyable<T> | null {
86-
workspace = workspace.getRootWorkspace() ?? workspace;
86+
workspace = workspace.isMutator
87+
? workspace
88+
: (workspace.getRootWorkspace() ?? workspace);
8789
return (globalRegistry
8890
.getObject(globalRegistry.Type.PASTER, copyData.paster, false)
8991
?.paste(copyData, workspace, coordinate) ?? null) as ICopyable<T> | null;

core/shortcut_items.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {KeyboardShortcut, ShortcutRegistry} from './shortcut_registry.js';
1818
import {Coordinate} from './utils/coordinate.js';
1919
import {KeyCodes} from './utils/keycodes.js';
2020
import {Rect} from './utils/rect.js';
21-
import type {WorkspaceSvg} from './workspace_svg.js';
21+
import {WorkspaceSvg} from './workspace_svg.js';
2222

2323
/**
2424
* Object holding the names of the default shortcut items.
@@ -131,7 +131,10 @@ export function registerCopy() {
131131
const selected = common.getSelected();
132132
if (!selected || !isCopyable(selected)) return false;
133133
copyData = selected.toCopyData();
134-
copyWorkspace = workspace;
134+
copyWorkspace =
135+
selected.workspace instanceof WorkspaceSvg
136+
? selected.workspace
137+
: workspace;
135138
copyCoords = isDraggable(selected)
136139
? selected.getRelativeToSurfaceXY()
137140
: null;

tests/mocha/clipboard_test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ suite('Clipboard', function () {
6161
);
6262
});
6363

64+
test('copied from a mutator pastes them into the mutator', async function () {
65+
const block = Blockly.serialization.blocks.append(
66+
{
67+
'type': 'controls_if',
68+
'id': 'blockId',
69+
'extraState': {
70+
'elseIfCount': 1,
71+
},
72+
},
73+
this.workspace,
74+
);
75+
const mutatorIcon = block.getIcon(Blockly.icons.IconType.MUTATOR);
76+
await mutatorIcon.setBubbleVisible(true);
77+
const mutatorWorkspace = mutatorIcon.getWorkspace();
78+
const elseIf = mutatorWorkspace.getBlocksByType('controls_if_elseif')[0];
79+
assert.notEqual(elseIf, undefined);
80+
assert.lengthOf(mutatorWorkspace.getAllBlocks(), 2);
81+
assert.lengthOf(this.workspace.getAllBlocks(), 1);
82+
const data = elseIf.toCopyData();
83+
Blockly.clipboard.paste(data, mutatorWorkspace);
84+
assert.lengthOf(mutatorWorkspace.getAllBlocks(), 3);
85+
assert.lengthOf(this.workspace.getAllBlocks(), 1);
86+
});
87+
6488
suite('pasted blocks are placed in unambiguous locations', function () {
6589
test('pasted blocks are bumped to not overlap', function () {
6690
const block = Blockly.serialization.blocks.append(

0 commit comments

Comments
 (0)