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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
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.tsindex 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.tsindex 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.tsindex 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.shortexport 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 IShortcutServiconstructor(@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 IShortcutServiregisterShortcut(shortcut: IShortcutItem): IDisposable {// first map shortcut to a number, so it could be converted and fetched quicklyconst 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.tsxindex 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.lessindex 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.tsindex 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.tsindex 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<IMoveSelectionCommandPexport 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<IMoveSelectionCommandParexport 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<IMoveSelectionCommandPexport 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<IMoveSelectionCommandexport 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<IMoveSelectionEnterAndTexport 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<IMoveSelectionEnterexport 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<IMoveSelectionEnterAnexport 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 endexport 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<IMoveSelectionCommaexport 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<IMoveSelectionCommandexport 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<IMoveSelectionCommaexport 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<IMoveSelectionCommexport 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<IExpandSelectionCommexport 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<IExpandSelectionCommanexport 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<IExpandSelectionCommexport 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<IExpandSelectionComexport 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<IExpandSelectionCexport 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<IExpandSelectionComexport 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<IExpandSelectionCexport 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<IExpandSelectionexport 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.tsindex 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.cexport 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.tsindex 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',};