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: support shortcut to confirm and cancel zen editor (#1232)

Ran Luoa10dbdc4b66e
diff --git a/packages/sheets-zen-editor/src/controllers/shortcuts/zen-editor.shortcut.ts b/packages/sheets-zen-editor/src/controllers/shortcuts/zen-editor.shortcut.ts
new file mode 100644
index 0000000000..97da18be92
--- /dev/null
+++ b/packages/sheets-zen-editor/src/controllers/shortcuts/zen-editor.shortcut.ts
@@ -0,0 +1,50 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IContextService } from '@univerjs/core';
+import { EDITOR_ACTIVATED, FOCUSING_DOC, FOCUSING_UNIVER_EDITOR } from '@univerjs/core';
+import { type IShortcutItem, KeyCode, MetaKeys } from '@univerjs/ui';
+
+import { CancelZenEditCommand, ConfirmZenEditCommand } from '../../commands/commands/zen-editor.command';
+
+export const ZenEditorConfirmShortcut: IShortcutItem = {
+ id: ConfirmZenEditCommand.id,
+ description: 'shortcut.sheets-zen-editor.confirm',
+ group: '4_sheet-edit',
+ preconditions: (contextService) => whenZenEditorActivated(contextService),
+ binding: KeyCode.ENTER | MetaKeys.ALT,
+};
+
+export const ZenEditorCancelShortcut: IShortcutItem = {
+ id: CancelZenEditCommand.id,
+ description: 'shortcut.sheets-zen-editor.cancel',
+ group: '4_sheet-edit',
+ preconditions: (contextService) => whenZenEditorActivated(contextService),
+ binding: KeyCode.ESC,
+};
+
+/**
+ * Requires the currently focused unit to be Doc and the zen editor is activated.
+ * @param contextService
+ * @returns
+ */
+export function whenZenEditorActivated(contextService: IContextService) {
+ return (
+ contextService.getContextValue(FOCUSING_DOC) &&
+ contextService.getContextValue(FOCUSING_UNIVER_EDITOR) &&
+ contextService.getContextValue(EDITOR_ACTIVATED)
+ );
+}
diff --git a/packages/sheets-zen-editor/src/controllers/zen-editor-ui.controller.ts b/packages/sheets-zen-editor/src/controllers/zen-editor-ui.controller.ts
index f61c30f4ff..11c00573cf 100644
--- a/packages/sheets-zen-editor/src/controllers/zen-editor-ui.controller.ts
+++ b/packages/sheets-zen-editor/src/controllers/zen-editor-ui.controller.ts
@@ -16,13 +16,14 @@
import { Disposable, ICommandService, LifecycleStages, OnLifecycle } from '@univerjs/core';
import type { IMenuItemFactory } from '@univerjs/ui';
-import { IMenuService, IZenZoneService } from '@univerjs/ui';
+import { IMenuService, IShortcutService, IZenZoneService } from '@univerjs/ui';
import { Inject, Injector } from '@wendellhu/redi';
import { CancelZenEditCommand, ConfirmZenEditCommand } from '../commands/commands/zen-editor.command';
import { OpenZenEditorOperation } from '../commands/operations/zen-editor.operation';
import { ZenEditorMenuItemFactory } from '../views/menu';
import { ZEN_EDITOR_COMPONENT, ZenEditor } from '../views/zen-editor';
+import { ZenEditorCancelShortcut, ZenEditorConfirmShortcut } from './shortcuts/zen-editor.shortcut';
@OnLifecycle(LifecycleStages.Ready, ZenEditorUIController)
export class ZenEditorUIController extends Disposable {
@@ -30,7 +31,8 @@ export class ZenEditorUIController extends Disposable {
@Inject(Injector) private readonly _injector: Injector,
@IZenZoneService private readonly _zenZoneService: IZenZoneService,
@ICommandService private readonly _commandService: ICommandService,
- @IMenuService private readonly _menuService: IMenuService
+ @IMenuService private readonly _menuService: IMenuService,
+ @IShortcutService private readonly _shortcutService: IShortcutService
) {
super();
@@ -41,6 +43,7 @@ export class ZenEditorUIController extends Disposable {
this._initCustomComponents();
this._initCommands();
this._initMenus();
+ this._initShortcuts();
}
private _initCustomComponents(): void {
@@ -63,4 +66,10 @@ export class ZenEditorUIController extends Disposable {
this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)));
});
}
+
+ private _initShortcuts(): void {
+ [ZenEditorConfirmShortcut, ZenEditorCancelShortcut].forEach((item) => {
+ this.disposeWithMe(this._shortcutService.registerShortcut(item));
+ });
+ }
}