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 new current file not rendered (#4728)

Wenzhao Hu5f6130a2b1e7
diff --git a/e2e/visual-comparison/sheets/sheets-gridlines.spec.ts b/e2e/visual-comparison/sheets/sheets-gridlines.spec.ts
index 0c8b6af067..f95b99b507 100644
--- a/e2e/visual-comparison/sheets/sheets-gridlines.spec.ts
+++ b/e2e/visual-comparison/sheets/sheets-gridlines.spec.ts
@@ -44,5 +44,5 @@ test('sheets no gridlines', async () => {
],
fullPage: true,
});
- expect(screenshot).toMatchSnapshot(filename, { maxDiffPixels: 5 });
+ expect(screenshot).toMatchSnapshot(filename, { maxDiffPixelRatio: 0.005 });
});
diff --git a/e2e/visual-comparison/sheets/sheets-gridlines.spec.ts-snapshots/sheets-no-gridlines-ci-chromium-linux.png b/e2e/visual-comparison/sheets/sheets-gridlines.spec.ts-snapshots/sheets-no-gridlines-ci-chromium-linux.png
index 30aa4fa001..90ac7a8ce0 100644
Binary files a/e2e/visual-comparison/sheets/sheets-gridlines.spec.ts-snapshots/sheets-no-gridlines-ci-chromium-linux.png and b/e2e/visual-comparison/sheets/sheets-gridlines.spec.ts-snapshots/sheets-no-gridlines-ci-chromium-linux.png differ
diff --git a/e2e/visual-comparison/sheets/sheets-scroll.spec.ts b/e2e/visual-comparison/sheets/sheets-scroll.spec.ts
index 3464a6d130..a190ee8762 100644
--- a/e2e/visual-comparison/sheets/sheets-scroll.spec.ts
+++ b/e2e/visual-comparison/sheets/sheets-scroll.spec.ts
@@ -79,7 +79,7 @@ test('cells rendering after scrolling', async () => {
const filename = generateSnapshotName('mergedCellsRenderingScrolling');
const screenshot = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
- await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixels: 5 });
+ await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixelRatio: 0.005 });
});
test('rendering after scrolling by API', async () => {
@@ -102,5 +102,5 @@ test('rendering after scrolling by API', async () => {
await page.waitForTimeout(1000);
const filename = generateSnapshotName('renderingAfterScrollByAPI');
const screenshot = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
- await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixels: 5 });
+ await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixelRatio: 0.005 });
});
diff --git a/e2e/visual-comparison/sheets/sheets-scroll.spec.ts-snapshots/mergedCellsRenderingScrolling-ci-chromium-linux.png b/e2e/visual-comparison/sheets/sheets-scroll.spec.ts-snapshots/mergedCellsRenderingScrolling-ci-chromium-linux.png
index 5c538c1d8b..3a6ccc7511 100644
Binary files a/e2e/visual-comparison/sheets/sheets-scroll.spec.ts-snapshots/mergedCellsRenderingScrolling-ci-chromium-linux.png and b/e2e/visual-comparison/sheets/sheets-scroll.spec.ts-snapshots/mergedCellsRenderingScrolling-ci-chromium-linux.png differ
diff --git a/e2e/visual-comparison/sheets/sheets-scroll.spec.ts-snapshots/renderingAfterScrollByAPI-ci-chromium-linux.png b/e2e/visual-comparison/sheets/sheets-scroll.spec.ts-snapshots/renderingAfterScrollByAPI-ci-chromium-linux.png
index 408ca197e9..264263698f 100644
Binary files a/e2e/visual-comparison/sheets/sheets-scroll.spec.ts-snapshots/renderingAfterScrollByAPI-ci-chromium-linux.png and b/e2e/visual-comparison/sheets/sheets-scroll.spec.ts-snapshots/renderingAfterScrollByAPI-ci-chromium-linux.png differ
diff --git a/e2e/visual-comparison/sheets/sheets-switching-render-unit.spec.ts-snapshots/switching-render-unit-ci-chromium-linux.png b/e2e/visual-comparison/sheets/sheets-switching-render-unit.spec.ts-snapshots/switching-render-unit-ci-chromium-linux.png
index 8f5b7212c5..0cdea085d0 100644
Binary files a/e2e/visual-comparison/sheets/sheets-switching-render-unit.spec.ts-snapshots/switching-render-unit-ci-chromium-linux.png and b/e2e/visual-comparison/sheets/sheets-switching-render-unit.spec.ts-snapshots/switching-render-unit-ci-chromium-linux.png differ
diff --git a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts
index 7bd9ec9c75..d1b4ec2672 100644
--- a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts
+++ b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts
@@ -55,7 +55,7 @@ test('diff default sheet content', async ({ page }) => {
const filename = generateSnapshotName('default-sheet');
const screenshot = await page.locator('#univer-sheet-main-canvas_test').screenshot();
- await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixels: 5 });
+ await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixelRatio: 0.005 });
});
test('diff demo sheet content', async ({ page }) => {
@@ -74,7 +74,7 @@ test('diff demo sheet content', async ({ page }) => {
const filename = generateSnapshotName('demo-sheet');
const screenshot = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
- await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixels: 5 });
+ await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixelRatio: 0.005 });
expect(errored).toBeFalsy();
});
@@ -98,7 +98,7 @@ test('diff merged cells rendering', async () => {
const filename = generateSnapshotName('mergedCellsRendering');
const screenshot = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
- await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixels: 5 });
+ await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixelRatio: 0.005 });
});
/**
@@ -121,7 +121,7 @@ test('diff sheet default style rendering', async () => {
const filename = generateSnapshotName('defaultstyle');
const screenshot = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
- await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixels: 5 });
+ await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixelRatio: 0.005 });
});
test('diff facade sheet hooks', async () => {
@@ -154,7 +154,7 @@ test('diff facade sheet hooks', async () => {
const filename = generateSnapshotName('facade-sheet-hooks');
const screenshot = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
- await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixels: 5 });
+ await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixelRatio: 0.005 });
});
test('diff set force string cell', async () => {
@@ -234,7 +234,7 @@ test('diff set force string cell', async () => {
const filename = generateSnapshotName('set-force-string-cell');
const screenshot = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
- await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixels: 5 });
+ await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixelRatio: 0.005 });
await page.waitForTimeout(2000);
await browser.close();
@@ -285,7 +285,7 @@ test('diff set text format number cell', async () => {
const filename = generateSnapshotName('set-text-format-number-cell');
const screenshot = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
- await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixels: 150 });
+ await expect(screenshot).toMatchSnapshot(filename, { maxDiffPixelRatio: 0.005 });
await page.waitForTimeout(2000);
await browser.close();
diff --git a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/default-sheet-fullpage-ci-chromium-linux.png b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/default-sheet-fullpage-ci-chromium-linux.png
index 705160ab59..0cdea085d0 100644
Binary files a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/default-sheet-fullpage-ci-chromium-linux.png and b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/default-sheet-fullpage-ci-chromium-linux.png differ
diff --git a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/defaultstyle-ci-chromium-linux.png b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/defaultstyle-ci-chromium-linux.png
index 1a7d212d63..603fe0b550 100644
Binary files a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/defaultstyle-ci-chromium-linux.png and b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/defaultstyle-ci-chromium-linux.png differ
diff --git a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/facade-sheet-hooks-ci-chromium-linux.png b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/facade-sheet-hooks-ci-chromium-linux.png
index 5c919b5a64..bd02e6fa2e 100644
Binary files a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/facade-sheet-hooks-ci-chromium-linux.png and b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/facade-sheet-hooks-ci-chromium-linux.png differ
diff --git a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/mergedCellsRendering-ci-chromium-linux.png b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/mergedCellsRendering-ci-chromium-linux.png
index 5c538c1d8b..3a6ccc7511 100644
Binary files a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/mergedCellsRendering-ci-chromium-linux.png and b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/mergedCellsRendering-ci-chromium-linux.png differ
diff --git a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/set-force-string-cell-ci-chromium-linux.png b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/set-force-string-cell-ci-chromium-linux.png
index 9c1af83f84..429a7db7d0 100644
Binary files a/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/set-force-string-cell-ci-chromium-linux.png and b/e2e/visual-comparison/sheets/sheets-visual-comparison.spec.ts-snapshots/set-force-string-cell-ci-chromium-linux.png differ
diff --git a/packages-experimental/uni-docs-ui/src/controllers/menu.ts b/packages-experimental/uni-docs-ui/src/controllers/menu.ts
index 8719639d16..234fb3f11b 100644
--- a/packages-experimental/uni-docs-ui/src/controllers/menu.ts
+++ b/packages-experimental/uni-docs-ui/src/controllers/menu.ts
@@ -14,14 +14,14 @@
* limitations under the License.
*/
+import type { IAccessor } from '@univerjs/core';
+import type { IMenuButtonItem, IMenuItem } from '@univerjs/ui';
import { BooleanNumber, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, ICommandService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { DocSelectionManagerService, DocSkeletonManagerService, SetTextSelectionsOperation } from '@univerjs/docs';
import { SetInlineFormatCommand } from '@univerjs/docs-ui';
import { DocumentEditArea, IRenderManagerService } from '@univerjs/engine-render';
-import { getHeaderFooterMenuHiddenObservable, getMenuHiddenObservable, type IMenuButtonItem, type IMenuItem, MenuItemType } from '@univerjs/ui';
-
+import { getHeaderFooterMenuHiddenObservable, getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
import { combineLatest, Observable } from 'rxjs';
-import type { IAccessor } from '@univerjs/core';
export const DOC_ITALIC_MUTATION_ID = 'doc.command.uni-italic';
export const DOC_BOLD_MUTATION_ID = 'doc.command.uni-bold';
@@ -267,15 +267,6 @@ function getInsertTableHiddenObservable(
});
});
- const currentRender = renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC);
- if (currentRender == null) {
- return subscriber.next(true);
- }
-
- const viewModel = currentRender.with(DocSkeletonManagerService).getViewModel();
-
- subscriber.next(viewModel.getEditArea() !== DocumentEditArea.BODY);
-
return () => subscription.unsubscribe();
});
}
diff --git a/packages/core/src/services/instance/instance.service.ts b/packages/core/src/services/instance/instance.service.ts
index 1c232523fa..eb35a33045 100644
--- a/packages/core/src/services/instance/instance.service.ts
+++ b/packages/core/src/services/instance/instance.service.ts
@@ -16,7 +16,6 @@
import type { Observable } from 'rxjs';
import type { IDisposable } from '../../common/di';
-
import type { UnitModel, UnitType } from '../../common/unit';
import type { Nullable } from '../../shared';
import { BehaviorSubject, distinctUntilChanged, filter, map, Subject } from 'rxjs';
@@ -29,6 +28,7 @@ import { SlideDataModel } from '../../slides/slide-model';
import { FOCUSING_DOC, FOCUSING_SHEET, FOCUSING_SLIDE, FOCUSING_UNIT } from '../context/context';
import { IContextService } from '../context/context.service';
+// eslint-disable-next-line ts/no-explicit-any
export type UnitCtor = new (...args: any[]) => UnitModel;
export interface ICreateUnitOptions {
@@ -60,15 +60,23 @@ export interface IUniverInstanceService {
/** Subscribe to curtain type of units' disposing. */
getTypeOfUnitDisposed$<T extends UnitModel>(type: UnitType): Observable<T>;
- /** An observable value that emits the id of the focused unit. */
+ /**
+ * An observable value that emits the id of the focused unit. A Univer app instance
+ * can only have 1 focused unit.
+ *
+ * You can use `getFocusedUnit` to get the currently focused unit, and
+ * `focusUnit` to focus a unit.
+ */
focused$: Observable<Nullable<string>>;
/** Focus a unit. */
focusUnit(unitId: string | null): void;
+ /** Get the currently focused unit. */
getFocusedUnit(): Nullable<UnitModel>;
+ /** @deprecated Use `getCurrentUnitOfType` instead. */
getCurrentUnitForType<T extends UnitModel>(type: UnitType): Nullable<T>;
+ getCurrentUnitOfType<T extends UnitModel>(type: UnitType): Nullable<T>;
setCurrentUnitForType(unitId: string): void;
-
getCurrentTypeOfUnit$<T extends UnitModel>(type: UnitType): Observable<Nullable<T>>;
/** Create a unit with snapshot info. */
@@ -148,6 +156,10 @@ export class UniverInstanceService extends Disposable implements IUniverInstance
return this._currentUnits.get(type) as Nullable<T>;
}
+ getCurrentUnitOfType<T extends UnitModel>(type: UnitType): Nullable<T> {
+ return this.getCurrentUnitForType(type);
+ }
+
setCurrentUnitForType(unitId: string): void {
const result = this._getUnitById(unitId);
if (!result) throw new Error(`[UniverInstanceService]: no document with unitId ${unitId}!`);
@@ -288,15 +300,15 @@ export class UniverInstanceService extends Disposable implements IUniverInstance
const index = units.indexOf(unit);
units.splice(index, 1);
- this._tryRemoveCurrentUnitForType(unitId, type);
- this._tryBlurUnitOnRemoval(unitId);
+ this._tryResetCurrentOnRemoval(unitId, type);
+ this._tryResetFocusOnRemoval(unitId);
this._unitDisposed$.next(unit);
return true;
}
- private _tryRemoveCurrentUnitForType(unitId: string, type: UnitType): void {
+ private _tryResetCurrentOnRemoval(unitId: string, type: UnitType): void {
const current = this.getCurrentUnitForType(type);
if (current?.getUnitId() === unitId) {
this._currentUnits.set(type, null);
@@ -304,7 +316,7 @@ export class UniverInstanceService extends Disposable implements IUniverInstance
}
}
- private _tryBlurUnitOnRemoval(unitId: string): void {
+ private _tryResetFocusOnRemoval(unitId: string): void {
if (this.focused?.getUnitId() === unitId) {
this._focused$.next(null);
}
diff --git a/packages/core/src/services/lifecycle/lifecycle.service.ts b/packages/core/src/services/lifecycle/lifecycle.service.ts
index e0ac1cb8f6..f385d8be40 100644
--- a/packages/core/src/services/lifecycle/lifecycle.service.ts
+++ b/packages/core/src/services/lifecycle/lifecycle.service.ts
@@ -15,7 +15,7 @@
*/
import type { Observable } from 'rxjs';
-import { BehaviorSubject, merge, of, skip } from 'rxjs';
+import { BehaviorSubject, filter, firstValueFrom, map, merge, of, skip } from 'rxjs';
import { Disposable } from '../../shared/lifecycle';
import { takeAfter } from '../../shared/rxjs';
@@ -59,6 +59,14 @@ export class LifecycleService extends Disposable {
super.dispose();
}
+ onStage(stage: LifecycleStages): Promise<void> {
+ return firstValueFrom(this.lifecycle$.pipe(
+ filter((s) => s >= stage),
+ takeAfter((s) => s === stage),
+ map(() => void 0)
+ ));
+ }
+
/**
* Subscribe to lifecycle changes and all previous stages and the current
* stage will be emitted immediately.
diff --git a/packages/docs-drawing-ui/src/commands/commands/insert-image.command.ts b/packages/docs-drawing-ui/src/commands/commands/insert-image.command.ts
index b94416da71..bee5e13952 100644
--- a/packages/docs-drawing-ui/src/commands/commands/insert-image.command.ts
+++ b/packages/docs-drawing-ui/src/commands/commands/insert-image.command.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
-import { CommandType, UniverInstanceType } from '@univerjs/core';
-import { IRenderManagerService } from '@univerjs/engine-render';
import type { ICommand, Nullable } from '@univerjs/core';
+import { CommandType, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
+import { getCurrentTypeOfRenderer, IRenderManagerService } from '@univerjs/engine-render';
import { DocDrawingUpdateRenderController } from '../../controllers/render-controllers/doc-drawing-update.render-controller';
export interface IInsertDocImageCommandParams {
@@ -27,8 +27,9 @@ export const InsertDocImageCommand: ICommand<IInsertDocImageCommandParams> = {
id: 'doc.command.insert-float-image',
type: CommandType.COMMAND,
handler: (accessor) => {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
const renderManagerService = accessor.get(IRenderManagerService);
- return renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC)
+ return getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC, univerInstanceService, renderManagerService)
?.with(DocDrawingUpdateRenderController)
.insertDocImage() ?? false;
},
diff --git a/packages/docs-thread-comment-ui/src/controllers/menu.ts b/packages/docs-thread-comment-ui/src/controllers/menu.ts
index d1e4c2a994..6a8f13717d 100644
--- a/packages/docs-thread-comment-ui/src/controllers/menu.ts
+++ b/packages/docs-thread-comment-ui/src/controllers/menu.ts
@@ -16,9 +16,9 @@
import type { IAccessor } from '@univerjs/core';
import type { IMenuButtonItem } from '@univerjs/ui';
-import { SHEET_EDITOR_UNITS, UniverInstanceType } from '@univerjs/core';
+import { IUniverInstanceService, SHEET_EDITOR_UNITS, UniverInstanceType } from '@univerjs/core';
import { DocSelectionManagerService, DocSkeletonManagerService } from '@univerjs/docs';
-import { DocumentEditArea, IRenderManagerService } from '@univerjs/engine-render';
+import { DocumentEditArea, IRenderManagerService, withCurrentTypeOfRenderer } from '@univerjs/engine-render';
import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
import { debounceTime, Observable } from 'rxjs';
import { StartAddCommentOperation, ToggleCommentPanelOperation } from '../commands/operations/show-comment-panel.operation';
@@ -26,8 +26,13 @@ import { StartAddCommentOperation, ToggleCommentPanelOperation } from '../comman
export const shouldDisableAddComment = (accessor: IAccessor) => {
const renderManagerService = accessor.get(IRenderManagerService);
const docSelectionManagerService = accessor.get(DocSelectionManagerService);
- const render = renderManagerService.getCurrent();
- const skeleton = render?.with(DocSkeletonManagerService).getSkeleton();
+ const skeleton = withCurrentTypeOfRenderer(
+ UniverInstanceType.UNIVER_DOC,
+ DocSkeletonManagerService,
+ accessor.get(IUniverInstanceService),
+ renderManagerService
+ )?.getSkeleton();
+
const editArea = skeleton?.getViewModel().getEditArea();
if (editArea === DocumentEditArea.FOOTER || editArea === DocumentEditArea.HEADER) {
return true;
diff --git a/packages/docs-ui/src/controllers/doc-header-footer.controller.ts b/packages/docs-ui/src/controllers/doc-header-footer.controller.ts
index 972ffa0ea8..bb3156229b 100644
--- a/packages/docs-ui/src/controllers/doc-header-footer.controller.ts
+++ b/packages/docs-ui/src/controllers/doc-header-footer.controller.ts
@@ -17,7 +17,7 @@
import type { DocumentDataModel, ICommandInfo } from '@univerjs/core';
import type { Documents, DocumentViewModel, IMouseEvent, IPageRenderConfig, IPathProps, IPointerEvent, IRenderContext, IRenderModule, RenderComponentType } from '@univerjs/engine-render';
import type { Nullable } from 'vitest';
-import { BooleanNumber, Disposable, DocumentFlavor, ICommandService, Inject, IUniverInstanceService, LocaleService, toDisposable, Tools } from '@univerjs/core';
+import { BooleanNumber, Disposable, DocumentFlavor, ICommandService, Inject, IUniverInstanceService, LocaleService, toDisposable, Tools, UniverInstanceType } from '@univerjs/core';
import { DocSkeletonManagerService, RichTextEditingMutation } from '@univerjs/docs';
import { DocumentEditArea, IRenderManagerService, PageLayoutType, Path, Rect, Vector2 } from '@univerjs/engine-render';
@@ -294,11 +294,12 @@ export class DocHeaderFooterController extends Disposable implements IRenderModu
const localeService = this._localeService;
// eslint-disable-next-line max-lines-per-function
- this._renderManagerService.currentRender$.subscribe((unitId) => {
- if (unitId == null) {
+ this._instanceSrv.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_DOC).subscribe((unit) => {
+ if (unit == null) {
return;
}
+ const unitId = unit.getUnitId();
const currentRender = this._renderManagerService.getRenderById(unitId);
if (this._editorService.isEditor(unitId) || this._instanceSrv.getUniverDocInstance(unitId) == null) {
return;
diff --git a/packages/docs-ui/src/controllers/menu/menu.ts b/packages/docs-ui/src/controllers/menu/menu.ts
index c6ce4bf924..1db196633d 100644
--- a/packages/docs-ui/src/controllers/menu/menu.ts
+++ b/packages/docs-ui/src/controllers/menu/menu.ts
@@ -16,7 +16,8 @@
import type { DocumentDataModel, IAccessor, PresetListType } from '@univerjs/core';
import type {
- IRichTextEditingMutationParams } from '@univerjs/docs';
+ IRichTextEditingMutationParams,
+} from '@univerjs/docs';
import type { IMenuButtonItem, IMenuItem, IMenuSelectorItem } from '@univerjs/ui';
import type { Subscription } from 'rxjs';
import {
@@ -70,8 +71,8 @@ function getInsertTableHiddenObservable(
if (unitId == null) {
return subscriber.next(true);
}
- const univerType = univerInstanceService.getUnitType(unitId);
+ const univerType = univerInstanceService.getUnitType(unitId);
if (univerType !== UniverInstanceType.UNIVER_DOC) {
return subscriber.next(true);
}
@@ -88,15 +89,6 @@ function getInsertTableHiddenObservable(
});
});
- const currentRender = renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC);
- if (currentRender == null) {
- return subscriber.next(true);
- }
-
- const viewModel = currentRender.with(DocSkeletonManagerService).getViewModel();
-
- subscriber.next(viewModel.getEditArea() !== DocumentEditArea.BODY);
-
return () => subscription.unsubscribe();
});
}
diff --git a/packages/docs-ui/src/services/docs-render.service.ts b/packages/docs-ui/src/services/docs-render.service.ts
index 6788adc4a5..3dbce49d15 100644
--- a/packages/docs-ui/src/services/docs-render.service.ts
+++ b/packages/docs-ui/src/services/docs-render.service.ts
@@ -57,11 +57,9 @@ export class DocsRenderService extends RxDisposable {
renderer.engine.getCanvas().getContext().setId(DOC_MAIN_CANVAS_ID);
}
});
+
if (!this._renderManagerService.has(unitId)) {
this._createRenderWithId(unitId);
-
- // NOTE@wzhudev: maybe not in univer mode
- this._renderManagerService.setCurrent(unitId);
}
}
diff --git a/packages/engine-render/src/index.ts b/packages/engine-render/src/index.ts
index 72e1a131f1..d77415099e 100644
--- a/packages/engine-render/src/index.ts
+++ b/packages/engine-render/src/index.ts
@@ -41,7 +41,7 @@ export * from './engine';
export * from './group';
export * from './layer';
export { IRenderingEngine, UniverRenderEnginePlugin } from './plugin';
-export { IRenderManagerService, type RenderComponentType, RenderManagerService } from './render-manager/render-manager.service';
+export { getCurrentTypeOfRenderer, IRenderManagerService, type RenderComponentType, RenderManagerService, withCurrentTypeOfRenderer } from './render-manager/render-manager.service';
export { type IRender, type IRenderContext, type IRenderModule, RenderUnit } from './render-manager/render-unit';
export * from './scene';
export { type IChangeObserverConfig } from './scene.transformer';
diff --git a/packages/engine-render/src/render-manager/render-manager.service.ts b/packages/engine-render/src/render-manager/render-manager.service.ts
index 5806101743..a03f1f2caa 100644
--- a/packages/engine-render/src/render-manager/render-manager.service.ts
+++ b/packages/engine-render/src/render-manager/render-manager.service.ts
@@ -23,7 +23,7 @@ import type { SheetComponent } from '../components/sheets/sheet-component';
import type { Slide } from '../components/slides/slide';
import type { IRender } from './render-unit';
import { createIdentifier, Disposable, Inject, Injector, IUniverInstanceService, remove, toDisposable, UniverInstanceType } from '@univerjs/core';
-import { BehaviorSubject, Subject } from 'rxjs';
+import { Subject } from 'rxjs';
import { Engine } from '../engine';
import { Scene } from '../scene';
import { RenderUnit } from './render-unit';
@@ -31,9 +31,6 @@ import { RenderUnit } from './render-unit';
export type RenderComponentType = SheetComponent | DocComponent | Slide | BaseObject;
export interface IRenderManagerService extends IDisposable {
- currentRender$: Observable<Nullable<string>>;
- getCurrent(): Nullable<IRender>;
-
addRender(unitId: string, renderer: IRender): void;
/**
@@ -43,7 +40,6 @@ export interface IRenderManagerService extends IDisposable {
*/
createRender(unitId: string): IRender;
removeRender(unitId: string): void;
- setCurrent(unitId: string): void;
/**
* Get RenderUnit By Id, RenderUnit implements IRender
* @param unitId
@@ -55,7 +51,6 @@ export interface IRenderManagerService extends IDisposable {
*/
getRenderUnitById(unitId: string): Nullable<IRender>;
getAllRenderersOfType(type: UniverInstanceType): RenderUnit[];
- getCurrentTypeOfRenderer(type: UniverInstanceType): Nullable<RenderUnit>;
getRenderAll(): Map<string, IRender>;
defaultEngine: Engine;
@@ -72,11 +67,7 @@ export interface IRenderManagerService extends IDisposable {
created$: Observable<IRender>;
disposed$: Observable<string>;
- /** @deprecated There will be multi units to render at the same time, so there is no *first*. */
- getFirst(): Nullable<IRender>;
-
has(unitId: string): boolean;
- withCurrentTypeOfUnit<T>(type: UniverInstanceType, id: DependencyIdentifier<T>): Nullable<T>;
/**
* add dep to _renderDependencies(type, dep)
@@ -93,13 +84,8 @@ const SCENE_NAMESPACE = '_UNIVER_SCENE_';
export class RenderManagerService extends Disposable implements IRenderManagerService {
private _defaultEngine!: Engine;
- private _currentUnitId: string = '';
-
private _renderMap: Map<string, IRender> = new Map();
- private readonly _currentRender$ = new BehaviorSubject<Nullable<string>>(this._currentUnitId);
- readonly currentRender$ = this._currentRender$.asObservable();
-
private readonly _createRender$ = new Subject<string>();
/** @deprecated */
readonly createRender$ = this._createRender$.asObservable();
@@ -132,8 +118,6 @@ export class RenderManagerService extends Disposable implements IRenderManagerSe
this._renderMap.forEach((item) => this._disposeItem(item));
this._renderDependencies.clear();
this._renderMap.clear();
- this._currentRender$.complete();
-
this._renderCreated$.complete();
this._renderDisposed$.complete();
}
@@ -217,20 +201,6 @@ export class RenderManagerService extends Disposable implements IRenderManagerSe
return renderUnits;
}
- getCurrentTypeOfRenderer(type: UniverInstanceType): Nullable<RenderUnit> {
- const current = this._univerInstanceService.getCurrentUnitForType(type);
- if (!current) return null;
-
- return this.getRenderById(current.getUnitId()) as RenderUnit;
- }
-
- withCurrentTypeOfUnit<T>(type: UniverInstanceType, id: DependencyIdentifier<T>): Nullable<T> {
- const current = this._univerInstanceService.getCurrentUnitForType(type);
- if (!current) return null;
-
- return this.getRenderById(current.getUnitId())?.with(id);
- }
-
/**
* init deps by injector.get(dep), and injector derives from renderer.
* @param renderer
@@ -329,19 +299,6 @@ export class RenderManagerService extends Disposable implements IRenderManagerSe
return this._renderMap.has(unitId);
}
- setCurrent(unitId: string): void {
- this._currentUnitId = unitId;
- this._currentRender$.next(unitId);
- }
-
- getCurrent(): Nullable<IRender> {
- return this._renderMap.get(this._currentUnitId);
- }
-
- getFirst(): Nullable<IRender> {
- return [...this.getRenderAll().values()][0];
- }
-
/**
* @deprecated use getRenderUnitById instead
* Get RenderUnit from this._renderMap.
@@ -389,3 +346,27 @@ export function isDisposable(thing: unknown): thing is IDisposable {
// eslint-disable-next-line ts/no-explicit-any
return !!thing && typeof (thing as any).dispose === 'function';
}
+
+export function getCurrentTypeOfRenderer(
+ type: UniverInstanceType,
+ instanceService: IUniverInstanceService,
+ renderManageService: IRenderManagerService
+): Nullable<IRender> {
+ const currentUnit = instanceService.getCurrentUnitForType(type);
+ if (currentUnit == null) {
+ return null;
+ }
+
+ return renderManageService.getRenderUnitById(currentUnit.getUnitId());
+}
+
+export function withCurrentTypeOfRenderer<T>(
+ type: UniverInstanceType,
+ id: DependencyIdentifier<T>,
+ instanceService: IUniverInstanceService,
+ renderManagerService: IRenderManagerService
+): Nullable<T> {
+ const renderer = getCurrentTypeOfRenderer(type, instanceService, renderManagerService);
+ if (renderer) return renderer.with(id);
+ return null;
+}
diff --git a/packages/sheets-data-validation-ui/src/views/widgets/checkbox-widget.ts b/packages/sheets-data-validation-ui/src/views/widgets/checkbox-widget.ts
index e905c59692..93e21cdf13 100644
--- a/packages/sheets-data-validation-ui/src/views/widgets/checkbox-widget.ts
+++ b/packages/sheets-data-validation-ui/src/views/widgets/checkbox-widget.ts
@@ -19,8 +19,8 @@ import type { IBaseDataValidationWidget, IFormulaResult } from '@univerjs/data-v
import type { IMouseEvent, IPointerEvent, UniverRenderingContext, UniverRenderingContext2D } from '@univerjs/engine-render';
import type { ISetRangeValuesCommandParams } from '@univerjs/sheets';
import type { CheckboxValidator } from '@univerjs/sheets-data-validation';
-import { HorizontalAlign, ICommandService, Inject, isFormulaString, ThemeService, UniverInstanceType, VerticalAlign } from '@univerjs/core';
-import { CheckboxShape as Checkbox, CURSOR_TYPE, fixLineWidthByScale, IRenderManagerService, Transform } from '@univerjs/engine-render';
+import { HorizontalAlign, ICommandService, Inject, isFormulaString, IUniverInstanceService, ThemeService, UniverInstanceType, VerticalAlign } from '@univerjs/core';
+import { CheckboxShape as Checkbox, CURSOR_TYPE, fixLineWidthByScale, getCurrentTypeOfRenderer, IRenderManagerService, Transform } from '@univerjs/engine-render';
import { SetRangeValuesCommand } from '@univerjs/sheets';
import { CHECKBOX_FORMULA_1, CHECKBOX_FORMULA_2, DataValidationFormulaService, getCellValueOrigin, getFormulaResult, isLegalFormulaResult, SheetDataValidationModel, transformCheckboxValue } from '@univerjs/sheets-data-validation';
@@ -69,6 +69,7 @@ export class CheckboxRender implements IBaseDataValidationWidget {
constructor(
@ICommandService private readonly _commandService: ICommandService,
+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@Inject(DataValidationFormulaService) private readonly _formulaService: DataValidationFormulaService,
@Inject(ThemeService) private readonly _themeService: ThemeService,
@Inject(IRenderManagerService) private readonly _renderManagerService: IRenderManagerService,
@@ -217,10 +218,14 @@ export class CheckboxRender implements IBaseDataValidationWidget {
};
onPointerEnter(info: ICellRenderContext, evt: IPointerEvent | IMouseEvent) {
- this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET)?.mainComponent?.setCursor(CURSOR_TYPE.POINTER);
+ getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET, this._univerInstanceService, this._renderManagerService)
+ ?.mainComponent
+ ?.setCursor(CURSOR_TYPE.POINTER);
}
onPointerLeave(info: ICellRenderContext, evt: IPointerEvent | IMouseEvent) {
- this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET)?.mainComponent?.setCursor(CURSOR_TYPE.DEFAULT);
+ getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET, this._univerInstanceService, this._renderManagerService)
+ ?.mainComponent
+ ?.setCursor(CURSOR_TYPE.DEFAULT);
}
}
diff --git a/packages/sheets-data-validation-ui/src/views/widgets/dropdown-multiple-widget.ts b/packages/sheets-data-validation-ui/src/views/widgets/dropdown-multiple-widget.ts
index d9271cb4ee..f7f2134623 100644
--- a/packages/sheets-data-validation-ui/src/views/widgets/dropdown-multiple-widget.ts
+++ b/packages/sheets-data-validation-ui/src/views/widgets/dropdown-multiple-widget.ts
@@ -20,8 +20,8 @@ import type { IMouseEvent, IPointerEvent, Spreadsheet, SpreadsheetSkeleton, Univ
import type { ListMultipleValidator } from '@univerjs/sheets-data-validation';
import type { IShowDataValidationDropdownParams } from '../../commands/operations/data-validation.operation';
import type { IDropdownInfo } from './dropdown-widget';
-import { HorizontalAlign, ICommandService, Inject, UniverInstanceType, VerticalAlign } from '@univerjs/core';
-import { CURSOR_TYPE, getFontStyleString, IRenderManagerService } from '@univerjs/engine-render';
+import { HorizontalAlign, ICommandService, Inject, IUniverInstanceService, UniverInstanceType, VerticalAlign } from '@univerjs/core';
+import { CURSOR_TYPE, getCurrentTypeOfRenderer, getFontStyleString, IRenderManagerService } from '@univerjs/engine-render';
import { getCellValueOrigin, SheetDataValidationModel } from '@univerjs/sheets-data-validation';
import { ShowDataValidationDropdown } from '../../commands/operations/data-validation.operation';
import { CELL_PADDING_H, CELL_PADDING_V, Dropdown, ICON_PLACE, layoutDropdowns, MARGIN_V } from './shape';
@@ -34,6 +34,7 @@ export class DropdownMultipleWidget implements IBaseDataValidationWidget {
constructor(
@ICommandService private readonly _commandService: ICommandService,
+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@Inject(IRenderManagerService) private readonly _renderManagerService: IRenderManagerService,
@Inject(SheetDataValidationModel) private readonly _dataValidationModel: SheetDataValidationModel
) {
@@ -255,10 +256,14 @@ export class DropdownMultipleWidget implements IBaseDataValidationWidget {
}
onPointerEnter(info: ICellRenderContext, evt: IPointerEvent | IMouseEvent) {
- this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET)?.mainComponent?.setCursor(CURSOR_TYPE.POINTER);
+ return getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET, this._univerInstanceService, this._renderManagerService)
+ ?.mainComponent
+ ?.setCursor(CURSOR_TYPE.POINTER);
}
onPointerLeave(info: ICellRenderContext, evt: IPointerEvent | IMouseEvent) {
- this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET)?.mainComponent?.setCursor(CURSOR_TYPE.DEFAULT);
+ return getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET, this._univerInstanceService, this._renderManagerService)
+ ?.mainComponent
+ ?.setCursor(CURSOR_TYPE.DEFAULT);
}
}
diff --git a/packages/sheets-data-validation-ui/src/views/widgets/dropdown-widget.ts b/packages/sheets-data-validation-ui/src/views/widgets/dropdown-widget.ts
index e5301377ed..673d8bb550 100644
--- a/packages/sheets-data-validation-ui/src/views/widgets/dropdown-widget.ts
+++ b/packages/sheets-data-validation-ui/src/views/widgets/dropdown-widget.ts
@@ -19,8 +19,8 @@ import type { IBaseDataValidationWidget } from '@univerjs/data-validation';
import type { IMouseEvent, IPointerEvent, SpreadsheetSkeleton, UniverRenderingContext, UniverRenderingContext2D } from '@univerjs/engine-render';
import type { ListValidator } from '@univerjs/sheets-data-validation';
import type { IShowDataValidationDropdownParams } from '../../commands/operations/data-validation.operation';
-import { BooleanNumber, DataValidationRenderMode, DataValidationType, DEFAULT_EMPTY_DOCUMENT_VALUE, DEFAULT_STYLES, DocumentDataModel, HorizontalAlign, ICommandService, Inject, LocaleService, Tools, UniverInstanceType, VerticalAlign, WrapStrategy } from '@univerjs/core';
-import { CURSOR_TYPE, Documents, DocumentSkeleton, DocumentViewModel, getDocsSkeletonPageSize, IRenderManagerService, Rect } from '@univerjs/engine-render';
+import { BooleanNumber, DataValidationRenderMode, DataValidationType, DEFAULT_EMPTY_DOCUMENT_VALUE, DEFAULT_STYLES, DocumentDataModel, HorizontalAlign, ICommandService, Inject, IUniverInstanceService, LocaleService, Tools, UniverInstanceType, VerticalAlign, WrapStrategy } from '@univerjs/core';
+import { CURSOR_TYPE, Documents, DocumentSkeleton, DocumentViewModel, getCurrentTypeOfRenderer, getDocsSkeletonPageSize, IRenderManagerService, Rect } from '@univerjs/engine-render';
import { getCellValueOrigin, SheetDataValidationModel } from '@univerjs/sheets-data-validation';
import { ShowDataValidationDropdown } from '../../commands/operations/data-validation.operation';
import { DROP_DOWN_DEFAULT_COLOR } from '../../const';
@@ -166,6 +166,7 @@ export class DropdownWidget implements IBaseDataValidationWidget {
private _dropdownInfoMap: Map<string, Map<string, IDropdownInfo>> = new Map();
constructor(
+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@Inject(LocaleService) private readonly _localeService: LocaleService,
@ICommandService private readonly _commandService: ICommandService,
@Inject(IRenderManagerService) private readonly _renderManagerService: IRenderManagerService,
@@ -521,10 +522,14 @@ export class DropdownWidget implements IBaseDataValidationWidget {
};
onPointerEnter(info: ICellRenderContext, evt: IPointerEvent | IMouseEvent) {
- this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET)?.mainComponent?.setCursor(CURSOR_TYPE.POINTER);
+ getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET, this._univerInstanceService, this._renderManagerService)
+ ?.mainComponent
+ ?.setCursor(CURSOR_TYPE.POINTER);
}
onPointerLeave(info: ICellRenderContext, evt: IPointerEvent | IMouseEvent) {
- this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET)?.mainComponent?.setCursor(CURSOR_TYPE.DEFAULT);
+ getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET, this._univerInstanceService, this._renderManagerService)
+ ?.mainComponent
+ ?.setCursor(CURSOR_TYPE.DEFAULT);
}
}
diff --git a/packages/sheets-drawing-ui/src/commands/commands/insert-image.command.ts b/packages/sheets-drawing-ui/src/commands/commands/insert-image.command.ts
index 88c105bece..5d76010924 100644
--- a/packages/sheets-drawing-ui/src/commands/commands/insert-image.command.ts
+++ b/packages/sheets-drawing-ui/src/commands/commands/insert-image.command.ts
@@ -15,8 +15,8 @@
*/
import type { ICommand, Nullable } from '@univerjs/core';
-import { CommandType, UniverInstanceType } from '@univerjs/core';
-import { IRenderManagerService } from '@univerjs/engine-render';
+import { CommandType, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
+import { getCurrentTypeOfRenderer, IRenderManagerService } from '@univerjs/engine-render';
import { SheetDrawingUpdateController } from '../../controllers/sheet-drawing-update.controller';
export interface IInsertImageCommandParams {
@@ -27,8 +27,13 @@ export const InsertFloatImageCommand: ICommand<IInsertImageCommandParams> = {
id: 'sheet.command.insert-float-image',
type: CommandType.COMMAND,
handler: async (accessor, params) => {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
const renderManagerService = accessor.get(IRenderManagerService);
- const sheetDrawingUpdateController = renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET)
+ const sheetDrawingUpdateController = getCurrentTypeOfRenderer(
+ UniverInstanceType.UNIVER_SHEET,
+ univerInstanceService,
+ renderManagerService
+ )
?.with(SheetDrawingUpdateController);
if (!sheetDrawingUpdateController) {
@@ -50,8 +55,14 @@ export const InsertCellImageCommand: ICommand = {
id: 'sheet.command.insert-cell-image',
type: CommandType.COMMAND,
handler: (accessor) => {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
const renderManagerService = accessor.get(IRenderManagerService);
- return renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET)
+
+ return getCurrentTypeOfRenderer(
+ UniverInstanceType.UNIVER_SHEET,
+ univerInstanceService,
+ renderManagerService
+ )
?.with(SheetDrawingUpdateController)
.insertCellImage() ?? false;
},
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts
index 18608ed64a..5a4fec1408 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { DependencyIdentifier, Injector, Univer, Workbook } from '@univerjs/core';
+import type { Injector, Univer, Workbook } from '@univerjs/core';
import type { ISelectionWithCoord } from '@univerjs/sheets';
import {
Disposable,
@@ -201,7 +201,6 @@ describe('Test format painter rules in controller', () => {
let get: Injector['get'];
let commandService: ICommandService;
let themeService: ThemeService;
- let formatPainterController: FormatPainterController;
beforeEach(() => {
class SheetSelectionRenderService {
@@ -223,14 +222,6 @@ describe('Test format painter rules in controller', () => {
getRenderById() {
return null;
}
-
- getCurrentTypeOfRenderer() {
- return {
- with: <T>(identifier: DependencyIdentifier<T>) => {
- return get(identifier);
- },
- };
- }
}
const injector = univer.__getInjector();
@@ -241,7 +232,7 @@ describe('Test format painter rules in controller', () => {
themeService = get(ThemeService);
themeService.setTheme(theme);
- formatPainterController = get(FormatPainterController);
+ get(FormatPainterController);
commandService.registerCommand(SetFormatPainterOperation);
commandService.registerCommand(SetInfiniteFormatPainterCommand);
commandService.registerCommand(SetOnceFormatPainterCommand);
diff --git a/packages/sheets-ui/src/controllers/auto-fill.controller.ts b/packages/sheets-ui/src/controllers/auto-fill.controller.ts
index 3305fd6872..46bf57ba53 100644
--- a/packages/sheets-ui/src/controllers/auto-fill.controller.ts
+++ b/packages/sheets-ui/src/controllers/auto-fill.controller.ts
@@ -44,7 +44,7 @@ import {
UniverInstanceType,
} from '@univerjs/core';
-import { DeviceInputEventType, IRenderManagerService } from '@univerjs/engine-render';
+import { DeviceInputEventType, getCurrentTypeOfRenderer, IRenderManagerService } from '@univerjs/engine-render';
import {
AddMergeUndoMutationFactory,
AddWorksheetMergeMutation,
@@ -184,7 +184,7 @@ export class AutoFillController extends Disposable {
// Each range change requires re-listening.
disposableCollection.dispose();
- const currentRenderer = this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET);
+ const currentRenderer = getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SLIDE, this._univerInstanceService, this._renderManagerService);
if (!currentRenderer) return;
const selectionRenderService = currentRenderer.with(ISheetSelectionRenderService);
diff --git a/packages/sheets-ui/src/controllers/checkbox.controller.ts b/packages/sheets-ui/src/controllers/checkbox.controller.ts
index 500a248db1..ba0d89d92b 100644
--- a/packages/sheets-ui/src/controllers/checkbox.controller.ts
+++ b/packages/sheets-ui/src/controllers/checkbox.controller.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import { Disposable, ICommandService, Inject, UniverInstanceType } from '@univerjs/core';
-import { CURSOR_TYPE, IRenderManagerService } from '@univerjs/engine-render';
+import { Disposable, ICommandService, Inject, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
+import { CURSOR_TYPE, getCurrentTypeOfRenderer, IRenderManagerService } from '@univerjs/engine-render';
import { ToggleCellCheckboxCommand } from '@univerjs/sheets';
import { HoverManagerService } from '../services/hover-manager.service';
@@ -23,6 +23,7 @@ export class SheetCheckboxController extends Disposable {
private _isPointer = false;
constructor(
@Inject(HoverManagerService) private _hoverManagerService: HoverManagerService,
+ @IUniverInstanceService private _instanceService: IUniverInstanceService,
@ICommandService private readonly _commandService: ICommandService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService
) {
@@ -32,7 +33,7 @@ export class SheetCheckboxController extends Disposable {
}
private get _mainComponent() {
- return this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET)?.mainComponent;
+ return getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET, this._instanceService, this._renderManagerService)?.mainComponent;
}
private _initHover() {
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index 5fd2e1a589..bf246840cf 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -71,7 +71,7 @@ import {
import { MessageType } from '@univerjs/design';
import { DocSelectionRenderService } from '@univerjs/docs-ui';
-import { IRenderManagerService } from '@univerjs/engine-render';
+import { IRenderManagerService, withCurrentTypeOfRenderer } from '@univerjs/engine-render';
import {
InsertColMutation,
InsertRowMutation,
@@ -137,7 +137,7 @@ export class SheetClipboardController extends RxDisposable {
constructor(
@Inject(Injector) private readonly _injector: Injector,
- @IUniverInstanceService private readonly _currentUniverSheet: IUniverInstanceService,
+ @IUniverInstanceService private readonly _instanceService: IUniverInstanceService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
@ICommandService private readonly _commandService: ICommandService,
@IContextService private readonly _contextService: IContextService,
@@ -312,7 +312,7 @@ export class SheetClipboardController extends RxDisposable {
},
getFilteredOutRows(range: IRange) {
const { startRow, endRow } = range;
- const worksheet = self._currentUniverSheet.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)?.getActiveSheet();
+ const worksheet = self._instanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)?.getActiveSheet();
const res: number[] = [];
if (!worksheet) {
return res;
@@ -585,7 +585,13 @@ export class SheetClipboardController extends RxDisposable {
}
private _generateDocumentDataModelSnapshot(snapshot: Partial<IDocumentData>) {
- const currentSkeleton = this._renderManagerService.withCurrentTypeOfUnit(UniverInstanceType.UNIVER_SHEET, SheetSkeletonManagerService)?.getCurrentParam();
+ const currentSkeleton = withCurrentTypeOfRenderer(
+ UniverInstanceType.UNIVER_SHEET,
+ SheetSkeletonManagerService,
+ this._instanceService,
+ this._renderManagerService
+ )?.getCurrentParam();
+
if (currentSkeleton == null) {
return null;
}
@@ -739,7 +745,7 @@ export class SheetClipboardController extends RxDisposable {
};
},
onPasteColumns(pasteTo, colProperties, payload) {
- const workbook = self._currentUniverSheet.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
+ const workbook = self._instanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const unitId = workbook.getUnitId();
const subUnitId = workbook.getActiveSheet()?.getSheetId();
@@ -817,7 +823,7 @@ export class SheetClipboardController extends RxDisposable {
label: 'specialPaste.besidesBorder',
},
onPasteCells: (pasteFrom, pasteTo, matrix, payload) => {
- const workbook = self._currentUniverSheet.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
+ const workbook = self._instanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const redoMutationsInfo: IMutationInfo[] = [];
const undoMutationsInfo: IMutationInfo[] = [];
const { range, unitId, subUnitId } = pasteTo;
@@ -878,7 +884,7 @@ export class SheetClipboardController extends RxDisposable {
}
private _getWorksheet(unitId: string, subUnitId: string): Worksheet {
- const worksheet = this._currentUniverSheet.getUniverSheetInstance(unitId)?.getSheetBySheetId(subUnitId);
+ const worksheet = this._instanceService.getUniverSheetInstance(unitId)?.getSheetBySheetId(subUnitId);
if (!worksheet) {
throw new Error(
diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
index 51272f41e6..2f0527299f 100644
--- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
@@ -209,11 +209,13 @@ export class EditingRenderController extends Disposable implements IRenderModule
private _initialCursorSync(d: DisposableCollection) {
d.add(this._cellEditorManagerService.focus$.pipe(filter((f) => !!f)).subscribe(() => {
- const docSelectionRenderManager = this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC)?.with(DocSelectionRenderService);
+ const currentDoc = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_DOC);
+ if (!currentDoc) return;
- if (docSelectionRenderManager) {
- docSelectionRenderManager.sync();
- }
+ const docSelectionRenderManager = this._renderManagerService.getRenderById(currentDoc?.getUnitId())?.with(DocSelectionRenderService);
+ if (!docSelectionRenderManager) return;
+
+ docSelectionRenderManager.sync();
}));
}
diff --git a/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts b/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts
index 62a9cb005f..366847735f 100644
--- a/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts
@@ -16,7 +16,8 @@
import type { Nullable } from '@univerjs/core';
import type {
- IRichTextEditingMutationParams } from '@univerjs/docs';
+ IRichTextEditingMutationParams,
+} from '@univerjs/docs';
import type { RenderComponentType } from '@univerjs/engine-render';
import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service';
import {
@@ -69,7 +70,7 @@ export class FormulaEditorController extends RxDisposable {
this._listenFxBtnClick();
this._handleContentChange();
- this._renderManagerService.currentRender$.pipe(takeUntil(this.dispose$)).subscribe((unitId) => {
+ this._univerInstanceService.focused$.pipe(takeUntil(this.dispose$)).subscribe((unitId) => {
this._create(unitId);
});
diff --git a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
index 44d7a1fdb2..1288885cb4 100644
--- a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
+++ b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
@@ -15,9 +15,9 @@
*/
import type { IUniverSheetsUIConfig } from './config.schema';
-import { Disposable, ICommandService, IConfigService, Inject, Injector, UniverInstanceType } from '@univerjs/core';
+import { Disposable, ICommandService, IConfigService, Inject, Injector, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { DocSelectionRenderService } from '@univerjs/docs-ui';
-import { IRenderManagerService } from '@univerjs/engine-render';
+import { getCurrentTypeOfRenderer, IRenderManagerService } from '@univerjs/engine-render';
import { HideGridlines } from '@univerjs/icons';
import {
@@ -363,8 +363,8 @@ export class SheetUIController extends Disposable {
this._layoutService.registerFocusHandler(UniverInstanceType.UNIVER_SHEET, (_unitId: string) => {
// DEBT: `_unitId` is not used hence we cannot support Univer mode now
const renderManagerService = this._injector.get(IRenderManagerService);
-
- const currentEditorRender = renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC);
+ const instanceService = this._injector.get(IUniverInstanceService);
+ const currentEditorRender = getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC, instanceService, renderManagerService);
const docSelectionRenderService = currentEditorRender?.with(DocSelectionRenderService);
docSelectionRenderService?.focus();
diff --git a/packages/sheets-ui/src/mobile-plugin.ts b/packages/sheets-ui/src/mobile-plugin.ts
index e7a004e38b..9c5ad4f7cd 100644
--- a/packages/sheets-ui/src/mobile-plugin.ts
+++ b/packages/sheets-ui/src/mobile-plugin.ts
@@ -183,6 +183,7 @@ export class UniverSheetsMobileUIPlugin extends Plugin {
this.disposeWithMe(univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET)
.pipe(filter((v) => !!v))
.subscribe((workbook) => {
+ if (this._univerInstanceService.getFocusedUnit() === workbook) return;
univerInstanceService.focusUnit(workbook!.getUnitId());
}));
}
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index c79020e2a4..78f30b96bb 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -52,7 +52,7 @@ import {
Tools,
UniverInstanceType,
} from '@univerjs/core';
-import { IRenderManagerService } from '@univerjs/engine-render';
+import { IRenderManagerService, withCurrentTypeOfRenderer } from '@univerjs/engine-render';
import {
getPrimaryForRange,
@@ -176,8 +176,13 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
@Inject(Injector) private readonly _injector: Injector
) {
super();
+
this._htmlToUSM = new HtmlToUSMService({
- getCurrentSkeleton: () => this._renderManagerService.withCurrentTypeOfUnit(UniverInstanceType.UNIVER_SHEET, SheetSkeletonManagerService)?.getCurrentParam(),
+ getCurrentSkeleton: () => withCurrentTypeOfRenderer(
+ UniverInstanceType.UNIVER_SHEET,
+ SheetSkeletonManagerService,
+ this._univerInstanceService,
+ this._renderManagerService)?.getCurrentParam(),
});
this._usmToHtml = new USMToHtmlService();
diff --git a/packages/sheets-ui/src/services/editor-bridge.service.ts b/packages/sheets-ui/src/services/editor-bridge.service.ts
index def83bb746..6604909b66 100644
--- a/packages/sheets-ui/src/services/editor-bridge.service.ts
+++ b/packages/sheets-ui/src/services/editor-bridge.service.ts
@@ -188,7 +188,10 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ
return;
}
- const ru = this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET);
+ const currentSheet = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET);
+ if (!currentSheet) return;
+
+ const ru = this._renderManagerService.getRenderUnitById(currentSheet.getUnitId());
if (!ru) return;
const skeleton = ru.with(SheetSkeletonManagerService).getSkeletonParam(currentEditCell.sheetId)?.skeleton;
@@ -304,11 +307,12 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ
// eslint-disable-next-line max-lines-per-function
getLatestEditCellState() {
const currentEditCell = this._currentEditCell;
- if (currentEditCell == null) {
- return;
- }
+ if (currentEditCell == null) return;
- const ru = this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET);
+ const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
+ if (!workbook) return;
+
+ const ru = this._renderManagerService.getRenderUnitById(workbook.getUnitId());
if (!ru) return;
const skeleton = ru.with(SheetSkeletonManagerService).getCurrentSkeleton();
@@ -318,9 +322,7 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ
const { primary, unitId, sheetId, scene, engine } = currentEditCell;
const { startRow, startColumn } = primary;
const primaryWithCoord = attachPrimaryWithCoord(skeleton, primary);
- if (primaryWithCoord == null) {
- return;
- }
+ if (primaryWithCoord == null) return;
const actualRangeWithCoord = convertCellToRange(primaryWithCoord);
const canvasOffset = getCanvasOffsetByEngine(engine);
@@ -335,7 +337,6 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ
endX = convertTransformToOffsetX(endX, scaleX, scrollXY);
endY = convertTransformToOffsetY(endY, scaleY, scrollXY);
- const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const worksheet = workbook.getActiveSheet();
if (!worksheet) return;
@@ -357,7 +358,7 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ
documentLayoutObject = cell && skeleton.getCellDocumentModelWithFormula(cell);
- // Rewrite the cellValueType to STRING to avoid render the value on the right side when number type.
+ // Rewrite the cellValueType to STRING to avoid render the value on the right side when number type.
const renderConfig = documentLayoutObject?.documentModel?.documentStyle.renderConfig;
if (renderConfig != null) {
renderConfig.cellValueType = CellValueType.STRING;
@@ -376,7 +377,7 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ
}
documentLayoutObject = blankModel;
}
- // background of canvas is set to transparent, so if no bgcolor sepcified in curr cell, set it to white.
+ // background of canvas is set to transparent, so if no bgcolor sepcified in curr cell, set it to white.
documentLayoutObject.fill = documentLayoutObject.fill || '#fff';
documentLayoutObject.documentModel?.setZoomRatio(Math.max(scaleX, scaleY));
diff --git a/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts b/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts
index a3e4c9ab3b..3720717c7f 100644
--- a/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts
+++ b/packages/sheets-ui/src/services/mark-selection/mark-selection.service.ts
@@ -15,6 +15,7 @@
*/
import type { Workbook } from '@univerjs/core';
+import type { RenderUnit } from '@univerjs/engine-render';
import type { ISelectionWithStyle } from '@univerjs/sheets';
import { createIdentifier, Disposable, Inject, IUniverInstanceService, ThemeService, Tools, UniverInstanceType } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
@@ -112,10 +113,10 @@ export class MarkSelectionService extends Disposable implements IMarkSelectionSe
return;
}
- const renderUnit = this._renderManagerService.getRenderById(unitId);
+ const renderUnit = this._renderManagerService.getRenderById(unitId) as RenderUnit;
if (!renderUnit) return;
- const skeleton = this._renderManagerService.withCurrentTypeOfUnit(UniverInstanceType.UNIVER_SHEET, SheetSkeletonManagerService)?.getCurrentSkeleton();
+ const skeleton = renderUnit.with(SheetSkeletonManagerService).getCurrentSkeleton();
if (!skeleton) return;
const { scene } = renderUnit;
diff --git a/packages/sheets-ui/src/services/selection/selection-shape-extension.ts b/packages/sheets-ui/src/services/selection/selection-shape-extension.ts
index 6e96aa34df..00dc4bcc5e 100644
--- a/packages/sheets-ui/src/services/selection/selection-shape-extension.ts
+++ b/packages/sheets-ui/src/services/selection/selection-shape-extension.ts
@@ -18,11 +18,9 @@ import type { IFreeze, Injector, IRange, IRangeWithCoord, Nullable, ThemeService
import type { IMouseEvent, IPointerEvent, Scene, SpreadsheetSkeleton, Viewport } from '@univerjs/engine-render';
import type { ISelectionWithStyle } from '@univerjs/sheets';
import type { Subscription } from 'rxjs';
-
import type { SelectionControl } from './selection-control';
-import { ColorKit, Quantity, UniverInstanceType } from '@univerjs/core';
-
-import { CURSOR_TYPE, IRenderManagerService, Rect, ScrollTimer, ScrollTimerType, SHEET_VIEWPORT_KEY, Vector2 } from '@univerjs/engine-render';
+import { ColorKit, IUniverInstanceService, Quantity, UniverInstanceType } from '@univerjs/core';
+import { CURSOR_TYPE, IRenderManagerService, Rect, ScrollTimer, ScrollTimerType, SHEET_VIEWPORT_KEY, Vector2, withCurrentTypeOfRenderer } from '@univerjs/engine-render';
import { SELECTION_CONTROL_BORDER_BUFFER_WIDTH } from '@univerjs/sheets';
import { SheetSkeletonManagerService } from '../sheet-skeleton-manager.service';
import { ISheetSelectionRenderService } from './base-selection-render.service';
@@ -96,6 +94,7 @@ export class SelectionShapeExtension {
private readonly _themeService: ThemeService;
private readonly _injector: Injector;
private _selectionHooks: Record<string, () => void>;
+
constructor(
private _control: SelectionControl,
options: ISelectionShapeExtensionOption
@@ -127,8 +126,12 @@ export class SelectionShapeExtension {
}
private _getFreeze() {
- const renderManagerService = this._injector.get(IRenderManagerService);
- const freeze = renderManagerService.withCurrentTypeOfUnit(UniverInstanceType.UNIVER_SHEET, SheetSkeletonManagerService)
+ const freeze = withCurrentTypeOfRenderer(
+ UniverInstanceType.UNIVER_SHEET,
+ SheetSkeletonManagerService,
+ this._injector.get(IUniverInstanceService),
+ this._injector.get(IRenderManagerService)
+ )
?.getCurrentParam()
?.skeleton
.getWorksheetConfig()
diff --git a/packages/sheets-ui/src/services/sheets-render.service.ts b/packages/sheets-ui/src/services/sheets-render.service.ts
index 9ada7c566a..f487708c37 100644
--- a/packages/sheets-ui/src/services/sheets-render.service.ts
+++ b/packages/sheets-ui/src/services/sheets-render.service.ts
@@ -77,18 +77,9 @@ export class SheetsRenderService extends RxDisposable {
.subscribe((workbook) => this._createRenderer(workbook));
this._instanceSrv.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET)
.forEach((workbook) => this._createRenderer(workbook));
-
this._instanceSrv.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.UNIVER_SHEET)
.pipe(takeUntil(this.dispose$))
.subscribe((workbook) => this._disposeRenderer(workbook));
-
- this.disposeWithMe(
- this._instanceSrv.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET)
- .subscribe((workbook) => {
- if (workbook) {
- this._renderManagerService.setCurrent(workbook.getUnitId());
- }
- }));
}
private _createRenderer(workbook: Workbook): void {
diff --git a/packages/slides-ui/src/controllers/slide-editing.render-controller.ts b/packages/slides-ui/src/controllers/slide-editing.render-controller.ts
index 5079758a66..073d533244 100644
--- a/packages/slides-ui/src/controllers/slide-editing.render-controller.ts
+++ b/packages/slides-ui/src/controllers/slide-editing.render-controller.ts
@@ -65,6 +65,7 @@ import {
DeviceInputEventType,
FIX_ONE_PIXEL_BLUR_OFFSET,
fixLineWidthByScale,
+ getCurrentTypeOfRenderer,
IRenderManagerService,
Rect,
ScrollBar,
@@ -199,11 +200,9 @@ export class SlideEditingRenderController extends Disposable implements IRenderM
private _initialCursorSync(d: DisposableCollection) {
d.add(this._cellEditorManagerService.focus$.pipe(filter((f) => !!f)).subscribe(() => {
- const docSelectionRenderManager = this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC)?.with(DocSelectionRenderService);
-
- if (docSelectionRenderManager) {
- docSelectionRenderManager.sync();
- }
+ getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC, this._instanceSrv, this._renderManagerService)
+ ?.with(DocSelectionRenderService)
+ .sync();
}));
}
@@ -252,11 +251,9 @@ export class SlideEditingRenderController extends Disposable implements IRenderM
// ---> _focus$.next --> editingRenderController
// _textSelectionRenderManager.sync() --> _updateInputPosition --> activate(left, top)
- const docSelectionRenderManager = this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC)?.with(DocSelectionRenderService);
-
- if (docSelectionRenderManager) {
- docSelectionRenderManager.activate(HIDDEN_EDITOR_POSITION, HIDDEN_EDITOR_POSITION);
- }
+ getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC, this._instanceSrv, this._renderManagerService)
+ ?.with(DocSelectionRenderService)
+ .activate(HIDDEN_EDITOR_POSITION, HIDDEN_EDITOR_POSITION);
}));
}
diff --git a/packages/slides-ui/src/controllers/slide.render-controller.ts b/packages/slides-ui/src/controllers/slide.render-controller.ts
index 1b955c197d..925815a4ab 100644
--- a/packages/slides-ui/src/controllers/slide.render-controller.ts
+++ b/packages/slides-ui/src/controllers/slide.render-controller.ts
@@ -14,8 +14,12 @@
* limitations under the License.
*/
+import type { EventState, IColorStyle, IPageElement, ISlidePage, Nullable, SlideDataModel, UnitModel } from '@univerjs/core';
+import type { BaseObject, IRenderContext, IRenderModule, IWheelEvent } from '@univerjs/engine-render';
+import type { PageID } from '../type';
import { debounce, getColorStyle, Inject, Injector, IUniverInstanceService, RxDisposable, UniverInstanceType } from '@univerjs/core';
import {
+ getCurrentTypeOfRenderer,
IRenderManagerService,
Rect,
Scene,
@@ -24,12 +28,7 @@ import {
Viewport,
} from '@univerjs/engine-render';
import { ObjectProvider, SLIDE_KEY } from '@univerjs/slides';
-import type { EventState, IColorStyle, IPageElement, ISlidePage, Nullable, SlideDataModel, UnitModel } from '@univerjs/core';
-
-import type { BaseObject, IRenderContext, IRenderModule, IWheelEvent } from '@univerjs/engine-render';
-import type { PageID } from '../type';
-// export const ICanvasView = createIdentifier<IUniverInstanceService>('univer.slide.canvas-view');
export class SlideRenderController extends RxDisposable implements IRenderModule {
private _objectProvider: ObjectProvider | null = null;
@@ -106,7 +105,7 @@ export class SlideRenderController extends RxDisposable implements IRenderModule
//#endregion
ScrollBar.attachTo(viewMain);
- this._renderManagerService.setCurrent(unitId);
+ // this._renderManagerService.setCurrent(unitId);
// #region create slide
const slide = this._createSlide(scene);
@@ -136,10 +135,7 @@ export class SlideRenderController extends RxDisposable implements IRenderModule
}
private _currentRender() {
- const renderUnit = this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SLIDE);
- return renderUnit;
- // const slideDataModel = this._univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE)!;
- // return this._renderManagerService.getRenderById(slideDataModel.getUnitId());
+ return getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SLIDE, this._univerInstanceService, this._renderManagerService);
}
private _refreshThumb = debounce(() => {
diff --git a/packages/slides-ui/src/services/slide-render.service.ts b/packages/slides-ui/src/services/slide-render.service.ts
index 8df1340776..14f70dfb47 100644
--- a/packages/slides-ui/src/services/slide-render.service.ts
+++ b/packages/slides-ui/src/services/slide-render.service.ts
@@ -74,9 +74,6 @@ export class SlideRenderService extends RxDisposable {
}
this._renderManagerService.createRender(unitId);
-
- // NOTE@wzhudev: maybe not in univer mode
- this._renderManagerService.setCurrent(unitId);
}
private _disposeRenderer(workbook: Workbook): void {
diff --git a/packages/ui/README.md b/packages/ui/README.md
index 2694fbd2de..0e8a13375d 100644
--- a/packages/ui/README.md
+++ b/packages/ui/README.md
@@ -10,6 +10,10 @@
`@univerjs/ui` defines basic UI services and provides a set of desktop Workbench implementations based on React.
+Please note that this implementation can only display only 1 unit in the workbench. If you would like to embed units
+into a rich text document, or a mind node, you should implement a new UI plugin for that. Refer to our uniui
+plugin for an example.
+
## Usage
### Installation
diff --git a/packages/ui/src/controllers/ui/ui-desktop.controller.tsx b/packages/ui/src/controllers/ui/ui-desktop.controller.tsx
index e91d6d4c30..715b97e62b 100644
--- a/packages/ui/src/controllers/ui/ui-desktop.controller.tsx
+++ b/packages/ui/src/controllers/ui/ui-desktop.controller.tsx
@@ -14,14 +14,12 @@
* limitations under the License.
*/
-import type { IDisposable, UnitModel } from '@univerjs/core';
-import type { RenderUnit } from '@univerjs/engine-render';
+import type { IDisposable } from '@univerjs/core';
import type { IUniverUIConfig } from '../config.schema';
import type { IWorkbenchOptions } from './ui.controller';
-import { Disposable, Inject, Injector, isInternalEditorID, IUniverInstanceService, LifecycleService, LifecycleStages, Optional, toDisposable } from '@univerjs/core';
+import { Inject, Injector, IUniverInstanceService, LifecycleService, toDisposable } from '@univerjs/core';
import { render as createRoot, unmount } from '@univerjs/design';
import { IRenderManagerService } from '@univerjs/engine-render';
-import { filter, take } from 'rxjs';
import { ILayoutService } from '../../services/layout/layout.service';
import { IMenuManagerService } from '../../services/menu/menu-manager.service';
import { BuiltInUIPart, IUIPartsService } from '../../services/parts/parts.service';
@@ -31,97 +29,35 @@ import { CanvasPopup } from '../../views/components/popup/CanvasPopup';
import { Ribbon } from '../../views/components/ribbon/Ribbon';
import { DesktopWorkbench } from '../../views/workbench/Workbench';
import { menuSchema } from '../menus/menu.schema';
+import { SingleUnitUIController } from './ui-shared.controller';
-const STEADY_TIMEOUT = 3000;
-
-export class DesktopUIController extends Disposable {
- private _steadyTimeout: NodeJS.Timeout;
- private _renderTimeout: NodeJS.Timeout;
-
+export class DesktopUIController extends SingleUnitUIController {
constructor(
private readonly _config: IUniverUIConfig,
- @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
- @IUniverInstanceService private readonly _instanceSrv: IUniverInstanceService,
- @Inject(Injector) private readonly _injector: Injector,
- @Inject(LifecycleService) private readonly _lifecycleService: LifecycleService,
- @IUIPartsService private readonly _uiPartsService: IUIPartsService,
- @IMenuManagerService private readonly _menuManagerService: IMenuManagerService,
- @Optional(ILayoutService) private readonly _layoutService?: ILayoutService
+ @Inject(Injector) injector: Injector,
+ @Inject(LifecycleService) lifecycleService: LifecycleService,
+ @IRenderManagerService renderManagerService: IRenderManagerService,
+ @ILayoutService layoutService: ILayoutService,
+ @IUniverInstanceService instanceService: IUniverInstanceService,
+ @IMenuManagerService menuManagerService: IMenuManagerService,
+ @IUIPartsService uiPartsService: IUIPartsService
) {
- super();
-
- this._initBuiltinComponents();
- this._initMenus();
- this._bootstrapWorkbench();
- }
-
- private _initMenus(): void {
- this._menuManagerService.mergeMenu(menuSchema);
- }
-
- private _currentRenderId: string | null = null;
-
- private _bootstrapWorkbench(): void {
- this.disposeWithMe(this._instanceSrv.unitDisposed$.subscribe((_unit: UnitModel) => {
- clearTimeout(this._steadyTimeout);
- }));
+ super(injector, instanceService, layoutService, lifecycleService, renderManagerService);
- this.disposeWithMe(
- bootstrap(this._injector, this._config, (contentElement, containerElement) => {
- if (this._layoutService) {
- this.disposeWithMe(this._layoutService.registerRootContainerElement(containerElement));
- this.disposeWithMe(this._layoutService.registerContentElement(contentElement));
- }
+ menuManagerService.mergeMenu(menuSchema);
- // When current render changes, we need to update the render unit.
- this._renderManagerService.currentRender$.subscribe((renderId) => {
- if (renderId) {
- this._changeRenderUnit(renderId, contentElement);
- }
- });
-
- // First render.
- this.disposeWithMe(this._lifecycleService.lifecycle$.pipe(filter((stage) => stage === LifecycleStages.Ready), take(1)).subscribe(() => {
- this._renderTimeout = setTimeout(() => {
- const allRenders = this._renderManagerService.getRenderAll();
-
- for (const [key, render] of allRenders) {
- if (isInternalEditorID(key) || !((render) as RenderUnit).isRenderUnit) continue;
- this._changeRenderUnit(key, contentElement);
- break; // We only render the first renderer when bootstrapping.
- }
-
- this._lifecycleService.stage = LifecycleStages.Rendered;
- this._steadyTimeout = setTimeout(() => {
- this._lifecycleService.stage = LifecycleStages.Steady;
- }, STEADY_TIMEOUT);
- }, 300);
- }));
- })
- );
+ this._initBuiltinComponents(uiPartsService);
+ this._bootstrapWorkbench();
}
- private _changeRenderUnit(rendererId: string, contentElement: HTMLElement): void {
- if (this._currentRenderId === rendererId) return;
-
- const currentRenderer = this._currentRenderId ? this._renderManagerService.getRenderById(this._currentRenderId) : null;
- const renderer = this._renderManagerService.getRenderById(rendererId)!;
- if (!renderer.unitId || isInternalEditorID(renderer.unitId)) return;
-
- currentRenderer?.deactivate();
- currentRenderer?.engine.unmount();
-
- renderer.engine.mount(contentElement);
- renderer.activate();
-
- this._currentRenderId = rendererId;
+ override bootstrap(callback: (contentElement: HTMLElement, containerElement: HTMLElement) => void): IDisposable {
+ return bootstrap(this._injector, this._config, callback);
}
- private _initBuiltinComponents(): void {
- this.disposeWithMe(this._uiPartsService.registerComponent(BuiltInUIPart.FLOATING, () => connectInjector(CanvasPopup, this._injector)));
- // this.disposeWithMe(this._uiPartsService.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(ContentDOMPopup, this._injector)));
- this.disposeWithMe(this._uiPartsService.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(FloatDom, this._injector)));
- this.disposeWithMe(this._uiPartsService.registerComponent(BuiltInUIPart.TOOLBAR, () => connectInjector(Ribbon, this._injector)));
+ private _initBuiltinComponents(uiPartsService: IUIPartsService): void {
+ this.disposeWithMe(uiPartsService.registerComponent(BuiltInUIPart.FLOATING, () => connectInjector(CanvasPopup, this._injector)));
+ this.disposeWithMe(uiPartsService.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(FloatDom, this._injector)));
+ this.disposeWithMe(uiPartsService.registerComponent(BuiltInUIPart.TOOLBAR, () => connectInjector(Ribbon, this._injector)));
}
}
diff --git a/packages/ui/src/controllers/ui/ui-mobile.controller.tsx b/packages/ui/src/controllers/ui/ui-mobile.controller.tsx
index d87f0dc891..3e9d7498a7 100644
--- a/packages/ui/src/controllers/ui/ui-mobile.controller.tsx
+++ b/packages/ui/src/controllers/ui/ui-mobile.controller.tsx
@@ -17,75 +17,40 @@
import type { IDisposable } from '@univerjs/core';
import type { IUniverUIConfig } from '../config.schema';
import type { IUIController, IWorkbenchOptions } from './ui.controller';
-import { Disposable, Inject, Injector, IUniverInstanceService, LifecycleService, LifecycleStages, Optional, toDisposable, UniverInstanceType } from '@univerjs/core';
+import { Inject, Injector, IUniverInstanceService, LifecycleService, toDisposable } from '@univerjs/core';
import { render as createRoot, unmount } from '@univerjs/design';
import { IRenderManagerService } from '@univerjs/engine-render';
import { ILayoutService } from '../../services/layout/layout.service';
-import { IMenuManagerService } from '../../services/menu/menu-manager.service';
import { BuiltInUIPart, IUIPartsService } from '../../services/parts/parts.service';
import { connectInjector } from '../../utils/di';
import { FloatDom } from '../../views/components/dom/FloatDom';
import { CanvasPopup } from '../../views/components/popup/CanvasPopup';
import { MobileWorkbench } from '../../views/mobile-workbench/MobileWorkbench';
-import { menuSchema } from '../menus/menu.schema';
+import { SingleUnitUIController } from './ui-shared.controller';
-const STEADY_TIMEOUT = 3000;
-
-export class MobileUIController extends Disposable implements IUIController {
+export class MobileUIController extends SingleUnitUIController implements IUIController {
constructor(
private readonly _config: IUniverUIConfig,
- @IUniverInstanceService private readonly _instanceService: IUniverInstanceService,
- @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
- @Inject(Injector) private readonly _injector: Injector,
- @Inject(LifecycleService) private readonly _lifecycleService: LifecycleService,
- @IUIPartsService private readonly _uiPartsService: IUIPartsService,
- @IMenuManagerService private readonly _menuManagerService: IMenuManagerService,
- @Optional(ILayoutService) private readonly _layoutService?: ILayoutService
+ @Inject(Injector) injector: Injector,
+ @Inject(LifecycleService) lifecycleService: LifecycleService,
+ @IRenderManagerService renderManagerService: IRenderManagerService,
+ @ILayoutService layoutService: ILayoutService,
+ @IUniverInstanceService instanceService: IUniverInstanceService,
+ @IUIPartsService uiPartsService: IUIPartsService
) {
- super();
-
- this._initBuiltinComponents();
+ super(injector, instanceService, layoutService, lifecycleService, renderManagerService);
- Promise.resolve().then(() => this._bootstrapWorkbench());
+ this._initBuiltinComponents(uiPartsService);
+ this._bootstrapWorkbench();
}
- private _initMenus(): void {
- this._menuManagerService.mergeMenu(menuSchema);
- }
-
- private _bootstrapWorkbench(): void {
- this.disposeWithMe(
- bootstrap(this._injector, this._config, (canvasElement, containerElement) => {
- if (this._layoutService) {
- this.disposeWithMe(this._layoutService.registerRootContainerElement(containerElement));
- this.disposeWithMe(this._layoutService.registerContentElement(canvasElement as HTMLCanvasElement));
- }
-
- this._renderManagerService.currentRender$.subscribe((renderId) => {
- if (renderId) {
- const render = this._renderManagerService.getRenderById(renderId)!;
- if (!render.unitId) return;
-
- const unitType = this._instanceService.getUnitType(render.unitId);
- if (unitType !== UniverInstanceType.UNIVER_SHEET) return;
-
- render.engine.setContainer(canvasElement);
- }
- });
-
- setTimeout(() => {
- const engine = this._renderManagerService.getFirst()?.engine;
- engine?.setContainer(canvasElement);
- this._lifecycleService.stage = LifecycleStages.Rendered;
- setTimeout(() => this._lifecycleService.stage = LifecycleStages.Steady, STEADY_TIMEOUT);
- }, 300);
- })
- );
+ override bootstrap(callback: (contentElement: HTMLElement, containerElement: HTMLElement) => void): IDisposable {
+ return bootstrap(this._injector, this._config, callback);
}
- private _initBuiltinComponents() {
- this.disposeWithMe(this._uiPartsService.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(CanvasPopup, this._injector)));
- this.disposeWithMe(this._uiPartsService.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(FloatDom, this._injector)));
+ private _initBuiltinComponents(uiPartsService: IUIPartsService) {
+ this.disposeWithMe(uiPartsService.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(CanvasPopup, this._injector)));
+ this.disposeWithMe(uiPartsService.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(FloatDom, this._injector)));
}
}
diff --git a/packages/ui/src/controllers/ui/ui-shared.controller.ts b/packages/ui/src/controllers/ui/ui-shared.controller.ts
new file mode 100644
index 0000000000..f5ee0c617a
--- /dev/null
+++ b/packages/ui/src/controllers/ui/ui-shared.controller.ts
@@ -0,0 +1,101 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IDisposable, Injector, IUniverInstanceService, LifecycleService } from '@univerjs/core';
+import type { IRenderManagerService } from '@univerjs/engine-render';
+import type { ILayoutService } from '../../services/layout/layout.service';
+import { Disposable, isInternalEditorID, LifecycleStages } from '@univerjs/core';
+
+const STEADY_TIMEOUT = 3000;
+
+export abstract class SingleUnitUIController extends Disposable {
+ protected _steadyTimeout: number;
+ protected _renderTimeout: number;
+
+ constructor(
+ protected readonly _injector: Injector,
+ protected readonly _instanceService: IUniverInstanceService,
+ protected readonly _layoutService: ILayoutService,
+ protected readonly _lifecycleService: LifecycleService,
+ protected readonly _renderManagerService: IRenderManagerService
+ ) {
+ super();
+ }
+
+ override dispose(): void {
+ super.dispose();
+
+ clearTimeout(this._steadyTimeout);
+ clearTimeout(this._renderTimeout);
+ }
+
+ protected _bootstrapWorkbench() {
+ this.disposeWithMe(this.bootstrap(async (contentElement, containerElement) => {
+ if (this._layoutService) {
+ this.disposeWithMe(this._layoutService.registerRootContainerElement(containerElement));
+ this.disposeWithMe(this._layoutService.registerContentElement(contentElement));
+ }
+
+ await this._lifecycleService.onStage(LifecycleStages.Ready);
+ this._renderTimeout = window.setTimeout(() => {
+ // First render
+ const allRenders = this._renderManagerService.getRenderAll();
+ for (const [key, render] of allRenders) {
+ if (this._changeRenderUnit(key, contentElement)) break;
+ }
+
+ // Render as focus shifts
+ this.disposeWithMe(this._instanceService.focused$.subscribe((unit) => {
+ if (unit) this._changeRenderUnit(unit, contentElement);
+ }));
+
+ // When renderer created, check if matches the focused.
+ this.disposeWithMe(this._renderManagerService.created$.subscribe((renderer) => {
+ if (renderer.unitId === this._instanceService.getFocusedUnit()?.getUnitId()) this._changeRenderUnit(renderer.unitId, contentElement);
+ }));
+
+ this.disposeWithMe(this._renderManagerService.disposed$.subscribe((renderer) => {
+ if (this._currentRenderId === renderer) this._currentRenderId = null;
+ }));
+
+ this._lifecycleService.stage = LifecycleStages.Rendered;
+ this._steadyTimeout = window.setTimeout(() => {
+ this._lifecycleService.stage = LifecycleStages.Steady;
+ }, STEADY_TIMEOUT);
+ }, 300);
+ }));
+ }
+
+ private _currentRenderId: string | null = null;
+ private _changeRenderUnit(rendererId: string, contentElement: HTMLElement): boolean {
+ if (this._currentRenderId === rendererId) return false;
+
+ const renderer = this._renderManagerService.getRenderById(rendererId)!;
+ if (!renderer || !renderer.unitId || isInternalEditorID(renderer.unitId)) return false;
+
+ const currentRenderer = this._currentRenderId
+ ? this._renderManagerService.getRenderById(this._currentRenderId)
+ : null;
+ currentRenderer?.deactivate();
+ currentRenderer?.engine.unmount();
+ renderer.engine.mount(contentElement);
+ renderer.activate();
+ this._currentRenderId = rendererId;
+ return true;
+ }
+
+ abstract bootstrap(callback: (contentElement: HTMLElement, containerElement: HTMLElement) => void): IDisposable;
+}