Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat: shortcut service support escape (#568)

Wenzhao Hue2d1f08bc751
diff --git a/packages/base-ui/src/commands/operations/toggle-shortcut-panel.operation.ts b/packages/base-ui/src/commands/operations/toggle-shortcut-panel.operation.ts
index e4578475cd..115e87a28f 100644
--- a/packages/base-ui/src/commands/operations/toggle-shortcut-panel.operation.ts
+++ b/packages/base-ui/src/commands/operations/toggle-shortcut-panel.operation.ts
@@ -10,16 +10,16 @@ export const ToggleShortcutPanelOperation: IOperation = {
id: 'base-ui.operation.toggle-shortcut-panel',
type: CommandType.OPERATION,
handler: (accessor: IAccessor) => {
- const shortcutPanelStatusService = accessor.get(ShortcutPanelService);
+ const shortcutPanelService = accessor.get(ShortcutPanelService);
const sidebarService = accessor.get(ISidebarService);
- const isOpen = shortcutPanelStatusService.isOpen;
+ const isOpen = shortcutPanelService.isOpen;
if (isOpen) {
- shortcutPanelStatusService.close();
+ shortcutPanelService.close();
sidebarService.close();
} else {
- shortcutPanelStatusService.open();
+ shortcutPanelService.open();
sidebarService.open({
header: { title: 'shortcut-panel.title' },
children: { label: ShortcutPanelComponentName },
diff --git a/packages/base-ui/src/services/shortcut/shortcut.service.ts b/packages/base-ui/src/services/shortcut/shortcut.service.ts
index 735a6117e6..c9073a3165 100644
--- a/packages/base-ui/src/services/shortcut/shortcut.service.ts
+++ b/packages/base-ui/src/services/shortcut/shortcut.service.ts
@@ -34,6 +34,10 @@ export interface IShortcutItem<P extends object = object> {
export interface IShortcutService {
shortcutChanged$: Observable<void>;
+ forceEscape(): IDisposable;
+ // registerCaptureSelector(selector: string): IDisposable;
+ // registerEscapeSelector(selector: string): IDisposable;
+
registerShortcut(shortcut: IShortcutItem): IDisposable;
getShortcutDisplay(shortcut: IShortcutItem): string;
getShortcutDisplayOfCommand(id: string): string | null;
@@ -49,6 +53,8 @@ export class DesktopShortcutService extends Disposable implements IShortcutServi
private readonly _shortcutChanged$ = new Subject<void>();
readonly shortcutChanged$ = this._shortcutChanged$.asObservable();
+ private _forceEscaped = false;
+
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IPlatformService private readonly _platformService: IPlatformService,
@@ -59,7 +65,7 @@ export class DesktopShortcutService extends Disposable implements IShortcutServi
// Register native keydown event handler to trigger shortcuts.
this.disposeWithMe(
fromDocumentEvent('keydown', (e: KeyboardEvent) => {
- this._resolveMouseEvent(e);
+ this._resolveKeyboardEvent(e);
})
);
}
@@ -133,7 +139,20 @@ export class DesktopShortcutService extends Disposable implements IShortcutServi
this._shortcutChanged$.next();
}
- private _resolveMouseEvent(e: KeyboardEvent): void {
+ forceEscape(): IDisposable {
+ this._forceEscaped = true;
+ return toDisposable(() => (this._forceEscaped = false));
+ }
+
+ private _resolveKeyboardEvent(e: KeyboardEvent): void {
+ // Should get the container element of the Univer instance and see if
+ // the event target is a descendant of the container element.
+ // Also we should check through escape list and force catching list.
+ // if the target is not focused on the univer instance we should ingore the keyboard event
+ if (this._forceEscaped) {
+ return;
+ }
+
const shouldPreventDefault = this._dispatch(e);
if (shouldPreventDefault) {
e.preventDefault();
diff --git a/packages/network/src/services/web-socket/web-socket.service.ts b/packages/network/src/services/web-socket/web-socket.service.ts
index 5c8ee812e2..d41d79f282 100644
--- a/packages/network/src/services/web-socket/web-socket.service.ts
+++ b/packages/network/src/services/web-socket/web-socket.service.ts
@@ -21,8 +21,6 @@ export interface ISocket {
close(code?: number, reason?: string): void;
- // TODO: Data type can support plain object as a type.
-
/**
* Send a message to the remote server.
*/
diff --git a/packages/ui-plugin-sheets/src/controllers/menu/merge.menu.ts b/packages/ui-plugin-sheets/src/controllers/menu/merge.menu.ts
index 93f96bc0aa..748236cd0c 100644
--- a/packages/ui-plugin-sheets/src/controllers/menu/merge.menu.ts
+++ b/packages/ui-plugin-sheets/src/controllers/menu/merge.menu.ts
@@ -6,13 +6,9 @@ import {
RemoveWorksheetMergeCommand,
} from '@univerjs/base-sheets';
import { IMenuButtonItem, IMenuSelectorItem, MenuGroup, MenuItemType, MenuPosition } from '@univerjs/base-ui';
-import { ICommandService } from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
export function CellMergeMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {
- // const permissionService = accessor.get(IPermissionService);
- const commandService = accessor.get(ICommandService);
-
return {
id: AddWorksheetMergeCommand.id,
icon: 'MergeAllSingle',
@@ -24,9 +20,6 @@ export function CellMergeMenuItemFactory(accessor: IAccessor): IMenuSelectorItem
};
}
export function CellMergeAllMenuItemFactory(accessor: IAccessor): IMenuButtonItem<string> {
- // const permissionService = accessor.get(IPermissionService);
- const commandService = accessor.get(ICommandService);
-
return {
id: AddWorksheetMergeAllCommand.id,
type: MenuItemType.BUTTON,
@@ -36,9 +29,6 @@ export function CellMergeAllMenuItemFactory(accessor: IAccessor): IMenuButtonIte
};
}
export function CellMergeVerticalMenuItemFactory(accessor: IAccessor): IMenuButtonItem<string> {
- // const permissionService = accessor.get(IPermissionService);
- const commandService = accessor.get(ICommandService);
-
return {
id: AddWorksheetMergeVerticalCommand.id,
type: MenuItemType.BUTTON,
@@ -48,9 +38,6 @@ export function CellMergeVerticalMenuItemFactory(accessor: IAccessor): IMenuButt
};
}
export function CellMergeHorizontalMenuItemFactory(accessor: IAccessor): IMenuButtonItem<string> {
- // const permissionService = accessor.get(IPermissionService);
- const commandService = accessor.get(ICommandService);
-
return {
id: AddWorksheetMergeHorizontalCommand.id,
type: MenuItemType.BUTTON,
@@ -60,9 +47,6 @@ export function CellMergeHorizontalMenuItemFactory(accessor: IAccessor): IMenuBu
};
}
export function CellMergeCancelMenuItemFactory(accessor: IAccessor): IMenuButtonItem<string> {
- // const permissionService = accessor.get(IPermissionService);
- const commandService = accessor.get(ICommandService);
-
return {
id: RemoveWorksheetMergeCommand.id,
type: MenuItemType.BUTTON,