Skip to content

Commit 28de9ef

Browse files
maribethbBeksOmega
andauthored
fix: paste in a better spot (#8123)
* fix: paste in a better spot * fix: include bump offset * chore: rename param --------- Co-authored-by: Beka Westberg <bwestberg@google.com>
1 parent fcdb8fc commit 28de9ef

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

core/clipboard/block_paster.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,21 @@ export class BlockPaster implements IPaster<BlockCopyData, BlockSvg> {
3030
copyData.blockState['y'] = coordinate.y;
3131
}
3232

33+
// After appending the block to the workspace, it will be bumped from its neighbors
34+
// However, the algorithm for deciding where to paste a block depends on
35+
// the starting position of the copied block, so we'll pass those coordinates along
36+
const initialCoordinates =
37+
coordinate ||
38+
new Coordinate(
39+
copyData.blockState['x'] || 0,
40+
copyData.blockState['y'] || 0,
41+
);
42+
3343
eventUtils.disable();
3444
let block;
3545
try {
3646
block = append(copyData.blockState, workspace) as BlockSvg;
37-
moveBlockToNotConflict(block);
47+
moveBlockToNotConflict(block, initialCoordinates);
3848
} finally {
3949
eventUtils.enable();
4050
}
@@ -56,12 +66,20 @@ export class BlockPaster implements IPaster<BlockCopyData, BlockSvg> {
5666
* Exported for testing.
5767
*
5868
* @param block The block to move to an unambiguous location.
69+
* @param originalPosition The initial coordinate to start searching from,
70+
* likely the position of the copied block.
5971
* @internal
6072
*/
61-
export function moveBlockToNotConflict(block: BlockSvg) {
73+
export function moveBlockToNotConflict(
74+
block: BlockSvg,
75+
originalPosition: Coordinate,
76+
) {
6277
const workspace = block.workspace;
6378
const snapRadius = config.snapRadius;
64-
const coord = block.getRelativeToSurfaceXY();
79+
const bumpOffset = Coordinate.difference(
80+
originalPosition,
81+
block.getRelativeToSurfaceXY(),
82+
);
6583
const offset = new Coordinate(0, 0);
6684
// getRelativeToSurfaceXY is really expensive, so we want to cache this.
6785
const otherCoords = workspace
@@ -70,8 +88,11 @@ export function moveBlockToNotConflict(block: BlockSvg) {
7088
.map((b) => b.getRelativeToSurfaceXY());
7189

7290
while (
73-
blockOverlapsOtherExactly(Coordinate.sum(coord, offset), otherCoords) ||
74-
blockIsInSnapRadius(block, offset, snapRadius)
91+
blockOverlapsOtherExactly(
92+
Coordinate.sum(originalPosition, offset),
93+
otherCoords,
94+
) ||
95+
blockIsInSnapRadius(block, Coordinate.sum(bumpOffset, offset), snapRadius)
7596
) {
7697
if (workspace.RTL) {
7798
offset.translate(-snapRadius, snapRadius * 2);
@@ -80,7 +101,7 @@ export function moveBlockToNotConflict(block: BlockSvg) {
80101
}
81102
}
82103

83-
block!.moveTo(Coordinate.sum(coord, offset));
104+
block!.moveTo(Coordinate.sum(originalPosition, offset));
84105
}
85106

86107
/**

0 commit comments

Comments
 (0)