|
6 | 6 |
|
7 | 7 | import { |
8 | 8 | BlockSvg, |
9 | | - RenderedConnection, |
| 9 | + Events, |
10 | 10 | ShortcutRegistry, |
11 | 11 | utils as BlocklyUtils, |
| 12 | + Connection, |
| 13 | + ConnectionType, |
12 | 14 | } from 'blockly'; |
13 | 15 | import * as Constants from '../constants'; |
14 | | -import type {WorkspaceSvg, IFocusableNode} from 'blockly'; |
| 16 | +import type {WorkspaceSvg} from 'blockly'; |
15 | 17 | import {Navigation} from '../navigation'; |
16 | 18 |
|
17 | 19 | const KeyCodes = BlocklyUtils.KeyCodes; |
@@ -76,66 +78,16 @@ export class DisconnectAction { |
76 | 78 | */ |
77 | 79 | disconnectBlocks(workspace: WorkspaceSvg) { |
78 | 80 | 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; |
133 | 84 |
|
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); |
136 | 89 |
|
137 | | - if (wasVisitingConnection) { |
138 | | - workspace.getCursor()?.setCurNode(superiorConnection); |
139 | | - } |
| 90 | + // Needed or we end up with passive focus. |
| 91 | + cursor.setCurNode(curNode); |
140 | 92 | } |
141 | 93 | } |
0 commit comments