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.

fix(ui): fix ribbon visibility subscription and menu hidden logic (#6963)

白熱788a8fb2831e
diff --git a/packages/ui/src/common/__tests__/menu-hidden-observable.spec.ts b/packages/ui/src/common/__tests__/menu-hidden-observable.spec.ts
index fda49db7da..654fb57554 100644
--- a/packages/ui/src/common/__tests__/menu-hidden-observable.spec.ts
+++ b/packages/ui/src/common/__tests__/menu-hidden-observable.spec.ts
@@ -55,6 +55,15 @@ function createMenuAccessor(options?: {
const flavor = docFlavorByUnitId[unitId];
return flavor == null ? null : createDocModel(flavor);
}),
+ getCurrentUnitOfType: vi.fn((type: UniverInstanceType) => {
+ if (type !== TARGET_TYPE) {
+ return null;
+ }
+ if (focusedUnitId == null) {
+ return null;
+ }
+ return { getUnitId: () => focusedUnitId };
+ }),
getCurrentUniverDocInstance: vi.fn(() => {
if (currentDocFlavor == null) {
return null;
diff --git a/packages/ui/src/common/menu-hidden-observable.ts b/packages/ui/src/common/menu-hidden-observable.ts
index eca006d426..382fe172da 100644
--- a/packages/ui/src/common/menu-hidden-observable.ts
+++ b/packages/ui/src/common/menu-hidden-observable.ts
@@ -46,12 +46,13 @@ export function getMenuHiddenObservable(
const focusedUniverInstance = univerInstanceService.getFocusedUnit();
if (focusedUniverInstance == null) {
- return subscriber.next(true);
+ const currentUnit = univerInstanceService.getCurrentUnitOfType(targetUniverType);
+ subscriber.next(currentUnit == null);
+ } else {
+ const univerType = univerInstanceService.getUnitType(focusedUniverInstance.getUnitId());
+ subscriber.next(univerType !== targetUniverType);
}
- const univerType = univerInstanceService.getUnitType(focusedUniverInstance.getUnitId());
- subscriber.next(univerType !== targetUniverType);
-
return () => subscription.unsubscribe();
});
}
@@ -75,12 +76,12 @@ export function getHeaderFooterMenuHiddenObservable(
const docDataModel = univerInstanceService.getCurrentUniverDocInstance();
if (docDataModel == null) {
- return subscriber.next(true);
+ subscriber.next(true);
+ } else {
+ const documentFlavor = docDataModel?.getSnapshot().documentStyle.documentFlavor;
+ subscriber.next(documentFlavor !== DocumentFlavor.TRADITIONAL);
}
- const documentFlavor = docDataModel?.getSnapshot().documentStyle.documentFlavor;
- subscriber.next(documentFlavor !== DocumentFlavor.TRADITIONAL);
-
return () => subscription.unsubscribe();
});
}
diff --git a/packages/ui/src/services/ribbon/ribbon.service.ts b/packages/ui/src/services/ribbon/ribbon.service.ts
index 629dd60d3e..4176008e13 100644
--- a/packages/ui/src/services/ribbon/ribbon.service.ts
+++ b/packages/ui/src/services/ribbon/ribbon.service.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { Observable } from 'rxjs';
+import type { Observable, Subscription } from 'rxjs';
import type { IMenuSchema } from '../menu/menu-manager.service';
import { createIdentifier, Disposable, IUniverInstanceService } from '@univerjs/core';
import { BehaviorSubject, combineLatest } from 'rxjs';
@@ -54,6 +54,7 @@ export class DesktopRibbonService extends Disposable implements IRibbonService {
private readonly _visibleContextualTabs = new Set<string>();
private readonly _contextualTabs = new Set<string>();
private _lastNonContextualActivatedTab: string = RibbonPosition.START;
+ private _hiddenSubscription: Subscription | null = null;
constructor(
@IMenuManagerService private readonly _menuManagerService: IMenuManagerService,
@@ -144,8 +145,9 @@ export class DesktopRibbonService extends Disposable implements IRibbonService {
return;
}
- // Only get the current value once, not continuously subscribe
- combineLatest(hiddenObservableMap)
+ this._hiddenSubscription?.unsubscribe();
+
+ this._hiddenSubscription = combineLatest(hiddenObservableMap)
.pipe(startWith(new Array(hiddenObservableMap.length).fill(false)))
.subscribe((hiddenMap) => {
const newRibbon: IMenuSchema[] = [];
@@ -189,8 +191,7 @@ export class DesktopRibbonService extends Disposable implements IRibbonService {
}
this._setRibbon(newRibbon);
- })
- .unsubscribe();
+ });
}
private _filterContextualTabs(ribbon: IMenuSchema[]): IMenuSchema[] {
@@ -217,6 +218,12 @@ export class DesktopRibbonService extends Disposable implements IRibbonService {
if (fallbackTab) {
this._activatedTab$.next(fallbackTab.key);
}
+ } else if (!activeGroup && ribbon.some((group) => group.key === RibbonPosition.START)) {
+ const fallbackTab = ribbon.find((group) => group.key === RibbonPosition.START)!;
+ this._activatedTab$.next(fallbackTab.key);
+ if (!fallbackTab.contextual) {
+ this._lastNonContextualActivatedTab = fallbackTab.key;
+ }
} else if (activeGroup && !activeGroup.contextual) {
this._lastNonContextualActivatedTab = activeGroup.key;
}
@@ -227,4 +234,10 @@ export class DesktopRibbonService extends Disposable implements IRibbonService {
private _isContextualTab(tab: string): boolean {
return this._contextualTabs.has(tab) || this._ribbon$.getValue().some((group) => group.key === tab && group.contextual);
}
+
+ override dispose(): void {
+ this._hiddenSubscription?.unsubscribe();
+ this._hiddenSubscription = null;
+ super.dispose();
+ }
}
diff --git a/packages/ui/src/views/components/ribbon/Ribbon.tsx b/packages/ui/src/views/components/ribbon/Ribbon.tsx
index 059b4e726b..8cf5e59689 100644
--- a/packages/ui/src/views/components/ribbon/Ribbon.tsx
+++ b/packages/ui/src/views/components/ribbon/Ribbon.tsx
@@ -203,7 +203,7 @@ export function Ribbon(props: IRibbonProps) {
'univer-h-9': ribbonType === 'classic' || (headerMenuComponents && headerMenuComponents.size > 0),
})}
>
- {ribbonType === 'classic' && ribbon.length > 1 && (
+ {ribbonType === 'classic' && ribbon.length >= 1 && (
<ClassicMenu
ribbon={ribbon}
activatedTab={activatedTab}
@@ -231,12 +231,11 @@ export function Ribbon(props: IRibbonProps) {
univer-box-border univer-grid univer-h-10 univer-grid-flow-col univer-items-center univer-px-3
univer-text-sm
`, {
- 'univer-grid-cols-[1fr] univer-justify-center': ribbonType === 'classic',
+ 'univer-grid-cols-[1fr] univer-justify-center': ribbonType === 'classic' || ribbon.length === 1,
'univer-grid-cols-[auto,1fr]': ribbon.length > 1 && ribbonType !== 'classic',
- 'univer-grid-cols-none': ribbon.length === 1,
}, borderBottomClassName)}
>
- {ribbonType === 'collapsed' && ribbon.length > 1 && (
+ {ribbonType === 'collapsed' && ribbon.length >= 1 && (
<DefaultMenu
ribbon={ribbon}
activatedTab={activatedTab}