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

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

import type {WorkspaceSvg} from 'blockly/core';

import * as Constants from '../constants';
import type {Navigation} from '../navigation';

const KeyCodes = BlocklyUtils.KeyCodes;

/**
* Class for registering a shortcut for the exit action.
*/
export class ExitAction {
constructor(
private navigation: Navigation,
private canCurrentlyNavigate: (ws: WorkspaceSvg) => boolean,
) {}

/**
* Adds the exit action shortcut to the registry.
*/
install() {
ShortcutRegistry.registry.register({
name: Constants.SHORTCUT_NAMES.EXIT,
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
callback: (workspace) => {
switch (this.navigation.getState(workspace)) {
case Constants.STATE.FLYOUT:
case Constants.STATE.TOOLBOX:
this.navigation.focusWorkspace(workspace);
return true;
default:
return false;
}
},
keyCodes: [KeyCodes.ESC],
allowCollision: true,
});
}

/**
* Removes the exit action shortcut.
*/
uninstall() {
ShortcutRegistry.registry.unregister(Constants.SHORTCUT_NAMES.EXIT);
}
}
29 changes: 9 additions & 20 deletions src/navigation_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {DeleteAction} from './actions/delete';
import {InsertAction} from './actions/insert';
import {Clipboard} from './actions/clipboard';
import {WorkspaceMovement} from './actions/ws_movement';
import {ExitAction} from './actions/exit';
import {DisconnectAction} from './actions/disconnect';

const KeyCodes = BlocklyUtils.KeyCodes;
Expand Down Expand Up @@ -83,6 +84,12 @@ export class NavigationController {
this.canCurrentlyEdit.bind(this),
);

exitAction: ExitAction = new ExitAction(
this.navigation,
this.canCurrentlyNavigate.bind(this),
);

hasNavigationFocus: boolean = false;
navigationFocus: NAVIGATION_FOCUS_MODE = NAVIGATION_FOCUS_MODE.NONE;

/**
Expand Down Expand Up @@ -528,26 +535,6 @@ export class NavigationController {
keyCodes: [KeyCodes.T],
},

/** Exit the current location and focus on the workspace. */
exit: {
name: Constants.SHORTCUT_NAMES.EXIT,
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
callback: (workspace) => {
switch (this.navigation.getState(workspace)) {
case Constants.STATE.FLYOUT:
this.navigation.focusWorkspace(workspace);
return true;
case Constants.STATE.TOOLBOX:
this.navigation.focusWorkspace(workspace);
return true;
default:
return false;
}
},
keyCodes: [KeyCodes.ESC],
allowCollision: true,
},

/** Announce the current location of the cursor. */
announceLocation: {
name: Constants.SHORTCUT_NAMES.ANNOUNCE,
Expand Down Expand Up @@ -694,6 +681,7 @@ export class NavigationController {
this.deleteAction.install();
this.insertAction.install();
this.workspaceMovement.install();
this.exitAction.install();
this.disconnectAction.install();

this.clipboard.install();
Expand All @@ -718,6 +706,7 @@ export class NavigationController {
this.disconnectAction.uninstall();
this.clipboard.uninstall();
this.workspaceMovement.uninstall();
this.exitAction.uninstall();
this.shortcutDialog.uninstall();

this.removeShortcutHandlers();
Expand Down