Skip to content

Commit b95067b

Browse files
committed
chore: position insertion marker in a single function
1 parent a2a9ab1 commit b95067b

2 files changed

Lines changed: 13 additions & 34 deletions

File tree

core/block_svg.ts

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,38 +1515,13 @@ export class BlockSvg
15151515
* or an insertion marker.
15161516
*
15171517
* @param sourceConnection The connection on the moving block's stack.
1518-
* @param targetConnection The connection that should stay stationary as this
1519-
* block is positioned.
1518+
* @param originalOffsetToTarget The connection original offset to the target connection
1519+
* @param originalOffsetInBlock The connection original offset in its block
15201520
* @internal
15211521
*/
15221522
positionNearConnection(
15231523
sourceConnection: RenderedConnection,
1524-
targetConnection: RenderedConnection,
1525-
) {
1526-
// We only need to position the new block if it's before the existing one,
1527-
// otherwise its position is set by the previous block.
1528-
if (
1529-
sourceConnection.type === ConnectionType.NEXT_STATEMENT ||
1530-
sourceConnection.type === ConnectionType.INPUT_VALUE
1531-
) {
1532-
const dx = targetConnection.x - sourceConnection.x;
1533-
const dy = targetConnection.y - sourceConnection.y;
1534-
1535-
this.moveBy(dx, dy);
1536-
}
1537-
}
1538-
1539-
/**
1540-
* Reposition a block after its connection has been resized, to match exactly the target block position.
1541-
* The block to position is usually either the first block in a dragged stack
1542-
* or an insertion marker.
1543-
*
1544-
* @param sourceConnection The connection on the moving block's stack.
1545-
* @param originalOffsetInBlock The connection original offset in its block, before the resize occured
1546-
* @internal
1547-
*/
1548-
repositionAfterConnectionResize(
1549-
sourceConnection: RenderedConnection,
1524+
originalOffsetToTarget: {x: number; y: number},
15501525
originalOffsetInBlock: Coordinate,
15511526
) {
15521527
// We only need to position the new block if it's before the existing one,
@@ -1555,8 +1530,12 @@ export class BlockSvg
15551530
sourceConnection.type === ConnectionType.NEXT_STATEMENT ||
15561531
sourceConnection.type === ConnectionType.INPUT_VALUE
15571532
) {
1558-
const dx = originalOffsetInBlock.x - sourceConnection.getOffsetInBlock().x;
1559-
const dy = originalOffsetInBlock.y - sourceConnection.getOffsetInBlock().y;
1533+
// First move the block to match the orginal target connection position
1534+
let dx = originalOffsetToTarget.x;
1535+
let dy = originalOffsetToTarget.y;
1536+
// Then adjust its position according to the connection resize
1537+
dx += originalOffsetInBlock.x - sourceConnection.getOffsetInBlock().x;
1538+
dy += originalOffsetInBlock.y - sourceConnection.getOffsetInBlock().y;
15601539

15611540
this.moveBy(dx, dy);
15621541
}

core/insertion_marker_manager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,16 +611,16 @@ export class InsertionMarkerManager {
611611
insertionMarker.queueRender();
612612
renderManagement.triggerQueuedRenders();
613613

614-
// Position so that the existing block doesn't move.
615-
insertionMarker.positionNearConnection(imConn, closest);
616614
// Connect() also renders the insertion marker.
617615
imConn.connect(closest);
618616

619-
let originalOffsetInBlock = imConn.getOffsetInBlock().clone();
617+
const originalOffsetToTarget = {x: closest.x - imConn.x, y: closest.y - imConn.y}
618+
const originalOffsetInBlock = imConn.getOffsetInBlock().clone();
620619
const imConnConst = imConn;
621620
renderManagement.finishQueuedRenders().then(() => {
621+
// Position so that the existing block doesn't move.
622+
insertionMarker?.positionNearConnection(imConnConst, originalOffsetToTarget, originalOffsetInBlock);
622623
insertionMarker?.getSvgRoot().setAttribute('visibility', 'visible');
623-
insertionMarker?.repositionAfterConnectionResize(imConnConst, originalOffsetInBlock);
624624
});
625625

626626
this.markerConnection = imConn;

0 commit comments

Comments
 (0)