Skip to content

Commit 6d7307f

Browse files
authored
feat: Make disconnection work on blocks and fields (#116)
Fixes #93.
1 parent 0913c2e commit 6d7307f

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/navigation.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,11 +1057,14 @@ export class Navigation {
10571057
if (!cursor) {
10581058
return;
10591059
}
1060-
const curNode = cursor.getCurNode();
1061-
if (!curNode.isConnection()) {
1062-
this.log(
1063-
'Cannot disconnect blocks when the cursor is not on a connection',
1064-
);
1060+
let curNode: Blockly.ASTNode | null = cursor.getCurNode();
1061+
let wasVisitingConnection = true;
1062+
while (curNode && !curNode.isConnection()) {
1063+
curNode = curNode.out();
1064+
wasVisitingConnection = false;
1065+
}
1066+
if (!curNode) {
1067+
this.log('Unable to find a connection to disconnect');
10651068
return;
10661069
}
10671070
const curConnection = curNode.getLocation() as Blockly.RenderedConnection;
@@ -1087,9 +1090,11 @@ export class Navigation {
10871090
const rootBlock = superiorConnection.getSourceBlock().getRootBlock();
10881091
rootBlock.bringToFront();
10891092

1090-
const connectionNode =
1091-
Blockly.ASTNode.createConnectionNode(superiorConnection);
1092-
workspace.getCursor()!.setCurNode(connectionNode!);
1093+
if (wasVisitingConnection) {
1094+
const connectionNode =
1095+
Blockly.ASTNode.createConnectionNode(superiorConnection);
1096+
workspace.getCursor()!.setCurNode(connectionNode!);
1097+
}
10931098
}
10941099

10951100
/**

0 commit comments

Comments
 (0)