Skip to content
Merged
Show file tree
Hide file tree
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
77 changes: 77 additions & 0 deletions src/actions/undo_redo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import {
ShortcutRegistry,
utils as BlocklyUtils,
ShortcutItems,
WorkspaceSvg,
} from 'blockly/core';

import * as Constants from '../constants';

Check warning on line 14 in src/actions/undo_redo.ts

View workflow job for this annotation

GitHub Actions / build

'Constants' is defined but never used
import type {Navigation} from '../navigation';

Check warning on line 15 in src/actions/undo_redo.ts

View workflow job for this annotation

GitHub Actions / build

'Navigation' is defined but never used

const KeyCodes = BlocklyUtils.KeyCodes;

Check warning on line 17 in src/actions/undo_redo.ts

View workflow job for this annotation

GitHub Actions / build

'KeyCodes' is assigned a value but never used

/**
* Class for registering a shortcut for undo/redo actions.
*/
export class UndoRedoAction {
private originalUndo?: ShortcutRegistry.KeyboardShortcut;
private originalRedo?: ShortcutRegistry.KeyboardShortcut;
/**
* Patches the existing undo/redo shortcuts in the registry.
*/
install() {
const undo =
ShortcutRegistry.registry.getRegistry()[ShortcutItems.names.UNDO];
if (undo) {
this.originalUndo = undo;
const patchedUndo = {
...this.originalUndo,
preconditionFn: (workspace: WorkspaceSvg) => {
return !!(
!workspace.isDragging() && undo.preconditionFn?.(workspace)
);
},
allowCollision: true,
};

ShortcutRegistry.registry.register(patchedUndo, true);
}

const redo =
ShortcutRegistry.registry.getRegistry()[ShortcutItems.names.REDO];
if (redo) {
this.originalRedo = redo;
const patchedRedo = {
...this.originalRedo,
preconditionFn: (workspace: WorkspaceSvg) => {
return !!(
!workspace.isDragging() && redo.preconditionFn?.(workspace)
);
},
allowCollision: true,
};

ShortcutRegistry.registry.register(patchedRedo, true);
}
}

/**
* Reverts the patched undo/redo shortcuts in the registry.
*/
uninstall() {
if (this.originalUndo) {
ShortcutRegistry.registry.register(this.originalUndo, true);
this.originalUndo = undefined;
}
if (this.originalRedo) {
ShortcutRegistry.registry.register(this.originalRedo, true);
this.originalRedo = undefined;
}
}
}
5 changes: 5 additions & 0 deletions src/navigation_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {DisconnectAction} from './actions/disconnect';
import {ActionMenu} from './actions/action_menu';
import {MoveActions} from './actions/move';
import {Mover} from './actions/mover';
import {UndoRedoAction} from './actions/undo_redo';

const KeyCodes = BlocklyUtils.KeyCodes;

Expand Down Expand Up @@ -72,6 +73,8 @@ export class NavigationController {

enterAction: EnterAction = new EnterAction(this.navigation);

undoRedoAction: UndoRedoAction = new UndoRedoAction();

actionMenu: ActionMenu = new ActionMenu(this.navigation);

moveActions = new MoveActions(this.mover);
Expand Down Expand Up @@ -289,6 +292,7 @@ export class NavigationController {
this.exitAction.install();
this.enterAction.install();
this.disconnectAction.install();
this.undoRedoAction.install();
this.actionMenu.install();

this.clipboard.install();
Expand All @@ -315,6 +319,7 @@ export class NavigationController {
this.arrowNavigation.uninstall();
this.exitAction.uninstall();
this.enterAction.uninstall();
this.undoRedoAction.uninstall();
this.actionMenu.uninstall();
this.shortcutDialog.uninstall();

Expand Down