From cc4419cd1d1c1a0b3dd8b20eb70e17ff47f986f1 Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Mon, 14 Apr 2025 15:26:12 +0100 Subject: [PATCH] feat: improve heuristic insert for statement blocks 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. --- src/navigation.ts | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/navigation.ts b/src/navigation.ts index dcf5e79c..72c112a0 100644 --- a/src/navigation.ts +++ b/src/navigation.ts @@ -675,11 +675,33 @@ export class Navigation { const stationaryType = stationaryNode.getType(); const stationaryLoc = stationaryNode.getLocation(); - if (stationaryNode.isConnection()) { - // Connect the moving block to the stationary connection using - // the most plausible connection on the moving block. + if (stationaryNode.getType() === Blockly.ASTNode.types.FIELD) { + const sourceBlock = stationaryNode.getSourceBlock(); + if (!sourceBlock) return false; + return this.tryToConnectBlock( + Blockly.ASTNode.createBlockNode(sourceBlock), + movingBlock, + ); + } else if (stationaryNode.isConnection()) { const stationaryAsConnection = stationaryLoc as Blockly.RenderedConnection; + + // Move to the block if we're trying to insert a statement block into + // a value connection. + if ( + !movingBlock.outputConnection && + stationaryAsConnection.type === Blockly.ConnectionType.INPUT_VALUE + ) { + const sourceBlock = stationaryNode.getSourceBlock(); + if (!sourceBlock) return false; + return this.tryToConnectBlock( + Blockly.ASTNode.createBlockNode(sourceBlock), + movingBlock, + ); + } + + // Connect the moving block to the stationary connection using + // the most plausible connection on the moving block. return this.insertBlock(movingBlock, stationaryAsConnection); } else if (stationaryType === Blockly.ASTNode.types.WORKSPACE) { return this.moveBlockToWorkspace(movingBlock, stationaryNode); @@ -715,6 +737,18 @@ export class Navigation { // 3. Output connection. This will wrap around or displace. if (stationaryBlock.outputConnection) { + // Move to parent if we're trying to insert a statement block. + if ( + !movingBlock.outputConnection && + stationaryNode.getType() === Blockly.ASTNode.types.BLOCK + ) { + const parent = stationaryNode.getSourceBlock()?.getParent(); + if (!parent) return false; + return this.tryToConnectBlock( + Blockly.ASTNode.createBlockNode(parent), + movingBlock, + ); + } return this.insertBlock(movingBlock, stationaryBlock.outputConnection); } }