Skip to content

Commit 7cd35aa

Browse files
authored
refactor: Don't use the cursor to find connection positions. (#516)
1 parent c9d8689 commit 7cd35aa

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/keyboard_drag_strategy.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
BlockSvg,
99
ConnectionType,
1010
RenderedConnection,
11-
LineCursor,
1211
dragging,
1312
utils,
1413
INavigable,
@@ -37,8 +36,6 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
3736
/** Where a constrained movement should start when traversing the tree. */
3837
private searchNode: RenderedConnection | null = null;
3938

40-
private cursor: LineCursor | null;
41-
4239
isNewBlock: boolean;
4340

4441
constructor(
@@ -47,12 +44,9 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
4744
) {
4845
super(block);
4946
this.isNewBlock = !!this.insertStartPoint;
50-
this.cursor = block.workspace.getCursor();
5147
}
5248

5349
override startDrag(e?: PointerEvent) {
54-
if (!this.cursor) throw new Error('precondition failure');
55-
this.cursor.setCurNode(this.block);
5650
super.startDrag(e);
5751
// Set position of the dragging block, so that it doesn't pop
5852
// to the top left of the workspace.
@@ -170,29 +164,39 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
170164
draggingBlock: BlockSvg,
171165
localConns: RenderedConnection[],
172166
): ConnectionCandidate | null {
173-
if (!this.cursor) throw new Error('precondition failure');
174-
175-
// Helper function for traversal.
176-
function isConnection(
177-
node: INavigable<any> | null,
178-
): node is RenderedConnection {
179-
return node instanceof RenderedConnection;
180-
}
181-
182167
const connectionChecker = draggingBlock.workspace.connectionChecker;
183168
let candidateConnection: ConnectionCandidate | null = null;
184-
let potential: INavigable<any> | null = this.searchNode;
169+
let potential: RenderedConnection | null = this.searchNode;
170+
const allConnections: RenderedConnection[] = [];
171+
for (const topBlock of draggingBlock.workspace.getTopBlocks(true)) {
172+
allConnections.push(
173+
...topBlock
174+
.getDescendants(true)
175+
.flatMap((block: BlockSvg) => block.getConnections_(false))
176+
.sort((a: RenderedConnection, b: RenderedConnection) => {
177+
let delta = a.y - b.y;
178+
if (delta === 0) {
179+
delta = a.x - b.x;
180+
}
181+
return delta;
182+
}),
183+
);
184+
}
185+
185186
const dir = this.currentDragDirection;
186187
while (potential && !candidateConnection) {
188+
const potentialIndex = allConnections.indexOf(potential);
187189
if (dir === Direction.Up || dir === Direction.Left) {
188-
potential = this.cursor.getPreviousNode(potential, isConnection, true);
190+
potential =
191+
allConnections[potentialIndex - 1] ??
192+
allConnections[allConnections.length - 1];
189193
} else if (dir === Direction.Down || dir === Direction.Right) {
190-
potential = this.cursor.getNextNode(potential, isConnection, true);
194+
potential = allConnections[potentialIndex + 1] ?? allConnections[0];
191195
}
192196

193197
localConns.forEach((conn: RenderedConnection) => {
194198
if (
195-
isConnection(potential) &&
199+
potential &&
196200
connectionChecker.canConnect(conn, potential, true, Infinity)
197201
) {
198202
candidateConnection = {

0 commit comments

Comments
 (0)