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.

71.7 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 {
    ICellData,
    ICellDataWithSpanAndDisplay,
    IDisposable,
    IMutationInfo,
    IRange,
    IRowAutoHeightInfo,
    Nullable,
    Workbook,
    Worksheet,
} from '@univerjs/core';
import type {
    IDiscreteRange,
    ISetRangeValuesMutationParams,
    ISetSelectionsOperationParams,
    ISetWorksheetRowAutoHeightMutationParams,
    ISheetRangeLocation,
} from '@univerjs/sheets';
import type { Observable } from 'rxjs';
import {
    BooleanNumber,
    CellModeEnum,
    cloneCellDataWithSpanAndDisplay,
    createIdentifier,
    Disposable,
    ErrorService,
    extractPureTextFromCell,
    generateRandomId,
    getEmptyCell,
    getNumfmtLocaleTag,
    ICommandService,
    ILogService,
    Inject,
    Injector,
    isFormulaString,
    isNotNullOrUndefined,
    IUndoRedoService,
    IUniverInstanceService,
    LocaleService,
    ObjectMatrix,
    sequenceExecute,
    SheetTypes,
    ThemeService,
    toDisposable,
    Tools,
    UniverInstanceType,
} from '@univerjs/core';
import { IRenderManagerService, withCurrentTypeOfRenderer } from '@univerjs/engine-render';
import {
    discreteRangeToRange,
    getPrimaryForRange,
    MoveRangeCommand,
    rangeToDiscreteRange,
    SetSelectionsOperation,
    SetWorksheetActiveOperation,
    SetWorksheetRowAutoHeightMutation,
    SetWorksheetRowAutoHeightMutationFactory,
    SheetInterceptorService,
    SheetsSelectionsService,
} from '@univerjs/sheets';
import {
    FILE__BMP_CLIPBOARD_MIME_TYPE,
    FILE__JPEG_CLIPBOARD_MIME_TYPE,
    FILE__WEBP_CLIPBOARD_MIME_TYPE,
    FILE_PNG_CLIPBOARD_MIME_TYPE,
    HTML_CLIPBOARD_MIME_TYPE,
    IClipboardInterfaceService,
    imageMimeTypeSet,
    INotificationService,
    IPlatformService,
    PLAIN_TEXT_CLIPBOARD_MIME_TYPE,
} from '@univerjs/ui';
import { BehaviorSubject } from 'rxjs';
import type { LocaleKey } from '../../locale/types';
import type {
    ICellDataWithSpanInfo,
    IClipboardPropertyItem,
    ICopyHookValueType,
    ICopyOptions,
    ICopyPastePayload,
    IPasteHookKeyType,
    IPasteHookValueType,
    IPasteOptionCache,
    IPasteTarget,
    ISheetClipboardHook,
    ISheetDiscreteRangeLocation,
    IUniverSheetCopyDataModel,
} from './type';
import { virtualizeDiscreteRanges } from '../../controllers/utils/range-tools';
import { IMarkSelectionService } from '../mark-selection/mark-selection.service';
import { SheetSkeletonManagerService } from '../sheet-skeleton-manager.service';
import { createCopyPasteSelectionStyle } from '../utils/selection-util';
import { cloneCellDataWithSpanInfo } from './clone';
import { CopyContentCache, extractId } from './copy-content-cache';
import { HtmlToUSMService } from './html-to-usm/converter';
import { LarkPastePlugin } from './html-to-usm/paste-plugins/plugin-lark';
import { UniverPastePlugin } from './html-to-usm/paste-plugins/plugin-univer';
import { WordPastePlugin } from './html-to-usm/paste-plugins/plugin-word';
import { COPY_TYPE } from './type';
import { USMToHtmlService } from './usm-to-html/convertor';
import {
    convertTextToTable,
    discreteRangeContainsRange,
    htmlContainsImage,
    htmlIsFromExcel,
    mergeSetRangeValues,
    rangeIntersectWithDiscreteRange,
    spilitLargeSetRangeValuesMutations,
} from './utils';

export const PREDEFINED_HOOK_NAME_COPY = {
    DEFAULT_COPY: 'default-copy',
    SPECIAL_COPY_FORMULA_ONLY: 'special-copy-formula-only',
} as const;
export const PREDEFINED_HOOK_NAME_PASTE = {
    DEFAULT_PASTE: 'default-paste',
    DEFAULT_PASTE_ROWS: 'default-paste-rows',
    DEFAULT_PASTE_COLUMNS: 'default-paste-columns',
    SPECIAL_PASTE_VALUE: 'special-paste-value',
    SPECIAL_PASTE_FORMAT: 'special-paste-format',
    SPECIAL_PASTE_COL_WIDTH: 'special-paste-col-width',
    SPECIAL_PASTE_BESIDES_BORDER: 'special-paste-besides-border',
    SPECIAL_PASTE_FORMULA: 'special-paste-formula',
} as const;
export const PREDEFINED_HOOK_NAME = {
    ...PREDEFINED_HOOK_NAME_COPY,
    ...PREDEFINED_HOOK_NAME_PASTE,
} as const;

const IMAGE_MIME_TO_EXTENSION = {
    [FILE_PNG_CLIPBOARD_MIME_TYPE]: 'png',
    [FILE__JPEG_CLIPBOARD_MIME_TYPE]: 'jpg',
    [FILE__WEBP_CLIPBOARD_MIME_TYPE]: 'webp',
    [FILE__BMP_CLIPBOARD_MIME_TYPE]: 'bmp',
} as const;

export const FORMULA_CLIPBOARD_MIME_TYPE = 'web application/x-univer-sheets-formula';

// The existing object HTML/cache format needs an empty slot, never a grid selection or Chart source range.
const OBJECT_CLIPBOARD_CARRIER_RANGE: IRange = { startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 };

function wrapHtmlForExcel(html: string): string {
    const table = html.match(/<table[\s\S]*<\/table>/)?.[0] ?? html;

    return `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Excel.Sheet"><meta name="Generator" content="Univer"></head><body><!--StartFragment-->${table}<!--EndFragment--></body></html>`;
}

interface IFormulaClipboardPayload {
    rowCount: number;
    columnCount: number;
    origin: {
        row: number;
        column: number;
    };
    formulas: Array<{
        row: number;
        column: number;
        f: string;
        ft?: ICellData['ft'];
        fd?: ICellData['fd'];
    }>;
}

interface ICopyContent {
    copyId: string;
    plain: string;
    html: string;
    matrixFragment: ObjectMatrix<ICellDataWithSpanAndDisplay>;
    discreteRange: IDiscreteRange;
    formulaClipboardPayload?: string;
}

/**
 * This service provide hooks for sheet features to supplement content or modify behavior of clipboard.
 */

// Univer clipboard HTML also contains Word compatibility metadata.
HtmlToUSMService.use(UniverPastePlugin);
HtmlToUSMService.use(WordPastePlugin);
HtmlToUSMService.use(LarkPastePlugin);

export interface ISheetClipboardService {
    showMenu$: Observable<boolean>;
    setShowMenu: (show: boolean) => void;
    getPasteMenuVisible: () => boolean;

    pasteOptionsCache$: Observable<IPasteOptionCache | null>;
    getPasteOptionsCache: () => IPasteOptionCache | null;
    updatePasteOptionsCache(cache: IPasteOptionCache | null): void;

    copy(options?: ICopyOptions): Promise<boolean>;
    cut(): Promise<boolean>;
    capturePasteTarget(): ISheetRangeLocation | null;
    paste(item: ClipboardItem, pasteType?: string, target?: ISheetRangeLocation | null): Promise<boolean>;
    pasteByCopyId(copyId: string, pasteType?: string, target?: ISheetRangeLocation | null): Promise<boolean>;
    legacyPaste(html?: string, text?: string, files?: File[], formulaClipboardPayload?: string, target?: ISheetRangeLocation | null): Promise<boolean>;

    rePasteWithPasteType(type: IPasteHookKeyType): boolean;
    disposePasteOptionsCache(): void;

    generateCopyContent(workbookId: string, worksheetId: string, range: IRange, options?: ICopyOptions): Nullable<ICopyContent>;
    copyContentCache(): CopyContentCache; // return the cache content for inner copy/cut/paste.
    addClipboardHook(hook: ISheetClipboardHook): IDisposable; // add a hook to the clipboard service
    getClipboardHooks(): ISheetClipboardHook[]; // get all hooks

    removeMarkSelection(): void;
}

export const ISheetClipboardService = createIdentifier<ISheetClipboardService>('sheet.clipboard-service');

export class SheetClipboardService extends Disposable implements ISheetClipboardService {
    private _clipboardHooks: ISheetClipboardHook[] = [];

    private readonly _clipboardHooks$ = new BehaviorSubject<ISheetClipboardHook[]>([]);
    readonly clipboardHooks$ = this._clipboardHooks$.asObservable();

    private _htmlToUSM: HtmlToUSMService;
    private _usmToHtml: USMToHtmlService;

    private _copyContentCache: CopyContentCache;
    private _copyMarkId: string | null = null;

    // Record the parsed matrix and row and column attributes
    private _pasteOptionsCache$ = new BehaviorSubject<IPasteOptionCache | null>(null);
    readonly pasteOptionsCache$ = this._pasteOptionsCache$.asObservable();

    //Control the visibility of the Paste Options menu
    private readonly _showMenu$ = new BehaviorSubject(false);
    readonly showMenu$ = this._showMenu$.asObservable();

    constructor(
        @ILogService private readonly _logService: ILogService,
        @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
        @Inject(SheetsSelectionsService) private readonly _selectionManagerService: SheetsSelectionsService,
        @IClipboardInterfaceService private readonly _clipboardInterfaceService: IClipboardInterfaceService,
        @IUndoRedoService private readonly _undoRedoService: IUndoRedoService,
        @ICommandService private readonly _commandService: ICommandService,
        @IMarkSelectionService private readonly _markSelectionService: IMarkSelectionService,
        @INotificationService private readonly _notificationService: INotificationService,
        @IPlatformService private readonly _platformService: IPlatformService,
        @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
        @Inject(ThemeService) private readonly _themeService: ThemeService,
        @Inject(LocaleService) private readonly _localeService: LocaleService,
        @Inject(ErrorService) private readonly _errorService: ErrorService,
        @Inject(Injector) private readonly _injector: Injector
    ) {
        super();

        this._htmlToUSM = new HtmlToUSMService({
            getCurrentSkeleton: () => withCurrentTypeOfRenderer(
                UniverInstanceType.UNIVER_SHEET,
                SheetSkeletonManagerService,
                this._univerInstanceService,
                this._renderManagerService
            )?.getCurrentParam(),
            getNumfmtParseOptions: () => {
                const workbook = this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
                return workbook
                    ? { locale: getNumfmtLocaleTag(workbook.getSnapshot().locale), dateSystem: workbook.getDateSystem() }
                    : undefined;
            },
        });

        this._usmToHtml = new USMToHtmlService();
        this._copyContentCache = new CopyContentCache();

        this.disposeWithMe(this._htmlToUSM);
        this._initUnitDisposed();
    }

    setShowMenu(show: boolean) {
        this._showMenu$.next(show);
    }

    getPasteMenuVisible() {
        return this._showMenu$.getValue();
    }

    getPasteOptionsCache() {
        return this._pasteOptionsCache$.getValue();
    }

    copyContentCache(): CopyContentCache {
        return this._copyContentCache;
    }

    generateCopyContent(workbookId: string, worksheetId: string, range: IRange, options?: ICopyOptions): Nullable<ICopyContent> {
        const { copyType = COPY_TYPE.COPY, copyHookType = PREDEFINED_HOOK_NAME_COPY.DEFAULT_COPY } = options || {};

        let hooks = this._clipboardHooks;
        if (copyHookType === PREDEFINED_HOOK_NAME_COPY.SPECIAL_COPY_FORMULA_ONLY) {
            hooks = this._clipboardHooks.filter((h) => h.id === copyHookType);
        }

        hooks.forEach((h) => h.onBeforeCopy?.(workbookId, worksheetId, range, copyType));
        const copyContent = this._generateCopyContent(workbookId, worksheetId, range, hooks, copyHookType);
        if (copyContent && copyType === COPY_TYPE.COPY) {
            copyContent.formulaClipboardPayload = this._generateFormulaClipboardPayload(
                workbookId,
                worksheetId,
                range,
                copyContent,
                hooks
            );
        }
        hooks.forEach((h) => h.onAfterCopy?.());
        return copyContent;
    }

    async copy(options?: ICopyOptions): Promise<boolean> {
        const selection = this._selectionManagerService.getCurrentLastSelection();
        const workbook = this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
        const worksheet = workbook?.getActiveSheet();
        if (!workbook || !worksheet) {
            return false;
        }

        const workbookId = workbook.getUnitId();
        const worksheetId = worksheet.getSheetId();
        const { copyType = COPY_TYPE.COPY, copyHookType = PREDEFINED_HOOK_NAME_COPY.DEFAULT_COPY } = options || {};
        const chartSheet = worksheet.getConfig().type === SheetTypes.CHART;
        if ((!selection && !chartSheet) || (chartSheet && copyHookType === PREDEFINED_HOOK_NAME_COPY.SPECIAL_COPY_FORMULA_ONLY)) {
            return false;
        }

        const focusedObjectHooks = copyHookType === PREDEFINED_HOOK_NAME_COPY.SPECIAL_COPY_FORMULA_ONLY
            ? []
            : this._clipboardHooks.filter((hook) => hook.onBeforeCopyFocusedObject);
        const focusedObjectCopied = focusedObjectHooks
            .map((hook) => hook.onBeforeCopyFocusedObject?.(workbookId, worksheetId, copyType))
            .some(Boolean);
        if (chartSheet && !focusedObjectCopied) {
            return false;
        }
        const range = chartSheet ? OBJECT_CLIPBOARD_CARRIER_RANGE : selection!.range;
        const copyContent = focusedObjectCopied
            ? this._generateObjectCopyContent(workbookId, worksheetId, range)
            : this.generateCopyContent(workbookId, worksheetId, range, { copyType, copyHookType });

        if (!copyContent) {
            return false;
        }

        // 2. extract copy content for both internal and external
        const { html, plain, matrixFragment, copyId, discreteRange, formulaClipboardPayload } = copyContent;

        // 3. cache the copy content for internal paste
        this._copyContentCache.set(copyId, {
            unitId: workbook.getUnitId(),
            subUnitId: worksheet.getSheetId(),
            range: discreteRange,
            matrix: matrixFragment,
            copyType,
            skipCellCopy: focusedObjectCopied,
        });

        // 4. write html and get plain text info the clipboard interface
        await this._clipboardInterfaceService.write(
            plain,
            wrapHtmlForExcel(html),
            formulaClipboardPayload ? { [FORMULA_CLIPBOARD_MIME_TYPE]: formulaClipboardPayload } : undefined
        );

        // 5. mark the copy range
        this._markSelectionService.removeAllShapes();
        if (focusedObjectCopied) {
            this._copyMarkId = null;
        } else {
            const style = createCopyPasteSelectionStyle(this._themeService);
            this._copyMarkId = this._markSelectionService.addShape({ ...selection!, style });
        }

        return true;
    }

    async cut(): Promise<boolean> {
        return this.copy({
            copyType: COPY_TYPE.CUT,
        });
    }

    capturePasteTarget(): ISheetRangeLocation | null {
        const workbook = this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
        const worksheet = workbook?.getActiveSheet();
        const selection = this._selectionManagerService.getCurrentLastSelection();
        if (!workbook || !worksheet) {
            return null;
        }
        const chartSheet = worksheet.getConfig().type === SheetTypes.CHART;
        if (!selection && !chartSheet) {
            return null;
        }
        return {
            unitId: workbook.getUnitId(),
            subUnitId: worksheet.getSheetId(),
            range: Tools.deepClone(chartSheet ? OBJECT_CLIPBOARD_CARRIER_RANGE : selection!.range),
        };
    }

    async paste(item: ClipboardItem, pasteType = PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE, target = this.capturePasteTarget()): Promise<boolean> {
        if (!target) {
            return false;
        }
        const types = item.types;
        const text =
            types.indexOf(PLAIN_TEXT_CLIPBOARD_MIME_TYPE) !== -1
                ? await item.getType(PLAIN_TEXT_CLIPBOARD_MIME_TYPE).then((blob) => blob && blob.text())
                : '';
        const html =
            types.indexOf(HTML_CLIPBOARD_MIME_TYPE) !== -1
                ? await item.getType(HTML_CLIPBOARD_MIME_TYPE).then((blob) => blob && blob.text())
                : '';
        const formulaClipboardPayload =
            types.indexOf(FORMULA_CLIPBOARD_MIME_TYPE) !== -1
                ? await item.getType(FORMULA_CLIPBOARD_MIME_TYPE).then((blob) => blob && blob.text()).catch(() => '')
                : '';

        const imageIndex = types.findIndex((type) => imageMimeTypeSet.has(type));

        const shouldUseHTMLPaste = imageIndex === -1 || !htmlContainsImage(html);
        if (html && shouldUseHTMLPaste) {
            // Firstly see if the html content is from Excel
            const copyId = extractId(html);
            const isInternalCopy = Boolean(copyId && this._copyContentCache.get(copyId));
            if (this._platformService.isWindows && htmlIsFromExcel(html) && !isInternalCopy) {
                this._notificationService.show({
                    type: 'warning',
                    title: this._localeService.t<LocaleKey>('sheets-ui.clipboard.shortCutNotify.title'),
                    content: this._localeService.t<LocaleKey>('sheets-ui.clipboard.shortCutNotify.useShortCutInstead'),
                });
                // Pasting should not be allowed here.
                // After the pop-up window prompts, can paste the contents of the clipboard as much as possible.
            }

            return this._pasteHTML(html, pasteType, formulaClipboardPayload, target);
        }

        // clipboard item from excel may contain image, so we need to check if the clipboard item is from excel
        if (imageIndex !== -1) {
            const imageMimeType = types[imageIndex]!;
            const imageBlob = await item.getType(imageMimeType);

            if (imageBlob) {
                const imageExtension = IMAGE_MIME_TO_EXTENSION[imageMimeType as keyof typeof IMAGE_MIME_TO_EXTENSION];
                const file = new File(
                    [imageBlob],
                    `clipboard-image.${imageExtension}`,
                    { type: imageMimeType }
                );

                return this._pasteFiles([file], pasteType, target);
            }
        }

        if (text) {
            return this._pastePlainText(text, pasteType, target);
        }

        this._logService.error('[SheetClipboardService]', 'No valid data on clipboard');

        return false;
    }

    async pasteByCopyId(copyId: string, pasteType = PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE, target = this.capturePasteTarget()): Promise<boolean> {
        if (!target) {
            return false;
        }
        return this._pasteInternal(copyId, pasteType, target);
    }

    async legacyPaste(html?: string, text?: string, files?: File[], formulaClipboardPayload?: string, target = this.capturePasteTarget()): Promise<boolean> {
        if (!target) {
            return false;
        }
        const shouldUseHTMLPaste = !files?.length || !htmlContainsImage(html ?? '');

        if (html && shouldUseHTMLPaste) {
            return this._pasteHTML(html, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE, formulaClipboardPayload, target);
        } else if (files?.length) {
            return this._pasteFiles(files, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE, target);
        } else if (text) {
            // Converts text with tabs and newlines into an HTML table
            if (/[\n\t]/.test(text)) {
                return this._pasteHTML(convertTextToTable(text), PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE, undefined, target);
            } else {
                return this._pastePlainText(text, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE, target);
            }
        } else {
            return this._pasteUnrecognized(target);
        }
    }

    rePasteWithPasteType(type: IPasteHookKeyType): boolean {
        const pasteOptionsCache = this._pasteOptionsCache$.getValue();
        if (!pasteOptionsCache) {
            return false;
        }

        const undoRedoService = this._injector.get(IUndoRedoService);
        const element = undoRedoService.pitchTopUndoElement();
        if (element) {
            const result = sequenceExecute(element.undoMutations, this._commandService);
            if (result.result) {
                undoRedoService.popUndoToRedo();
            }
        }

        const { cellMatrix, rowProperties = [], colProperties = [], source, target } = pasteOptionsCache;

        this._pasteUSM({
            cellMatrix,
            colProperties,
            rowProperties,
        }, target, PREDEFINED_HOOK_NAME_PASTE[type], source);

        return true;
    }

    updatePasteOptionsCache(cache: IPasteOptionCache | null): void {
        this._pasteOptionsCache$.next(cache);
    }

    addClipboardHook(hook: ISheetClipboardHook): IDisposable {
        if (this._clipboardHooks.findIndex((h) => h.id === hook.id) !== -1) {
            this._logService.error('[SheetClipboardService]', 'hook already exists', hook.id);
            return { dispose: () => { /* empty */ } };
        }

        // hook added should be ordered at meaning while
        const insertIndex = this._clipboardHooks.findIndex((existingHook) => {
            const existingHookPriority = existingHook.priority || 0;
            const hookPriority = hook.priority || 0;
            return hookPriority < existingHookPriority;
        });

        this._clipboardHooks.splice(insertIndex !== -1 ? insertIndex : this._clipboardHooks.length, 0, hook);

        this._notifyClipboardHook();
        return toDisposable(() => {
            const index = this._clipboardHooks.indexOf(hook);
            if (index > -1) {
                this._clipboardHooks.splice(index, 1);
                this._notifyClipboardHook();
            }
        });
    }

    getClipboardHooks(): ISheetClipboardHook[] {
        return this._clipboardHooks;
    }

    private _generateObjectCopyContent(unitId: string, subUnitId: string, range: IRange): Nullable<ICopyContent> {
        const workbook = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
        const worksheet = workbook?.getSheetBySheetId(subUnitId);
        if (!worksheet) {
            return null;
        }

        const copyId = generateRandomId(6);
        const matrixFragment = new ObjectMatrix<ICellDataWithSpanAndDisplay>();
        matrixFragment.setValue(0, 0, getEmptyCell());
        const discreteRange: IDiscreteRange = {
            rows: [range.startRow],
            cols: [range.startColumn],
        };

        return {
            copyId,
            plain: '',
            html: this._usmToHtml.convert(matrixFragment, discreteRange, [], copyId),
            matrixFragment,
            discreteRange,
        };
    }

    private _generateCopyContent(unitId: string, subUnitId: string, range: IRange, hooks: ISheetClipboardHook[], copyHookType: ICopyHookValueType = PREDEFINED_HOOK_NAME_COPY.DEFAULT_COPY): Nullable<ICopyContent> {
        const workbook = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
        const worksheet = workbook?.getSheetBySheetId(subUnitId);

        if (!workbook || !worksheet) {
            return null;
        }

        // get filtered out rows those are filtered out by plugins (e.g. filter feature)
        const filteredRows = hooks.reduce((acc, cur) => {
            const rows = cur.getFilteredOutRows?.(unitId, subUnitId, range);
            rows?.forEach((r) => acc.add(r));
            return acc;
        }, new Set<number>());

        // calculate selection matrix, span cells would only - maybe warn uses that cells are too may in the future
        const { startColumn, startRow, endColumn, endRow } = range;
        let rowIndex = startRow;

        const matrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn, CellModeEnum.Both);
        const matrixFragment = new ObjectMatrix<ICellDataWithSpanInfo>();
        const plainMatrix = new ObjectMatrix<ICellDataWithSpanAndDisplay>();
        const discreteRange: IDiscreteRange = { rows: [], cols: [] };

        const handleMatrixOnCellHook = copyHookType === PREDEFINED_HOOK_NAME_COPY.SPECIAL_COPY_FORMULA_ONLY ? hooks.find((h) => h.handleMatrixOnCell) : undefined;
        const getCellValueBySpecialMatrixHooks = hooks.filter((h) => h.getCellValueBySpecialMatrix);

        for (let r = startRow; r <= endRow; r++) {
            if (filteredRows.has(r)) {
                continue;
            }
            discreteRange.rows.push(r);
            for (let c = startColumn; c <= endColumn; c++) {
                if (handleMatrixOnCellHook) {
                    handleMatrixOnCellHook.handleMatrixOnCell?.(r, c, rowIndex - startRow, c - startColumn, matrix, matrixFragment, plainMatrix);
                    continue;
                }

                const cellData = getCellValueBySpecialMatrixHooks.reduce<ICellDataWithSpanAndDisplay | undefined>(
                    (acc, hook) => acc || hook.getCellValueBySpecialMatrix!(r, c),
                    undefined
                ) || matrix.getValue(r, c);
                if (cellData) {
                    const newCellData = cloneCellDataWithSpanAndDisplay(cellData)!;
                    plainMatrix.setValue(rowIndex - startRow, c - startColumn, {
                        ...getEmptyCell(),
                        ...newCellData,
                    });

                    delete newCellData.displayV;
                    matrixFragment.setValue(rowIndex - startRow, c - startColumn, {
                        ...getEmptyCell(),
                        ...newCellData,
                    });
                } else {
                    plainMatrix.setValue(rowIndex - startRow, c - startColumn, getEmptyCell());
                    matrixFragment.setValue(rowIndex - startRow, c - startColumn, getEmptyCell());
                    matrix.setValue(r, c, getEmptyCell());
                }
            }
            rowIndex += 1;
        }

        for (let c = startColumn; c <= endColumn; c++) {
            discreteRange.cols.push(c);
        }

        // convert matrix to html
        const copyId = generateRandomId(6);
        const html = this._usmToHtml.convert(matrix, discreteRange, hooks, copyId);
        const plain = getMatrixPlainText(plainMatrix);

        return {
            copyId,
            plain,
            html,
            matrixFragment,
            discreteRange,
        };
    }

    private _generateFormulaClipboardPayload(
        unitId: string,
        subUnitId: string,
        range: IRange,
        copyContent: ICopyContent,
        hooks: ISheetClipboardHook[]
    ): string | undefined {
        if (this._hasFilteredRows(unitId, subUnitId, range, hooks)) {
            return undefined;
        }

        const { startRow, startColumn, endRow, endColumn } = range;
        const rowCount = endRow - startRow + 1;
        const columnCount = endColumn - startColumn + 1;
        const formulaOnlyHook = hooks.find((hook) => hook.id === PREDEFINED_HOOK_NAME_COPY.SPECIAL_COPY_FORMULA_ONLY);
        const formulas: IFormulaClipboardPayload['formulas'] = [];

        for (let row = 0; row < rowCount; row++) {
            for (let column = 0; column < columnCount; column++) {
                const formula = formulaOnlyHook?.onCopyCellContent?.(startRow + row, startColumn + column) ||
                    copyContent.matrixFragment.getValue(row, column)?.f;

                if (typeof formula === 'string' && isFormulaString(formula)) {
                    formulas.push({
                        row,
                        column,
                        f: formula,
                        ft: copyContent.matrixFragment.getValue(row, column)?.ft,
                        fd: copyContent.matrixFragment.getValue(row, column)?.fd,
                    });
                }
            }
        }

        if (!formulas.length) {
            return undefined;
        }

        return JSON.stringify({
            rowCount,
            columnCount,
            origin: {
                row: startRow,
                column: startColumn,
            },
            formulas,
        } satisfies IFormulaClipboardPayload);
    }

    private _hasFilteredRows(unitId: string, subUnitId: string, range: IRange, hooks: ISheetClipboardHook[]): boolean {
        return hooks.some((hook) => {
            const rows = hook.getFilteredOutRows?.(unitId, subUnitId, range);
            return !!rows?.length;
        });
    }

    private _notifyClipboardHook() {
        this._clipboardHooks$.next(this._clipboardHooks);
    }

    private async _executePaste(generateMutations: (hook: ISheetClipboardHook, payload: {
        unitId: string;
        subUnitId: string;
        range: IDiscreteRange;
    }) => undefined | ({ undos: IMutationInfo[]; redos: IMutationInfo[] }), location: ISheetRangeLocation, pastePayload: ICopyPastePayload): Promise<boolean> {
        const target = this._getPastingTarget(location);
        if (!target.subUnitId || !target.selection) {
            return false;
        }
        const range = this._injector.invoke((accessor) => {
            return rangeToDiscreteRange(target.selection!.range, accessor, target.unitId, target.subUnitId);
        });

        if (!range) {
            return false;
        }

        const { unitId, subUnitId } = target;
        const hooks = this._clipboardHooks;
        const enabledHooks: ISheetClipboardHook[] = [];
        const disableCopying = hooks.some(
            (h) => enabledHooks.push(h) && h.onBeforePaste?.({ unitId, subUnitId, range }, pastePayload) === false
        );
        if (disableCopying) {
            enabledHooks.forEach((h) => h.onAfterPaste?.(false));
            return false;
        }

        const redoMutationsInfo: IMutationInfo[] = [];
        const undoMutationsInfo: IMutationInfo[] = [];
        enabledHooks.forEach((h) => {
            const contentReturn = generateMutations(h, {
                unitId,
                subUnitId,
                range,
            });

            if (contentReturn) {
                redoMutationsInfo.push(...contentReturn.redos);
                undoMutationsInfo.push(...contentReturn.undos);
            }
        });
        const result = redoMutationsInfo.every((m) => this._commandService.executeCommand(m.id, m.params));
        if (result) {
            // add to undo redo services
            this._undoRedoService.pushUndoRedo({
                unitID: unitId,
                undoMutations: undoMutationsInfo,
                redoMutations: redoMutationsInfo,
            });
        }

        return result;
    }

    private async _pasteFiles(files: File[], pasteType: IPasteHookValueType, target: ISheetRangeLocation): Promise<boolean> {
        return this._executePaste((h, payload) => {
            return h.onPasteFiles?.(payload, files, { pasteType });
        }, target, { pasteType });
    }

    private async _pastePlainText(text: string, pasteType: IPasteHookValueType, target: ISheetRangeLocation): Promise<boolean> {
        return this._executePaste((h, payload) => {
            return h.onPastePlainText?.(payload, text, { pasteType });
        }, target, { pasteType });
    }

    private _pasteUnrecognized(target: ISheetRangeLocation) {
        return this._executePaste((h, payload) => {
            return h.onPasteUnrecognized?.(payload);
        }, target, { pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE });
    }

    private async _pasteHTML(html: string, pasteType: IPasteHookValueType, formulaClipboardPayload: string | undefined, target: ISheetRangeLocation): Promise<boolean> {
        const copyId = extractId(html);
        if (copyId && this._copyContentCache.get(copyId)) {
            return this._pasteInternal(copyId, pasteType, target);
        }
        return this._pasteExternal(html, pasteType, formulaClipboardPayload, target);
    }

    private async _pasteExternal(html: string, pasteType: IPasteHookValueType, formulaClipboardPayload: string | undefined, target: ISheetRangeLocation): Promise<boolean> {
        // this._logService.log('[SheetClipboardService]', 'pasting external content', html);

        // steps of pasting:

        // 1. get properties of the table by parsing raw html content, including col properties / row properties
        // cell properties and cell contents.
        const { rowProperties, colProperties, cellMatrix } = this._htmlToUSM.convert(html);
        if (!cellMatrix) {
            return false;
        }

        const sourceRange = formulaClipboardPayload
            ? this._applyFormulaClipboardPayload(cellMatrix, formulaClipboardPayload)
            : undefined;

        return this._pasteExternalData({ rowProperties, colProperties, cellMatrix }, pasteType, sourceRange, target);
    }

    private _applyFormulaClipboardPayload(
        cellMatrix: ObjectMatrix<ICellDataWithSpanInfo>,
        text: string
    ): IDiscreteRange | undefined {
        let payload: IFormulaClipboardPayload;
        try {
            payload = JSON.parse(text) as IFormulaClipboardPayload;
        } catch {
            return undefined;
        }

        if (
            !payload ||
            !payload.origin ||
            !Array.isArray(payload.formulas) ||
            payload.rowCount <= 0 ||
            payload.columnCount <= 0
        ) {
            return undefined;
        }

        cellMatrix.setValue(payload.rowCount - 1, payload.columnCount - 1, {
            ...getEmptyCell(),
            ...cellMatrix.getValue(payload.rowCount - 1, payload.columnCount - 1),
        });
        payload.formulas.forEach((formula) => {
            if (
                isFormulaString(formula.f) &&
                formula.row >= 0 &&
                formula.row < payload.rowCount &&
                formula.column >= 0 &&
                formula.column < payload.columnCount
            ) {
                cellMatrix.setValue(formula.row, formula.column, {
                    ...getEmptyCell(),
                    ...cellMatrix.getValue(formula.row, formula.column),
                    f: formula.f,
                    ft: formula.ft,
                    fd: formula.fd,
                });
            }
        });

        return {
            rows: Array.from({ length: payload.rowCount }, (_, index) => payload.origin.row + index),
            cols: Array.from({ length: payload.columnCount }, (_, index) => payload.origin.column + index),
        };
    }

    private _pasteExternalData(
        data: IUniverSheetCopyDataModel,
        pasteType: IPasteHookValueType,
        sourceRange: IDiscreteRange | undefined,
        target: ISheetRangeLocation
    ): boolean {
        const { rowProperties, colProperties, cellMatrix } = data;
        if (!cellMatrix) {
            return false;
        }

        // 2. get filtered rows in the target pasting area and get the final pasting matrix
        // we also handle transpose pasting at this step
        // note: handle transpose before filtering
        // matrix before adjustment -> transpose -> filtering -> matrix under adjustment
        // TODO: not implemented yet

        // 3. call hooks with cell position and properties and get mutations (both do mutations and undo mutations)
        // we also handle 'copy value only' or 'copy style only' as this step
        const pasteTarget = this._getPastedRange(cellMatrix, target);

        // pastedRange.endColumn = pastedRange.startColumn + colCount;
        // pastedRange.endRow = pastedRange.startRow + rowCount;

        // If PastedRange is null, it means that the paste fails
        if (!pasteTarget) {
            return false;
        }

        const worksheet = this._univerInstanceService
            .getUnit<Workbook>(pasteTarget.unitId, UniverInstanceType.UNIVER_SHEET)
            ?.getSheetBySheetId(pasteTarget.subUnitId);
        if (!worksheet) {
            return false;
        }

        const mergeData = worksheet?.getMergeData();

        if (mergeData.length) {
            const pastedRangeLapWithMergedCell = mergeData.some((m) => {
                return rangeIntersectWithDiscreteRange(m, pasteTarget.pastedRange) && !discreteRangeContainsRange(pasteTarget.pastedRange, m);
            });
            if (pastedRangeLapWithMergedCell) {
                this._errorService.emit(this._localeService.t<LocaleKey>('sheets-ui.clipboard.paste.overlappingMergedCells'));
                return false;
            }
        }

        // 4. execute these mutations by the one method
        const source = sourceRange
            ? {
                range: sourceRange,
                unitId: pasteTarget.unitId,
                subUnitId: pasteTarget.subUnitId,
                copyType: COPY_TYPE.COPY,
                copyId: '',
            }
            : undefined;

        return this._pasteUSM(
            {
                rowProperties,
                colProperties,
                cellMatrix,
            },
            pasteTarget,
            pasteType,
            source
        );
    }

    // eslint-disable-next-line max-lines-per-function
    private async _pasteInternal(copyId: string, pasteType: IPasteHookValueType, target: ISheetRangeLocation): Promise<boolean> {
        const cachedData = this._copyContentCache.get(copyId);
        const { range, matrix: cachedMatrix, unitId: copyUnitId, subUnitId: copySubUnitId, skipCellCopy } = cachedData || {};
        if (!cachedMatrix || !cachedData || !range || !copyUnitId || !copySubUnitId) {
            return false;
        }

        // MoveRange only supports one workbook. Reject before resource hooks can apply a partial cut.
        if (!skipCellCopy && cachedData.copyType === COPY_TYPE.CUT && copyUnitId !== target.unitId) {
            return false;
        }

        const worksheet = this._univerInstanceService.getUnit<Workbook>(copyUnitId, UniverInstanceType.UNIVER_SHEET)?.getSheetBySheetId(copySubUnitId);
        const cellMatrix = new ObjectMatrix<ICellDataWithSpanInfo>();
        if (skipCellCopy) {
            cellMatrix.setValue(0, 0, getEmptyCell());
        } else {
            const { mapFunc } = virtualizeDiscreteRanges([range]);
            cachedMatrix.forValue((row, col, value) => {
                const { row: actualRow, col: actualColumn } = mapFunc(row, col);
                /**
                 * This only needs to include a combination of default styles, row and column styles, and cell styles.
                 * Conditional formatting, data validation, and number format are not included here because they will be applied automatically after pasting.
                 * So here we use `getComposedCellStyleByCellData` to get the final style of the cell raw data.
                 */
                const cellData = worksheet?.getCellRaw(actualRow, actualColumn);
                const style = worksheet?.getComposedCellStyleByCellData(actualRow, actualColumn, cellData);
                const newValue = cloneCellDataWithSpanInfo(value)!;
                if (style && Object.keys(style).length > 0) {
                    newValue.s = style;
                }
                cellMatrix.setValue(row, col, newValue);

                if (value.colSpan || value.rowSpan) {
                    for (let rStart = 0; rStart < value.rowSpan!; rStart++) {
                        for (let cStart = 0; cStart < value.colSpan!; cStart++) {
                            if (rStart === 0 && cStart === 0) {
                                continue;
                            }

                            const r = row + rStart;
                            const c = col + cStart;
                            cellMatrix.setValue(r, c, { s: style });
                        }
                    }
                }
            });
        }
        const pasteTarget = this._getPastedRange(cellMatrix, target);
        if (!pasteTarget) {
            return false;
        }

        const pasteToWorksheet = this._univerInstanceService
            .getUnit<Workbook>(pasteTarget.unitId, UniverInstanceType.UNIVER_SHEET)
            ?.getSheetBySheetId(pasteTarget.subUnitId);
        if (!pasteToWorksheet) {
            return false;
        }

        const mergeData = pasteToWorksheet?.getMergeData();

        if (mergeData) {
            const pastedRangeLapWithMergedCell = mergeData.some((m) => {
                return rangeIntersectWithDiscreteRange(m, pasteTarget.pastedRange) && !discreteRangeContainsRange(pasteTarget.pastedRange, m);
            });
            if (pastedRangeLapWithMergedCell) {
                this._errorService.emit(this._localeService.t<LocaleKey>('sheets-ui.clipboard.paste.overlappingMergedCells'));
                return false;
            }
        }

        const pasteFromWorkbook = this._univerInstanceService.getUnit<Workbook>(copyUnitId);
        if (!pasteFromWorkbook) {
            return false;
        }
        const pasteFromWorksheet = pasteFromWorkbook.getSheetBySheetId(copySubUnitId);
        if (!pasteFromWorksheet) {
            return false;
        }

        const colProperties: IClipboardPropertyItem[] = [];
        const rowProperties: IClipboardPropertyItem[] = [];
        if (!skipCellCopy) {
            const colManager = pasteFromWorksheet.getColumnManager();
            const rowManager = pasteFromWorksheet.getRowManager();

            range.cols.forEach((i) => {
                colProperties.push({ width: `${colManager.getColumnWidth(i)}` });
            });

            range.rows.forEach((j) => {
                rowProperties.push({ height: `${rowManager.getRowHeight(j)}` });
            });
        }

        if (cachedData.copyType === COPY_TYPE.CUT) {
            const start = pasteTarget.pastedRange.rows[0];
            const end = range.rows[range.rows.length - 1] - range.rows[0] + start;
            pasteTarget.pastedRange.rows = Array.from(new Array(end + 1).keys()).slice(start);
            if (!skipCellCopy) {
                const canMove = await this._injector.get(SheetInterceptorService).beforeCommandExecute({
                    id: MoveRangeCommand.id,
                    params: {
                        fromUnitId: copyUnitId,
                        fromSubUnitId: copySubUnitId,
                        fromRange: discreteRangeToRange(range),
                        toUnitId: target.unitId,
                        toSubUnitId: target.subUnitId,
                        toRange: discreteRangeToRange(pasteTarget.pastedRange),
                    },
                });
                if (!canMove) {
                    return false;
                }
            }
        }

        const pasteRes = this._pasteUSM(
            {
                cellMatrix,
                colProperties,
                rowProperties,
            }, // paste data
            pasteTarget,
            pasteType,
            {
                range, // paste source
                unitId: cachedData.unitId,
                subUnitId: cachedData.subUnitId,
                copyType: cachedData.copyType,
                copyId,
                skipCellCopy,
            }
        );

        if (pasteRes && cachedData.copyType === COPY_TYPE.CUT) {
            this._copyContentCache.set(copyId, { ...cachedData, matrix: null });
            if (this._copyMarkId) {
                this._markSelectionService.removeShape(this._copyMarkId);
            }
            this._copyMarkId = null;
        }

        return pasteRes;
    }

    // eslint-disable-next-line max-lines-per-function
    private _pasteUSM(
        data: IUniverSheetCopyDataModel,
        target: IPasteTarget,
        pasteType: IPasteHookValueType,
        source?: ISheetDiscreteRangeLocation & { copyId: string; copyType: COPY_TYPE; skipCellCopy?: boolean }
    ): boolean {
        const { rowProperties, colProperties, cellMatrix } = data;
        const { unitId, subUnitId, pastedRange } = target;
        const objectOnly = source?.skipCellCopy === true;
        const payload: ICopyPastePayload = {
            copyType: source?.copyType || COPY_TYPE.COPY,
            copyId: source?.copyId,
            pasteType,
        };
        const hooks = this._clipboardHooks;
        const enabledHooks: ISheetClipboardHook[] = [];
        const executePaste = (): boolean => {
            const disableCopying = hooks.some(
                (h) => enabledHooks.push(h) && h.onBeforePaste?.({ unitId, subUnitId, range: pastedRange }, payload) === false
            );
            if (disableCopying) {
                if (!objectOnly) {
                    enabledHooks.forEach((h) => h.onAfterPaste?.(false));
                }
                return false;
            }
            if (!cellMatrix) {
                return false;
            }

            const pasteFrom = source
                ? {
                    unitId: source.unitId,
                    subUnitId: source.subUnitId,
                    range: source.range,
                }
                : null;

            let redoMutationsInfo: IMutationInfo[] = [];
            let undoMutationsInfo: IMutationInfo[] = [];

            // if hooks are not special or default, it will be executed in any case.
            // other hooks will be executed only when the paste type is the same as the hook name, including the default one
            const pasteHooks = source?.skipCellCopy
                ? enabledHooks.filter((hook) => hook.onBeforeCopyFocusedObject)
                : enabledHooks;
            const filteredHooks: ISheetClipboardHook[] = pasteHooks.filter(
                (h) => (!h.specialPasteInfo && h.id !== PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE) || pasteType === h.id
            );
            filteredHooks.forEach((h) => {
                if (!objectOnly) {
                    const rowReturn = h.onPasteRows?.({ range: pastedRange, unitId, subUnitId });
                    if (rowReturn) {
                        redoMutationsInfo.push(...rowReturn.redos);
                        undoMutationsInfo.push(...rowReturn.undos);
                    }

                    const colReturn = h.onPasteColumns?.(
                        { range: pastedRange, unitId, subUnitId },
                        colProperties || [],
                        { pasteType }
                    );
                    if (colReturn) {
                        redoMutationsInfo.push(...colReturn.redos);
                        undoMutationsInfo.push(...colReturn.undos);
                    }
                }
                const contentReturn = h.onPasteCells?.(
                    pasteFrom,
                    { range: pastedRange, unitId, subUnitId },
                    cellMatrix,
                    payload
                );
                if (contentReturn) {
                    redoMutationsInfo.push(...contentReturn.redos);
                    undoMutationsInfo.push(...contentReturn.undos);
                }
            });

            if (objectOnly && redoMutationsInfo.length === 0) {
                return false;
            }

            // Object-only pastes preserve the target selection; their range is only a cache carrier.
            if (!objectOnly && payload.copyType === COPY_TYPE.COPY) {
                const setSelectionOperation = this._getSetSelectionOperation(unitId, subUnitId, pastedRange, cellMatrix, pasteType);
                if (setSelectionOperation) {
                    redoMutationsInfo.push(setSelectionOperation);
                }
            }

            redoMutationsInfo = mergeSetRangeValues(redoMutationsInfo);
            undoMutationsInfo = mergeSetRangeValues(undoMutationsInfo);

            // Split large SetRangeValuesMutation to avoid performance issues
            redoMutationsInfo = redoMutationsInfo.flatMap((mutation) =>
                spilitLargeSetRangeValuesMutations(mutation as IMutationInfo<ISetRangeValuesMutationParams>, {
                    threshold: 20000,
                    maxCellsPerChunk: 10000,
                })
            );
            undoMutationsInfo = undoMutationsInfo.flatMap((mutation) =>
                spilitLargeSetRangeValuesMutations(mutation as IMutationInfo<ISetRangeValuesMutationParams>, {
                    threshold: 20000,
                    maxCellsPerChunk: 10000,
                })
            );

            undoMutationsInfo.push({ id: SetWorksheetActiveOperation.id, params: { unitId: target.unitId, subUnitId: target.subUnitId } });

            this._logService.log('[SheetClipboardService]', 'pasting mutations', {
                undoMutationsInfo,
                redoMutationsInfo,
            });

            const result = redoMutationsInfo.every((m) => this._commandService.syncExecuteCommand(m.id, m.params));
            if (result) {
                // handle auto row height for pasted range
                const autoHeightMutation = objectOnly
                    ? null
                    : this._getPastedRangeAutoHeightMutation(
                        unitId,
                        subUnitId,
                        pastedRange
                    );
                if (autoHeightMutation) {
                    this._commandService.syncExecuteCommand(autoHeightMutation.redo.id, autoHeightMutation.redo.params);
                    redoMutationsInfo.push(autoHeightMutation.redo);
                    undoMutationsInfo.unshift(autoHeightMutation.undo);
                }

                // add to undo redo services
                this._undoRedoService.pushUndoRedo({
                    unitID: unitId,
                    undoMutations: undoMutationsInfo,
                    redoMutations: redoMutationsInfo,
                });

                this.updatePasteOptionsCache({
                    target: {
                        pastedRange,
                        unitId,
                        subUnitId,
                    },
                    cellMatrix,
                    rowProperties,
                    colProperties,
                    pasteType,
                    source,
                });
                this.setShowMenu(true);
            }

            if (!objectOnly) {
                filteredHooks.forEach((h) => h.onAfterPaste?.(result));
            }
            return result;
        };
        let result = false;
        let pasteThrew = false;
        let pasteError: unknown;
        try {
            result = executePaste();
        } catch (error) {
            if (!objectOnly) {
                throw error;
            }
            pasteThrew = true;
            pasteError = error;
        }
        if (!objectOnly) {
            return result;
        }
        let afterPasteThrew = false;
        let firstAfterPasteError: unknown;
        for (const hook of enabledHooks) {
            try {
                hook.onAfterPaste?.(result);
            } catch (error) {
                if (!afterPasteThrew) {
                    afterPasteThrew = true;
                    firstAfterPasteError = error;
                }
            }
        }
        if (pasteThrew) {
            throw pasteError;
        }
        if (afterPasteThrew) {
            throw firstAfterPasteError;
        }
        return result;
    }

    private _getPastedRangeAutoHeightMutation(
        unitId: string,
        subUnitId: string,
        pastedRange: IDiscreteRange
    ): {
        undo: IMutationInfo;
        redo: IMutationInfo;
    } | null {
        const worksheet = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)?.getSheetBySheetId(subUnitId);
        if (!worksheet) {
            return null;
        }

        const skeleton = this._renderManagerService.getRenderUnitById(unitId)?.with(SheetSkeletonManagerService).ensureSkeleton(subUnitId);
        if (!skeleton) {
            return null;
        }

        const rowManager = worksheet.getRowManager();
        const defaultRowHeight = worksheet.getConfig().defaultRowHeight;

        const { rows, cols } = pastedRange;
        const pasteRangeStartRow = rows[0];
        const pasteRangeEndRow = rows[rows.length - 1];
        const pasteRangeStartColumn = cols[0];
        const pasteRangeEndColumn = cols[cols.length - 1];

        // Recalculate auto-height rows while preserving explicit and legacy manual row heights.
        const needAutoHeight: IRowAutoHeightInfo[] = [];
        for (let r = pasteRangeStartRow; r <= pasteRangeEndRow; r++) {
            const rowData = rowManager.getRow(r);
            const hasLegacyManualHeight = rowData?.ia == null && rowData?.ah == null && typeof rowData?.h === 'number';
            const isManualHeight = rowData?.ia === BooleanNumber.FALSE || hasLegacyManualHeight;
            if (!isManualHeight) {
                const autoHeight = skeleton.calculateAutoHeightInRange([
                    {
                        startRow: r,
                        endRow: r,
                        startColumn: pasteRangeStartColumn,
                        endColumn: pasteRangeEndColumn,
                    },
                ]).filter((info) => info.autoHeight !== defaultRowHeight);
                needAutoHeight.push(...autoHeight);
            }
        }

        if (needAutoHeight.length > 0) {
            const setWorksheetRowAutoHeightMutationParams: ISetWorksheetRowAutoHeightMutationParams = {
                unitId,
                subUnitId,
                rowsAutoHeightInfo: needAutoHeight,
            };

            return {
                redo: {
                    id: SetWorksheetRowAutoHeightMutation.id,
                    params: setWorksheetRowAutoHeightMutationParams,
                },
                undo: {
                    id: SetWorksheetRowAutoHeightMutation.id,
                    params: SetWorksheetRowAutoHeightMutationFactory(setWorksheetRowAutoHeightMutationParams, worksheet),
                },
            };
        }

        return null;
    }

    private _getSetSelectionOperation(
        unitId: string,
        subUnitId: string,
        range: IDiscreteRange,
        cellMatrix: ObjectMatrix<ICellDataWithSpanAndDisplay>,
        pasteType?: string
    ) {
        const worksheet = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)?.getSheetBySheetId(subUnitId);
        if (!worksheet) {
            return null;
        }
        const { rows, cols } = range;
        const startRow = rows[0];
        const startColumn = cols[0];
        const endRow = rows[rows.length - 1];
        const endColumn = cols[cols.length - 1];

        const primaryCell = {
            startRow,
            endRow: startRow,
            startColumn,
            endColumn: startColumn,
        };

        const primary = getPrimaryForRange(primaryCell, worksheet);

        const mainCell = cellMatrix.getValue(0, 0);
        const rowSpan = mainCell?.rowSpan || 1;
        const colSpan = mainCell?.colSpan || 1;

        const shouldExpandPrimary = pasteType === PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE
            || pasteType === PREDEFINED_HOOK_NAME_PASTE.SPECIAL_PASTE_BESIDES_BORDER
            || pasteType === PREDEFINED_HOOK_NAME_PASTE.SPECIAL_PASTE_FORMAT;

        if (shouldExpandPrimary && (rowSpan > 1 || colSpan > 1)) {
            const mergeRange = {
                startRow,
                endRow: startRow + rowSpan - 1,
                startColumn,
                endColumn: startColumn + colSpan - 1,
            };

            primary.startRow = mergeRange.startRow;
            primary.endRow = mergeRange.endRow;
            primary.startColumn = mergeRange.startColumn;
            primary.endColumn = mergeRange.endColumn;

            primary.isMerged = true;
            primary.isMergedMainCell = true;
        }

        // selection does not require undo
        const setSelectionsParam: ISetSelectionsOperationParams = {
            unitId,
            subUnitId,
            selections: [{
                range: {
                    startRow,
                    endRow,
                    startColumn,
                    endColumn,
                },
                primary,
                style: null,
            }],
        };
        return {
            id: SetSelectionsOperation.id,
            params: setSelectionsParam,
        };
    }

    private _getPastingTarget(location?: ISheetRangeLocation) {
        if (location) {
            const worksheet = this._univerInstanceService.getUnit<Workbook>(location.unitId, UniverInstanceType.UNIVER_SHEET)
                ?.getSheetBySheetId(location.subUnitId);
            return {
                unitId: location.unitId,
                subUnitId: worksheet?.getSheetId(),
                selection: worksheet ? { range: location.range } : null,
            };
        }
        const workbook = this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
        const worksheet = workbook.getActiveSheet();
        const selection = this._selectionManagerService.getCurrentLastSelection();
        return {
            unitId: workbook.getUnitId(),
            subUnitId: worksheet?.getSheetId(),
            selection,
        };
    }

    /**
     * Handles copying one range to another range, obtained by the following rules
     *
     * [Content to be assigned] => [Target range]
     *
     * I. There are no merged cells in the upper left corner of the pasted area
     *
     * 1. 1 -> 1: 1 => 1
     * 2. N -> 1: N => N
     * 3. 1 -> N: N => N
     * 4. N1 -> N2:
     *     1) N1 <N2: If N2 is a multiple of N1 (X), N1 * X => N2; If not, N1 => N1 (refer to office excel, different from google sheet)
     *     2) N1> N2: N1 => N1
     *
     * The above four cases can be combined and processed as
     *
     * Case 1, 1/2/4-2 merged into N1 => N1
     * Case 2, 3/4-1 merge into N1 * X => N2 or Case 1
     *
     * In the end we only need to judge whether N2 is a multiple of N1
     *
     * II. The pasted area contains merged cells
     *
     * 1. If N2 is a multiple of N1,
     *   1) If N2 === N1, paste this area directly and the range remains unchanged.
     *   2) Otherwise, determine whether other cells are included
     *     1] If included, tile, the range remains unchanged
     *     2] If not included, determine whether the source data is a combined cell
     *       1} If yes, tile, the range remains unchanged
     *       2} If not, only the content will be pasted, the original style will be discarded, and the scope will remain unchanged.
     *
     * 2. If N2 is not a multiple of N1, determine whether the upper left corner cell (merged or non-merged or combined) is consistent with the size of the original data.
     *   1) If consistent, only paste this area;
     *   2) If inconsistent, then determine whether the pasted area contains other cells.
     *     1] If yes, pasting is not allowed and an error will pop up;
     *     2] If not, only the content will be pasted and the original style will be discarded.
     *
     * @param rowCount
     * @param colCount
     * @param cellMatrix
     * @param range
     */
    // eslint-disable-next-line max-lines-per-function, complexity
    private _transformPastedData(
        rowCount: number,
        colCount: number,
        cellMatrix: ObjectMatrix<ICellDataWithSpanAndDisplay>
    ): IPasteTarget | null {
        const target = this._getPastingTarget();
        const { selection, unitId, subUnitId } = target;
        if (!subUnitId || !selection) {
            return null;
        }

        const discreteRange = this._injector.invoke((accessor) => {
            return rangeToDiscreteRange(selection.range, accessor, unitId, subUnitId);
        });

        if (!discreteRange) {
            return null;
        }

        const { ranges: [vRange], mapFunc } = virtualizeDiscreteRanges([discreteRange]);
        const { startRow, startColumn, endRow, endColumn } = vRange;

        const destinationRows = endRow - startRow + 1;
        const destinationColumns = endColumn - startColumn + 1;

        const workbook = this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
        const worksheet = workbook?.getActiveSheet();
        if (!worksheet) {
            return null;
        }

        const mergeData = worksheet.getMergeData();
        // get all merged cells
        const mergedCellsInRange = mergeData.filter((rect) =>
            discreteRange.rows.includes(rect.startRow) && discreteRange.cols.includes(rect.startColumn)
        );
        const mergedRange = mergedCellsInRange[0];

        let mergedRangeStartRow = 0;
        let mergedRangeStartColumn = 0;
        let mergedRangeEndRow = 0;
        let mergedRangeEndColumn = 0;
        if (mergedRange) {
            mergedRangeStartRow = mergedRange.startRow;
            mergedRangeStartColumn = mergedRange.startColumn;
            mergedRangeEndRow = mergedRange.endRow;
            mergedRangeEndColumn = mergedRange.endColumn;
        }

        // judge whether N2 is a multiple of N1
        if (destinationRows % rowCount === 0 && destinationColumns % colCount === 0) {
            // N2 !== N1
            if (mergedCellsInRange.length > 0 && (destinationRows !== rowCount || destinationColumns !== colCount)) {
                // Only merged cells, not other cells
                if (
                    mergedRangeStartRow === discreteRange.rows[0] &&
                    mergedRangeStartColumn === discreteRange.cols[0] &&
                    mergedRangeEndRow === discreteRange.rows[destinationRows - 1] &&
                    mergedRangeEndColumn === discreteRange.cols[destinationColumns - 1]
                ) {
                    const isMultiple = isMultipleCells(cellMatrix);
                    if (isMultiple) {
                        for (let r = 0; r < destinationRows; r++) {
                            for (let c = 0; c < destinationColumns; c++) {
                                const cell = cellMatrix.getValue(r % rowCount, c % colCount);
                                cell && cellMatrix.setValue(r, c, cell);
                            }
                        }
                    } else {
                        cellMatrix.forValue((row, col, cell) => {
                            cell.s = null;
                            delete cell.colSpan;
                            delete cell.rowSpan;
                        });
                    }
                } else {
                    for (let r = 0; r < destinationRows; r++) {
                        for (let c = 0; c < destinationColumns; c++) {
                            const cell = cellMatrix.getValue(r % rowCount, c % colCount);
                            cell && cellMatrix.setValue(r, c, cell);
                        }
                    }
                }
            } else {
                /**
                 * Expand cellMatrix content according to the destination size
                 * A1,B1  =>  A1,B1,C1,D1
                 * A2,B2      A2,B2,C2,D2
                 *            A3,B3,C3,D3
                 *            A4,B4,C4,D4
                 */
                for (let r = 0; r < destinationRows; r++) {
                    for (let c = 0; c < destinationColumns; c++) {
                        const cell = cellMatrix.getValue(r % rowCount, c % colCount);
                        cell && cellMatrix.setValue(r, c, cell);
                    }
                }
            }
        } else if (mergedCellsInRange.length > 0) {
            const { row: topRow, col: leftCol } = mapFunc(startRow, startColumn);
            const isMatch = this._topLeftCellsMatch(rowCount, colCount, {
                topRow,
                leftCol,
            });
            if (isMatch) {
                // Expand or shrink the destination to the same size as the original range
                const newDiscreteRange = this._expandOrShrinkRowsCols(unitId, subUnitId, discreteRange, colCount, rowCount);

                discreteRange.rows = newDiscreteRange.rows;
                discreteRange.cols = newDiscreteRange.cols;
            } else if (endRow > mergedRange.endRow || endColumn > mergedRange.endColumn) {
                return null;
            } else {
                cellMatrix.forValue((row, col, cell) => {
                    cell.s = null;
                    delete cell.colSpan;
                    delete cell.rowSpan;
                });
            }
        } else {
            // Expand or shrink the destination to the same size as the original range
            const newDiscreteRange = this._expandOrShrinkRowsCols(unitId, subUnitId, discreteRange, colCount, rowCount);

            discreteRange.rows = newDiscreteRange.rows;
            discreteRange.cols = newDiscreteRange.cols;
        }

        return {
            pastedRange: discreteRange,
            unitId,
            subUnitId,
        };
    }

    private _getPastedRange(cellMatrix: ObjectMatrix<ICellDataWithSpanAndDisplay>, location: ISheetRangeLocation) {
        const target = this._getPastingTarget(location);
        const { selection, unitId, subUnitId } = target;
        if (!subUnitId || !selection) {
            return null;
        }
        const discreteRange = this._injector.invoke((accessor) => {
            return rangeToDiscreteRange(selection.range, accessor, unitId, subUnitId);
        });

        if (!discreteRange) {
            return null;
        }
        const { startColumn, endColumn, startRow, endRow } = cellMatrix.getDataRange();
        const rowCount = endRow - startRow + 1;
        const colCount = endColumn - startColumn + 1;

        if (rowCount <= 0 || colCount <= 0) return null;

        const pasteSelectionRangeRowLen = discreteRange.rows.length;
        const pasteSelectionRangeColLen = discreteRange.cols.length;

        const worksheet = this._univerInstanceService
            .getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)
            ?.getSheetBySheetId(subUnitId);
        if (!worksheet) {
            return null;
        }

        const mergeData = worksheet?.getMergeData();

        if (pasteSelectionRangeRowLen % rowCount === 0 && pasteSelectionRangeColLen % colCount === 0) {
            const hasLapWithMerge = mergeData?.some((merge) => rangeIntersectWithDiscreteRange(merge, discreteRange));
            if (!hasLapWithMerge) {
                for (let r = 0; r < pasteSelectionRangeRowLen; r++) {
                    for (let c = 0; c < pasteSelectionRangeColLen; c++) {
                        const cell = cellMatrix.getValue(r % rowCount, c % colCount);
                        cell && cellMatrix.setValue(r, c, cell);
                    }
                }
                return {
                    pastedRange: discreteRange,
                    unitId,
                    subUnitId,
                };
            }
        }
        const newDiscreteRange = this._expandOrShrinkRowsCols(unitId, subUnitId, discreteRange, colCount, rowCount);

        discreteRange.rows = newDiscreteRange.rows;
        discreteRange.cols = newDiscreteRange.cols;

        return {
            pastedRange: discreteRange,
            unitId,
            subUnitId,
        };
    }

    private _expandOrShrinkRowsCols(unitId: string, subUnitId: string, range: IDiscreteRange, colCount: number, rowCount: number) {
        const { rows, cols } = range;
        const workbook = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
        const worksheet = workbook?.getSheetBySheetId(subUnitId);
        let newRows: number[];
        let newCols: number[];
        if (rows.length >= rowCount) {
            newRows = rows.slice(0, rowCount);
        } else {
            newRows = rows.slice(0);
            let rowIndex = rows[rows.length - 1] + 1;
            while (newRows.length < rowCount) {
                if (!worksheet!.getRowFiltered(rowIndex)) {
                    newRows.push(rowIndex);
                }
                rowIndex++;
            }
        }

        if (cols.length >= colCount) {
            newCols = cols.slice(0, colCount);
        } else {
            newCols = cols.slice(0);
            let colIndex = cols[cols.length - 1] + 1;
            while (newCols.length < colCount) {
                newCols.push(colIndex);
                colIndex++;
            }
        }

        return {
            rows: newRows,
            cols: newCols,
        };
    }

    /**
     * Determine whether the cells starting from the upper left corner of the range (merged or non-merged or combined) are consistent with the size of the original data
     * @param cellMatrix
     * @param range
     */
    private _topLeftCellsMatch(rowCount: number, colCount: number, range: { topRow: number; leftCol: number }): boolean {
        const workbook = this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
        const worksheet = workbook?.getActiveSheet();
        if (!worksheet) {
            return false;
        }

        const { topRow, leftCol } = range;
        const isRowAcross = rowAcrossMergedCell(
            topRow + rowCount - 1,
            leftCol,
            leftCol + colCount - 1,
            worksheet
        );
        const isColAcross = columnAcrossMergedCell(
            leftCol + colCount - 1,
            topRow,
            topRow + rowCount - 1,
            worksheet
        );

        return !isRowAcross && !isColAcross;
    }

    removeMarkSelection() {
        if (this._copyMarkId) {
            this._markSelectionService.removeShape(this._copyMarkId);
            this._copyMarkId = null;
        }
    }

    private _initUnitDisposed() {
        this.disposeWithMe(
            this._univerInstanceService.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
                if (workbook) {
                    const copyCache = this.copyContentCache();
                    copyCache.clearWithUnitId(workbook.getUnitId());
                }
            })
        );
    }

    disposePasteOptionsCache() {
        this.setShowMenu(false);
        this.updatePasteOptionsCache(null);
    }
}

// #region copy generation

export function getMatrixPlainText(matrix: ObjectMatrix<ICellDataWithSpanAndDisplay>) {
    let plain = '';
    const matrixLength = matrix.getLength();
    matrix.forRow((row, cols) => {
        const arr: string[] = [];
        cols.forEach((col) => {
            const cell = matrix.getValue(row, col);
            if (cell) {
                const cellText = getCellTextForClipboard(cell);
                arr.push(cellText);
            }
        });
        plain += arr.join('\t');
        if (row !== matrixLength - 1) {
            plain += '\n';
        }
    });

    return plain;
}

function getCellTextForClipboard(cell: ICellDataWithSpanAndDisplay) {
    if (isNotNullOrUndefined(cell.displayV)) {
        return cell.displayV;
    }
    return extractPureTextFromCell(cell);
}

export const escapeSpecialCode = (cellStr: string) =>
    cellStr
        .replace(/&/g, '&amp;')
        .replace(/\uFEFF/g, '')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;');

function rowAcrossMergedCell(row: number, startColumn: number, endColumn: number, worksheet: Worksheet): boolean {
    return worksheet
        .getMergeData()
        .some(
            (mergedCell) =>
                mergedCell.startRow <= row &&
                row < mergedCell.endRow &&
                startColumn <= mergedCell.startColumn &&
                mergedCell.startColumn <= endColumn
        );
}

function columnAcrossMergedCell(col: number, startRow: number, endRow: number, worksheet: Worksheet): boolean {
    return worksheet
        .getMergeData()
        .some(
            (mergedCell) =>
                mergedCell.startColumn <= col &&
                col < mergedCell.endColumn &&
                startRow <= mergedCell.startRow &&
                mergedCell.startRow <= endRow
        );
}

/**
 * Determine whether CellMatrix consists of multiple cells, it must consist of 2 or more cells. It can be an ordinary cell or merge cell
 * @param cellMatrix
 */
function isMultipleCells(cellMatrix: ObjectMatrix<ICellDataWithSpanAndDisplay>): boolean {
    let count = 0;
    cellMatrix.forValue((row, col, cell) => {
        if (cell) {
            count++;
        }

        if (count > 1) {
            return false;
        }
    });
    return count > 1;
}

// #endregion