From 6d7307f9d9e7f410b2869ee4ad62e1e2e5a37996 Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Mon, 11 Nov 2024 18:24:31 +0000 Subject: [PATCH] feat: Make disconnection work on blocks and fields (#116) Fixes #93. --- src/navigation.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/navigation.ts b/src/navigation.ts index 199affc..0c876e1 100644 --- a/src/navigation.ts +++ b/src/navigation.ts @@ -1057,11 +1057,14 @@ export class Navigation { if (!cursor) { return; } - const curNode = cursor.getCurNode(); - if (!curNode.isConnection()) { - this.log( - 'Cannot disconnect blocks when the cursor is not on a connection', - ); + let curNode: Blockly.ASTNode | null = cursor.getCurNode(); + let wasVisitingConnection = true; + while (curNode && !curNode.isConnection()) { + curNode = curNode.out(); + wasVisitingConnection = false; + } + if (!curNode) { + this.log('Unable to find a connection to disconnect'); return; } const curConnection = curNode.getLocation() as Blockly.RenderedConnection; @@ -1087,9 +1090,11 @@ export class Navigation { const rootBlock = superiorConnection.getSourceBlock().getRootBlock(); rootBlock.bringToFront(); - const connectionNode = - Blockly.ASTNode.createConnectionNode(superiorConnection); - workspace.getCursor()!.setCurNode(connectionNode!); + if (wasVisitingConnection) { + const connectionNode = + Blockly.ASTNode.createConnectionNode(superiorConnection); + workspace.getCursor()!.setCurNode(connectionNode!); + } } /**