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: refactor doc view model
  • Loading branch information
wzhudev committed Jun 18, 2024
commit a55b62d6fe78ed19d4b2286b430eb4022ef6289c
3 changes: 1 addition & 2 deletions examples/src/sheets/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import type { IUniverRPCMainThreadConfig } from '@univerjs/rpc';
import { UniverRPCMainThreadPlugin } from '@univerjs/rpc';
import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';
import { UniverSheetsNumfmtPlugin } from '@univerjs/sheets-numfmt';
import { UniverSheetsZenEditorPlugin } from '@univerjs/sheets-zen-editor';
import { UniverSheetsDataValidationPlugin } from '@univerjs/sheets-data-validation';
import { UniverSheetsDrawingUIPlugin } from '@univerjs/sheets-drawing-ui';
import { FUniver } from '@univerjs/facade';
Expand Down Expand Up @@ -76,7 +75,7 @@ univer.registerPlugin(UniverSheetsUIPlugin);
// sheet feature plugins

univer.registerPlugin(UniverSheetsNumfmtPlugin);
univer.registerPlugin(UniverSheetsZenEditorPlugin);
// univer.registerPlugin(UniverSheetsZenEditorPlugin);
univer.registerPlugin(UniverFormulaEnginePlugin, {
notExecuteFormula: true,
});
Expand Down
10 changes: 7 additions & 3 deletions packages/docs-ui/src/services/docs-render.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,21 @@ export class DocsRenderService extends RxDisposable {
this._instanceSrv.getAllUnitsForType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC)
.forEach((documentModel) => this._createRenderer(documentModel));

// TODO@wzhudev: maybe we should rollback here later
this._instanceSrv.getTypeOfUnitDisposed$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC)
.pipe(takeUntil(this.dispose$))
.subscribe((doc) => this._disposeRenderer(doc));
}

private _createRenderer(doc: DocumentDataModel) {
const unitId = doc.getUnitId();
this._createRenderWithId(unitId);

// NOTE@wzhudev: maybe not in univer mode
this._renderManagerService.setCurrent(unitId);
if (!this._renderManagerService.has(unitId)) {
this._createRenderWithId(unitId);

// NOTE@wzhudev: maybe not in univer mode
this._renderManagerService.setCurrent(unitId);
}
}

private _createRenderWithId(unitId: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';

import { DocViewModelManagerService } from '../../../services/doc-view-model-manager.service';
import { TextSelectionManagerService } from '../../../services/text-selection-manager.service';
import { DocStateChangeManagerService } from '../../../services/doc-state-change-manager.service';
import { IMEInputManagerService } from '../../../services/ime-input-manager.service';
Expand Down Expand Up @@ -104,7 +103,6 @@ export function createCommandTestBed(workbookData?: IDocumentData, dependencies?

override onStarting(injector: Injector): void {
injector.add([TextSelectionManagerService]);
injector.add([DocViewModelManagerService]);
injector.add([DocStateChangeManagerService]);
injector.add([IMEInputManagerService]);
injector.add([
Expand Down
17 changes: 8 additions & 9 deletions packages/docs/src/commands/mutations/core-editing.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

import { CommandType, IUniverInstanceService, JSONX } from '@univerjs/core';
import type { IMutation, IMutationCommonParams, JSONXActions, Nullable } from '@univerjs/core';
import type { ITextRangeWithStyle } from '@univerjs/engine-render';
import { DocViewModelManagerService } from '../../services/doc-view-model-manager.service';
import { IRenderManagerService, type ITextRangeWithStyle } from '@univerjs/engine-render';
import { serializeTextRange, TextSelectionManagerService } from '../../services/text-selection-manager.service';
import type { IDocStateChangeParams } from '../../services/doc-state-change-manager.service';
import { DocStateChangeManagerService } from '../../services/doc-state-change-manager.service';
import { IMEInputManagerService } from '../../services/ime-input-manager.service';
import { DocSkeletonManagerService } from '../../services/doc-skeleton-manager.service';

export interface IRichTextEditingMutationParams extends IMutationCommonParams {
unitId: string;
Expand Down Expand Up @@ -59,10 +59,13 @@ export const RichTextEditingMutation: IMutation<IRichTextEditingMutationParams,
noNeedSetTextRange,
} = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
const documentDataModel = univerInstanceService.getUniverDocInstance(unitId);
const renderManagerService = accessor.get(IRenderManagerService);

const docViewModelManagerService = accessor.get(DocViewModelManagerService);
const documentViewModel = docViewModelManagerService.getViewModel(unitId);
const documentDataModel = univerInstanceService.getUniverDocInstance(unitId);
const documentViewModel = renderManagerService.getRenderById(unitId)?.with(DocSkeletonManagerService).getViewModel();
if (documentDataModel == null || documentViewModel == null) {
throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${unitId}`);
}

const textSelectionManagerService = accessor.get(TextSelectionManagerService);
const selections = textSelectionManagerService.getSelections() ?? [];
Expand All @@ -73,10 +76,6 @@ export const RichTextEditingMutation: IMutation<IRichTextEditingMutationParams,

const imeInputManagerService = accessor.get(IMEInputManagerService);

if (documentDataModel == null || documentViewModel == null) {
throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${unitId}`);
}

// TODO: `disabled` is only used for read only demo, and will be removed in the future.
const disabled = !!documentDataModel.getSnapshot().disabled;

Expand Down
2 changes: 0 additions & 2 deletions packages/docs/src/doc-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import { SetTextSelectionsOperation } from './commands/operations/text-selection
import { IMEInputController } from './controllers/ime-input.controller';
import { MoveCursorController } from './controllers/move-cursor.controller';
import { NormalInputController } from './controllers/normal-input.controller';
import { DocViewModelManagerService } from './services/doc-view-model-manager.service';
import { IMEInputManagerService } from './services/ime-input-manager.service';
import { TextSelectionManagerService } from './services/text-selection-manager.service';
import { DocStateChangeManagerService } from './services/doc-state-change-manager.service';
Expand Down Expand Up @@ -129,7 +128,6 @@ export class UniverDocsPlugin extends Plugin {
(
[
// services
[DocViewModelManagerService],
[DocStateChangeManagerService],
[IMEInputManagerService],
[
Expand Down
1 change: 0 additions & 1 deletion packages/docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export {
} from './commands/operations/text-selection.operation';
export { type IUniverDocsConfig, UniverDocsPlugin } from './doc-plugin';
export { DocSkeletonManagerService } from './services/doc-skeleton-manager.service';
export { DocViewModelManagerService } from './services/doc-view-model-manager.service';
export { TextSelectionManagerService, serializeTextRange } from './services/text-selection-manager.service';
export { DocStateChangeManagerService, type IDocStateChangeParams } from './services/doc-state-change-manager.service';
export { IMEInputManagerService } from './services/ime-input-manager.service';
Expand Down
70 changes: 42 additions & 28 deletions packages/docs/src/services/doc-skeleton-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
*/

import type { DocumentDataModel, Nullable } from '@univerjs/core';
import { LocaleService, RxDisposable } from '@univerjs/core';
import type { DocumentViewModel, IRenderContext, IRenderModule } from '@univerjs/engine-render';
import { DocumentSkeleton } from '@univerjs/engine-render';
import { DOCS_NORMAL_EDITOR_UNIT_ID_KEY, IUniverInstanceService, LocaleService, RxDisposable, UniverInstanceType } from '@univerjs/core';
import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
import { DocumentSkeleton, DocumentViewModel } from '@univerjs/engine-render';
import { Inject } from '@wendellhu/redi';
import { BehaviorSubject, takeUntil } from 'rxjs';

import { DocViewModelManagerService } from './doc-view-model-manager.service';

/**
* This service is for document build and manage doc skeletons.
* This service is for document build and manage doc skeletons. It also manages
* DocumentViewModels.
*/
export class DocSkeletonManagerService extends RxDisposable implements IRenderModule {
private _skeleton: DocumentSkeleton;
private _docViewModel: DocumentViewModel;

private readonly _currentSkeleton$ = new BehaviorSubject<Nullable<DocumentSkeleton>>(null);
readonly currentSkeleton$ = this._currentSkeleton$.asObservable();
Expand All @@ -39,11 +39,19 @@ export class DocSkeletonManagerService extends RxDisposable implements IRenderMo
constructor(
private readonly _context: IRenderContext<DocumentDataModel>,
@Inject(LocaleService) private readonly _localeService: LocaleService,
@Inject(DocViewModelManagerService) private readonly _docViewModelManagerService: DocViewModelManagerService
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService
) {
super();

this._init();
this._update();

this._univerInstanceService.getCurrentTypeOfUnit$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC)
.pipe(takeUntil(this.dispose$))
.subscribe((documentModel) => {
if (documentModel?.getUnitId() === this._context.unitId) {
this._update();
}
});
}

override dispose(): void {
Expand All @@ -53,30 +61,24 @@ export class DocSkeletonManagerService extends RxDisposable implements IRenderMo
this._currentSkeleton$.complete();
}

private _init() {
// TODO@wzhudev: useless when we move doc view model manager service to render unit
this._docViewModelManagerService.currentDocViewModel$
.pipe(takeUntil(this.dispose$))
.subscribe((docViewModel) => {
if (!docViewModel) {
return;
}

this._setCurrent(docViewModel.docViewModel);
});
private _update() {
const documentDataModel = this._context.unit;
const unitId = this._context.unitId;

this._docViewModelManagerService.getAllModel().forEach((docViewModel) => {
this._setCurrent(docViewModel.docViewModel);
});
}
// No need to build view model, if data model has no body.
if (documentDataModel.getBody() == null) {
return;
}

getSkeleton(): DocumentSkeleton {
return this._skeleton;
}
// Always need to reset document data model, because cell editor change doc instance every time.
if (this._docViewModel && unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY) {
this._docViewModel.reset(documentDataModel);
} else if (!this._docViewModel) {
this._docViewModel = this._buildDocViewModel(documentDataModel);
}

private _setCurrent(docViewModel: DocumentViewModel): Nullable<DocumentSkeleton> {
if (!this._skeleton) {
this._skeleton = this._buildSkeleton(docViewModel);
this._skeleton = this._buildSkeleton(this._docViewModel);
}

const skeleton = this._skeleton;
Expand All @@ -88,7 +90,19 @@ export class DocSkeletonManagerService extends RxDisposable implements IRenderMo
return this.getSkeleton();
}

getSkeleton(): DocumentSkeleton {
return this._skeleton;
}

getViewModel(): DocumentViewModel {
return this._docViewModel;
}

private _buildSkeleton(documentViewModel: DocumentViewModel) {
return DocumentSkeleton.create(documentViewModel, this._localeService);
}

private _buildDocViewModel(documentDataModel: DocumentDataModel) {
return new DocumentViewModel(documentDataModel);
}
}
1 change: 1 addition & 0 deletions packages/engine-render/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export class Engine extends ThinEngine<Scene> {

override dispose() {
super.dispose();

const eventPrefix = getPointerPrefix();
const canvasEle = this.getCanvasElement();
canvasEle.removeEventListener(`${eventPrefix}leave`, this._pointerLeaveEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
UniverInstanceType,
} from '@univerjs/core';
import {
DocViewModelManagerService,
DocSkeletonManagerService,
MoveCursorOperation,
ReplaceContentCommand,
TextSelectionManagerService,
Expand Down Expand Up @@ -168,7 +168,6 @@ export class PromptController extends Disposable {
@Inject(ISelectionRenderService) private readonly _selectionRenderService: ISelectionRenderService,
@Inject(IDescriptionService) private readonly _descriptionService: IDescriptionService,
@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,
@Inject(DocViewModelManagerService) private readonly _docViewModelManagerService: DocViewModelManagerService,
@IContextMenuService private readonly _contextMenuService: IContextMenuService,
@IEditorService private readonly _editorService: IEditorService
) {
Expand Down Expand Up @@ -1340,13 +1339,11 @@ export class PromptController extends Disposable {
const documentDataModel = this._univerInstanceService.getCurrentUniverDocInstance();

const editorUnitId = documentDataModel!.getUnitId();

if (!this._editorService.isEditor(editorUnitId)) {
return;
}

const docViewModel = this._docViewModelManagerService.getViewModel(editorUnitId);

const docViewModel = this._renderManagerService.getRenderById(editorUnitId)?.with(DocSkeletonManagerService).getViewModel();
if (docViewModel == null || documentDataModel == null) {
return;
}
Expand Down