@@ -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 )
0 commit comments