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.

6.6 KB
Raw Blame
/**
 * 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 { DocumentDataModel, Nullable } from '@univerjs/core';
import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
import {
    DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
    DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
    DocumentFlavor,
    IConfigService,
    Inject,
    isInternalEditorID,
    IUniverInstanceService,
    LocaleService,
    RxDisposable,
    SHEET_TEXT_LINE_GAP,
    UniverInstanceType,
} from '@univerjs/core';
import { DocumentSkeleton, DocumentViewModel, getDocumentLayoutPresentation, SheetCellDocumentViewModel } from '@univerjs/engine-render';
import { BehaviorSubject, takeUntil } from 'rxjs';
import { DocLayoutExecutorService } from './doc-layout-executor.service';

/**
 * 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();

    // CurrentSkeletonBefore for pre-triggered logic during registration
    private readonly _currentSkeletonBefore$ = new BehaviorSubject<Nullable<DocumentSkeleton>>(null);
    readonly currentSkeletonBefore$ = this._currentSkeletonBefore$.asObservable();

    private readonly _currentViewModel$ = new BehaviorSubject<Nullable<DocumentViewModel>>(null);
    readonly currentViewModel$ = this._currentViewModel$.asObservable();

    constructor(
        private readonly _context: Pick<IRenderContext<DocumentDataModel>, 'type' | 'unit' | 'unitId'>,
        @Inject(LocaleService) private readonly _localeService: LocaleService,
        @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
        @Inject(DocLayoutExecutorService) private readonly _docLayoutExecutorService: DocLayoutExecutorService,
        @IConfigService private readonly _configService: IConfigService
    ) {
        super();

        this._init();

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

    override dispose(): void {
        super.dispose();

        this._currentSkeletonBefore$.complete();
        this._currentSkeleton$.complete();
    }

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

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

    getLayoutPerformanceMetrics(): ReturnType<DocLayoutExecutorService['getPerformanceMetrics']> {
        return this._docLayoutExecutorService.getPerformanceMetrics(this._context.unitId);
    }

    supportsIncrementalLayout(): boolean {
        const documentFlavor = this._context.unit.getSnapshot().documentStyle?.documentFlavor;

        return this._docLayoutExecutorService.getExecutor() != null &&
            !isInternalEditorID(this._context.unitId) &&
            !getDocumentLayoutPresentation(this._context.unit) &&
            (documentFlavor === DocumentFlavor.TRADITIONAL || documentFlavor === DocumentFlavor.MODERN);
    }

    recalculate(): DocumentSkeleton {
        const skeleton = this._skeleton;
        skeleton.calculate();
        this._publishSkeleton(skeleton);
        return skeleton;
    }

    private _publishSkeleton(skeleton: DocumentSkeleton): void {
        this._currentSkeletonBefore$.next(skeleton);
        this._currentSkeleton$.next(skeleton);
    }

    private _init() {
        const documentDataModel = this._context.unit;
        this._update(documentDataModel);
    }

    private _update(documentDataModel: DocumentDataModel) {
        const unitId = this._context.unitId;

        // No need to build view model, if data model has no body.
        if (documentDataModel.getBody() == null) {
            return;
        }

        // Always need to reset document data model, because cell editor change doc instance every time.
        if (this._docViewModel && isInternalEditorID(unitId)) {
            this._docViewModel.reset(documentDataModel);

            this._context.unit = documentDataModel;
        } else if (!this._docViewModel) {
            this._docViewModel = this._buildDocViewModel(documentDataModel);
        }

        if (!this._skeleton) {
            this._skeleton = this._buildSkeleton(this._docViewModel);
        }

        if (this.supportsIncrementalLayout()) {
            // The optional layout executor owns interactive progressive layout. Consumers that
            // do not register it keep the historical synchronous initialization and editing path.
            this._publishSkeleton(this._skeleton);
        } else {
            // Internal editors, UNSPECIFIED documents, and applications without the optional
            // executor depend on a complete skeleton immediately for caret/input geometry.
            this.recalculate();
        }

        // sub: packages/docs/src/services/doc-interceptor/doc-interceptor.service.ts
        this._currentViewModel$.next(this._docViewModel);
    }

    private _buildSkeleton(documentViewModel: DocumentViewModel) {
        const isSheetEditor = this._context.unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY ||
            this._context.unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY;
        return DocumentSkeleton.create(documentViewModel, this._localeService, {
            lineGap: isSheetEditor ? this._configService.getConfig<number>(SHEET_TEXT_LINE_GAP) : undefined,
        });
    }

    private _buildDocViewModel(documentDataModel: DocumentDataModel) {
        return this._context.unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY || this._context.unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY
            ? new SheetCellDocumentViewModel(documentDataModel)
            : new DocumentViewModel(documentDataModel);
    }
}