@@ -18,6 +18,7 @@ import {
18
18
registry ,
19
19
utils ,
20
20
WorkspaceSvg ,
21
+ ShortcutRegistry ,
21
22
} from 'blockly' ;
22
23
import * as Constants from '../constants' ;
23
24
import { Direction , getXYFromDirection } from '../drag_direction' ;
@@ -36,6 +37,11 @@ const UNCONSTRAINED_MOVE_DISTANCE = 20;
36
37
*/
37
38
const CONSTRAINED_ADDITIONAL_PADDING = 70 ;
38
39
40
+ /**
41
+ * Identifier for a keyboard shortcut that commits the in-progress move.
42
+ */
43
+ const COMMIT_MOVE_SHORTCUT = 'commitMove' ;
44
+
39
45
/**
40
46
* Low-level code for moving blocks with keyboard shortcuts.
41
47
*/
@@ -140,6 +146,48 @@ export class Mover {
140
146
// (otherwise dragging will break).
141
147
getFocusManager ( ) . focusNode ( block ) ;
142
148
block . getFocusableElement ( ) . addEventListener ( 'blur' , blurListener ) ;
149
+
150
+ // Register a keyboard shortcut under the key combos of all existing
151
+ // keyboard shortcuts that commits the move before allowing the real
152
+ // shortcut to proceed. This avoids all kinds of fun brokenness when
153
+ // deleting/copying/otherwise acting on a block in move mode.
154
+ const shortcutKeys = Object . values ( ShortcutRegistry . registry . getRegistry ( ) )
155
+ . flatMap ( ( shortcut ) => shortcut . keyCodes )
156
+ . filter ( ( keyCode ) => {
157
+ return (
158
+ keyCode &&
159
+ ! [
160
+ utils . KeyCodes . RIGHT ,
161
+ utils . KeyCodes . LEFT ,
162
+ utils . KeyCodes . UP ,
163
+ utils . KeyCodes . DOWN ,
164
+ utils . KeyCodes . ENTER ,
165
+ utils . KeyCodes . ESC ,
166
+ ] . includes (
167
+ typeof keyCode === 'number'
168
+ ? keyCode
169
+ : parseInt ( `${ keyCode . split ( '+' ) . pop ( ) } ` ) ,
170
+ )
171
+ ) ;
172
+ } )
173
+ // Convince TS there aren't undefined values.
174
+ . filter ( ( keyCode ) : keyCode is string | number => ! ! keyCode ) ;
175
+
176
+ const commitMoveShortcut = {
177
+ name : COMMIT_MOVE_SHORTCUT ,
178
+ preconditionFn : ( workspace : WorkspaceSvg ) => {
179
+ return ! ! this . moves . get ( workspace ) ;
180
+ } ,
181
+ callback : ( workspace : WorkspaceSvg ) => {
182
+ this . finishMove ( workspace ) ;
183
+ return false ;
184
+ } ,
185
+ keyCodes : shortcutKeys ,
186
+ allowCollision : true ,
187
+ } ;
188
+
189
+ ShortcutRegistry . registry . register ( commitMoveShortcut ) ;
190
+
143
191
return true ;
144
192
}
145
193
@@ -208,6 +256,7 @@ export class Mover {
208
256
* @returns The info for the block.
209
257
*/
210
258
private preDragEndCleanup ( workspace : WorkspaceSvg ) {
259
+ ShortcutRegistry . registry . unregister ( COMMIT_MOVE_SHORTCUT ) ;
211
260
clearMoveHints ( workspace ) ;
212
261
213
262
const info = this . moves . get ( workspace ) ;
0 commit comments