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: wire ToolbarItem tooltip to live shortcut bindings (#7079)

白熱b34a3fb0f836
diff --git a/packages/sheets-ui/src/controllers/mobile/mobile-sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/mobile/mobile-sheet-ui.controller.ts
index 5843b7eef4..9936acb42e 100644
--- a/packages/sheets-ui/src/controllers/mobile/mobile-sheet-ui.controller.ts
+++ b/packages/sheets-ui/src/controllers/mobile/mobile-sheet-ui.controller.ts
@@ -32,7 +32,6 @@ import {
connectInjector,
ILayoutService,
IMenuManagerService,
- IShortcutService,
IUIPartsService,
} from '@univerjs/ui';
import { DeleteRangeMoveLeftConfirmCommand } from '../../commands/commands/delete-range-move-left-confirm.command';
@@ -104,59 +103,6 @@ import { DEFINED_NAME_CONTAINER } from '../../views/defined-name/component-name'
import { DefinedNameContainer } from '../../views/defined-name/DefinedNameContainer';
import { MobileSheetBar } from '../../views/mobile/sheet-bar/MobileSheetBar';
import { RenderSheetContent } from '../../views/sheet-container/SheetContainer';
-import {
- EditorBreakLineShortcut,
- EditorCursorCtrlEnterShortcut,
- EditorCursorEnterShortcut,
- EditorCursorEscShortcut,
- EditorCursorTabShortcut,
- EditorDeleteLeftShortcut,
- EditorDeleteLeftShortcutInActive,
- EditorDeleteRightShortcut,
- generateArrowSelectionShortCutItem,
- ShiftEditorDeleteLeftShortcut,
- StartEditWithF2Shortcut,
-} from '../shortcuts/editor.shortcut';
-import { SetColHiddenShortcutItem, SetRowHiddenShortcutItem } from '../shortcuts/operation.shortcut';
-import {
- ExpandSelectionDownShortcutItem,
- ExpandSelectionEndDownShortcutItem,
- ExpandSelectionEndLeftShortcutItem,
- ExpandSelectionEndRightShortcutItem,
- ExpandSelectionEndUpShortcutItem,
- ExpandSelectionLeftShortcutItem,
- ExpandSelectionRightShortcutItem,
- ExpandSelectionUpShortcutItem,
- // MoveBackSelectionShortcutItem,
- MoveSelectionDownShortcutItem,
- MoveSelectionEndDownShortcutItem,
- MoveSelectionEndLeftShortcutItem,
- MoveSelectionEndRightShortcutItem,
- MoveSelectionEndUpShortcutItem,
- MoveSelectionEnterShortcutItem,
- MoveSelectionEnterUpShortcutItem,
- MoveSelectionLeftShortcutItem,
- MoveSelectionRightShortcutItem,
- MoveSelectionTabLeftShortcutItem,
- MoveSelectionTabShortcutItem,
- MoveSelectionUpShortcutItem,
- SelectAllShortcutItem,
-} from '../shortcuts/selection.shortcut';
-import {
- SetBoldShortcutItem,
- SetItalicShortcutItem,
- SetStrikeThroughShortcutItem,
- SetUnderlineShortcutItem,
-} from '../shortcuts/style.shortcut';
-import { ClearSelectionValueShortcutItem, ClearSelectionValueShortcutItemMac, ShiftClearSelectionValueShortcutItem, ShiftDeleteSelectionValueShortcutItem } from '../shortcuts/value.shortcut';
-import {
- PreventDefaultResetZoomShortcutItem,
- PreventDefaultZoomInShortcutItem,
- PreventDefaultZoomOutShortcutItem,
- ResetZoomShortcutItem,
- ZoomInShortcutItem,
- ZoomOutShortcutItem,
-} from '../shortcuts/view.shortcut';
export class SheetUIMobileController extends Disposable {
constructor(
@@ -164,7 +110,6 @@ export class SheetUIMobileController extends Disposable {
@Inject(ComponentManager) protected readonly _componentManager: ComponentManager,
@ILayoutService protected readonly _layoutService: ILayoutService,
@ICommandService protected readonly _commandService: ICommandService,
- @IShortcutService protected readonly _shortcutService: IShortcutService,
@IMenuManagerService protected readonly _menuManagerService: IMenuManagerService,
@IUIPartsService protected readonly _uiPartsService: IUIPartsService,
@IConfigService protected readonly _configService: IConfigService
@@ -178,7 +123,6 @@ export class SheetUIMobileController extends Disposable {
this._initComponents();
this._initCommands();
this._initMenus();
- this._initShortcuts();
this._initWorkbenchParts();
this._initFocusHandler();
}
@@ -277,71 +221,6 @@ export class SheetUIMobileController extends Disposable {
this._menuManagerService.mergeMenu(menuSchema);
}
- private _initShortcuts(): void {
- [
- // selection shortcuts
- MoveSelectionDownShortcutItem,
- MoveSelectionUpShortcutItem,
- MoveSelectionLeftShortcutItem,
- MoveSelectionRightShortcutItem,
- MoveSelectionTabShortcutItem,
- MoveSelectionTabLeftShortcutItem,
- MoveSelectionEnterShortcutItem,
- MoveSelectionEnterUpShortcutItem,
- // MoveBackSelectionShortcutItem,
- MoveSelectionEndDownShortcutItem,
- MoveSelectionEndUpShortcutItem,
- MoveSelectionEndLeftShortcutItem,
- MoveSelectionEndRightShortcutItem,
- ExpandSelectionDownShortcutItem,
- ExpandSelectionUpShortcutItem,
- ExpandSelectionLeftShortcutItem,
- ExpandSelectionRightShortcutItem,
- ExpandSelectionEndDownShortcutItem,
- ExpandSelectionEndUpShortcutItem,
- ExpandSelectionEndLeftShortcutItem,
- ExpandSelectionEndRightShortcutItem,
- SelectAllShortcutItem,
-
- // view shortcuts
- ZoomInShortcutItem,
- ZoomOutShortcutItem,
- ResetZoomShortcutItem,
- PreventDefaultResetZoomShortcutItem,
- PreventDefaultZoomInShortcutItem,
- PreventDefaultZoomOutShortcutItem,
-
- // toggle cell style shortcuts
- SetBoldShortcutItem,
- SetItalicShortcutItem,
- SetUnderlineShortcutItem,
- SetStrikeThroughShortcutItem,
-
- // cell content editing shortcuts
- ClearSelectionValueShortcutItem,
- ClearSelectionValueShortcutItemMac,
- ShiftClearSelectionValueShortcutItem,
- ShiftDeleteSelectionValueShortcutItem,
- ...generateArrowSelectionShortCutItem(),
- EditorCursorEnterShortcut,
- StartEditWithF2Shortcut,
- EditorCursorTabShortcut,
- EditorBreakLineShortcut,
- EditorDeleteLeftShortcut,
- EditorDeleteRightShortcut,
- EditorDeleteLeftShortcutInActive,
- EditorCursorEscShortcut,
- EditorCursorCtrlEnterShortcut,
- ShiftEditorDeleteLeftShortcut,
-
- // operation shortcuts
- SetRowHiddenShortcutItem,
- SetColHiddenShortcutItem,
- ].forEach((item) => {
- this.disposeWithMe(this._shortcutService.registerShortcut(item));
- });
- }
-
private _initWorkbenchParts(): void {
const uiController = this._uiPartsService;
const injector = this._injector;
diff --git a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
index f04c0265d9..396b671d1b 100644
--- a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
+++ b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
@@ -140,6 +140,8 @@ import {
} from './shortcuts/selection.shortcut';
import {
SetBoldShortcutItem,
+ SetFontDecreaseShortcutItem,
+ SetFontIncreaseShortcutItem,
SetItalicShortcutItem,
SetStrikeThroughShortcutItem,
SetUnderlineShortcutItem,
@@ -315,6 +317,8 @@ export class SheetUIController extends Disposable {
SetItalicShortcutItem,
SetUnderlineShortcutItem,
SetStrikeThroughShortcutItem,
+ SetFontIncreaseShortcutItem,
+ SetFontDecreaseShortcutItem,
// cell content editing shortcuts
ClearSelectionValueShortcutItem,
diff --git a/packages/sheets-ui/src/controllers/shortcuts/style.shortcut.ts b/packages/sheets-ui/src/controllers/shortcuts/style.shortcut.ts
index 87e236aaf3..310b4eb661 100644
--- a/packages/sheets-ui/src/controllers/shortcuts/style.shortcut.ts
+++ b/packages/sheets-ui/src/controllers/shortcuts/style.shortcut.ts
@@ -19,6 +19,8 @@ import { KeyCode, MetaKeys } from '@univerjs/ui';
import {
SetRangeBoldCommand,
+ SetRangeFontDecreaseCommand,
+ SetRangeFontIncreaseCommand,
SetRangeItalicCommand,
SetRangeStrickThroughCommand,
SetRangeUnderlineCommand,
@@ -60,3 +62,21 @@ export const SetStrikeThroughShortcutItem: IShortcutItem = {
preconditions: (contextService) => whenSheetFocusedInlineFormat(contextService),
binding: KeyCode.X | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
};
+
+export const SetFontIncreaseShortcutItem: IShortcutItem = {
+ id: SetRangeFontIncreaseCommand.id,
+ description: 'sheets-ui.toolbar.fontSizeIncrease',
+ group: '4_sheet-edit',
+ groupTitle: 'sheets-ui.shortcut.sheet-edit',
+ preconditions: (contextService) => whenSheetFocusedInlineFormat(contextService),
+ binding: KeyCode.PERIOD | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
+};
+
+export const SetFontDecreaseShortcutItem: IShortcutItem = {
+ id: SetRangeFontDecreaseCommand.id,
+ description: 'sheets-ui.toolbar.fontSizeDecrease',
+ group: '4_sheet-edit',
+ groupTitle: 'sheets-ui.shortcut.sheet-edit',
+ preconditions: (contextService) => whenSheetFocusedInlineFormat(contextService),
+ binding: KeyCode.COMMA | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
+};
diff --git a/packages/ui/src/views/components/ribbon/ToolbarItem.tsx b/packages/ui/src/views/components/ribbon/ToolbarItem.tsx
index adf6742f1b..e47edbf76c 100644
--- a/packages/ui/src/views/components/ribbon/ToolbarItem.tsx
+++ b/packages/ui/src/views/components/ribbon/ToolbarItem.tsx
@@ -26,7 +26,7 @@ import { CustomLabel } from '../../../components/custom-label/CustomLabel';
import { ILayoutService } from '../../../services/layout/layout.service';
import { MenuItemType } from '../../../services/menu/menu';
import { useDependency, useObservable } from '../../../utils/di';
-import { useToolbarItemStatus } from './hook';
+import { useToolbarItemStatus, useToolbarShortcutDisplay } from './hook';
import { ToolbarButton } from './ToolbarButton';
import { DropdownMenuWrapper, TooltipWrapper } from './TooltipButtonWrapper';
@@ -191,7 +191,11 @@ export const ToolbarItem = forwardRef<ITooltipWrapperRef, IDisplayMenuItem<IMenu
const { tooltip, shortcut, icon, title, label, id, commandId, type, slot, params } = props;
- const tooltipTitle = localeService.t(tooltip ?? '') + (shortcut ? ` (${shortcut})` : '');
+ const shortcutDisplay = useToolbarShortcutDisplay({ id, commandId, shortcut });
+ let tooltipTitle = tooltip ? localeService.t(tooltip) : '';
+ if (shortcutDisplay) {
+ tooltipTitle += ` (${shortcutDisplay})`;
+ }
const { selections } = props as IDisplayMenuItem<IMenuSelectorItem>;
const selections$ = useMemo(() => {
diff --git a/packages/ui/src/views/components/ribbon/hook.ts b/packages/ui/src/views/components/ribbon/hook.ts
index 042d094401..af5c21b048 100644
--- a/packages/ui/src/views/components/ribbon/hook.ts
+++ b/packages/ui/src/views/components/ribbon/hook.ts
@@ -16,8 +16,11 @@
import type { Observable, Subscription } from 'rxjs';
import type { IDisplayMenuItem, IMenuItem } from '../../../services/menu/menu';
-import { useEffect, useState } from 'react';
+import { useEffect, useMemo, useState } from 'react';
+import { map, of, startWith } from 'rxjs';
import { isMenuButtonSelectorItem } from '../../../services/menu/menu';
+import { IShortcutService } from '../../../services/shortcut/shortcut.service';
+import { useDependency, useObservable } from '../../../utils/di';
export interface IToolbarItemStatus {
disabled: boolean;
@@ -76,3 +79,24 @@ export function useToolbarItemStatus(menuItem: IDisplayMenuItem<IMenuItem>): ITo
return { disabled, value, activated, hidden, selectionsValue };
}
+
+export function useToolbarShortcutDisplay({
+ id,
+ commandId,
+ shortcut,
+}: Pick<IDisplayMenuItem<IMenuItem>, 'id' | 'commandId' | 'shortcut'>): string | null {
+ const shortcutService = useDependency(IShortcutService);
+ const shortcutCommandId = commandId ?? id;
+
+ const shortcut$ = useMemo(
+ () => shortcut
+ ? of(shortcut)
+ : shortcutService.shortcutChanged$.pipe(
+ map(() => shortcutService.getShortcutDisplayOfCommand(shortcutCommandId)),
+ startWith(shortcutService.getShortcutDisplayOfCommand(shortcutCommandId))
+ ),
+ [shortcut, shortcutService, shortcutCommandId]
+ );
+
+ return useObservable(shortcut$, null, true) as string | null;
+}