Skip to content

Commit

Permalink
chore: position insertion marker in a single function
Browse files Browse the repository at this point in the history
  • Loading branch information
clementcontet committed Sep 12, 2023
1 parent a2a9ab1 commit b95067b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
39 changes: 9 additions & 30 deletions core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1515,38 +1515,13 @@ export class BlockSvg
* or an insertion marker.
*
* @param sourceConnection The connection on the moving block's stack.
* @param targetConnection The connection that should stay stationary as this
* block is positioned.
* @param originalOffsetToTarget The connection original offset to the target connection
* @param originalOffsetInBlock The connection original offset in its block
* @internal
*/
positionNearConnection(
sourceConnection: RenderedConnection,
targetConnection: RenderedConnection,
) {
// We only need to position the new block if it's before the existing one,
// otherwise its position is set by the previous block.
if (
sourceConnection.type === ConnectionType.NEXT_STATEMENT ||
sourceConnection.type === ConnectionType.INPUT_VALUE
) {
const dx = targetConnection.x - sourceConnection.x;
const dy = targetConnection.y - sourceConnection.y;

this.moveBy(dx, dy);
}
}

/**
* Reposition a block after its connection has been resized, to match exactly the target block position.
* The block to position is usually either the first block in a dragged stack
* or an insertion marker.
*
* @param sourceConnection The connection on the moving block's stack.
* @param originalOffsetInBlock The connection original offset in its block, before the resize occured
* @internal
*/
repositionAfterConnectionResize(
sourceConnection: RenderedConnection,
originalOffsetToTarget: {x: number; y: number},
originalOffsetInBlock: Coordinate,
) {
// We only need to position the new block if it's before the existing one,
Expand All @@ -1555,8 +1530,12 @@ export class BlockSvg
sourceConnection.type === ConnectionType.NEXT_STATEMENT ||
sourceConnection.type === ConnectionType.INPUT_VALUE
) {
const dx = originalOffsetInBlock.x - sourceConnection.getOffsetInBlock().x;
const dy = originalOffsetInBlock.y - sourceConnection.getOffsetInBlock().y;
// First move the block to match the orginal target connection position
let dx = originalOffsetToTarget.x;
let dy = originalOffsetToTarget.y;
// Then adjust its position according to the connection resize
dx += originalOffsetInBlock.x - sourceConnection.getOffsetInBlock().x;
dy += originalOffsetInBlock.y - sourceConnection.getOffsetInBlock().y;

this.moveBy(dx, dy);
}
Expand Down
8 changes: 4 additions & 4 deletions core/insertion_marker_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,16 +611,16 @@ export class InsertionMarkerManager {
insertionMarker.queueRender();
renderManagement.triggerQueuedRenders();

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

let originalOffsetInBlock = imConn.getOffsetInBlock().clone();
const originalOffsetToTarget = {x: closest.x - imConn.x, y: closest.y - imConn.y}
const originalOffsetInBlock = imConn.getOffsetInBlock().clone();
const imConnConst = imConn;
renderManagement.finishQueuedRenders().then(() => {
// Position so that the existing block doesn't move.
insertionMarker?.positionNearConnection(imConnConst, originalOffsetToTarget, originalOffsetInBlock);
insertionMarker?.getSvgRoot().setAttribute('visibility', 'visible');
insertionMarker?.repositionAfterConnectionResize(imConnConst, originalOffsetInBlock);
});

this.markerConnection = imConn;
Expand Down

0 comments on commit b95067b

Please sign in to comment.