Skip to content

Commit 1ad0608

Browse files
authored
feat: Stop on workspace during constrained move (#9649)
1 parent 6059d1f commit 1ad0608

2 files changed

Lines changed: 31 additions & 23 deletions

File tree

packages/blockly/core/dragging/block_drag_strategy.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -573,18 +573,13 @@ export class BlockDragStrategy implements IDragStrategy {
573573
const direction = this.getDirectionToNewLocation(
574574
Coordinate.sum(this.startLoc!, delta),
575575
);
576-
const candidate = this.findTraversalCandidate(direction);
577-
if (candidate) {
578-
return candidate;
579-
}
580-
581-
delta = new Coordinate(0, 0);
576+
return this.findTraversalCandidate(direction);
582577
}
583578

584579
// If we do not have a candidate yet, we fallback to the closest one nearby.
585580
let radius = this.getSearchRadius();
586581
const localConns = this.getLocalConnections(this.block);
587-
let candidate = null;
582+
let candidate: ConnectionCandidate | null = null;
588583

589584
for (const conn of localConns) {
590585
const {connection: neighbour, radius: rad} = conn.closest(radius, delta);
@@ -775,30 +770,40 @@ export class BlockDragStrategy implements IDragStrategy {
775770
* @returns A candidate connection and radius, or null if none was found.
776771
*/
777772
findTraversalCandidate(direction: Direction): ConnectionCandidate | null {
778-
const currentPairIndex = this.allConnectionPairs.findIndex(
773+
const pairs = this.allConnectionPairs;
774+
if (direction === Direction.NONE || !pairs.length) {
775+
return this.connectionCandidate;
776+
}
777+
const forwardTraversal =
778+
direction === Direction.RIGHT || direction === Direction.DOWN;
779+
const currentPairIndex = pairs.findIndex(
779780
(pair) =>
780781
this.connectionCandidate?.local === pair.local &&
781782
this.connectionCandidate?.neighbour === pair.neighbour,
782783
);
783-
if (currentPairIndex !== -1) {
784-
if (direction === Direction.UP || direction === Direction.LEFT) {
785-
const nextPair =
786-
this.allConnectionPairs[currentPairIndex - 1] ??
787-
this.allConnectionPairs[this.allConnectionPairs.length - 1];
788-
return {...nextPair, distance: 0};
789-
} else if (
790-
direction === Direction.DOWN ||
791-
direction === Direction.RIGHT
792-
) {
793-
const nextPair =
794-
this.allConnectionPairs[currentPairIndex + 1] ??
795-
this.allConnectionPairs[0];
796-
return {...nextPair, distance: 0};
784+
785+
if (forwardTraversal) {
786+
if (currentPairIndex === -1) {
787+
return this.pairToCandidate(pairs[0]);
788+
} else if (currentPairIndex === pairs.length - 1) {
789+
return null;
790+
} else {
791+
return this.pairToCandidate(pairs[currentPairIndex + 1]);
792+
}
793+
} else {
794+
if (currentPairIndex === -1) {
795+
return this.pairToCandidate(pairs[pairs.length - 1]);
796+
} else if (currentPairIndex === 0) {
797+
return null;
798+
} else {
799+
return this.pairToCandidate(pairs[currentPairIndex - 1]);
797800
}
798801
}
799-
return null;
800802
}
801803

804+
private pairToCandidate(pair: ConnectionPair): ConnectionCandidate {
805+
return {...pair, distance: 0};
806+
}
802807
/**
803808
* Returns the cardinal direction that the block being dragged would have to
804809
* move in to reach the given location.

packages/blockly/tests/mocha/keyboard_movement_test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ suite('Keyboard-driven movement', function () {
583583
{id: 'controls_if', index: 6, ownIndex: 0}, // "Else" statement input.
584584
{id: 'controls_if', index: 1, ownIndex: 0}, // Next.
585585
{id: 'p5_draw', index: 0, ownIndex: 0}, // Statement input.
586+
null, // Disconnected on workspace
586587
];
587588
/**
588589
* Expected connection candidates when moving STATEMENT_SIMPLE after
@@ -653,6 +654,7 @@ suite('Keyboard-driven movement', function () {
653654
{id: 'controls_if', index: 6, ownIndex: 0}, // "Else" statement input.
654655
{id: 'controls_if', index: 1, ownIndex: 0}, // Next.
655656
{id: 'p5_draw', index: 0, ownIndex: 0}, // Statement input.
657+
null, // Disconnected on workspace
656658
];
657659
/**
658660
* Expected connection candidates when moving STATEMENT_COMPLEX after
@@ -761,6 +763,7 @@ suite('Keyboard-driven movement', function () {
761763
{id: 'join2', index: 1, ownIndex: 0}, // Join block ADD0 input.
762764
{id: 'join2', index: 2, ownIndex: 0}, // Join block ADD1 input.
763765
// Skip input of unattached join block.
766+
null, // Disconnected on workspace
764767
];
765768
/**
766769
* Expected connection candidates when moving BLOCK_SIMPLE, after

0 commit comments

Comments
 (0)