Skip to content

Commit 09e5d0e

Browse files
feat: improve heuristic insert for statement blocks (#432)
Move up to the block if we try to insert a statement block in a value input, value connection, field or value block. This has a similar feel to the heuristic that uses the value input on a block if you insert a value block.
1 parent 7c9df72 commit 09e5d0e

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/navigation.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,33 @@ export class Navigation {
675675
const stationaryType = stationaryNode.getType();
676676
const stationaryLoc = stationaryNode.getLocation();
677677

678-
if (stationaryNode.isConnection()) {
679-
// Connect the moving block to the stationary connection using
680-
// the most plausible connection on the moving block.
678+
if (stationaryNode.getType() === Blockly.ASTNode.types.FIELD) {
679+
const sourceBlock = stationaryNode.getSourceBlock();
680+
if (!sourceBlock) return false;
681+
return this.tryToConnectBlock(
682+
Blockly.ASTNode.createBlockNode(sourceBlock),
683+
movingBlock,
684+
);
685+
} else if (stationaryNode.isConnection()) {
681686
const stationaryAsConnection =
682687
stationaryLoc as Blockly.RenderedConnection;
688+
689+
// Move to the block if we're trying to insert a statement block into
690+
// a value connection.
691+
if (
692+
!movingBlock.outputConnection &&
693+
stationaryAsConnection.type === Blockly.ConnectionType.INPUT_VALUE
694+
) {
695+
const sourceBlock = stationaryNode.getSourceBlock();
696+
if (!sourceBlock) return false;
697+
return this.tryToConnectBlock(
698+
Blockly.ASTNode.createBlockNode(sourceBlock),
699+
movingBlock,
700+
);
701+
}
702+
703+
// Connect the moving block to the stationary connection using
704+
// the most plausible connection on the moving block.
683705
return this.insertBlock(movingBlock, stationaryAsConnection);
684706
} else if (stationaryType === Blockly.ASTNode.types.WORKSPACE) {
685707
return this.moveBlockToWorkspace(movingBlock, stationaryNode);
@@ -715,6 +737,18 @@ export class Navigation {
715737

716738
// 3. Output connection. This will wrap around or displace.
717739
if (stationaryBlock.outputConnection) {
740+
// Move to parent if we're trying to insert a statement block.
741+
if (
742+
!movingBlock.outputConnection &&
743+
stationaryNode.getType() === Blockly.ASTNode.types.BLOCK
744+
) {
745+
const parent = stationaryNode.getSourceBlock()?.getParent();
746+
if (!parent) return false;
747+
return this.tryToConnectBlock(
748+
Blockly.ASTNode.createBlockNode(parent),
749+
movingBlock,
750+
);
751+
}
718752
return this.insertBlock(movingBlock, stationaryBlock.outputConnection);
719753
}
720754
}

0 commit comments

Comments
 (0)