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: add more shortcuts (#536)

Wenzhao Hue1c21b2e46a8
diff --git a/packages/base-ui/src/services/menu/menu.service.ts b/packages/base-ui/src/services/menu/menu.service.ts
index 3b0faa1c45..137e1dba4a 100644
--- a/packages/base-ui/src/services/menu/menu.service.ts
+++ b/packages/base-ui/src/services/menu/menu.service.ts
@@ -118,7 +118,7 @@ export class DesktopMenuService extends Disposable implements IMenuService {
}
private getDisplayMenuItems(menuItem: IMenuItem): IDisplayMenuItem<IMenuItem> {
- const shortcut = this._shortcutService.getShortcutDisplay(menuItem.id);
+ const shortcut = this._shortcutService.getShortcutDisplayOfCommand(menuItem.id);
if (!shortcut) {
return menuItem;
}
diff --git a/packages/base-ui/src/services/shortcut/keycode.ts b/packages/base-ui/src/services/shortcut/keycode.ts
index 2cd7cc1404..a363f8ed76 100644
--- a/packages/base-ui/src/services/shortcut/keycode.ts
+++ b/packages/base-ui/src/services/shortcut/keycode.ts
@@ -50,6 +50,9 @@ export const KeyCodeToChar: { [key: number]: string } = {
[KeyCode.X]: 'X',
[KeyCode.Y]: 'Y',
[KeyCode.Z]: 'Z',
+
+ [KeyCode.MINUS]: '-',
+ [KeyCode.EQUAL]: '=',
};
/** KeyCode that maps to browser standard keycode. */
diff --git a/packages/base-ui/src/services/shortcut/shortcut.service.ts b/packages/base-ui/src/services/shortcut/shortcut.service.ts
index 5051c6f83f..735a6117e6 100644
--- a/packages/base-ui/src/services/shortcut/shortcut.service.ts
+++ b/packages/base-ui/src/services/shortcut/shortcut.service.ts
@@ -3,7 +3,6 @@ import { createIdentifier, IDisposable } from '@wendellhu/redi';
import { Observable, Subject } from 'rxjs';
import { fromDocumentEvent } from '../../common/lifecycle';
-import { IFocusService } from '../focus/focus.service';
import { IPlatformService } from '../platform/platform.service';
import { KeyCodeToChar, MetaKeys } from './keycode';
@@ -36,7 +35,8 @@ export interface IShortcutService {
shortcutChanged$: Observable<void>;
registerShortcut(shortcut: IShortcutItem): IDisposable;
- getShortcutDisplay(id: string): string | null;
+ getShortcutDisplay(shortcut: IShortcutItem): string;
+ getShortcutDisplayOfCommand(id: string): string | null;
getAllShortcuts(): IShortcutItem[];
}
@@ -44,8 +44,7 @@ export const IShortcutService = createIdentifier<IShortcutService>('univer.short
export class DesktopShortcutService extends Disposable implements IShortcutService {
private readonly _shortCutMapping = new Map<number, Set<IShortcutItem>>();
-
- private readonly _idToShortcut = new Map<string, number>();
+ private readonly _commandIDMapping = new Map<string, Set<IShortcutItem>>();
private readonly _shortcutChanged$ = new Subject<void>();
readonly shortcutChanged$ = this._shortcutChanged$.asObservable();
@@ -53,8 +52,7 @@ export class DesktopShortcutService extends Disposable implements IShortcutServi
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IPlatformService private readonly _platformService: IPlatformService,
- @IContextService private readonly _contextService: IContextService,
- @IFocusService private readonly _focusService: IFocusService
+ @IContextService private readonly _contextService: IContextService
) {
super();
@@ -75,30 +73,49 @@ export class DesktopShortcutService extends Disposable implements IShortcutServi
registerShortcut(shortcut: IShortcutItem): IDisposable {
// first map shortcut to a number, so it could be converted and fetched quickly
const binding = this._getBindingFromItem(shortcut);
- const existing = this._shortCutMapping.get(binding);
-
- if (existing) {
- existing.add(shortcut);
+ const bindingSet = this._shortCutMapping.get(binding);
+ if (bindingSet) {
+ bindingSet.add(shortcut);
} else {
this._shortCutMapping.set(binding, new Set([shortcut]));
}
- this._idToShortcut.set(shortcut.id, binding);
- this._emitShortcutChanged();
+ const commandID = shortcut.id;
+ const commandIDSet = this._commandIDMapping.get(commandID);
+ if (commandIDSet) {
+ commandIDSet.add(shortcut);
+ } else {
+ this._commandIDMapping.set(commandID, new Set([shortcut]));
+ }
+ this._emitShortcutChanged();
return toDisposable(() => {
+ this._shortCutMapping.get(binding)?.delete(shortcut);
+ if (this._shortCutMapping.get(binding)?.size === 0) {
+ this._shortCutMapping.delete(binding);
+ }
+
+ this._commandIDMapping.get(commandID)?.delete(shortcut);
+ if (this._commandIDMapping.get(commandID)?.size === 0) {
+ this._commandIDMapping.delete(commandID);
+ }
+
this._emitShortcutChanged();
- this._idToShortcut.delete(shortcut.id);
- this._shortCutMapping.delete(binding);
});
}
- getShortcutDisplay(id: string): string | null {
- const binding = this._idToShortcut.get(id);
- if (!binding) {
+ getShortcutDisplayOfCommand(id: string): string | null {
+ const set = this._commandIDMapping.get(id);
+ // if (!set || set.size > 1) {
+ if (!set) {
return null;
}
+ return this.getShortcutDisplay(set.values().next().value);
+ }
+
+ getShortcutDisplay(shortcut: IShortcutItem): string {
+ const binding = this._getBindingFromItem(shortcut);
const ctrlKey = binding & MetaKeys.CTRL_COMMAND;
const shiftKey = binding & MetaKeys.SHIFT;
const altKey = binding & MetaKeys.ALT;
diff --git a/packages/base-ui/src/views/components/shortcut-panel/ShortcutPanel.tsx b/packages/base-ui/src/views/components/shortcut-panel/ShortcutPanel.tsx
index 831199947f..fe197739c8 100644
--- a/packages/base-ui/src/views/components/shortcut-panel/ShortcutPanel.tsx
+++ b/packages/base-ui/src/views/components/shortcut-panel/ShortcutPanel.tsx
@@ -33,7 +33,7 @@ export function ShortcutPanel() {
const group = shortcut.group!;
const shortcutItem: IRenderShortcutItem = {
title: localeService.t(shortcut.description ?? shortcut.id),
- shortcut: shortcutService.getShortcutDisplay(shortcut.id)!,
+ shortcut: shortcutService.getShortcutDisplay(shortcut),
};
if (!/\d+_[a-zA-Z0-9]/.test(group)) {
diff --git a/packages/base-ui/src/views/components/shortcut-panel/index.module.less b/packages/base-ui/src/views/components/shortcut-panel/index.module.less
index 604c5e64b8..5e0f02275a 100644
--- a/packages/base-ui/src/views/components/shortcut-panel/index.module.less
+++ b/packages/base-ui/src/views/components/shortcut-panel/index.module.less
@@ -2,6 +2,7 @@
&-group-title {
height: 40px;
font-size: 13px;
+ font-weight: 700;
line-height: 40px;
}
@@ -23,6 +24,7 @@
text-overflow: ellipsis;
text-wrap: nowrap;
+
}
&-shortcut {
diff --git a/packages/ui-plugin-sheets/src/controllers/sheet-ui.controller.ts b/packages/ui-plugin-sheets/src/controllers/sheet-ui.controller.ts
index 381220e72f..1fabd27e24 100644
--- a/packages/ui-plugin-sheets/src/controllers/sheet-ui.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/sheet-ui.controller.ts
@@ -153,7 +153,7 @@ import {
ExpandSelectionLeftShortcutItem,
ExpandSelectionRightShortcutItem,
ExpandSelectionUpShortcutItem,
- MoveBackSelectionShortcutItem,
+ // MoveBackSelectionShortcutItem,
MoveSelectionDownShortcutItem,
MoveSelectionEndDownShortcutItem,
MoveSelectionEndLeftShortcutItem,
@@ -326,7 +326,7 @@ export class SheetUIController extends Disposable {
MoveSelectionTabLeftShortcutItem,
MoveSelectionEnterShortcutItem,
MoveSelectionEnterUpShortcutItem,
- MoveBackSelectionShortcutItem,
+ // MoveBackSelectionShortcutItem,
MoveSelectionEndDownShortcutItem,
MoveSelectionEndUpShortcutItem,
MoveSelectionEndLeftShortcutItem,
diff --git a/packages/ui-plugin-sheets/src/controllers/shortcuts/selection.shortcut.ts b/packages/ui-plugin-sheets/src/controllers/shortcuts/selection.shortcut.ts
index 0fd2703cb5..a7bf1aca78 100644
--- a/packages/ui-plugin-sheets/src/controllers/shortcuts/selection.shortcut.ts
+++ b/packages/ui-plugin-sheets/src/controllers/shortcuts/selection.shortcut.ts
@@ -16,6 +16,8 @@ import { whenEditorNotActivated } from './utils';
export const MoveSelectionDownShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
+ description: 'shortcut.sheet.select-below-cell',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_DOWN,
priority: 100,
preconditions: whenEditorNotActivated,
@@ -26,6 +28,8 @@ export const MoveSelectionDownShortcutItem: IShortcutItem<IMoveSelectionCommandP
export const MoveSelectionUpShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
+ description: 'shortcut.sheet.select-up-cell',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_UP,
priority: 100,
preconditions: whenEditorNotActivated,
@@ -36,6 +40,8 @@ export const MoveSelectionUpShortcutItem: IShortcutItem<IMoveSelectionCommandPar
export const MoveSelectionLeftShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
+ description: 'shortcut.sheet.select-left-cell',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_LEFT,
priority: 100,
preconditions: whenEditorNotActivated,
@@ -46,6 +52,8 @@ export const MoveSelectionLeftShortcutItem: IShortcutItem<IMoveSelectionCommandP
export const MoveSelectionRightShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
+ description: 'shortcut.sheet.select-right-cell',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_RIGHT,
priority: 100,
preconditions: whenEditorNotActivated,
@@ -56,6 +64,8 @@ export const MoveSelectionRightShortcutItem: IShortcutItem<IMoveSelectionCommand
export const MoveSelectionTabShortcutItem: IShortcutItem<IMoveSelectionEnterAndTabCommandParams> = {
id: MoveSelectionEnterAndTabCommand.id,
+ description: 'shortcut.sheet.select-next-cell',
+ group: '3_sheet-view',
binding: KeyCode.TAB,
priority: 100,
preconditions: whenEditorNotActivated,
@@ -67,6 +77,8 @@ export const MoveSelectionTabShortcutItem: IShortcutItem<IMoveSelectionEnterAndT
export const MoveSelectionTabLeftShortcutItem: IShortcutItem<IMoveSelectionEnterAndTabCommandParams> = {
id: MoveSelectionEnterAndTabCommand.id,
+ description: 'shortcut.sheet.select-previous-cell',
+ group: '3_sheet-view',
binding: KeyCode.TAB | MetaKeys.SHIFT,
priority: 100,
preconditions: whenEditorNotActivated,
@@ -78,6 +90,8 @@ export const MoveSelectionTabLeftShortcutItem: IShortcutItem<IMoveSelectionEnter
export const MoveSelectionEnterShortcutItem: IShortcutItem<IMoveSelectionEnterAndTabCommandParams> = {
id: MoveSelectionEnterAndTabCommand.id,
+ description: 'shortcut.sheet.select-below-cell',
+ group: '3_sheet-view',
binding: KeyCode.ENTER,
priority: 100,
preconditions: whenEditorNotActivated,
@@ -89,6 +103,8 @@ export const MoveSelectionEnterShortcutItem: IShortcutItem<IMoveSelectionEnterAn
export const MoveSelectionEnterUpShortcutItem: IShortcutItem<IMoveSelectionEnterAndTabCommandParams> = {
id: MoveSelectionEnterAndTabCommand.id,
+ description: 'shortcut.sheet.select-up-cell',
+ group: '3_sheet-view',
binding: KeyCode.ENTER | MetaKeys.SHIFT,
priority: 100,
preconditions: whenEditorNotActivated,
@@ -98,20 +114,24 @@ export const MoveSelectionEnterUpShortcutItem: IShortcutItem<IMoveSelectionEnter
},
};
-export const MoveBackSelectionShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
- id: MoveSelectionCommand.id,
- binding: KeyCode.TAB | MetaKeys.SHIFT,
- priority: 100,
- preconditions: whenEditorNotActivated,
- staticParameters: {
- direction: Direction.LEFT,
- },
-};
+// export const MoveBackSelectionShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
+// id: MoveSelectionCommand.id,
+// description: 'shortcut.sheet.select-previous-cell',
+// group: '3_sheet-view',
+// binding: KeyCode.TAB | MetaKeys.SHIFT,
+// priority: 100,
+// preconditions: whenEditorNotActivated,
+// staticParameters: {
+// direction: Direction.LEFT,
+// },
+// };
// move selection to continuous end
export const MoveSelectionEndDownShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
+ description: 'shortcut.sheet.select-below-value-cell',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_DOWN | MetaKeys.CTRL_COMMAND,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -122,6 +142,8 @@ export const MoveSelectionEndDownShortcutItem: IShortcutItem<IMoveSelectionComma
export const MoveSelectionEndUpShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
+ description: 'shortcut.sheet.select-up-value-cell',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_UP | MetaKeys.CTRL_COMMAND,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -132,6 +154,8 @@ export const MoveSelectionEndUpShortcutItem: IShortcutItem<IMoveSelectionCommand
export const MoveSelectionEndLeftShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
+ description: 'shortcut.sheet.select-left-value-cell',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_LEFT | MetaKeys.CTRL_COMMAND,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -142,6 +166,8 @@ export const MoveSelectionEndLeftShortcutItem: IShortcutItem<IMoveSelectionComma
export const MoveSelectionEndRightShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
+ description: 'shortcut.sheet.select-right-value-cell',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_RIGHT | MetaKeys.CTRL_COMMAND,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -152,6 +178,8 @@ export const MoveSelectionEndRightShortcutItem: IShortcutItem<IMoveSelectionComm
export const ExpandSelectionDownShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
id: ExpandSelectionCommand.id,
+ description: 'shortcut.sheet.expand-selection-down',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_DOWN | MetaKeys.SHIFT,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -161,6 +189,8 @@ export const ExpandSelectionDownShortcutItem: IShortcutItem<IExpandSelectionComm
export const ExpandSelectionUpShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
id: ExpandSelectionCommand.id,
+ description: 'shortcut.sheet.expand-selection-up',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_UP | MetaKeys.SHIFT,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -170,6 +200,8 @@ export const ExpandSelectionUpShortcutItem: IShortcutItem<IExpandSelectionComman
export const ExpandSelectionLeftShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
id: ExpandSelectionCommand.id,
+ description: 'shortcut.sheet.expand-selection-left',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_LEFT | MetaKeys.SHIFT,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -179,6 +211,8 @@ export const ExpandSelectionLeftShortcutItem: IShortcutItem<IExpandSelectionComm
export const ExpandSelectionRightShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
id: ExpandSelectionCommand.id,
+ description: 'shortcut.sheet.expand-selection-right',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_RIGHT | MetaKeys.SHIFT,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -190,6 +224,8 @@ export const ExpandSelectionRightShortcutItem: IShortcutItem<IExpandSelectionCom
export const ExpandSelectionEndDownShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
id: ExpandSelectionCommand.id,
+ description: 'shortcut.sheet.expand-selection-to-below-gap',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_DOWN | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -200,6 +236,8 @@ export const ExpandSelectionEndDownShortcutItem: IShortcutItem<IExpandSelectionC
export const ExpandSelectionEndUpShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
id: ExpandSelectionCommand.id,
+ description: 'shortcut.sheet.expand-selection-to-above-gap',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_UP | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -210,6 +248,8 @@ export const ExpandSelectionEndUpShortcutItem: IShortcutItem<IExpandSelectionCom
export const ExpandSelectionEndLeftShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
id: ExpandSelectionCommand.id,
+ description: 'shortcut.sheet.expand-selection-to-left-gap',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_LEFT | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -220,6 +260,8 @@ export const ExpandSelectionEndLeftShortcutItem: IShortcutItem<IExpandSelectionC
export const ExpandSelectionEndRightShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
id: ExpandSelectionCommand.id,
+ description: 'shortcut.sheet.expand-selection-to-right-gap',
+ group: '3_sheet-view',
binding: KeyCode.ARROW_RIGHT | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
preconditions: whenEditorNotActivated,
staticParameters: {
@@ -230,6 +272,8 @@ export const ExpandSelectionEndRightShortcutItem: IShortcutItem<IExpandSelection
export const SelectAllShortcutItem: IShortcutItem<ISelectAllCommandParams> = {
id: SelectAllCommand.id,
+ description: 'shortcut.sheet.select-all',
+ group: '3_sheet-view',
binding: KeyCode.A | MetaKeys.CTRL_COMMAND,
preconditions: whenEditorNotActivated,
staticParameters: {
diff --git a/packages/ui-plugin-sheets/src/controllers/shortcuts/view.shortcut.ts b/packages/ui-plugin-sheets/src/controllers/shortcuts/view.shortcut.ts
index eae7c92db5..6fe9430347 100644
--- a/packages/ui-plugin-sheets/src/controllers/shortcuts/view.shortcut.ts
+++ b/packages/ui-plugin-sheets/src/controllers/shortcuts/view.shortcut.ts
@@ -4,7 +4,9 @@ import { ChangeZoomRatioCommand } from '../../commands/commands/set-zoom-ratio.c
export const ZoomInShortcutItem: IShortcutItem = {
id: ChangeZoomRatioCommand.id,
+ description: 'shortcut.sheet.zoom-in',
binding: KeyCode.EQUAL | MetaKeys.CTRL_COMMAND,
+ group: '3_sheet-view',
staticParameters: {
delta: 0.2,
},
@@ -12,7 +14,9 @@ export const ZoomInShortcutItem: IShortcutItem = {
export const ZoomOutShortcutItem: IShortcutItem = {
id: ChangeZoomRatioCommand.id,
+ description: 'shortcut.sheet.zoom-out',
binding: KeyCode.MINUS | MetaKeys.CTRL_COMMAND,
+ group: '3_sheet-view',
staticParameters: {
delta: -0.2,
},
@@ -20,7 +24,9 @@ export const ZoomOutShortcutItem: IShortcutItem = {
export const ResetZoomShortcutItem: IShortcutItem = {
id: ChangeZoomRatioCommand.id,
+ description: 'shortcut.sheet.reset-zoom',
binding: KeyCode.Digit0 | MetaKeys.CTRL_COMMAND,
+ group: '3_sheet-view',
staticParameters: {
reset: true,
},
diff --git a/packages/ui-plugin-sheets/src/locale/en-US.ts b/packages/ui-plugin-sheets/src/locale/en-US.ts
index 6530b18b99..7b3991aa6e 100644
--- a/packages/ui-plugin-sheets/src/locale/en-US.ts
+++ b/packages/ui-plugin-sheets/src/locale/en-US.ts
@@ -557,4 +557,33 @@ export default {
placeholder: 'Select range or input value',
tooltip: 'Select range',
},
+
+ shortcut: {
+ sheet: {
+ 'zoom-in': 'Zoom in',
+ 'zoom-out': 'Zoom out',
+ 'reset-zoom': 'Reset zoom level',
+ 'select-below-cell': 'Select the cell below',
+ 'select-up-cell': 'Select the cell above',
+ 'select-left-cell': 'Select the left cell',
+ 'select-right-cell': 'Select the right cell',
+ 'select-next-cell': 'Select the next cell',
+ 'select-previous-cell': 'Select the previous cell',
+ 'select-up-value-cell': 'Select the cell above that has value',
+ 'select-below-value-cell': 'Select the cell below that has value',
+ 'select-left-value-cell': 'Select the cell left that has value',
+ 'select-right-value-cell': 'Select the cell right that has value',
+ 'expand-selection-down': 'Expand selection down',
+ 'expand-selection-up': 'Expand selection up',
+ 'expand-selection-left': 'Expand selection left',
+ 'expand-selection-right': 'Expand selection right',
+ 'expand-selection-to-left-gap': 'Expand selection to the left gap',
+ 'expand-selection-to-below-gap': 'Expand selection to the below gap',
+ 'expand-selection-to-right-gap': 'Expand selection to the right gap',
+ 'expand-selection-to-up-gap': 'Expand selection to the up gap',
+ 'select-all': 'Select all',
+ },
+ },
+
+ 'sheet-view': 'Sheet View',
};