Skip to content

Commit 0bf917d

Browse files
authored
refactor: Move exit action into its own class. (#305)
1 parent 6ad95c0 commit 0bf917d

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed

src/actions/exit.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import {ShortcutRegistry, utils as BlocklyUtils} from 'blockly/core';
8+
9+
import type {WorkspaceSvg} from 'blockly/core';
10+
11+
import * as Constants from '../constants';
12+
import type {Navigation} from '../navigation';
13+
14+
const KeyCodes = BlocklyUtils.KeyCodes;
15+
16+
/**
17+
* Class for registering a shortcut for the exit action.
18+
*/
19+
export class ExitAction {
20+
constructor(
21+
private navigation: Navigation,
22+
private canCurrentlyNavigate: (ws: WorkspaceSvg) => boolean,
23+
) {}
24+
25+
/**
26+
* Adds the exit action shortcut to the registry.
27+
*/
28+
install() {
29+
ShortcutRegistry.registry.register({
30+
name: Constants.SHORTCUT_NAMES.EXIT,
31+
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
32+
callback: (workspace) => {
33+
switch (this.navigation.getState(workspace)) {
34+
case Constants.STATE.FLYOUT:
35+
case Constants.STATE.TOOLBOX:
36+
this.navigation.focusWorkspace(workspace);
37+
return true;
38+
default:
39+
return false;
40+
}
41+
},
42+
keyCodes: [KeyCodes.ESC],
43+
allowCollision: true,
44+
});
45+
}
46+
47+
/**
48+
* Removes the exit action shortcut.
49+
*/
50+
uninstall() {
51+
ShortcutRegistry.registry.unregister(Constants.SHORTCUT_NAMES.EXIT);
52+
}
53+
}

src/navigation_controller.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {DeleteAction} from './actions/delete';
3131
import {InsertAction} from './actions/insert';
3232
import {Clipboard} from './actions/clipboard';
3333
import {WorkspaceMovement} from './actions/ws_movement';
34+
import {ExitAction} from './actions/exit';
3435
import {EnterAction} from './actions/enter';
3536
import {DisconnectAction} from './actions/disconnect';
3637

@@ -84,12 +85,15 @@ export class NavigationController {
8485
this.canCurrentlyEdit.bind(this),
8586
);
8687

88+
exitAction: ExitAction = new ExitAction(
89+
this.navigation,
90+
this.canCurrentlyNavigate.bind(this),
91+
8792
enterAction: EnterAction = new EnterAction(
8893
this.navigation,
8994
this.canCurrentlyEdit.bind(this),
9095
);
9196

92-
hasNavigationFocus: boolean = false;
9397
navigationFocus: NAVIGATION_FOCUS_MODE = NAVIGATION_FOCUS_MODE.NONE;
9498

9599
/**
@@ -489,26 +493,6 @@ export class NavigationController {
489493
keyCodes: [KeyCodes.T],
490494
},
491495

492-
/** Exit the current location and focus on the workspace. */
493-
exit: {
494-
name: Constants.SHORTCUT_NAMES.EXIT,
495-
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
496-
callback: (workspace) => {
497-
switch (this.navigation.getState(workspace)) {
498-
case Constants.STATE.FLYOUT:
499-
this.navigation.focusWorkspace(workspace);
500-
return true;
501-
case Constants.STATE.TOOLBOX:
502-
this.navigation.focusWorkspace(workspace);
503-
return true;
504-
default:
505-
return false;
506-
}
507-
},
508-
keyCodes: [KeyCodes.ESC],
509-
allowCollision: true,
510-
},
511-
512496
/** Announce the current location of the cursor. */
513497
announceLocation: {
514498
name: Constants.SHORTCUT_NAMES.ANNOUNCE,
@@ -655,6 +639,7 @@ export class NavigationController {
655639
this.deleteAction.install();
656640
this.insertAction.install();
657641
this.workspaceMovement.install();
642+
this.exitAction.install();
658643
this.enterAction.install();
659644
this.disconnectAction.install();
660645

@@ -680,6 +665,7 @@ export class NavigationController {
680665
this.disconnectAction.uninstall();
681666
this.clipboard.uninstall();
682667
this.workspaceMovement.uninstall();
668+
this.exitAction.uninstall();
683669
this.enterAction.uninstall();
684670
this.shortcutDialog.uninstall();
685671

0 commit comments

Comments
 (0)