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(ui): add contextual ribbon tabs support (#6902)

白熱0a0a81ac42ff
diff --git a/packages/ui/src/services/__tests__/core-services.spec.ts b/packages/ui/src/services/__tests__/core-services.spec.ts
index 38c0679d73..29938b7bd9 100644
--- a/packages/ui/src/services/__tests__/core-services.spec.ts
+++ b/packages/ui/src/services/__tests__/core-services.spec.ts
@@ -19,7 +19,7 @@ import { BehaviorSubject, Subject } from 'rxjs';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { MenuItemType } from '../menu/menu';
import { MenuManagerService } from '../menu/menu-manager.service';
-import { RibbonPosition, RibbonStartGroup } from '../menu/types';
+import { MenuManagerPosition, RibbonPosition, RibbonStartGroup } from '../menu/types';
import { UIPartsService } from '../parts/parts.service';
import { DesktopRibbonService } from '../ribbon/ribbon.service';
@@ -69,9 +69,26 @@ describe('MenuManagerService', () => {
},
});
+ service.appendRootMenu({
+ [MenuManagerPosition.RIBBON]: {
+ contextualTab: {
+ order: 99,
+ contextual: true,
+ contextualGroup: {
+ order: 0,
+ customItem: {
+ order: 1,
+ menuItemFactory: customFactory,
+ },
+ },
+ },
+ },
+ });
+
const historyMenu = service.getMenuByPositionKey(RibbonStartGroup.HISTORY);
const flatHistory = service.getFlatMenuByPositionKey(RibbonStartGroup.HISTORY);
const customRoot = service.getMenuByPositionKey('customRoot');
+ const ribbonMenu = service.getMenuByPositionKey(MenuManagerPosition.RIBBON);
const mergedCustomItem = flatHistory.find((item) => item.item?.id === 'customItem');
expect(changed).toHaveBeenCalled();
@@ -80,6 +97,7 @@ describe('MenuManagerService', () => {
expect(flatHistory.length).toBeGreaterThanOrEqual(historyMenu.length);
expect(mergedCustomItem?.item?.tooltip).toBe('tooltip-from-config');
expect(customRoot.length).toBe(1);
+ expect(ribbonMenu.find((item) => item.key === 'contextualTab')?.contextual).toBe(true);
const complete = vi.fn();
service.menuChanged$.subscribe({ complete });
@@ -167,6 +185,118 @@ describe('DesktopRibbonService', () => {
service.dispose();
});
+
+ it('should show, activate, and restore contextual ribbon tabs', () => {
+ const ribbonData: any[] = [
+ {
+ key: RibbonPosition.START,
+ order: 0,
+ children: [
+ {
+ key: RibbonStartGroup.HISTORY,
+ order: 0,
+ children: [
+ {
+ key: 'home-child',
+ order: 0,
+ item: {
+ id: 'home-child',
+ type: MenuItemType.BUTTON,
+ },
+ },
+ ],
+ },
+ ],
+ },
+ {
+ key: RibbonPosition.INSERT,
+ order: 1,
+ children: [
+ {
+ key: 'insert-group',
+ order: 0,
+ children: [
+ {
+ key: 'insert-child',
+ order: 0,
+ item: {
+ id: 'insert-child',
+ type: MenuItemType.BUTTON,
+ },
+ },
+ ],
+ },
+ ],
+ },
+ {
+ key: 'contextual.shape',
+ order: 100,
+ contextual: true,
+ children: [
+ {
+ key: 'contextual-group',
+ order: 0,
+ children: [
+ {
+ key: 'contextual-child',
+ order: 0,
+ item: {
+ id: 'contextual-child',
+ type: MenuItemType.BUTTON,
+ },
+ },
+ ],
+ },
+ ],
+ },
+ ];
+
+ const menuChanged$ = new Subject<void>();
+ const focused$ = new Subject<any>();
+ const menuManagerService = {
+ menuChanged$,
+ getMenuByPositionKey: vi.fn(() => ribbonData),
+ };
+ const univerInstanceService = {
+ focused$,
+ };
+
+ const service = new DesktopRibbonService(menuManagerService as any, univerInstanceService as any);
+
+ let activatedTab = '';
+ let currentRibbon: any[] = [];
+
+ service.activatedTab$.subscribe((v) => (activatedTab = v));
+ service.ribbon$.subscribe((v) => (currentRibbon = v));
+
+ expect(currentRibbon.map((item) => item.key)).toEqual([RibbonPosition.START, RibbonPosition.INSERT]);
+
+ service.setActivatedTab(RibbonPosition.INSERT);
+ service.showContextualTab('contextual.shape', { activate: true });
+
+ expect(currentRibbon.map((item) => item.key)).toEqual([
+ RibbonPosition.START,
+ RibbonPosition.INSERT,
+ 'contextual.shape',
+ ]);
+ expect(activatedTab).toBe('contextual.shape');
+
+ service.hideContextualTab('contextual.shape');
+
+ expect(currentRibbon.map((item) => item.key)).toEqual([RibbonPosition.START, RibbonPosition.INSERT]);
+ expect(activatedTab).toBe(RibbonPosition.INSERT);
+
+ service.showContextualTab('contextual.shape');
+ expect(activatedTab).toBe(RibbonPosition.INSERT);
+
+ service.setActivatedTab('contextual.shape');
+ service.hideAllContextualTabs();
+
+ expect(currentRibbon.map((item) => item.key)).toEqual([RibbonPosition.START, RibbonPosition.INSERT]);
+ expect(activatedTab).toBe(RibbonPosition.INSERT);
+
+ service.dispose();
+ });
});
describe('UIPartsService', () => {
diff --git a/packages/ui/src/services/menu/menu-manager.service.ts b/packages/ui/src/services/menu/menu-manager.service.ts
index dc3c5f0047..cb9a8ccdb9 100644
--- a/packages/ui/src/services/menu/menu-manager.service.ts
+++ b/packages/ui/src/services/menu/menu-manager.service.ts
@@ -30,6 +30,7 @@ export interface IMenuSchema {
key: string;
order: number;
title?: string;
+ contextual?: boolean;
item?: IMenuItem;
children?: IMenuSchema[];
quickLayout?: ContextMenuQuickLayout;
@@ -52,6 +53,7 @@ export type MenuSchemaType = {
order?: number;
menuItemFactory?: (accessor: IAccessor) => IMenuItem;
title?: string;
+ contextual?: boolean;
quickLayout?: ContextMenuQuickLayout;
} | {
[key: string]: MenuSchemaType;
@@ -283,6 +285,7 @@ export class MenuManagerService extends Disposable implements IMenuManagerServic
key,
order: value.order,
title: value.title,
+ contextual: value.contextual,
quickLayout: value.quickLayout,
};
diff --git a/packages/ui/src/services/ribbon/ribbon.service.ts b/packages/ui/src/services/ribbon/ribbon.service.ts
index 267d7eb760..629dd60d3e 100644
--- a/packages/ui/src/services/ribbon/ribbon.service.ts
+++ b/packages/ui/src/services/ribbon/ribbon.service.ts
@@ -31,6 +31,9 @@ export interface IRibbonService {
fakeToolbarVisible$: Observable<boolean>;
setActivatedTab(tab: string): void;
+ showContextualTab(tab: string, options?: { activate?: boolean }): void;
+ hideContextualTab(tab: string): void;
+ hideAllContextualTabs(): void;
setCollapsedIds(ids: string[]): void;
setFakeToolbarVisible(visible: boolean): void;
}
@@ -48,6 +51,10 @@ export class DesktopRibbonService extends Disposable implements IRibbonService {
private readonly _fakeToolbarVisible$ = new BehaviorSubject<boolean>(false);
readonly fakeToolbarVisible$ = this._fakeToolbarVisible$.asObservable();
+ private readonly _visibleContextualTabs = new Set<string>();
+ private readonly _contextualTabs = new Set<string>();
+ private _lastNonContextualActivatedTab: string = RibbonPosition.START;
+
constructor(
@IMenuManagerService private readonly _menuManagerService: IMenuManagerService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService
@@ -57,9 +64,39 @@ export class DesktopRibbonService extends Disposable implements IRibbonService {
}
setActivatedTab(tab: string): void {
+ if (!this._isContextualTab(tab)) {
+ this._lastNonContextualActivatedTab = tab;
+ }
+
this._activatedTab$.next(tab);
}
+ showContextualTab(tab: string, options?: { activate?: boolean }): void {
+ this._visibleContextualTabs.add(tab);
+ this._updateRibbon();
+
+ if (options?.activate) {
+ this.setActivatedTab(tab);
+ }
+ }
+
+ hideContextualTab(tab: string): void {
+ if (!this._visibleContextualTabs.delete(tab)) {
+ return;
+ }
+
+ this._updateRibbon();
+ }
+
+ hideAllContextualTabs(): void {
+ if (this._visibleContextualTabs.size === 0) {
+ return;
+ }
+
+ this._visibleContextualTabs.clear();
+ this._updateRibbon();
+ }
+
setCollapsedIds(ids: string[]): void {
this._collapsedIds$.next(ids);
}
@@ -80,7 +117,9 @@ export class DesktopRibbonService extends Disposable implements IRibbonService {
}
private _updateRibbon() {
- const ribbon = this._menuManagerService.getMenuByPositionKey(MenuManagerPosition.RIBBON);
+ const ribbon = this._filterContextualTabs(
+ this._menuManagerService.getMenuByPositionKey(MenuManagerPosition.RIBBON)
+ );
// Collect all hidden$ Observables and their corresponding paths
const hiddenObservableMap: Observable<boolean>[] = [];
@@ -101,7 +140,7 @@ export class DesktopRibbonService extends Disposable implements IRibbonService {
}
if (hiddenObservableMap.length === 0) {
- this._ribbon$.next(ribbon);
+ this._setRibbon(ribbon);
return;
}
@@ -149,8 +188,43 @@ export class DesktopRibbonService extends Disposable implements IRibbonService {
}
}
- this._ribbon$.next(newRibbon);
+ this._setRibbon(newRibbon);
})
.unsubscribe();
}
+
+ private _filterContextualTabs(ribbon: IMenuSchema[]): IMenuSchema[] {
+ this._contextualTabs.clear();
+
+ ribbon.forEach((group) => {
+ if (group.contextual) {
+ this._contextualTabs.add(group.key);
+ }
+ });
+
+ return ribbon.filter((group) => !group.contextual || this._visibleContextualTabs.has(group.key));
+ }
+
+ private _setRibbon(ribbon: IMenuSchema[]): void {
+ const activatedTab = this._activatedTab$.getValue();
+ const activeGroup = ribbon.find((group) => group.key === activatedTab);
+
+ if (!activeGroup && this._contextualTabs.has(activatedTab)) {
+ const fallbackTab = ribbon.find((group) => group.key === this._lastNonContextualActivatedTab && !group.contextual)
+ ?? ribbon.find((group) => group.key === RibbonPosition.START)
+ ?? ribbon[0];
+
+ if (fallbackTab) {
+ this._activatedTab$.next(fallbackTab.key);
+ }
+ } else if (activeGroup && !activeGroup.contextual) {
+ this._lastNonContextualActivatedTab = activeGroup.key;
+ }
+
+ this._ribbon$.next(ribbon);
+ }
+
+ private _isContextualTab(tab: string): boolean {
+ return this._contextualTabs.has(tab) || this._ribbon$.getValue().some((group) => group.key === tab && group.contextual);
+ }
}
diff --git a/packages/ui/src/views/components/ribbon/ribbon-menu/DefaultMenu.tsx b/packages/ui/src/views/components/ribbon/ribbon-menu/DefaultMenu.tsx
index 6ccda47a1b..16d3df409a 100644
--- a/packages/ui/src/views/components/ribbon/ribbon-menu/DefaultMenu.tsx
+++ b/packages/ui/src/views/components/ribbon/ribbon-menu/DefaultMenu.tsx
@@ -57,7 +57,7 @@ export function DefaultMenu({
overlay={(
<div className="univer-grid univer-gap-1 univer-px-2 univer-py-1">
{ribbon.map((group) => {
- const Icon = iconMap[group.key as RibbonPosition];
+ const Icon = iconMap[group.key as RibbonPosition] ?? MoreFunctionIcon;
return (
<a