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.

19.9 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, IPosition, ISelectionCell, Nullable, Workbook } from '@univerjs/core';
import type { Engine, IDocumentLayoutObject, Scene } from '@univerjs/engine-render';
import type { KeyCode } from '@univerjs/ui';
import type { Observable } from 'rxjs';
import {
    CellValueType,
    convertCellToRange,
    createIdentifier,
    Disposable,
    DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
    EDITOR_ACTIVATED,
    FOCUSING_EDITOR_STANDALONE,
    IContextService,
    Inject,
    IUniverInstanceService,
    SheetTypes,
    ThemeService,
    toDisposable,
    UniverInstanceType,
} from '@univerjs/core';
import { getCanvasOffsetByEngine, IEditorService } from '@univerjs/docs-ui';
import {
    convertTextRotation,
    convertTransformToOffsetX,
    convertTransformToOffsetY,
    DeviceInputEventType,
    IRenderManagerService,
} from '@univerjs/engine-render';
import {
    attachPrimaryWithCoord,
    BEFORE_CELL_EDIT,
    SheetInterceptorService,
    SheetSkeletonService,
} from '@univerjs/sheets';
import { DISABLE_AUTO_FOCUS_KEY } from '@univerjs/ui';
import { BehaviorSubject, map, switchMap } from 'rxjs';
import { getViewportByCell } from '../common/utils';

export interface IEditorBridgeServiceVisibleParam {
    visible: boolean;
    eventType: DeviceInputEventType;
    unitId: string;
    keycode?: KeyCode;
    isShift?: boolean;
    initialValue?: string;
    pointerPosition?: { x: number; y: number };
}

export interface ICurrentEditCellParam {
    scene: Scene;
    engine: Engine;
    unitId: string;
    sheetId: string;
    primary: ISelectionCell;
}

export interface ICellEditorState {
    unitId: string;
    sheetId: string;
    row: number;
    column: number;
    documentLayoutObject: IDocumentLayoutObject;
    editorUnitId: string;
    isInArrayFormulaRange?: Nullable<boolean>;
    isPercentFormat?: boolean;
}

export interface ICellEditorLayout {
    position: IPosition;
    canvasOffset: { left: number; top: number };
    scaleX: number;
    scaleY: number;
}

export interface IEditorBridgeServiceParam extends ICellEditorState, ICellEditorLayout {

}

export interface IEditorBridgeService {
    currentEditCellState$: Observable<Nullable<ICellEditorState>>;
    currentEditCellLayout$: Observable<Nullable<ICellEditorLayout>>;
    currentEditCell$: Observable<Nullable<IEditorBridgeServiceParam>>;
    visible$: Observable<IEditorBridgeServiceVisibleParam>;
    forceKeepVisible$: Observable<boolean>;

    dispose(): void;
    refreshEditCellState(): void;
    refreshEditCellPosition(resetSizeOnly?: boolean): void;
    setEditCell(param: ICurrentEditCellParam): void;
    getEditCellState(): Readonly<Nullable<IEditorBridgeServiceParam>>;
    getEditCellLayout(): Readonly<Nullable<ICellEditorLayout>>;
    getEditLocation(): Readonly<Nullable<ICellEditorState>>;
    updateEditLocation(row: number, col: number): void;
    // Gets the DocumentDataModel of the latest table cell based on the latest cell contents
    getLatestEditCellState(): Readonly<Nullable<IEditorBridgeServiceParam>>;
    /**
     * Do not use it directly. Use the `SetCellEditVisibleOperation` command instead.
     */
    changeVisible(param: IEditorBridgeServiceVisibleParam): void;
    /**
     * Mark the open editor content as pending submission. Besides user edits, an external write that
     * replaces the edited cell's content is marked too, so the draft still wins when editing ends.
     */
    changeEditorDirty(dirtyStatus: boolean): void;
    getEditorDirty(): boolean;
    isVisible(): IEditorBridgeServiceVisibleParam;
    enableForceKeepVisible(): void;
    disableForceKeepVisible(): void;
    isForceKeepVisible(): boolean;
    getCurrentEditorId(): string;
    helpFunctionVisible$: BehaviorSubject<boolean>;
}

export class EditorBridgeService extends Disposable implements IEditorBridgeService, IDisposable {
    private _editorUnitId: string = DOCS_NORMAL_EDITOR_UNIT_ID_KEY;
    private _editorIsDirty: boolean = false;

    private _visibleParams: IEditorBridgeServiceVisibleParam = {
        visible: false,
        eventType: DeviceInputEventType.Dblclick,
        unitId: '',
    };

    private _currentEditCell: Nullable<ICurrentEditCellParam> = null;
    private _currentEditCellState: Nullable<ICellEditorState> = null;
    private _currentEditCellLayout: Nullable<ICellEditorLayout> = null;

    helpFunctionVisible$ = new BehaviorSubject(true);

    // TODO: @weird94 this should split into to subjects, documentDataModel & position
    private readonly _currentEditCellState$ = new BehaviorSubject<Nullable<ICellEditorState>>(null);
    readonly currentEditCellState$ = this._currentEditCellState$.asObservable();

    private readonly _currentEditCellLayout$ = new BehaviorSubject<Nullable<ICellEditorLayout>>(null);
    readonly currentEditCellLayout$ = this._currentEditCellLayout$.asObservable();

    readonly currentEditCell$ = this._currentEditCellState$.pipe(
        switchMap((editCellState) => this._currentEditCellLayout$.pipe(map((layout) => (editCellState && layout ? { ...editCellState, ...layout } : null))))
    );

    private readonly _visibleParams$ = new BehaviorSubject<IEditorBridgeServiceVisibleParam>(this._visibleParams);
    readonly visible$ = this._visibleParams$.asObservable();

    private readonly _afterVisibleParams$ = new BehaviorSubject<IEditorBridgeServiceVisibleParam>(this._visibleParams);
    readonly afterVisible$ = this._afterVisibleParams$.asObservable();

    private readonly _forceKeepVisible$ = new BehaviorSubject(false);
    readonly forceKeepVisible$ = this._forceKeepVisible$.asObservable();

    constructor(
        @Inject(SheetInterceptorService) private readonly _sheetInterceptorService: SheetInterceptorService,
        @Inject(SheetSkeletonService) private readonly _sheetSkeletonService: SheetSkeletonService,
        @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
        @Inject(ThemeService) private readonly _themeService: ThemeService,
        @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
        @IEditorService private readonly _editorService: IEditorService,
        @IContextService private readonly _contextService: IContextService
    ) {
        super();

        this.disposeWithMe(toDisposable(() => {
            this._currentEditCellState$.complete();
            this._currentEditCell = null;
        }));

        this.disposeWithMe(this._univerInstanceService.getTypeOfUnitDisposed$(UniverInstanceType.UNIVER_SHEET).subscribe((unit) => {
            if (unit.getUnitId() === this._currentEditCellState?.unitId) {
                this._clearCurrentEditCellState();
            }
        }));
    }

    refreshEditCellState() {
        const editCellState = this.getLatestEditCellState();
        if (!editCellState) {
            this._currentEditCellState = null;
            this._currentEditCellLayout = null;
            this._currentEditCellState$.next(null);
            this._currentEditCellLayout$.next(null);
            return;
        }
        const { position, scaleX, scaleY, canvasOffset, ...rest } = editCellState;
        this._currentEditCellState = rest;
        this._currentEditCellLayout = { position, scaleX, scaleY, canvasOffset };
        this._currentEditCellState$.next(this._currentEditCellState);
        this._currentEditCellLayout$.next(this._currentEditCellLayout);
    }

    refreshEditCellPosition(resetSizeOnly?: boolean) {
        if (!this._currentEditCell || !this._currentEditCellState) {
            return;
        }

        const { unitId, sheetId, primary: selectedCell, scene, engine } = this._currentEditCell;
        const workbook = this._getWorkbookForEditUnit(unitId);
        if (!workbook || workbook.getUnitId() !== unitId) {
            return;
        }

        const worksheet = workbook.getActiveSheet();
        if (!worksheet || worksheet.getSheetId() !== sheetId) {
            return;
        }

        const renderUnit = this._renderManagerService.getRenderUnitById(unitId);
        if (!renderUnit) {
            return;
        }

        const skeleton = this._sheetSkeletonService.getSkeleton(unitId, sheetId);
        if (!skeleton) {
            return;
        }

        const primary = worksheet.getCellInfoInMergeData(selectedCell.actualRow, selectedCell.actualColumn);
        const primaryWithCoord = attachPrimaryWithCoord(skeleton, primary);
        const actualRangeWithCoord = convertCellToRange(primaryWithCoord);
        const canvasOffset = getCanvasOffsetByEngine(engine);

        let { startX, startY, endX, endY } = actualRangeWithCoord;

        const { scaleX, scaleY } = scene.getAncestorScale();
        const viewport = getViewportByCell(primary.startRow, primary.startColumn, scene, worksheet);
        if (!viewport) {
            return;
        }
        const scrollXY = scene.getViewportScrollXY(viewport);

        startX = convertTransformToOffsetX(startX, scaleX, scrollXY);
        startY = convertTransformToOffsetY(startY, scaleY, scrollXY);
        endX = convertTransformToOffsetX(endX, scaleX, scrollXY);
        endY = convertTransformToOffsetY(endY, scaleY, scrollXY);

        if (resetSizeOnly && this._currentEditCellLayout) {
            endX = endX - startX + this._currentEditCellLayout.position.startX;
            endY = endY - startY + this._currentEditCellLayout.position.startY;
            startX = this._currentEditCellLayout.position.startX;
            startY = this._currentEditCellLayout.position.startY;
        }

        this._currentEditCellLayout = {
            position: {
                startX,
                startY,
                endX,
                endY,
            },
            canvasOffset,
            scaleX,
            scaleY,
        };
        this._currentEditCellLayout$.next(this._currentEditCellLayout);
    }

    setEditCell(param: ICurrentEditCellParam) {
        const workbook = this._getWorkbookForEditUnit(param.unitId);
        if (this._getSheetType(workbook) === SheetTypes.CHART ||
            workbook?.getSheetBySheetId(param.sheetId)?.getConfig().type === SheetTypes.CHART) {
            return;
        }
        this._currentEditCell = param;

        /**
         * If there is no editor currently focused, then default to selecting the sheet editor to prevent the editorService from using the previously selected editor object.
         * todo: wzhudev: In boundless mode, it is necessary to switch to the corresponding editorId based on the host's unitId.
         */
        if (!this._editorService.getFocusEditor()) {
            if (this._contextService.getContextValue(DISABLE_AUTO_FOCUS_KEY)) {
                const editorDocument = this._univerInstanceService.getUnit(
                    DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
                    UniverInstanceType.UNIVER_DOC
                );
                if (editorDocument) {
                    this._univerInstanceService.setCurrentUnitForType(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
                }
            } else {
                this._editorService.focus(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
                /**
                 * Fix: When the sheet loads for the first time, copying and pasting triggers the editor, and the edits are ineffective.
                 */
                this._contextService.setContextValue(EDITOR_ACTIVATED, false);
                this._contextService.setContextValue(FOCUSING_EDITOR_STANDALONE, false);
            }
        }

        const editCellState = this.getLatestEditCellState();
        if (!editCellState) {
            this._currentEditCellState = editCellState;
            this._currentEditCellLayout = editCellState;
            this._currentEditCellState$.next(editCellState);
            this._currentEditCellLayout$.next(editCellState);
            return;
        }

        const { position, canvasOffset, scaleX, scaleY, ...rest } = editCellState;
        this._currentEditCellState = rest;
        this._currentEditCellLayout = { position, canvasOffset, scaleX, scaleY };
        this._currentEditCellState$.next(this._currentEditCellState);
        this._currentEditCellLayout$.next(this._currentEditCellLayout);
    }

    private _clearCurrentEditCellState() {
        this._currentEditCellState = null;
        this._currentEditCellState$.next(null);
        this._currentEditCellLayout = null;
        this._currentEditCellLayout$.next(null);
    }

    getEditCellState(): Readonly<Nullable<IEditorBridgeServiceParam>> {
        if (this._currentEditCellState == null || this._currentEditCellLayout == null) {
            return null;
        }

        return { ...this._currentEditCellState, ...this._currentEditCellLayout };
    }

    getEditCellLayout(): Readonly<Nullable<ICellEditorLayout>> {
        return this._currentEditCellLayout;
    }

    getEditLocation(): Readonly<Nullable<ICellEditorState>> {
        return this._currentEditCellState;
    }

    updateEditLocation(row: number, column: number) {
        if (this._currentEditCellState) {
            this._currentEditCellState = {
                ...this._currentEditCellState,
                row,
                column,
            };
        }
    }

    getLatestEditCellState() {
        if (!this._currentEditCell) {
            return;
        }

        const { unitId, sheetId, primary: selectedCell, scene, engine } = this._currentEditCell;
        const workbook = this._getWorkbookForEditUnit(unitId);
        if (!workbook || workbook.getUnitId() !== unitId) {
            return;
        }

        const worksheet = workbook.getSheetBySheetId(sheetId);
        if (!worksheet || workbook.getActiveSheet() !== worksheet ||
            (worksheet.getConfig().type ?? SheetTypes.GRID) !== SheetTypes.GRID) {
            return;
        }

        const renderUnit = this._renderManagerService.getRenderUnitById(unitId);
        if (!renderUnit) {
            return;
        }

        const skeleton = this._sheetSkeletonService.getSkeleton(unitId, sheetId);
        if (!skeleton) {
            return;
        }

        const primary = worksheet.getCellInfoInMergeData(selectedCell.actualRow, selectedCell.actualColumn);
        const { startRow, startColumn } = primary;
        const primaryWithCoord = attachPrimaryWithCoord(skeleton, primary);
        const actualRangeWithCoord = convertCellToRange(primaryWithCoord);
        const canvasOffset = getCanvasOffsetByEngine(engine);

        let { startX, startY, endX, endY } = actualRangeWithCoord;

        const { scaleX, scaleY } = scene.getAncestorScale();
        const viewport = getViewportByCell(startRow, startColumn, scene, worksheet);
        if (!viewport) {
            return;
        }
        const scrollXY = scene.getViewportScrollXY(viewport);

        startX = convertTransformToOffsetX(startX, scaleX, scrollXY);
        startY = convertTransformToOffsetY(startY, scaleY, scrollXY);
        endX = convertTransformToOffsetX(endX, scaleX, scrollXY);
        endY = convertTransformToOffsetY(endY, scaleY, scrollXY);

        const location = {
            workbook,
            worksheet,
            unitId: workbook.getUnitId(),
            subUnitId: worksheet.getSheetId(),
            row: startRow,
            col: startColumn,
            origin: worksheet.getCellRaw(startRow, startColumn),
        };

        let documentLayoutObject: Nullable<IDocumentLayoutObject>;
        const cell = this._sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(BEFORE_CELL_EDIT)(
            worksheet.getCell(startRow, startColumn),
            location
        );

        documentLayoutObject = cell && worksheet.getCellDocumentModelWithFormula(cell, location.row, location.col);

        // Rewrite the cellValueType to STRING to avoid render the value on the right side when number type.
        const renderConfig = documentLayoutObject?.documentModel?.documentStyle.renderConfig;
        if (renderConfig != null) {
            renderConfig.cellValueType = CellValueType.STRING;
        }

        if (!documentLayoutObject || documentLayoutObject.documentModel == null) {
            const blankModel = worksheet.getBlankCellDocumentModel(cell, location.row, location.col);

            if (documentLayoutObject != null) {
                const { verticalAlign, horizontalAlign, wrapStrategy, textRotation, fill } = documentLayoutObject;
                const { centerAngle, vertexAngle } = convertTextRotation(textRotation);
                blankModel.documentModel!.documentStyle.renderConfig = {
                    ...renderConfig,
                    verticalAlign,
                    horizontalAlign,
                    wrapStrategy,
                    background: { rgb: fill },
                    centerAngle,
                    vertexAngle,
                };
            }
            documentLayoutObject = blankModel;
        }
        documentLayoutObject.documentModel?.setZoomRatio(Math.max(scaleX, scaleY));

        if (cell?.isInArrayFormulaRange === true) {
            const body = documentLayoutObject.documentModel?.getBody();
            if (body) {
                body.textRuns = [
                    {
                        st: 0,
                        ed: body.dataStream.length - 2,
                        ts: {
                            cl: {
                                rgb: this._themeService.getColorFromTheme('gray.300'),
                            },
                        },
                    },
                ];
            }
        }

        return {
            position: {
                startX,
                startY,
                endX,
                endY,
            },
            scaleX,
            scaleY,
            canvasOffset,
            row: startRow,
            column: startColumn,
            unitId,
            sheetId,
            documentLayoutObject,
            editorUnitId: this._editorUnitId,
            isInArrayFormulaRange: cell?.isInArrayFormulaRange,
            isPercentFormat: cell?.isPercentFormat,
        };
    }

    private _getSheetType(workbook: Nullable<Workbook>): SheetTypes | undefined {
        return workbook?.getActiveSheet()?.getConfig().type;
    }

    private _getWorkbookForEditUnit(unitId: string): Nullable<Workbook> {
        return this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET) ??
            this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
    }

    getCurrentEditorId() {
        return this._editorUnitId;
    }

    changeVisible(params: IEditorBridgeServiceVisibleParam) {
        if (params.visible && this._getSheetType(this._getWorkbookForEditUnit(params.unitId)) === SheetTypes.CHART) {
            return;
        }
        this._visibleParams = params;

        // Reset the dirty status when the editor is visible.
        if (params.visible) {
            this._editorIsDirty = false;
        }

        this._visibleParams$.next(this._visibleParams);
        this._afterVisibleParams$.next(this._visibleParams);
    }

    isVisible() {
        return this._visibleParams;
    }

    enableForceKeepVisible(): void {
        this._forceKeepVisible$.next(true);
    }

    disableForceKeepVisible(): void {
        this._forceKeepVisible$.next(false);
    }

    isForceKeepVisible(): boolean {
        return this._forceKeepVisible$.getValue();
    }

    changeEditorDirty(dirtyStatus: boolean) {
        this._editorIsDirty = dirtyStatus;
    }

    getEditorDirty() {
        return this._editorIsDirty;
    }
}

export const IEditorBridgeService = createIdentifier<IEditorBridgeService>('univer.sheet-editor-bridge.service');