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.

5.4 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 { ICreateUnitOptions, IDisposable, Workbook } from '@univerjs/core';
import {
    IContextService,
    Inject,
    IUniverInstanceService,
    RxDisposable,
    ThemeService,
    toDisposable,
    UniverInstanceType,
} from '@univerjs/core';
import { IRenderManagerService, RENDER_RAW_FORMULA_KEY, Spreadsheet } from '@univerjs/engine-render';
import { distinctUntilChanged, takeUntil } from 'rxjs';

const SHEET_MAIN_CANVAS_ID = 'univer-sheet-main-canvas';

/**
 * This controller is responsible for managing units of a specific kind (UniverSheet) to be rendered on the canvas.
 */
export class SheetsRenderService extends RxDisposable {
    private _skeletonChangeMutations = new Set<string>();
    private readonly _selfRenderingMutations = new Map<string, number>();

    constructor(
        @IContextService private readonly _contextService: IContextService,
        @IUniverInstanceService private readonly _instanceSrv: IUniverInstanceService,
        @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
        @Inject(ThemeService) private readonly _themeService: ThemeService
    ) {
        super();

        // It should be initialized after all other plugins are ready.
        Promise.resolve().then(() => this._init());
    }

    /**
     * Register a mutation id that will trigger the skeleton change.
     *
     * @param mutationId the id of the mutation
     * @returns a disposable to unregister the mutation
     */
    registerSkeletonChangingMutations(mutationId: string): IDisposable {
        if (this._skeletonChangeMutations.has(mutationId)) {
            return toDisposable(() => { });
        }

        this._skeletonChangeMutations.add(mutationId);
        return toDisposable(() => this._skeletonChangeMutations.delete(mutationId));
    }

    /**
     * Examine if a mutation would make the skeleton to change.
     */
    checkMutationShouldTriggerRerender(id: string): boolean {
        return this._skeletonChangeMutations.has(id);
    }

    /** The owning plugin must invalidate the affected sheets when its derived view changes. */
    registerSelfRenderingMutations(mutationId: string): IDisposable {
        this._selfRenderingMutations.set(mutationId, (this._selfRenderingMutations.get(mutationId) ?? 0) + 1);
        return toDisposable(() => {
            const remaining = (this._selfRenderingMutations.get(mutationId) ?? 1) - 1;
            if (remaining) {
                this._selfRenderingMutations.set(mutationId, remaining);
            } else {
                this._selfRenderingMutations.delete(mutationId);
            }
        });
    }

    shouldAutomaticallyRenderMutation(mutationId: string): boolean {
        return !this._selfRenderingMutations.has(mutationId);
    }

    private _init() {
        this._initWorkbookListener();
        this._initContextListener();
    }

    private _initWorkbookListener(): void {
        this._instanceSrv.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.UNIVER_SHEET)
            .pipe(takeUntil(this.dispose$))
            .subscribe((event) => this._createRenderer(event.unit, event.options));
        this._instanceSrv.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET)
            .forEach((workbook) => this._createRenderer(workbook, this._instanceSrv.getUnitCreateOptions(workbook.getUnitId()) ?? undefined));
        this._instanceSrv.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.UNIVER_SHEET)
            .pipe(takeUntil(this.dispose$))
            .subscribe((workbook) => this._disposeRenderer(workbook));
    }

    private _createRenderer(workbook: Workbook, createUnitOptions?: ICreateUnitOptions): void {
        if (createUnitOptions?.skipAutoRender) {
            return;
        }

        const unitId = workbook.getUnitId();
        const renderer = this._renderManagerService.createRender(unitId, createUnitOptions);
        const canvas = renderer.engine.getCanvas();
        canvas.setId(`${SHEET_MAIN_CANVAS_ID}_${unitId}`);
        canvas.getCanvasEle().dataset.uUnitId = unitId;
        canvas.getContext().setId(`${SHEET_MAIN_CANVAS_ID}_${unitId}`);
    }

    private _disposeRenderer(workbook: Workbook): void {
        const unitId = workbook.getUnitId();
        this._renderManagerService.removeRender(unitId);
    }

    private _initContextListener(): void {
        // If we toggle the raw formula, we need to refresh all spreadsheets.
        this.disposeWithMe(this._contextService.subscribeContextValue$(RENDER_RAW_FORMULA_KEY)
            .pipe(distinctUntilChanged())
            .subscribe(() => {
                this._renderManagerService.getRenderAll().forEach((renderer) => {
                    if (renderer.mainComponent instanceof Spreadsheet) {
                        (renderer.mainComponent as Spreadsheet).makeForceDirty(true);
                    }
                });
            }));
    }
}