Skip to content

Commit b0b1611

Browse files
feat: use core looping feature
1 parent 90de052 commit b0b1611

File tree

1 file changed

+7
-75
lines changed

1 file changed

+7
-75
lines changed

src/keyboard_drag_strategy.ts

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,20 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
144144
const cursor = draggingBlock.workspace.getCursor() as LineCursor;
145145
if (!cursor) return null;
146146

147+
// Helper function for traversal.
148+
function isConnection(node: ASTNode | null): boolean {
149+
return !!node && node.isConnection();
150+
}
151+
147152
const connectionChecker = draggingBlock.workspace.connectionChecker;
148153
let candidateConnection: ConnectionCandidate | null = null;
149154
let potential: ASTNode | null = this.searchNode;
150155
const dir = this.currentDragDirection;
151156
while (potential && !candidateConnection) {
152157
if (dir === Direction.Up || dir === Direction.Left) {
153-
potential = this.getPreviousNode(potential, cursor);
158+
potential = cursor.getPreviousNode(potential, isConnection, true);
154159
} else if (dir === Direction.Down || dir === Direction.Right) {
155-
potential = this.getNextNode(potential, cursor);
160+
potential = cursor.getNextNode(potential, isConnection, true);
156161
}
157162

158163
localConns.forEach((conn: RenderedConnection) => {
@@ -283,77 +288,4 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
283288
override shouldHealStack(e: PointerEvent | undefined): boolean {
284289
return true;
285290
}
286-
287-
/**
288-
* Get the previous node in the tree, with loopback to the last
289-
* stack on the workspace if needed.
290-
*
291-
* @param start Where to start traversal.
292-
* @param cursor The workspace's cursor
293-
* @returns The previous node, or null if there were no valid nodes
294-
* on the workspace.
295-
*/
296-
private getPreviousNode(start: ASTNode, cursor: LineCursor) {
297-
let potential: ASTNode | null = start;
298-
potential = cursor.getPreviousNode(potential, (node) => {
299-
// @ts-expect-error isConnectionType is private.
300-
return node && ASTNode.isConnectionType(node.getType());
301-
});
302-
if (!potential) {
303-
// Loop back to last block if it exists.
304-
// @ts-expect-error workspace is private
305-
const topBlocks = this.workspace.getTopBlocks(true);
306-
if (!topBlocks.length) return null;
307-
308-
// Find the last stack.
309-
const lastTopBlockNode = ASTNode.createStackNode(
310-
topBlocks[topBlocks.length - 1],
311-
);
312-
let prevNode = lastTopBlockNode;
313-
let nextNode: ASTNode | null = lastTopBlockNode;
314-
// Iterate until you fall off the end of the stack.
315-
while (nextNode) {
316-
prevNode = nextNode;
317-
nextNode = cursor.getNextNode(prevNode, (node) => {
318-
return !!node;
319-
});
320-
}
321-
322-
// Resume searching.
323-
potential = cursor.getPreviousNode(prevNode, (node) => {
324-
// @ts-expect-error isConnectionType is private.
325-
return node && ASTNode.isConnectionType(node.getType());
326-
});
327-
}
328-
return potential;
329-
}
330-
331-
/**
332-
* Get the next node in the tree, with loopback to the first
333-
* stack on the workspace if needed.
334-
*
335-
* @param start Where to start traversal.
336-
* @param cursor The workspace's cursor
337-
* @returns The next node, or null if there were no valid nodes
338-
* on the workspace.
339-
*/
340-
private getNextNode(start: ASTNode, cursor: LineCursor) {
341-
let potential: ASTNode | null = start;
342-
potential = cursor.getNextNode(potential, (node) => {
343-
// @ts-expect-error isConnectionType is private.
344-
return node && ASTNode.isConnectionType(node.getType());
345-
});
346-
if (!potential) {
347-
// Loop back to first block if it exists.
348-
// @ts-expect-error workspace is private
349-
const topBlocks = this.workspace.getTopBlocks(true);
350-
if (!topBlocks.length) return null;
351-
const initial = ASTNode.createTopNode(topBlocks[0]);
352-
potential = cursor.getNextNode(initial, (node) => {
353-
// @ts-expect-error isConnectionType is private.
354-
return node && ASTNode.isConnectionType(node.getType());
355-
});
356-
}
357-
return potential;
358-
}
359291
}

0 commit comments

Comments
 (0)