Skip to content

Commit 41994da

Browse files
feat: align disconnect with move + mouse behaviours (#542)
* feat: align disconnect with move + mouse behaviours - Single statement block to match move. Perhaps both could do multi-block with uppercase. - Just call unplug like the drag strategy does. This means the disconnected block ends up the current node. Previous (presumably accidental) behaviour was to select the connection in a way that can't normally be navigated to causing it to persist oddly. * fix: add missing setGroup(false)
1 parent b3587ef commit 41994da

File tree

1 file changed

+13
-61
lines changed

1 file changed

+13
-61
lines changed

src/actions/disconnect.ts

Lines changed: 13 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
import {
88
BlockSvg,
9-
RenderedConnection,
9+
Events,
1010
ShortcutRegistry,
1111
utils as BlocklyUtils,
12+
Connection,
13+
ConnectionType,
1214
} from 'blockly';
1315
import * as Constants from '../constants';
14-
import type {WorkspaceSvg, IFocusableNode} from 'blockly';
16+
import type {WorkspaceSvg} from 'blockly';
1517
import {Navigation} from '../navigation';
1618

1719
const KeyCodes = BlocklyUtils.KeyCodes;
@@ -76,66 +78,16 @@ export class DisconnectAction {
7678
*/
7779
disconnectBlocks(workspace: WorkspaceSvg) {
7880
const cursor = workspace.getCursor();
79-
if (!cursor) {
80-
return;
81-
}
82-
let curNode: IFocusableNode | null = cursor.getCurNode();
83-
let wasVisitingConnection = true;
84-
while (
85-
curNode &&
86-
!(curNode instanceof RenderedConnection && curNode.isConnected())
87-
) {
88-
if (curNode instanceof BlockSvg) {
89-
const previous = curNode.previousConnection;
90-
const output = curNode.outputConnection;
91-
if (previous?.isConnected()) {
92-
curNode = previous;
93-
break;
94-
} else if (output?.isConnected()) {
95-
curNode = output;
96-
break;
97-
}
98-
}
99-
100-
curNode = workspace.getNavigator().getParent(curNode);
101-
wasVisitingConnection = false;
102-
}
103-
if (!curNode) {
104-
console.log('Unable to find a connection to disconnect');
105-
return;
106-
}
107-
if (!(curNode instanceof RenderedConnection && curNode.isConnected())) {
108-
return;
109-
}
110-
const targetConnection = curNode.targetConnection;
111-
if (!targetConnection) {
112-
throw new Error('Must have target if connected');
113-
}
114-
115-
const superiorConnection = curNode.isSuperior()
116-
? curNode
117-
: targetConnection;
118-
119-
const inferiorConnection = curNode.isSuperior()
120-
? targetConnection
121-
: curNode;
122-
123-
if (inferiorConnection.getSourceBlock().isShadow()) {
124-
return;
125-
}
126-
127-
if (!inferiorConnection.getSourceBlock().isMovable()) {
128-
return;
129-
}
130-
131-
superiorConnection.disconnect();
132-
inferiorConnection.bumpAwayFrom(superiorConnection);
81+
if (!cursor) return;
82+
const curNode = cursor.getCurNode();
83+
if (!(curNode instanceof BlockSvg)) return;
13384

134-
const rootBlock = superiorConnection.getSourceBlock().getRootBlock();
135-
rootBlock.bringToFront();
85+
const healStack = !curNode.outputConnection?.isConnected();
86+
Events.setGroup(true);
87+
curNode.unplug(healStack);
88+
Events.setGroup(false);
13689

137-
if (wasVisitingConnection) {
138-
workspace.getCursor()?.setCurNode(superiorConnection);
139-
}
90+
// Needed or we end up with passive focus.
91+
cursor.setCurNode(curNode);
14092
}
14193
}

0 commit comments

Comments
 (0)