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.

12.1 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 { IDisposable, IRange, Nullable } from '@univerjs/core';
import type { IOtherFormulaMarkDirtyParams } from '../commands/mutations/formula.mutation';
import type { ISetFormulaCalculationResultMutation } from '../commands/mutations/set-formula-calculation.mutation';
import type {
    IRemoveOtherFormulaMutationParams,
    ISetOtherFormulaMutationParams,
} from '../commands/mutations/set-other-formula.mutation';
import type { IOtherFormulaResult } from './formula-common';
import {
    Disposable,
    generateRandomId,
    ICommandService,
    Inject,
    LifecycleService,
    LifecycleStages,
    ObjectMatrix,
    toDisposable,
} from '@univerjs/core';
import { Subject } from 'rxjs';
import { OtherFormulaMarkDirty } from '../commands/mutations/formula.mutation';
import { SetFormulaCalculationResultMutation } from '../commands/mutations/set-formula-calculation.mutation';
import { RemoveOtherFormulaMutation, SetOtherFormulaMutation } from '../commands/mutations/set-other-formula.mutation';
import { IActiveDirtyManagerService } from './active-dirty-manager.service';
import { FormulaResultStatus } from './formula-common';

export enum OtherFormulaBizType {
    DEFAULT = 'default',
    DATA_VALIDATION = 'dv',
    DATA_VALIDATION_CUSTOM = 'dv-custom',
    CONDITIONAL_FORMATTING = 'cf',
    DOC = 'doc',
    SLIDE = 'slide',
    SHAPE = 'shape',
}

export class RegisterOtherFormulaService extends Disposable {
    private _formulaCacheMap: Map<string, Map<string, Map<string, IOtherFormulaResult>>> = new Map();

    private _mutationSyncHandler?: (unitId: string) => () => Promise<void>;

    private _formulaChangeWithRange$ = new Subject<{ unitId: string; subUnitId: string; formulaText: string; formulaId: string; ranges: IRange[] }>();
    public formulaChangeWithRange$ = this._formulaChangeWithRange$.asObservable();

    // FIXME: this design could be improved.
    private _formulaResult$ = new Subject<Record<string, Record<string, IOtherFormulaResult[]>>>();
    public formulaResult$ = this._formulaResult$.asObservable();

    private _otherFormulaResultApplied$ = new Subject<ISetFormulaCalculationResultMutation>();
    public otherFormulaResultApplied$ = this._otherFormulaResultApplied$.asObservable();

    constructor(
        @ICommandService private readonly _commandService: ICommandService,
        @IActiveDirtyManagerService private _activeDirtyManagerService: IActiveDirtyManagerService,
        @Inject(LifecycleService) private readonly _lifecycleService: LifecycleService
    ) {
        super();
        this._initFormulaRegister();
        this._initFormulaCalculationResultChange();
    }

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

        this._formulaChangeWithRange$.complete();
        this._formulaResult$.complete();
        this._otherFormulaResultApplied$.complete();
    }

    private _ensureCacheMap(unitId: string, subUnitId: string) {
        let unitMap = this._formulaCacheMap.get(unitId);

        if (!unitMap) {
            unitMap = new Map();
            this._formulaCacheMap.set(unitId, unitMap);
        }

        let subUnitMap = unitMap.get(subUnitId);

        if (!subUnitMap) {
            subUnitMap = new Map();
            unitMap.set(subUnitId, subUnitMap);
        }

        return subUnitMap;
    }

    private _createFormulaId(unitId: string, subUnitId: string, bizType: OtherFormulaBizType, bizId: string) {
        return `formula.${unitId}_${subUnitId}_${bizType}_${bizId}_${generateRandomId(8)}`;
    }

    private _initFormulaRegister() {
        this._activeDirtyManagerService.register(
            OtherFormulaMarkDirty.id,
            {
                commandId: OtherFormulaMarkDirty.id,
                getDirtyData(commandInfo) {
                    const params = commandInfo.params as IOtherFormulaMarkDirtyParams;
                    return {
                        dirtyUnitOtherFormulaMap: params,
                    };
                },
            }
        );

        const handleRegister = (option: { unitId: string; subUnitId: string; formulaText: string; formulaId: string; ranges: IRange[] }) => {
            const { unitId, subUnitId, formulaText, formulaId, ranges } = option;
            const cacheMap = this._ensureCacheMap(unitId, subUnitId);
            // formula already deleted
            if (!cacheMap.has(formulaId)) {
                return;
            }

            const params: ISetOtherFormulaMutationParams = {
                unitId,
                subUnitId,
                formulaMap: {
                    [formulaId]: {
                        f: formulaText,
                        ranges,
                    },
                },
            };
            const waitForMutationSync = this._mutationSyncHandler?.(unitId);

            this._commandService.executeCommand(SetOtherFormulaMutation.id, params, { onlyLocal: true }).then(async () => {
                if (this._disposed) return;
                await waitForMutationSync?.();
                if (this._disposed) return;
                this._commandService.executeCommand(
                    OtherFormulaMarkDirty.id,
                    { [unitId]: { [subUnitId]: { [formulaId]: true } } },
                    { onlyLocal: true }
                );
            });
        };

        this.disposeWithMe(this._formulaChangeWithRange$.subscribe((option) => {
            if (
                this._commandService.hasCommand(SetOtherFormulaMutation.id)
                && this._commandService.hasCommand(OtherFormulaMarkDirty.id)
            ) {
                handleRegister(option);
                return;
            }

            this._lifecycleService.onStage(LifecycleStages.Ready).then(() => {
                if (!this._disposed) {
                    handleRegister(option);
                }
            });
        }));
    }

    setMutationSyncHandler(handler: (unitId: string) => () => Promise<void>): IDisposable {
        this._mutationSyncHandler = handler;
        return toDisposable(() => {
            if (this._mutationSyncHandler === handler) this._mutationSyncHandler = undefined;
        });
    }

    private _initFormulaCalculationResultChange() {
        // Gets the result of the formula calculation and caches it
        this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
            if (commandInfo.id === SetFormulaCalculationResultMutation.id) {
                const params = commandInfo.params as ISetFormulaCalculationResultMutation;

                const { unitOtherData } = params;
                const results: Record<string, Record<string, IOtherFormulaResult[]>> = {};
                for (const unitId in unitOtherData) {
                    const unitData = unitOtherData[unitId];
                    const unitResults: Record<string, IOtherFormulaResult[]> = {};
                    results[unitId] = unitResults;
                    for (const subUnitId in unitData) {
                        const cacheMap = this._ensureCacheMap(unitId, subUnitId);
                        const subUnitData = unitData[subUnitId];
                        const subUnitResults: IOtherFormulaResult[] = [];
                        unitResults[subUnitId] = subUnitResults;
                        for (const formulaId in subUnitData) {
                            const current = subUnitData[formulaId];
                            if (cacheMap.has(formulaId)) {
                                const item = cacheMap.get(formulaId);

                                if (!item) {
                                    continue;
                                }

                                if (!item.result) {
                                    item.result = {};
                                }

                                const resultMatrix = new ObjectMatrix(current);
                                const resultObject = new ObjectMatrix(item.result);

                                resultMatrix.forValue((row, col, value) => {
                                    resultObject.setValue(row, col, value);
                                });

                                item.status = FormulaResultStatus.SUCCESS;
                                item.callbacks.forEach((callback) => {
                                    callback(current);
                                });
                                item.callbacks.clear();
                                subUnitResults.push(item);
                            }
                        }
                    }
                }
                this._formulaResult$.next(results);
                this._otherFormulaResultApplied$.next(params);
            }
        }));
    }

    registerFormulaWithRange(unitId: string, subUnitId: string, formulaText: string, ranges: IRange[] = [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 }], extra?: Record<string, any>, bizType: OtherFormulaBizType = OtherFormulaBizType.DEFAULT, bizId: string = '') {
        const formulaId = this._createFormulaId(unitId, subUnitId, bizType, bizId);
        const cacheMap = this._ensureCacheMap(unitId, subUnitId);

        cacheMap.set(formulaId, {
            result: undefined,
            status: FormulaResultStatus.WAIT,
            formulaId,
            callbacks: new Set(),
            extra,
        });

        this._formulaChangeWithRange$.next({
            unitId,
            subUnitId,
            formulaText,
            formulaId,
            ranges,
        });
        return formulaId;
    }

    getFormulaDirtyMap(unitId: string): Record<string, Record<string, boolean>> {
        const unitMap = this._formulaCacheMap.get(unitId);
        if (!unitMap) {
            return {};
        }

        return Object.fromEntries(
            Array.from(unitMap.entries(), ([subUnitId, formulas]) => [
                subUnitId,
                Object.fromEntries(Array.from(formulas.keys(), (formulaId) => [formulaId, true])),
            ])
        );
    }

    deleteFormula(unitId: string, subUnitId: string, formulaIdList: string[]) {
        const params: IRemoveOtherFormulaMutationParams = {
            unitId,
            subUnitId,
            formulaIdList,
        };
        this._mutationSyncHandler?.(unitId);
        this._commandService.executeCommand(RemoveOtherFormulaMutation.id, params, { onlyLocal: true });
        const cacheMap = this._ensureCacheMap(unitId, subUnitId);
        formulaIdList.forEach((id) => cacheMap.delete(id));
    }

    getFormulaValue(unitId: string, subUnitId: string, formulaId: string): Promise<Nullable<IOtherFormulaResult>> {
        const cacheMap = this._ensureCacheMap(unitId, subUnitId);
        const item = cacheMap.get(formulaId);
        if (!item) {
            return Promise.resolve(null);
        }

        if (item.status === FormulaResultStatus.SUCCESS || item.status === FormulaResultStatus.ERROR) {
            return Promise.resolve(item);
        }

        return new Promise((resolve) => {
            item.callbacks.add(() => {
                resolve(cacheMap.get(formulaId));
            });
        });
    }

    getFormulaValueSync(unitId: string, subUnitId: string, formulaId: string): Nullable<IOtherFormulaResult> {
        const cacheMap = this._ensureCacheMap(unitId, subUnitId);
        return cacheMap.get(formulaId);
    }

    markFormulaDirty(unitId: string, subUnitId: string, formulaId: string) {
        const cache = this.getFormulaValueSync(unitId, subUnitId, formulaId);
        if (!cache) return;
        cache.status = FormulaResultStatus.WAIT;
        this._commandService.executeCommand(
            OtherFormulaMarkDirty.id,
            { [unitId]: { [subUnitId]: { [formulaId]: true } } },
            { onlyLocal: true }
        );
    }
}