Skip to content

Commit 413316a

Browse files
committed
fix: Only calculate available connections once.
1 parent 3696e2b commit 413316a

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/keyboard_drag_strategy.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
3636
/** Where a constrained movement should start when traversing the tree. */
3737
private searchNode: RenderedConnection | null = null;
3838

39+
/** List of all connections available on the workspace. */
40+
private allConnections: RenderedConnection[] = [];
41+
3942
constructor(
4043
private block: BlockSvg,
4144
public moveType: MoveType,
@@ -46,6 +49,22 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
4649

4750
override startDrag(e?: PointerEvent) {
4851
super.startDrag(e);
52+
53+
for (const topBlock of this.block.workspace.getTopBlocks(true)) {
54+
this.allConnections.push(
55+
...topBlock
56+
.getDescendants(true)
57+
.flatMap((block: BlockSvg) => block.getConnections_(false))
58+
.sort((a: RenderedConnection, b: RenderedConnection) => {
59+
let delta = a.y - b.y;
60+
if (delta === 0) {
61+
delta = a.x - b.x;
62+
}
63+
return delta;
64+
}),
65+
);
66+
}
67+
4968
// Set position of the dragging block, so that it doesn't pop
5069
// to the top left of the workspace.
5170
// @ts-expect-error block and startLoc are private.
@@ -91,6 +110,7 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
91110

92111
override endDrag(e?: PointerEvent) {
93112
super.endDrag(e);
113+
this.allConnections = [];
94114
this.block.removeIcon(MoveIcon.type);
95115
}
96116

@@ -168,31 +188,17 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
168188
const connectionChecker = draggingBlock.workspace.connectionChecker;
169189
let candidateConnection: ConnectionCandidate | null = null;
170190
let potential: RenderedConnection | null = this.searchNode;
171-
const allConnections: RenderedConnection[] = [];
172-
for (const topBlock of draggingBlock.workspace.getTopBlocks(true)) {
173-
allConnections.push(
174-
...topBlock
175-
.getDescendants(true)
176-
.flatMap((block: BlockSvg) => block.getConnections_(false))
177-
.sort((a: RenderedConnection, b: RenderedConnection) => {
178-
let delta = a.y - b.y;
179-
if (delta === 0) {
180-
delta = a.x - b.x;
181-
}
182-
return delta;
183-
}),
184-
);
185-
}
186191

187192
const dir = this.currentDragDirection;
188193
while (potential && !candidateConnection) {
189-
const potentialIndex = allConnections.indexOf(potential);
194+
const potentialIndex = this.allConnections.indexOf(potential);
190195
if (dir === Direction.Up || dir === Direction.Left) {
191196
potential =
192-
allConnections[potentialIndex - 1] ??
193-
allConnections[allConnections.length - 1];
197+
this.allConnections[potentialIndex - 1] ??
198+
this.allConnections[this.allConnections.length - 1];
194199
} else if (dir === Direction.Down || dir === Direction.Right) {
195-
potential = allConnections[potentialIndex + 1] ?? allConnections[0];
200+
potential =
201+
this.allConnections[potentialIndex + 1] ?? this.allConnections[0];
196202
}
197203

198204
localConns.forEach((conn: RenderedConnection) => {

0 commit comments

Comments
 (0)