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.

11.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, JSONXActions, Nullable } from '@univerjs/core';
import type { IRichTextEditingMutationParams } from '../commands/mutations/core-editing.mutation';
import type { IDocStateChangeInfo, IDocStateChangeParams } from './doc-state-emit.service';
import {
    createIdentifier,
    ICommandService,
    Inject,
    IUndoRedoService,
    IUniverInstanceService,
    JSONX,
    Optional,
    RedoCommandId,
    RxDisposable,
    UndoCommandId,
    UniverInstanceType,
} from '@univerjs/core';
import { BehaviorSubject, takeUntil } from 'rxjs';
import { DocStateEmitService } from './doc-state-emit.service';

type ChangeStateCacheType = 'history' | 'collaboration';

const DEBOUNCE_DELAY = 300;

interface IStateCache {
    history: IDocStateChangeParams[];
    collaboration: IDocStateChangeParams[];
}

export interface IDocStateChangeInterceptorService {
    transformChangeStateInfo(changeStateInfo: IDocStateChangeInfo): Nullable<IDocStateChangeInfo>;
}

export const IDocStateChangeInterceptorService = createIdentifier<IDocStateChangeInterceptorService>('doc.state-change-interceptor-service');

// This class sends out state-changing events, what the state is, the data model,
// and cursor & selection information. It mainly serves history and collaboration.
export class DocStateChangeManagerService extends RxDisposable {
    private readonly _docStateChange$ = new BehaviorSubject<Nullable<IDocStateChangeParams>>(null);
    readonly docStateChange$ = this._docStateChange$.asObservable();
    // This cache used for history compose.
    private _historyStateCache: Map<string, IDocStateChangeParams[]> = new Map();
    // This cache used for collaboration state compose.
    private _changeStateCache: Map<string, IDocStateChangeParams[]> = new Map();
    private _historyTimer: Nullable<ReturnType<typeof setTimeout>> = null;
    private _changeStateCacheTimer: Nullable<ReturnType<typeof setTimeout>> = null;

    constructor(
        @Optional(IUndoRedoService) private _undoRedoService: Nullable<IUndoRedoService>,
        @ICommandService private readonly _commandService: ICommandService,
        @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
        @Inject(DocStateEmitService) private readonly _docStateEmitService: DocStateEmitService,
        @Optional(IDocStateChangeInterceptorService) private readonly _docStateChangeInterceptorService?: IDocStateChangeInterceptorService
    ) {
        super();

        this._initialize();
        this._listenDocStateChange();
    }

    override dispose(): void {
        if (this._historyTimer != null) {
            clearTimeout(this._historyTimer);
            this._historyTimer = null;
        }
        if (this._changeStateCacheTimer != null) {
            clearTimeout(this._changeStateCacheTimer);
            this._changeStateCacheTimer = null;
        }
        this._historyStateCache.clear();
        this._changeStateCache.clear();
        this._docStateChange$.complete();
        super.dispose();
    }

    getStateCache(unitId: string) {
        return {
            history: this._historyStateCache.get(unitId) ?? [],
            collaboration: this._changeStateCache.get(unitId) ?? [],
        };
    }

    setStateCache(unitId: string, cache: IStateCache) {
        this._historyStateCache.set(unitId, cache.history);
        this._changeStateCache.set(unitId, cache.collaboration);
    }

    clearHistory(unitId: string): void {
        this._historyStateCache.delete(unitId);
    }

    flushPendingChanges(unitId: string): void {
        this._pushHistory(unitId);
        this._emitChangeState(unitId);
    }

    private _setChangeState(changeState: IDocStateChangeParams) {
        this._cacheChangeState(changeState, 'history');
        // Mutations by user or historyService need collaboration.
        this._cacheChangeState(changeState, 'collaboration');
    }

    private _initialize() {
        this.disposeWithMe(
            this._commandService.beforeCommandExecuted((command) => {
                if (command.id === UndoCommandId || command.id === RedoCommandId) {
                    const univerDoc = this._univerInstanceService.getCurrentUnitOfType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);
                    if (univerDoc == null) {
                        return;
                    }

                    const unitId = univerDoc.getUnitId();

                    this._pushHistory(unitId);
                    this._emitChangeState(unitId);
                }
            })
        );
    }

    private _listenDocStateChange() {
        this._docStateEmitService.docStateChangeParams$.pipe(takeUntil(this.dispose$)).subscribe((changeStateInfo) => {
            if (changeStateInfo == null) {
                return;
            }

            const interceptedChangeStateInfo = this._docStateChangeInterceptorService?.transformChangeStateInfo(changeStateInfo) ?? changeStateInfo;
            if (interceptedChangeStateInfo == null) {
                return;
            }

            if (interceptedChangeStateInfo.isSync) {
                return;
            }

            const { isCompositionEnd: _isCompositionEnd, isSync: _isSync, syncer: _syncer, ...changeState } = interceptedChangeStateInfo;

            this._setChangeState(changeState);
        });
    }

    private _cacheChangeState(changeState: IDocStateChangeParams, type: ChangeStateCacheType = 'history') {
        const { trigger, unitId, noHistory, debounce = false } = changeState;
        if (noHistory || (type === 'history' && trigger == null)) {
            return;
        }

        if (type === 'history' && (trigger === RedoCommandId || trigger === UndoCommandId)) {
            return;
        }

        const stateCache = type === 'history'
            ? this._historyStateCache
            : this._changeStateCache;

        const cb = type === 'history'
            ? this._pushHistory.bind(this)
            : this._emitChangeState.bind(this);

        const pendingStates = stateCache.get(unitId);
        // Switching segments ends the current debounce group so body, header, and footer edits keep separate history entries.
        if (pendingStates?.length && pendingStates[pendingStates.length - 1].segmentId !== changeState.segmentId) {
            cb(unitId);
        }

        if (stateCache.has(unitId)) {
            const cacheStates = stateCache.get(unitId);

            cacheStates?.push(changeState);
        } else {
            stateCache.set(unitId, [changeState]);
        }

        if (debounce) {
            if (type === 'history') {
                if (this._historyTimer) {
                    clearTimeout(this._historyTimer);
                }

                this._historyTimer = setTimeout(() => {
                    cb(unitId);
                }, DEBOUNCE_DELAY);
            } else {
                if (this._changeStateCacheTimer) {
                    clearTimeout(this._changeStateCacheTimer);
                }

                this._changeStateCacheTimer = setTimeout(() => {
                    cb(unitId);
                }, DEBOUNCE_DELAY);
            }
        } else {
            cb(unitId);
        }
    }

    private _pushHistory(unitId: string) {
        const undoRedoService = this._undoRedoService;
        const cacheStates = this._historyStateCache.get(unitId);

        if (undoRedoService == null || !Array.isArray(cacheStates) || cacheStates.length === 0) {
            return;
        }

        const len = cacheStates.length;
        // Use the first state.commandId as commandId, because we will only have one core mutation type.
        const commandId = cacheStates[0].commandId;

        const firstState = cacheStates[0];
        const lastState = cacheStates[len - 1];

        const redoParams: IRichTextEditingMutationParams = {
            unitId,
            trigger: RedoCommandId,
            ...(lastState.redoState.layoutState === undefined ? {} : { layoutState: lastState.redoState.layoutState }),
            actions: cacheStates.reduce((acc, cur) => JSONX.compose(acc, cur.redoState.actions), null as JSONXActions),
            textRanges: lastState.redoState.textRanges,
            segmentId: lastState.segmentId,
            options: lastState.redoState.options,
            isEditing: lastState.redoState.isEditing,
        };

        const undoParams: IRichTextEditingMutationParams = {
            unitId,
            trigger: UndoCommandId,
            ...(firstState.undoState.layoutState === undefined ? {} : { layoutState: firstState.undoState.layoutState }),
            // Always need to put undoParams after redoParams, because `reverse` will change the `cacheStates` order.
            actions: cacheStates.reverse().reduce((acc, cur) => JSONX.compose(acc, cur.undoState.actions), null as JSONXActions),
            textRanges: firstState.undoState.textRanges,
            segmentId: firstState.segmentId,
            options: firstState.undoState.options,
            isEditing: firstState.undoState.isEditing,
        };

        undoRedoService.pushUndoRedo({
            unitID: unitId,
            undoMutations: [{ id: commandId, params: undoParams }],
            redoMutations: [{ id: commandId, params: redoParams }],
        });

        // Empty the cacheState.
        cacheStates.length = 0;
    }

    private _emitChangeState(unitId: string) {
        const cacheStates = this._changeStateCache.get(unitId);

        if (!Array.isArray(cacheStates) || cacheStates.length === 0) {
            return;
        }

        const len = cacheStates.length;
        // Use the first state.commandId as commandId, because we will only have one core mutation type.
        const { commandId, trigger, segmentId, noHistory, debounce } = cacheStates[0];

        const firstState = cacheStates[0];
        const lastState = cacheStates[len - 1];

        const redoState: IRichTextEditingMutationParams = {
            unitId,
            actions: cacheStates.reduce((acc, cur) => JSONX.compose(acc, cur.redoState.actions), null as JSONXActions),
            textRanges: lastState.redoState.textRanges,
            segmentId: lastState.segmentId,
            options: lastState.redoState.options,
            isEditing: lastState.redoState.isEditing,
        };

        const undoState: IRichTextEditingMutationParams = {
            unitId,
            // Always need to put undoParams after redoParams, because `reverse` will change the `cacheStates` order.
            actions: cacheStates.reverse().reduce((acc, cur) => JSONX.compose(acc, cur.undoState.actions), null as JSONXActions),
            textRanges: firstState.undoState.textRanges,
            segmentId: firstState.segmentId,
            options: firstState.undoState.options,
            isEditing: firstState.undoState.isEditing,
        };

        const changeState: IDocStateChangeParams = {
            commandId,
            unitId,
            trigger,
            redoState,
            undoState,
            segmentId,
            noHistory,
            debounce,
        };

        // Empty the cacheState.
        cacheStates.length = 0;

        this._docStateChange$.next(changeState);
    }
}