Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions src/keyboard_drag_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
BlockSvg,
ConnectionType,
RenderedConnection,
LineCursor,
dragging,
utils,
INavigable,

Check warning on line 13 in src/keyboard_drag_strategy.ts

View workflow job for this annotation

GitHub Actions / Eslint check

'INavigable' is defined but never used
} from 'blockly';
import {Direction, getDirectionFromXY} from './drag_direction';
import {showUnconstrainedMoveHint} from './hints';
Expand All @@ -37,8 +36,6 @@
/** Where a constrained movement should start when traversing the tree. */
private searchNode: RenderedConnection | null = null;

private cursor: LineCursor | null;

isNewBlock: boolean;

constructor(
Expand All @@ -47,12 +44,9 @@
) {
super(block);
this.isNewBlock = !!this.insertStartPoint;
this.cursor = block.workspace.getCursor();
}

override startDrag(e?: PointerEvent) {
if (!this.cursor) throw new Error('precondition failure');
this.cursor.setCurNode(this.block);
super.startDrag(e);
// Set position of the dragging block, so that it doesn't pop
// to the top left of the workspace.
Expand Down Expand Up @@ -170,29 +164,39 @@
draggingBlock: BlockSvg,
localConns: RenderedConnection[],
): ConnectionCandidate | null {
if (!this.cursor) throw new Error('precondition failure');

// Helper function for traversal.
function isConnection(
node: INavigable<any> | null,
): node is RenderedConnection {
return node instanceof RenderedConnection;
}

const connectionChecker = draggingBlock.workspace.connectionChecker;
let candidateConnection: ConnectionCandidate | null = null;
let potential: INavigable<any> | null = this.searchNode;
let potential: RenderedConnection | null = this.searchNode;
const allConnections: RenderedConnection[] = [];
for (const topBlock of draggingBlock.workspace.getTopBlocks(true)) {
allConnections.push(
...topBlock
.getDescendants(true)
.flatMap((block: BlockSvg) => block.getConnections_(false))
.sort((a: RenderedConnection, b: RenderedConnection) => {
let delta = a.y - b.y;
if (delta === 0) {
delta = a.x - b.x;
}
return delta;
}),
);
}

const dir = this.currentDragDirection;
while (potential && !candidateConnection) {
const potentialIndex = allConnections.indexOf(potential);
if (dir === Direction.Up || dir === Direction.Left) {
potential = this.cursor.getPreviousNode(potential, isConnection, true);
potential =
allConnections[potentialIndex - 1] ??
allConnections[allConnections.length - 1];
} else if (dir === Direction.Down || dir === Direction.Right) {
potential = this.cursor.getNextNode(potential, isConnection, true);
potential = allConnections[potentialIndex + 1] ?? allConnections[0];
}

localConns.forEach((conn: RenderedConnection) => {
if (
isConnection(potential) &&
potential &&
connectionChecker.canConnect(conn, potential, true, Infinity)
) {
candidateConnection = {
Expand Down
Loading