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): scope surfaces to root workbench unit (#7580)

白熱ee000ad14a95
diff --git a/packages/docs-ui/src/views/DocSideMenu.tsx b/packages/docs-ui/src/views/DocSideMenu.tsx
index 7001be2b42..770b0fddbd 100644
--- a/packages/docs-ui/src/views/DocSideMenu.tsx
+++ b/packages/docs-ui/src/views/DocSideMenu.tsx
@@ -27,7 +27,7 @@ import {
UniverInstanceType,
} from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
-import { useConfigValue, useDependency, useEvent, useObservable } from '@univerjs/ui';
+import { IWorkbenchService, useConfigValue, useDependency, useEvent, useObservable } from '@univerjs/ui';
import { useEffect, useMemo, useState } from 'react';
import { debounceTime, map, startWith, throttleTime } from 'rxjs';
import { VIEWPORT_KEY } from '../basics/docs-view-key';
@@ -85,8 +85,10 @@ const TITLE_ID = '__title';
export function DocSideMenu() {
const config = useConfigValue<IUniverDocsUIConfig>(DOCS_UI_PLUGIN_CONFIG_KEY);
+ const workbenchService = useDependency(IWorkbenchService);
+ const rootUnitType = useObservable(workbenchService.rootUnitType$, null, true);
- if (config?.toc) {
+ if (rootUnitType === UniverInstanceType.UNIVER_DOC && config?.toc) {
return <DocSideMenuContent />;
}
@@ -95,7 +97,12 @@ export function DocSideMenu() {
function DocSideMenuContent() {
const instanceService = useDependency(IUniverInstanceService);
- const currentDoc = useObservable(useMemo(() => instanceService.getCurrentTypeOfUnit$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC), []));
+ const currentDoc = useObservable(
+ () => instanceService.getCurrentTypeOfUnit$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC),
+ undefined,
+ false,
+ [instanceService]
+ );
const renderManagerService = useDependency(IRenderManagerService);
const documentData = useObservable(
currentDoc
@@ -134,7 +141,7 @@ function DocSideMenuContent() {
);
const { left, canvasHeight, scaleY } = layout;
- const paragraphs = documentData?.body?.paragraphs ?? [];
+ const paragraphs = useMemo(() => documentData?.body?.paragraphs ?? [], [documentData?.body?.paragraphs]);
const paragraphMap = useMemo(() => {
const map = new Map<number, IParagraph>();
paragraphs.forEach((p) => {
@@ -143,8 +150,6 @@ function DocSideMenuContent() {
return map;
}, [paragraphs]);
const mode = left < 180 ? 'float' : 'side-bar';
- let minLevel = Infinity;
-
const paragraphMenus = paragraphs
?.filter((p) =>
p.paragraphStyle?.namedStyleType !== undefined &&
@@ -153,7 +158,6 @@ function DocSideMenuContent() {
)
.map((p) => {
const level = transformNamedStyleTypeToLevel(p.paragraphStyle!.namedStyleType!);
- minLevel = Math.min(minLevel, level);
const bound = paragraphBounds?.get(p.startIndex);
if (!bound) return null;
const { paragraphStart, paragraphEnd } = bound;
@@ -203,7 +207,7 @@ function DocSideMenuContent() {
return () => {
sub.unsubscribe();
};
- }, [renderer]);
+ }, [handleScroll, renderer]);
const handleClick = useEvent((menu: ISideMenuItem) => {
const viewport = renderer?.scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
diff --git a/packages/docs-ui/src/views/doc-footer/DocFooter.tsx b/packages/docs-ui/src/views/doc-footer/DocFooter.tsx
index ddc8c6efaf..0fc204ac28 100644
--- a/packages/docs-ui/src/views/doc-footer/DocFooter.tsx
+++ b/packages/docs-ui/src/views/doc-footer/DocFooter.tsx
@@ -14,10 +14,9 @@
* limitations under the License.
*/
-import type { Workbook } from '@univerjs/core';
import type { IUniverDocsUIConfig } from '../../config/config';
-import { IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
-import { useConfigValue, useDependency, useObservable } from '@univerjs/ui';
+import { UniverInstanceType } from '@univerjs/core';
+import { IWorkbenchService, useConfigValue, useDependency, useObservable } from '@univerjs/ui';
import { DOCS_UI_PLUGIN_CONFIG_KEY } from '../../config/config';
import { CountBar } from '../count-bar';
import { DocStatistics } from '../doc-statistics/DocStatistics';
@@ -39,13 +38,12 @@ function DocFooterContent() {
}
export function DocFooter() {
- const univerInstanceService = useDependency(IUniverInstanceService);
- const workbook = useObservable(() => univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET), undefined, undefined, []);
- const slide = useObservable(() => univerInstanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_SLIDE), undefined, undefined, []);
+ const workbenchService = useDependency(IWorkbenchService);
+ const rootUnitType = useObservable(workbenchService.rootUnitType$, null, true);
- if (workbook || slide) {
+ if (rootUnitType !== UniverInstanceType.UNIVER_DOC) {
return null;
}
return <DocFooterContent />;
-};
+}
diff --git a/packages/sheets-ui/src/views/sheet-container/SheetContainer.tsx b/packages/sheets-ui/src/views/sheet-container/SheetContainer.tsx
index 377bb7a410..a35d777bdd 100644
--- a/packages/sheets-ui/src/views/sheet-container/SheetContainer.tsx
+++ b/packages/sheets-ui/src/views/sheet-container/SheetContainer.tsx
@@ -18,12 +18,11 @@ import type { Workbook, Worksheet } from '@univerjs/core';
import type { IUniverSheetsUIConfig } from '../../config/config';
import {
Injector,
- isInternalEditorID,
IUniverInstanceService,
UniverInstanceType,
} from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
-import { ComponentManager, ContextMenuPosition, IMenuManagerService, ToolbarItem, useConfigValue, useDependency, useObservable } from '@univerjs/ui';
+import { ComponentManager, ContextMenuPosition, IMenuManagerService, IWorkbenchService, ToolbarItem, useConfigValue, useDependency, useObservable } from '@univerjs/ui';
import { useEffect, useMemo } from 'react';
import { EMPTY, merge } from 'rxjs';
import { SHEETS_UI_PLUGIN_CONFIG_KEY } from '../../config/config';
@@ -46,11 +45,11 @@ export function RenderSheetFooter() {
const showFooter = config?.footer ?? true;
const workbook = useRootWorkbenchWorkbook();
const activeWorkbookEmbeddedRender = useActiveWorkbookIsEmbeddedRender(workbook);
- const focusedUnitType = useFocusedUnitType();
+ const rootUnitType = useWorkbenchRootUnitType();
const activeEmbedTab = useActiveSheetEmbedTabData(workbook);
if (!workbook || !showFooter) return null;
if (activeWorkbookEmbeddedRender) return null;
- if (!activeEmbedTab && focusedUnitType != null && focusedUnitType !== UniverInstanceType.UNIVER_SHEET) return null;
+ if (rootUnitType !== UniverInstanceType.UNIVER_SHEET) return null;
const footerMenus = menuManagerService.getMenuByPositionKey(ContextMenuPosition.FOOTER_MENU);
const {
@@ -100,23 +99,12 @@ export function RenderSheetHeader() {
const workbook = useRootWorkbenchWorkbook();
const hasWorkbook = !!workbook;
const activeWorkbookEmbeddedRender = useActiveWorkbookIsEmbeddedRender(workbook);
- const focusedUnitType = useFocusedUnitType();
+ const rootUnitType = useWorkbenchRootUnitType();
const activeEmbedTab = useActiveSheetEmbedTabData(workbook);
if (!hasWorkbook) return null;
if (activeWorkbookEmbeddedRender) return null;
if (activeEmbedTab) return null;
- if (focusedUnitType != null && focusedUnitType !== UniverInstanceType.UNIVER_SHEET) {
- return (
- <div
- aria-hidden
- className="
- univer-h-7 univer-border-b univer-border-gray-200 univer-bg-gray-0
- dark:!univer-border-gray-700 dark:!univer-bg-gray-900
- "
- data-u-comp="formula-bar-placeholder"
- />
- );
- }
+ if (rootUnitType !== UniverInstanceType.UNIVER_SHEET) return null;
if (config?.formulaBar !== false) {
return <FormulaBar />;
}
@@ -134,9 +122,8 @@ export function RenderSheetContent() {
const activeEmbedTab = useActiveSheetEmbedTabData(workbook);
const injector = useDependency(Injector);
const activeWorkbookEmbeddedRender = useActiveWorkbookIsEmbeddedRender(workbook);
- const focusedUnitType = useFocusedUnitType();
- // An active embed tab remains the root Sheet surface; other product focus hides the Sheet workbench.
- const rootWorkbenchOwnsSheet = activeEmbedTab != null || focusedUnitType == null || focusedUnitType === UniverInstanceType.UNIVER_SHEET;
+ const rootUnitType = useWorkbenchRootUnitType();
+ const rootWorkbenchOwnsSheet = rootUnitType === UniverInstanceType.UNIVER_SHEET;
// We use string keys to avoid a hard dependency on sheets-shape-ui.
const ShapeTextEditorContainer = componentManager.get('SheetShapeTextEditorContainer') ?? componentManager.get('ShapeTextEditorContainer');
@@ -274,19 +261,9 @@ function useRootWorkbenchWorkbook(): Workbook | null {
}, [activeWorkbook, instanceService, runtimeFocusCoordinator, runtimeSessionLifecycle]);
}
-function useFocusedUnitType(): UniverInstanceType | null {
- const univerInstanceService = useDependency(IUniverInstanceService);
- const focusedUnitId = useObservable(() => univerInstanceService.focused$, null, false, [univerInstanceService]);
- return useMemo(() => {
- if (!focusedUnitId) return null;
-
- if (isInternalEditorID(focusedUnitId) && univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_SHEET)) {
- return UniverInstanceType.UNIVER_SHEET;
- }
-
- const focusedUnit = univerInstanceService.getUnit(focusedUnitId);
- return focusedUnit?.type ?? null;
- }, [focusedUnitId, univerInstanceService]);
+function useWorkbenchRootUnitType(): UniverInstanceType | null {
+ const workbenchService = useDependency(IWorkbenchService);
+ return useObservable(workbenchService.rootUnitType$, null, true);
}
function useActiveSheetEmbedTabData(workbook: Workbook | null): { worksheet: Worksheet } | undefined {
diff --git a/packages/ui/src/controllers/ui/__tests__/ui-shared.controller.spec.ts b/packages/ui/src/controllers/ui/__tests__/ui-shared.controller.spec.ts
index 22f5639742..d261e31ed2 100644
--- a/packages/ui/src/controllers/ui/__tests__/ui-shared.controller.spec.ts
+++ b/packages/ui/src/controllers/ui/__tests__/ui-shared.controller.spec.ts
@@ -15,7 +15,17 @@
*/
import type { IDisposable } from '@univerjs/core';
-import { Injector, LifecycleStages, LifecycleUnreachableError } from '@univerjs/core';
+import {
+ ContextService,
+ DesktopLogService,
+ IContextService,
+ ILogService,
+ Injector,
+ IUniverInstanceService,
+ LifecycleStages,
+ LifecycleUnreachableError,
+ UniverInstanceService,
+} from '@univerjs/core';
import { Subject } from 'rxjs';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { IWorkbenchService, WorkbenchService } from '../../../services/workbench/workbench.service';
@@ -78,6 +88,9 @@ describe('SingleUnitUIController', () => {
});
const injector = new Injector([
+ [IContextService, { useClass: ContextService }],
+ [ILogService, { useClass: DesktopLogService }],
+ [IUniverInstanceService, { useClass: UniverInstanceService }],
[IWorkbenchService, { useClass: WorkbenchService }],
]);
const skeletonStates: boolean[] = [];
diff --git a/packages/ui/src/services/workbench/__tests__/workbench.service.spec.ts b/packages/ui/src/services/workbench/__tests__/workbench.service.spec.ts
index ec4590ef27..9ba1a017e2 100644
--- a/packages/ui/src/services/workbench/__tests__/workbench.service.spec.ts
+++ b/packages/ui/src/services/workbench/__tests__/workbench.service.spec.ts
@@ -14,19 +14,25 @@
* limitations under the License.
*/
-import { Injector } from '@univerjs/core';
-import { beforeEach, describe, expect, it } from 'vitest';
+import { IUniverInstanceService, Univer, UniverInstanceType } from '@univerjs/core';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { IWorkbenchService, WorkbenchService } from '../workbench.service';
describe('WorkbenchService', () => {
+ let univer: Univer;
+ let instanceService: IUniverInstanceService;
let service: IWorkbenchService;
beforeEach(() => {
- const injector = new Injector();
+ univer = new Univer();
+ const injector = univer.__getInjector();
injector.add([IWorkbenchService, { useClass: WorkbenchService }]);
+ instanceService = injector.get(IUniverInstanceService);
service = injector.get(IWorkbenchService);
});
+ afterEach(() => univer.dispose());
+
it('keeps the skeleton visible until every loading token is disposed', () => {
const states: boolean[] = [];
service.skeletonVisible$.subscribe((visible) => states.push(visible));
@@ -39,6 +45,36 @@ describe('WorkbenchService', () => {
second.dispose();
expect(states).toEqual([false, true, false]);
- expect((service as any)._collection._disposables).toHaveLength(0);
+ });
+
+ it('identifies root units without treating editors or embedded units as workbench roots', () => {
+ const states: Array<UniverInstanceType | null> = [];
+ service.rootUnitType$.subscribe((unitType) => states.push(unitType));
+
+ const rootDoc = instanceService.createUnit(UniverInstanceType.UNIVER_DOC, { id: 'root-doc' });
+ instanceService.createUnit(UniverInstanceType.UNIVER_DOC, { id: '__INTERNAL_EDITOR__doc' });
+ const backgroundSheet = instanceService.createUnit(
+ UniverInstanceType.UNIVER_SHEET,
+ { id: 'background-sheet' },
+ { makeCurrent: false }
+ );
+
+ instanceService.focusUnit(backgroundSheet.getUnitId());
+
+ const embeddedDoc = instanceService.createUnit(
+ UniverInstanceType.UNIVER_DOC,
+ { id: 'embedded-doc' },
+ { makeCurrent: false, skipAutoRender: true, embeddedRender: true }
+ );
+ instanceService.focusUnit(embeddedDoc.getUnitId());
+ instanceService.disposeUnit(backgroundSheet.getUnitId());
+
+ expect(rootDoc.getUnitId()).toBe('root-doc');
+ expect(states).toEqual([
+ null,
+ UniverInstanceType.UNIVER_DOC,
+ UniverInstanceType.UNIVER_SHEET,
+ null,
+ ]);
});
});
diff --git a/packages/ui/src/services/workbench/workbench.service.ts b/packages/ui/src/services/workbench/workbench.service.ts
index 5b4aa48de8..74daa51e3d 100644
--- a/packages/ui/src/services/workbench/workbench.service.ts
+++ b/packages/ui/src/services/workbench/workbench.service.ts
@@ -14,12 +14,13 @@
* limitations under the License.
*/
-import type { IDisposable } from '@univerjs/core';
+import type { ICreateUnitOptions, IDisposable, UniverInstanceType } from '@univerjs/core';
import type { Observable } from 'rxjs';
-import { createIdentifier, Disposable, toDisposable } from '@univerjs/core';
-import { BehaviorSubject } from 'rxjs';
+import { createIdentifier, Disposable, Inject, isInternalEditorID, IUniverInstanceService, toDisposable } from '@univerjs/core';
+import { BehaviorSubject, Subscription } from 'rxjs';
export interface IWorkbenchService {
+ readonly rootUnitType$: Observable<UniverInstanceType | null>;
readonly skeletonVisible$: Observable<boolean>;
acquireSkeleton(): IDisposable;
@@ -29,10 +30,52 @@ export const IWorkbenchService = createIdentifier<IWorkbenchService>('univer.ui.
export class WorkbenchService extends Disposable implements IWorkbenchService {
private readonly _tokens = new Set<symbol>();
+ private readonly _subscriptions = new Subscription();
+ private _rootUnitId: string | null = null;
+ private readonly _rootUnitType$ = new BehaviorSubject<UniverInstanceType | null>(null);
private readonly _skeletonVisible$ = new BehaviorSubject(false);
+ readonly rootUnitType$ = this._rootUnitType$.asObservable();
readonly skeletonVisible$ = this._skeletonVisible$.asObservable();
+ constructor(@Inject(IUniverInstanceService) univerInstanceService: IUniverInstanceService) {
+ super();
+
+ this._subscriptions.add(univerInstanceService.unitAdded$.subscribe(({ unit, options }) => {
+ const unitId = unit.getUnitId();
+ if (options?.makeCurrent === false || !isWorkbenchRootUnit(unitId, options)) {
+ return;
+ }
+
+ this._setRootUnit(unitId, unit.type);
+ }));
+
+ this._subscriptions.add(univerInstanceService.focused$.subscribe((unitId) => {
+ if (!unitId) {
+ return;
+ }
+
+ const unit = univerInstanceService.getUnit(unitId);
+ if (unit && isWorkbenchRootUnit(unitId, univerInstanceService.getUnitCreateOptions(unitId) ?? undefined)) {
+ this._setRootUnit(unitId, unit.type);
+ }
+ }));
+
+ this._subscriptions.add(univerInstanceService.unitDisposed$.subscribe((unit) => {
+ const unitId = unit.getUnitId();
+ if (this._rootUnitId === unitId) {
+ this._setRootUnit(null, null);
+ }
+ }));
+ }
+
+ private _setRootUnit(unitId: string | null, unitType: UniverInstanceType | null): void {
+ this._rootUnitId = unitId;
+ if (this._rootUnitType$.getValue() !== unitType) {
+ this._rootUnitType$.next(unitType);
+ }
+ }
+
acquireSkeleton(): IDisposable {
const token = Symbol('workbench-skeleton');
this._tokens.add(token);
@@ -51,8 +94,15 @@ export class WorkbenchService extends Disposable implements IWorkbenchService {
}
override dispose(): void {
- super.dispose();
+ this._subscriptions.unsubscribe();
this._tokens.clear();
+ this._rootUnitId = null;
+ this._rootUnitType$.complete();
this._skeletonVisible$.complete();
+ super.dispose();
}
}
+
+function isWorkbenchRootUnit(unitId: string, options?: ICreateUnitOptions): boolean {
+ return !isInternalEditorID(unitId) && !options?.skipAutoRender && !options?.embeddedRender;
+}
diff --git a/packages/ui/src/views/workbench/__tests__/Workbench.spec.tsx b/packages/ui/src/views/workbench/__tests__/Workbench.spec.tsx
index 1378cc1540..2f312f271a 100644
--- a/packages/ui/src/views/workbench/__tests__/Workbench.spec.tsx
+++ b/packages/ui/src/views/workbench/__tests__/Workbench.spec.tsx
@@ -22,12 +22,18 @@ import type { IDisposable } from '@univerjs/core';
import type { ComponentType } from 'react';
import { act, cleanup, render } from '@testing-library/react';
import {
+ ContextService,
+ DesktopLogService,
IConfigService,
+ IContextService,
+ ILogService,
Injector,
+ IUniverInstanceService,
LifecycleService,
LifecycleStages,
LocaleService,
ThemeService,
+ UniverInstanceService,
} from '@univerjs/core';
import { BehaviorSubject, of, Subject } from 'rxjs';
import { afterEach, describe, expect, it, vi } from 'vitest';
@@ -67,6 +73,9 @@ describe('DesktopWorkbenchContent lifecycle', () => {
}],
[ThemeSwitcherService, { useValue: { injectThemeToHead: () => {} } }],
[IConfigService, { useValue: { getConfig: () => ({ popupRootId: 'test-popup-root' }) } }],
+ [IContextService, { useClass: ContextService }],
+ [ILogService, { useClass: DesktopLogService }],
+ [IUniverInstanceService, { useClass: UniverInstanceService }],
[IUIPartsService, { useClass: UIPartsService }],
[IWorkbenchService, { useClass: WorkbenchService }],
[ISidebarService, {