Skip to content

Commit 7eb748b

Browse files
committed
fix: Fix bug that caused blocks to be inserted into an immovable stack.
1 parent 0888bc2 commit 7eb748b

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/navigation.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,28 @@ export class Navigation {
429429
return connection as Blockly.RenderedConnection;
430430
}
431431

432-
// 2. Connect statement blocks to next connection.
432+
// 2. Connect statement blocks to next connection. Only return a next
433+
// connection to which the statement block can actually connect; some
434+
// may be ineligible because they are e.g. in the middle of an immovable
435+
// stack.
433436
if (stationaryNode.nextConnection && !movingHasOutput) {
434-
return stationaryNode.nextConnection;
437+
let nextConnection: Blockly.RenderedConnection | null =
438+
stationaryNode.nextConnection;
439+
while (nextConnection) {
440+
if (
441+
movingBlock.workspace.connectionChecker.canConnect(
442+
movingBlock.previousConnection,
443+
nextConnection,
444+
true,
445+
Infinity,
446+
)
447+
) {
448+
return nextConnection;
449+
}
450+
nextConnection =
451+
nextConnection.getSourceBlock().getNextBlock()?.nextConnection ??
452+
null;
453+
}
435454
}
436455

437456
// 3. Output connection. This will wrap around or displace.

test/webdriverio/test/insert_test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import * as chai from 'chai';
8+
import * as Blockly from 'blockly';
89
import {Key} from 'webdriverio';
910
import {
1011
getFocusedBlockType,
@@ -103,4 +104,32 @@ suite('Insert test', function () {
103104
await getFocusedBlockType(this.browser),
104105
);
105106
});
107+
108+
test('Does not insert between immovable blocks', async function () {
109+
// Focus the create canvas block; we want to ensure that the newly
110+
// inserted block is not attached to its next connection, because doing
111+
// so would splice it into an immovable stack.
112+
await focusOnBlock(this.browser, 'create_canvas_1');
113+
await this.browser.execute(() => {
114+
Blockly.getMainWorkspace()
115+
.getAllBlocks()
116+
.forEach((b) => b.setMovable(false));
117+
});
118+
await tabNavigateToToolbox(this.browser);
119+
120+
// Insert 'if' block
121+
await keyRight(this.browser);
122+
// Choose.
123+
await sendKeyAndWait(this.browser, Key.Enter);
124+
// Confirm position.
125+
await sendKeyAndWait(this.browser, Key.Enter);
126+
127+
// Assert inserted inside first block p5_setup not at top-level.
128+
chai.assert.equal('controls_if', await getFocusedBlockType(this.browser));
129+
await keyUp(this.browser);
130+
chai.assert.equal(
131+
'p5_background_color',
132+
await getFocusedBlockType(this.browser),
133+
);
134+
});
106135
});

0 commit comments

Comments
 (0)