Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/actions/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ const createSerializedKey = ShortcutRegistry.registry.createSerializedKey.bind(
* Actions for moving blocks with keyboard shortcuts.
*/
export class MoveActions {
/**
* Stored to enable us to restore monkey patch.
*/
private oldShortcutRegistryOnKeyDown:
| typeof ShortcutRegistry.registry.onKeyDown
| null = null;

constructor(private mover: Mover) {}

private shortcuts: ShortcutRegistry.KeyboardShortcut[] = [
Expand Down Expand Up @@ -154,6 +161,32 @@ export class MoveActions {
for (const menuItem of this.menuItems) {
ContextMenuRegistry.registry.register(menuItem);
}

// Monkey patch shortcut registry to finish any in-progress move for all
// non-move-related actions.
this.oldShortcutRegistryOnKeyDown = ShortcutRegistry.registry.onKeyDown;
ShortcutRegistry.registry.onKeyDown = (workspace, e) => {
if (!this.oldShortcutRegistryOnKeyDown) return false;
// @ts-expect-error private method
const key = ShortcutRegistry.registry.serializeKeyEvent(e);
const moveShortcutNames =
ShortcutRegistry.registry.getShortcutNamesByKeyCode(key);
if (
!this.shortcuts.some((shortcut) =>
moveShortcutNames?.includes(shortcut.name),
)
) {
if (this.mover.isMoving(workspace)) {
this.mover.finishMove(workspace);
}
}

return this.oldShortcutRegistryOnKeyDown.call(
ShortcutRegistry.registry,
workspace,
e,
);
};
}

/**
Expand All @@ -166,5 +199,9 @@ export class MoveActions {
for (const menuItem of this.menuItems) {
ContextMenuRegistry.registry.unregister(menuItem.id);
}

if (this.oldShortcutRegistryOnKeyDown) {
ShortcutRegistry.registry.onKeyDown = this.oldShortcutRegistryOnKeyDown;
}
}
}