Skip to content

Commit 306d4fc

Browse files
authored
feat: Commit moves before running keyboard shortcuts. (#564)
* feat: Commit moves before running keyboard shortcuts. * fix: Handle aborting moves.
1 parent 86e0950 commit 306d4fc

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/actions/mover.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
registry,
1919
utils,
2020
WorkspaceSvg,
21+
ShortcutRegistry,
2122
} from 'blockly';
2223
import * as Constants from '../constants';
2324
import {Direction, getXYFromDirection} from '../drag_direction';
@@ -36,6 +37,11 @@ const UNCONSTRAINED_MOVE_DISTANCE = 20;
3637
*/
3738
const CONSTRAINED_ADDITIONAL_PADDING = 70;
3839

40+
/**
41+
* Identifier for a keyboard shortcut that commits the in-progress move.
42+
*/
43+
const COMMIT_MOVE_SHORTCUT = 'commitMove';
44+
3945
/**
4046
* Low-level code for moving blocks with keyboard shortcuts.
4147
*/
@@ -140,6 +146,48 @@ export class Mover {
140146
// (otherwise dragging will break).
141147
getFocusManager().focusNode(block);
142148
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+
143191
return true;
144192
}
145193

@@ -208,6 +256,7 @@ export class Mover {
208256
* @returns The info for the block.
209257
*/
210258
private preDragEndCleanup(workspace: WorkspaceSvg) {
259+
ShortcutRegistry.registry.unregister(COMMIT_MOVE_SHORTCUT);
211260
clearMoveHints(workspace);
212261

213262
const info = this.moves.get(workspace);

0 commit comments

Comments
 (0)