Skip to content

Commit 9425f39

Browse files
authored
fix: improve terminal position detection (#9973)
1 parent dce3315 commit 9425f39

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

packages/blockly/core/dragging/block_drag_strategy.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,19 +1085,26 @@ export class BlockDragStrategy implements IDragStrategy {
10851085
}
10861086

10871087
const topBlocks = block.workspace.getTopBlocks(true);
1088+
const blockRect = block.getBoundingRectangle();
1089+
1090+
// A block is terminal if it is above (moving up) or below (moving down) all other blocks
1091+
// that have valid connections with it. We need to consider the full shape of the blocks,
1092+
// not just their top-left origins.
1093+
const isMovingUp = direction === Direction.UP;
1094+
const edge = isMovingUp ? 'top' : 'bottom';
1095+
const directionalCompare = isMovingUp
1096+
? (a: number, b: number) => a <= b
1097+
: (a: number, b: number) => a >= b;
1098+
const blocksToConsider = topBlocks.filter(
1099+
(b) =>
1100+
b.id !== block.id &&
1101+
directionalCompare(b.getBoundingRectangle()[edge], blockRect[edge]),
1102+
);
10881103

1089-
const index = topBlocks.indexOf(block);
1090-
const delta = direction === Direction.UP ? -1 : 1;
1091-
const start = index + delta;
1092-
1093-
// Generally terminal blocks will be at the start or end of the sorted list
1094-
// of top blocks, but it still counts if all of the blocks before/after it
1095-
// have no valid connection points for the block in question.
10961104
const blockConnections = block.getConnections_(false);
1097-
for (let i = start; i >= 0 && i < topBlocks.length; i += delta) {
1098-
const topBlock = topBlocks[i];
1105+
for (const topBlock of blocksToConsider) {
1106+
const stackConnections = this.getAllConnections(topBlock);
10991107
for (const a of blockConnections) {
1100-
const stackConnections = this.getAllConnections(topBlock);
11011108
for (const b of stackConnections) {
11021109
if (
11031110
block.workspace.connectionChecker.canConnect(a, b, true, Infinity)

packages/blockly/tests/mocha/keyboard_movement_test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,7 @@ suite('Keyboard-driven movement', function () {
14391439
const boolean = this.workspace.newBlock('logic_boolean');
14401440
boolean.initSvg();
14411441
boolean.render();
1442+
this.workspace.cleanUp();
14421443

14431444
Blockly.getFocusManager().focusNode(boolean);
14441445
startMove(this.workspace);
@@ -1522,6 +1523,7 @@ suite('Keyboard-driven movement', function () {
15221523
const boolean = this.workspace.newBlock('logic_boolean');
15231524
boolean.initSvg();
15241525
boolean.render();
1526+
this.workspace.cleanUp();
15251527

15261528
Blockly.getFocusManager().focusNode(boolean);
15271529
startMove(this.workspace);

0 commit comments

Comments
 (0)