Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(docs-ui): refactor to RenderUnit #2539

Open
wants to merge 22 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: move doc skeleton manager into render unit
  • Loading branch information
wzhudev committed Jun 17, 2024
commit a270e8043d5325648476e3ed7aee8c3fe37753b4
11 changes: 3 additions & 8 deletions examples/src/docs/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
*/

/* eslint-disable node/prefer-global/process */

import { LocaleType, Univer, UniverInstanceType } from '@univerjs/core';
import { defaultTheme } from '@univerjs/design';
import { UniverDocsPlugin } from '@univerjs/docs';
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { UniverUIPlugin } from '@univerjs/ui';
import { UniverDrawingPlugin } from '@univerjs/drawing';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverDebuggerPlugin } from '@univerjs/debugger';
import { UniverDrawingUIPlugin } from '@univerjs/drawing-ui';
import { UniverDocsDrawingPlugin } from '@univerjs/docs-drawing';
import { UniverDocsDrawingUIPlugin } from '@univerjs/docs-drawing-ui';
import { DEFAULT_DOCUMENT_DATA_CN } from '../data';
import { enUS, ruRU, zhCN } from '../locales';

Expand Down Expand Up @@ -58,7 +55,7 @@ univer.registerPlugin(UniverUIPlugin, {
container: 'app',
footer: false,
});
univer.registerPlugin(UniverDrawingPlugin);

univer.registerPlugin(UniverDocsPlugin);
univer.registerPlugin(UniverDocsUIPlugin, {
container: 'univerdoc',
Expand All @@ -69,9 +66,7 @@ univer.registerPlugin(UniverDocsUIPlugin, {
},
});

univer.registerPlugin(UniverDrawingUIPlugin);
univer.registerPlugin(UniverDocsDrawingPlugin);
univer.registerPlugin(UniverDocsDrawingUIPlugin);
// univer.registerPlugin(UniverDocsDrawingUIPlugin);

univer.createUnit(UniverInstanceType.UNIVER_DOC, DEFAULT_DOCUMENT_DATA_CN);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import type { ICommandInfo, Nullable, Workbook } from '@univerjs/core';
import { checkForSubstrings, Disposable, ICommandService, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import { Inject } from '@wendellhu/redi';
import { IRenderManagerService, ITextSelectionRenderManager, ScrollBar } from '@univerjs/engine-render';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import { CoverContentCommand, DocSkeletonManagerService, RichTextEditingMutation, VIEWPORT_KEY } from '@univerjs/docs';
Expand All @@ -29,12 +28,10 @@ export class DocEditorBridgeController extends Disposable {

constructor(
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,
@IEditorService private readonly _editorService: IEditorService,
@ICommandService private readonly _commandService: ICommandService,
@ITextSelectionRenderManager private readonly _textSelectionRenderManager: ITextSelectionRenderManager,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService

) {
super();
this._initialize();
Expand Down Expand Up @@ -78,10 +75,13 @@ export class DocEditorBridgeController extends Disposable {
return;
}

const skeleton = this._docSkeletonManagerService.getSkeletonByUnitId(unitId)?.skeleton;

const editorDataModel = this._univerInstanceService.getUniverDocInstance(unitId);
if (!editorDataModel) {
return;
}

const skeleton = this._renderManagerService.getRenderById(editorDataModel.getUnitId())
?.with(DocSkeletonManagerService).getSkeleton()?.skeleton;
if (editor == null || editor.render == null || skeleton == null || editorDataModel == null) {
return;
}
Expand Down
117 changes: 15 additions & 102 deletions packages/docs-ui/src/controllers/doc-floating-object.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,29 @@
* limitations under the License.
*/

import type { ICommandInfo } from '@univerjs/core';
import type { DocumentDataModel, ICommandInfo } from '@univerjs/core';
import {
BooleanNumber,
DEFAULT_DOCUMENT_SUB_COMPONENT_ID,
Disposable,
ICommandService,
LifecycleStages,
OnLifecycle,
PositionedObjectLayoutType,
} from '@univerjs/core';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import { DocSkeletonManagerService, RichTextEditingMutation, SetDocZoomRatioOperation } from '@univerjs/docs';
import type { Documents, DocumentSkeleton, IRender } from '@univerjs/engine-render';
import { IRenderManagerService, Liquid } from '@univerjs/engine-render';
import type { Documents, DocumentSkeleton, IRenderContext, IRenderModule } from '@univerjs/engine-render';
import { Liquid } from '@univerjs/engine-render';
import { IEditorService } from '@univerjs/ui';
import { Inject } from '@wendellhu/redi';

@OnLifecycle(LifecycleStages.Steady, DocFloatingObjectController)
export class DocFloatingObjectController extends Disposable {
export class DocFloatingObjectRenderController extends Disposable implements IRenderModule {
private _liquid = new Liquid();

private _pageMarginCache = new Map<string, { marginLeft: number; marginTop: number }>();

constructor(
private readonly _context: IRenderContext<DocumentDataModel>,
@Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
@ICommandService private readonly _commandService: ICommandService,
@IEditorService private readonly _editorService: IEditorService
) {
Expand All @@ -51,81 +48,12 @@ export class DocFloatingObjectController extends Disposable {
}

private _initialize() {
this._initialRenderRefresh();

this._updateOnPluginChange();
}

private _updateOnPluginChange() {
// this._drawingManagerService.pluginUpdate$.subscribe((params) => {
// const docsSkeletonObject = this._docSkeletonManagerService.getCurrent();

// if (docsSkeletonObject == null) {
// return;
// }

// const { unitId, skeleton } = docsSkeletonObject;

// const currentRender = this._renderManagerService.getRenderById(unitId);

// if (currentRender == null) {
// return;
// }

// const { mainComponent, components, scene } = currentRender;

// const docsComponent = mainComponent as Documents;

// const { left: docsLeft, top: docsTop } = docsComponent;

// params.forEach((param) => {
// const { unitId, subUnitId, drawingId, drawing } = param;

// const { left = 0, top = 0, width = 0, height = 0, angle, flipX, flipY, skewX, skewY } = drawing;

// const cache = this._pageMarginCache.get(drawingId);

// const marginLeft = cache?.marginLeft || 0;
// const marginTop = cache?.marginTop || 0;

// skeleton
// ?.getViewModel()
// .getDataModel()
// .updateDrawing(drawingId, {
// left: left - docsLeft - marginLeft,
// top: top - docsTop - marginTop,
// height,
// width,
// });
// });

// skeleton?.calculate();
// mainComponent?.makeDirty();
// });
}

private _initialRenderRefresh() {
this._docSkeletonManagerService.currentSkeleton$.subscribe((param) => {
if (param == null) {
this._docSkeletonManagerService.currentSkeleton$.subscribe((skeleton) => {
if (skeleton == null) {
return;
}

const { skeleton: documentSkeleton, unitId } = param;

const currentRender = this._renderManagerService.getRenderById(unitId);

if (currentRender == null) {
return;
}

const { mainComponent } = currentRender;

const docsComponent = mainComponent as Documents;

// TODO: Why NEED change skeleton here?
docsComponent.changeSkeleton(documentSkeleton);

this._refreshDrawing(unitId, documentSkeleton, currentRender);
this._refreshDrawing(skeleton);
});
}

Expand All @@ -136,43 +64,28 @@ export class DocFloatingObjectController extends Disposable {
this._commandService.onCommandExecuted((command: ICommandInfo) => {
if (updateCommandList.includes(command.id)) {
const params = command.params as IRichTextEditingMutationParams;
const { unitId: commandUnitId } = params;

const docsSkeletonObject = this._docSkeletonManagerService.getCurrent();
const { unitId } = params;

if (docsSkeletonObject == null) {
return;
}

const { unitId, skeleton } = docsSkeletonObject;

if (commandUnitId !== unitId) {
return;
}

const currentRender = this._renderManagerService.getRenderById(unitId);

if (currentRender == null) {
const skeleton = this._docSkeletonManagerService.getSkeleton();
if (!skeleton) {
return;
}

if (this._editorService.isEditor(unitId)) {
currentRender.mainComponent?.makeDirty();
this._context.mainComponent?.makeDirty();
return;
}

this._refreshDrawing(unitId, skeleton, currentRender);

// this.calculatePagePosition(currentRender);
this._refreshDrawing(skeleton);
}
})
);
}

private _refreshDrawing(unitId: string, skeleton: DocumentSkeleton, currentRender: IRender) {
private _refreshDrawing(skeleton: DocumentSkeleton) {
const skeletonData = skeleton?.getSkeletonData();

const { mainComponent, scene } = currentRender;
const { mainComponent, scene, unitId } = this._context;

const documentComponent = mainComponent as Documents;

Expand Down
2 changes: 1 addition & 1 deletion packages/docs-ui/src/controllers/drawing.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class DocFloatingObjectController extends Disposable {
const params = command.params as IRichTextEditingMutationParams;
const { unitId: commandUnitId } = params;

const docsSkeletonObject = this._docSkeletonManagerService.getCurrent();
const docsSkeletonObject = this._docSkeletonManagerService.getSkeleton();

if (docsSkeletonObject == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class DocBackScrollRenderController extends RxDisposable implements IRend
private _scrollToSelection(unitId: string) {
const activeTextRange = this._textSelectionManagerService.getActiveRange();
const docObject = neoGetDocObject(this._context);
const skeleton = this._docSkeletonManagerService.getCurrent()?.skeleton;
const skeleton = this._docSkeletonManagerService.getSkeleton()?.skeleton;

if (activeTextRange == null || docObject == null || skeleton == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type { DocumentDataModel, EventState, ICommandInfo, Nullable } from '@univerjs/core';
import { ICommandService, IConfigService, RxDisposable } from '@univerjs/core';
import type { IDocSkeletonManagerParam, IRichTextEditingMutationParams } from '@univerjs/docs';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import { DOCS_COMPONENT_BACKGROUND_LAYER_INDEX, DOCS_COMPONENT_DEFAULT_Z_INDEX, DOCS_COMPONENT_HEADER_LAYER_INDEX, DOCS_COMPONENT_MAIN_LAYER_INDEX, DOCS_VIEW_KEY, DocSkeletonManagerService, RichTextEditingMutation, VIEWPORT_KEY } from '@univerjs/docs';
import type { DocumentSkeleton, IRenderContext, IRenderModule, IWheelEvent } from '@univerjs/engine-render';
import { DocBackground, Documents, EVENT_TYPE, Layer, PageLayoutType, ScrollBar, Viewport } from '@univerjs/engine-render';
Expand Down Expand Up @@ -130,21 +130,20 @@ export class DocRenderController extends RxDisposable implements IRenderModule {
});
}

private _create(param: Nullable<IDocSkeletonManagerParam>) {
if (!param) {
private _create(skeleton: Nullable<DocumentSkeleton>) {
if (!skeleton) {
return;
}

const { skeleton: documentSkeleton } = param;
const { mainComponent, components } = this._context;

const docsComponent = mainComponent as Documents;
const docBackground = components.get(DOCS_VIEW_KEY.BACKGROUND) as DocBackground;

docsComponent.changeSkeleton(documentSkeleton);
docBackground.changeSkeleton(documentSkeleton);
docsComponent.changeSkeleton(skeleton);
docBackground.changeSkeleton(skeleton);

this._recalculateSizeBySkeleton(documentSkeleton);
this._recalculateSizeBySkeleton(skeleton);
}

private _initCommandListener() {
Expand All @@ -156,13 +155,11 @@ export class DocRenderController extends RxDisposable implements IRenderModule {
const params = command.params as IRichTextEditingMutationParams;
const { unitId } = params;

const docsSkeletonObject = this._docSkeletonManagerService.getSkeletonByUnitId(unitId);
if (docsSkeletonObject == null) {
const skeleton = this._docSkeletonManagerService.getSkeleton();
if (!skeleton) {
return;
}

const { skeleton } = docsSkeletonObject;

// TODO: `disabled` is only used for read only demo, and will be removed in the future.
const disabled = !!skeleton.getViewModel().getDataModel().getSnapshot().disabled;
if (disabled) {
Expand Down