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.

15.2 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 { ICustomRange, IDisposable, INeedCheckDisposable, ITextRange, Nullable, Workbook } from '@univerjs/core';
import type { IBoundRectNoAngle } from '@univerjs/engine-render';
import type { ISheetLocationBase } from '@univerjs/sheets';
import type { ICanvasPopup } from '@univerjs/sheets-ui';
import type { Observable } from 'rxjs';
import {
    BuildTextUtils,
    createIdentifier,
    CustomRangeType,
    Disposable,
    DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
    Inject,
    Injector,
    IUniverInstanceService,
    UniverInstanceType,
} from '@univerjs/core';
import { DocSelectionManagerService } from '@univerjs/docs';
import { calcDocRangePositions } from '@univerjs/docs-ui';
import { IRenderManagerService } from '@univerjs/engine-render';
import { getCustomRangePosition, getEditingCustomRangePosition, IEditorBridgeService, SheetCanvasPopManagerService } from '@univerjs/sheets-ui';
import { IDialogService } from '@univerjs/ui';
import { BehaviorSubject, Subject } from 'rxjs';
import { HyperLinkEditSourceType } from '../types/enums/edit-source';
import { CellLinkEdit } from '../views/CellLinkEdit';
import { CellLinkPopup } from '../views/CellLinkPopup';

export interface IHyperLinkPopup {
    unitId: string;
    subUnitId: string;
    disposable: INeedCheckDisposable;
    row: number;
    col: number;
    editPermission?: boolean;
    copyPermission?: boolean;
    customRange?: Nullable<ICustomRange>;
    type: HyperLinkEditSourceType;
    showAll?: boolean;
}

export interface IHyperLinkEditing {
    unitId: string;
    subUnitId: string;
    row: number;
    col: number;
    customRangeId?: string;
    type: HyperLinkEditSourceType;
}

export interface ISheetsHyperLinkPopupService {
    readonly currentPopup: IHyperLinkPopup | null;
    readonly currentPopup$: Observable<IHyperLinkPopup | null>;
    readonly currentEditing: (IHyperLinkEditing & { customRange?: ICustomRange; label?: string }) | null;
    readonly currentEditing$: Observable<(IHyperLinkEditing & { customRange?: ICustomRange; label?: string }) | null>;
    setIsKeepVisible(value: boolean): void;
    getIsKeepVisible(): boolean;
    showPopup(location: IHyperLinkPopupOptions): void;
    hideCurrentPopup(type?: HyperLinkEditSourceType, force?: boolean): void;
    startAddEditing(link: IHyperLinkEditing): void;
    startEditing(link: Required<IHyperLinkEditing>): void;
    endEditing(type?: HyperLinkEditSourceType): void;
}

export const ISheetsHyperLinkPopupService = createIdentifier<ISheetsHyperLinkPopupService>('sheets-hyper-link-ui.popup.service');

const isEqualLink = (a: IHyperLinkPopupOptions, b: Omit<IHyperLinkPopup, 'disposable' | 'editPermission'>) => {
    return (
        a.unitId === b.unitId
        && a.subUnitId === b.subUnitId
        && a.row === b.row
        && a.col === b.col
        && a.customRange?.rangeId === b.customRange?.rangeId
        && a.type === b.type
    );
};

export interface IHyperLinkPopupOptions extends ISheetLocationBase {
    editPermission?: boolean;
    copyPermission?: boolean;
    customRange?: Nullable<ICustomRange>;
    customRangeRect?: Nullable<IBoundRectNoAngle>;
    showAll?: boolean;
    type: HyperLinkEditSourceType;
}

export class SheetsHyperLinkPopupService extends Disposable implements ISheetsHyperLinkPopupService {
    protected _currentPopup: IHyperLinkPopup | null = null;
    protected _currentPopup$ = new Subject<IHyperLinkPopup | null>();
    currentPopup$ = this._currentPopup$.asObservable();
    protected _currentEditingPopup: Nullable<IDisposable> = null;

    protected _currentEditing$ = new BehaviorSubject<(IHyperLinkEditing & { customRange?: ICustomRange; label?: string }) | null>(null);
    currentEditing$ = this._currentEditing$.asObservable();

    private _isKeepVisible: boolean = false;

    get currentPopup() {
        return this._currentPopup;
    }

    get currentEditing() {
        return this._currentEditing$.getValue();
    }

    constructor(
        @Inject(SheetCanvasPopManagerService) protected readonly _sheetCanvasPopManagerService: SheetCanvasPopManagerService,
        @Inject(Injector) protected readonly _injector: Injector,
        @IUniverInstanceService protected readonly _univerInstanceService: IUniverInstanceService,
        @IEditorBridgeService private readonly _editorBridgeService: IEditorBridgeService,
        @Inject(DocSelectionManagerService) protected readonly _textSelectionManagerService: DocSelectionManagerService,
        @IRenderManagerService protected readonly _renderManagerService: IRenderManagerService,
        @IDialogService protected readonly _dialogService: IDialogService
    ) {
        super();

        this.disposeWithMe(() => {
            this.hideCurrentPopup();
            this.endEditing();

            this._currentEditing$.complete();
            this._currentPopup$.complete();
        });
    }

    public setIsKeepVisible(v: boolean) {
        this._isKeepVisible = v;
    }

    public getIsKeepVisible() {
        return this._isKeepVisible;
    }

    showPopup(location: IHyperLinkPopupOptions) {
        if (this._currentPopup && isEqualLink(location, this._currentPopup)) {
            return;
        }

        this.hideCurrentPopup(undefined, true);
        const currentEditing = this._currentEditing$.getValue();
        if (currentEditing && isEqualLink(location, currentEditing)) {
            return;
        }

        const { unitId, subUnitId, row, col, customRangeRect, customRange } = location;
        let disposable: Nullable<INeedCheckDisposable>;
        const popup: ICanvasPopup = {
            componentKey: CellLinkPopup.componentKey,
            direction: 'bottom',
            onClickOutside: () => {
                this.hideCurrentPopup();
            },
            onClick: () => {
                this.hideCurrentPopup(location.type, true);
            },
        };
        if (location.type === HyperLinkEditSourceType.EDITING) {
            if (!customRange) {
                return;
            }
            disposable = customRangeRect && this._sheetCanvasPopManagerService.attachPopupToAbsolutePosition(
                customRangeRect,
                popup
            );
        } else {
            if (location.showAll) {
                disposable = this._sheetCanvasPopManagerService.attachPopupToCell(location.row, location.col, popup, unitId, subUnitId);
            } else {
                if (!customRange) {
                    return;
                }

                disposable = customRangeRect && this._sheetCanvasPopManagerService.attachPopupByPosition(
                    customRangeRect,
                    popup,
                    location
                );
            }
        }

        if (disposable) {
            if (this._currentPopup) {
                this._currentPopup.disposable?.dispose();
            }
            this._currentPopup = {
                unitId,
                subUnitId,
                disposable,
                row,
                col,
                editPermission: !!location.editPermission,
                copyPermission: !!location.copyPermission,
                customRange,
                type: location.type,
                showAll: location.showAll,
            };
            this._currentPopup$.next(this._currentPopup);
        }
    }

    hideCurrentPopup(type?: HyperLinkEditSourceType, force?: boolean) {
        if (!this._currentPopup) {
            return;
        }

        if (((!type || type === this._currentPopup.type) && this._currentPopup.disposable.canDispose()) || force) {
            this._currentPopup?.disposable?.dispose();
            this._currentPopup = null;
            this._currentPopup$.next(null);
        }
    }

    override dispose(): void {
        super.dispose();
        this.hideCurrentPopup();
        this.endEditing();
        this._currentPopup$.complete();
        this._currentEditing$.complete();
    }

    protected _getEditingRange(): Nullable<ITextRange & { label: string }> {
        const visible = this._editorBridgeService.isVisible().visible;
        const state = this._editorBridgeService.getEditCellState();
        if (visible && state) {
            const textRange = this._textSelectionManagerService.getActiveTextRange();
            const body = state.documentLayoutObject.documentModel?.getBody();
            if (!body) {
                return null;
            }
            if (!textRange || textRange.collapsed) {
                return {
                    startOffset: 0,
                    endOffset: body.dataStream.length - 2,
                    collapsed: body.dataStream.length - 2 === 0,
                    label: BuildTextUtils.transform.getPlainText(body.dataStream),
                };
            }
            const links = BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(textRange, body.customRanges?.filter((i) => i.rangeType === CustomRangeType.HYPERLINK) ?? []);
            let start = textRange.startOffset;
            let end = textRange.endOffset;
            links.forEach((link) => {
                start = Math.min(start, link.startIndex);
                end = Math.max(end, link.endIndex + 1);
            });
            return {
                startOffset: start,
                endOffset: end,
                collapsed: start === end,
                label: BuildTextUtils.transform.getPlainText(body.dataStream.slice(start, end)),
            };
        }

        return null;
    }

    private get _editPopup() {
        const popup: ICanvasPopup = {
            componentKey: CellLinkEdit.componentKey,
            direction: 'vertical',
            onClickOutside: () => {
                this.endEditing();
            },
            onContextMenu: () => {
                this.endEditing();
            },
            hiddenType: 'hide',
        };
        return popup;
    }

    startAddEditing(link: IHyperLinkEditing) {
        const { unitId, subUnitId, type } = link;
        if (type === HyperLinkEditSourceType.EDITING) {
            const range = this._getEditingRange();

            if (!range) {
                return;
            }

            this._textSelectionManagerService.replaceDocRanges([{ ...range }], { unitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY, subUnitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY });
            const currentRender = this._renderManagerService.getRenderUnitById(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
            if (!currentRender) {
                return;
            }
            const rects = calcDocRangePositions(range, currentRender);
            if (!rects?.length) {
                return;
            }
            this._currentEditingPopup = this._sheetCanvasPopManagerService.attachPopupToAbsolutePosition(
                rects.pop()!,
                this._editPopup,
                unitId,
                subUnitId
            );

            this._currentEditing$.next({
                ...link,
                label: range?.label ?? '',
            });
        } else {
            const workbook = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
            const worksheet = workbook?.getSheetBySheetId(subUnitId);
            const cell = worksheet?.getCellRaw(link.row, link.col);
            const label = cell?.p ? BuildTextUtils.transform.getPlainText(cell.p.body?.dataStream ?? '') : (cell?.v ?? '').toString();
            this._currentEditingPopup = this._sheetCanvasPopManagerService.attachPopupToCell(
                link.row,
                link.col,
                this._editPopup,
                unitId,
                subUnitId
            );
            this._currentEditing$.next({
                ...link,
                label,
            });
        }
    }

    startEditing(link: Required<IHyperLinkEditing>) {
        this._currentEditingPopup?.dispose();
        this.hideCurrentPopup(undefined, true);

        const { unitId, subUnitId } = link;
        let customRange;
        let label;
        if (link.type === HyperLinkEditSourceType.EDITING) {
            const customRangeInfo = getEditingCustomRangePosition(this._injector, link.unitId, link.subUnitId, link.row, link.col, link.customRangeId);
            if (!customRangeInfo || !customRangeInfo.rects?.length) {
                return;
            }
            customRange = customRangeInfo.customRange;
            label = customRangeInfo.label;
            this._textSelectionManagerService.replaceDocRanges([
                {
                    startOffset: customRange.startIndex,
                    endOffset: customRange.endIndex + 1,
                },
            ]);
            this._currentEditingPopup = this._sheetCanvasPopManagerService.attachPopupToAbsolutePosition(
                customRangeInfo.rects.pop()!,
                this._editPopup,
                unitId,
                subUnitId
            );
        } else {
            const workbook = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
            const worksheet = workbook?.getSheetBySheetId(subUnitId);
            const cell = worksheet?.getCellRaw(link.row, link.col);
            const style = workbook?.getStyles().getStyleByCell(cell);
            const tr = style?.tr;

            const customRangeInfo = getCustomRangePosition(this._injector, link.unitId, link.subUnitId, link.row, link.col, link.customRangeId);
            if (!customRangeInfo || !customRangeInfo.rects?.length) {
                return;
            }
            customRange = customRangeInfo.customRange;
            label = customRangeInfo.label;
            if (tr) {
                this._currentEditingPopup = this._sheetCanvasPopManagerService.attachPopupToCell(
                    link.row,
                    link.col,
                    this._editPopup,
                    unitId,
                    subUnitId
                );
            } else {
                this._currentEditingPopup = this._sheetCanvasPopManagerService.attachPopupByPosition(
                    customRangeInfo.rects.pop()!,
                    this._editPopup,
                    {
                        unitId,
                        subUnitId,
                        row: link.row,
                        col: link.col,
                    }
                );
            }
        }
        const editing = {
            ...link,
            customRange,
            label,
        };
        this._currentEditing$.next(editing);
    }

    endEditing(type?: HyperLinkEditSourceType) {
        if (this.getIsKeepVisible()) {
            return;
        }
        const current = this._currentEditing$.getValue();
        if (current && (!type || type === current.type)) {
            this._currentEditingPopup?.dispose();
            this._currentEditing$.next(null);
        }
    }
}