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.

23.1 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type { IMutationInfo, IRange, Nullable, Workbook } from '@univerjs/core';
import type { IDrawingJsonUndo1 } from '@univerjs/drawing';
import type { IDiscreteRange } from '@univerjs/sheets';
import type { IRemoveSheetDrawingCommandParams, ISheetDrawing, ISheetImage } from '@univerjs/sheets-drawing';
import type { IPasteHookValueType, ISheetDiscreteRangeLocation } from '@univerjs/sheets-ui';
import {
    BooleanNumber,
    CustomCommandExecutionError,
    DrawingTypeEnum,
    generateRandomId,
    ICommandService,
    ImageSourceType,
    Inject,
    IUniverInstanceService,
    RxDisposable,
    SheetTypes,
    Tools,
    UniverInstanceType,
} from '@univerjs/core';
import { IDrawingManagerService } from '@univerjs/drawing';
import { IRenderManagerService } from '@univerjs/engine-render';
import {
    attachRangeWithCoord,
    discreteRangeToRange,
    SheetPermissionCheckController,
    SheetSkeletonService,
    WorkbookCopyPermission,
    WorkbookEditablePermission,
    WorksheetCopyPermission,
    WorksheetEditPermission,
} from '@univerjs/sheets';
import {
    DrawingApplyType,
    RemoveSheetDrawingCommand,
    SetDrawingApplyMutation,
    SheetDrawingAnchorType,
    transformDrawingsToChartSheet,
    transformToAxisAlignPosition,
    transformToDrawingPosition,
} from '@univerjs/sheets-drawing';
import {
    COPY_TYPE,
    ISheetClipboardService,
    PREDEFINED_HOOK_NAME_PASTE,
    SHEET_VIEW_KEY,
    SheetPasteCommand,
    virtualizeDiscreteRanges,
} from '@univerjs/sheets-ui';
import { IClipboardInterfaceService } from '@univerjs/ui';
import { takeUntil, timer } from 'rxjs';
import { InsertFloatImageCommand } from '../commands/commands/insert-image.command';

const IMAGE_PNG_MIME_TYPE = 'image/png';

function base64ToBlob(base64: string) {
    const arr = base64.split(',');
    const binStr = atob(arr[1]);
    const len = binStr.length;
    const bytes = new Uint8Array(len);

    for (let i = 0; i < len; i++) {
        bytes[i] = binStr.charCodeAt(i);
    }

    return new Blob([bytes], { type: IMAGE_PNG_MIME_TYPE });
}

function copyBase64ToClipboard(base64: string) {
    const item = new ClipboardItem({ [IMAGE_PNG_MIME_TYPE]: base64ToBlob(base64) });
    navigator.clipboard.write([item]).catch((err) => {
        console.error('Could not copy image using clipboard API: ', err);
    });
}

function focusDocument() {
    function createInputElement() {
        const input = document.createElement('input');
        input.style.position = 'absolute';
        input.style.height = '1px';
        input.style.width = '1px';
        input.style.opacity = '0';

        return input;
    }

    const activeElement = document.activeElement;
    const input = createInputElement();
    document.body.appendChild(input);
    input.focus();

    return () => {
        input.blur();
        document.body.removeChild(input);
        if (activeElement instanceof HTMLElement) {
            activeElement.focus();
        }
    };
}

const specialPastes: IPasteHookValueType[] = [
    PREDEFINED_HOOK_NAME_PASTE.SPECIAL_PASTE_COL_WIDTH,
    PREDEFINED_HOOK_NAME_PASTE.SPECIAL_PASTE_VALUE,
    PREDEFINED_HOOK_NAME_PASTE.SPECIAL_PASTE_FORMAT,
    PREDEFINED_HOOK_NAME_PASTE.SPECIAL_PASTE_FORMULA,
];

export class SheetsDrawingCopyPasteController extends RxDisposable {
    override dispose(): void {
        this._copyInfo = null;
        super.dispose();
    }

    private _copyInfo: Nullable<{
        drawings: ISheetDrawing[];
        copyRange?: IRange;
        unitId: string;
        subUnitId: string;
    }>;

    constructor(
        @ISheetClipboardService private _sheetClipboardService: ISheetClipboardService,
        @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
        @Inject(SheetSkeletonService) private readonly _sheetSkeletonService: SheetSkeletonService,
        @IDrawingManagerService private readonly _drawingService: IDrawingManagerService,
        @IClipboardInterfaceService private readonly _clipboardInterfaceService: IClipboardInterfaceService,
        @ICommandService private readonly _commandService: ICommandService,
        @IUniverInstanceService private readonly _instanceService: IUniverInstanceService,
        @Inject(SheetPermissionCheckController) private readonly _permissions: SheetPermissionCheckController
    ) {
        super();
        this._initCopyPaste();
    }

    private get _focusedDrawings() {
        return this._drawingService.getFocusDrawings() as ISheetDrawing[];
    }

    private _initCopyPaste() {
        this.disposeWithMe(this._sheetClipboardService.addClipboardHook({
            id: 'SHEET_IMAGE_UI_PLUGIN',

            onBeforeCopyFocusedObject: (unitId, subUnitId, copyType) => {
                this._copyInfo = null;
                const drawings = this._focusedDrawings.filter((drawing) => drawing.drawingType === DrawingTypeEnum.DRAWING_IMAGE);
                if (drawings.length === 0) {
                    return false;
                }

                const [drawing] = drawings;

                if (copyType === COPY_TYPE.CUT) {
                    const worksheet = this._instanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)?.getSheetBySheetId(subUnitId);
                    // Image CUT deletes immediately and then replaces the clipboard with native image data.
                    // Chart Sheet cannot consume that fallback, so reject before removing the source image.
                    if (worksheet?.getConfig().type === SheetTypes.CHART) {
                        return false;
                    }
                    const params: IRemoveSheetDrawingCommandParams = {
                        unitId,
                        drawings,
                    };
                    // Delete the drawing when it is cut
                    this._commandService.executeCommand<IRemoveSheetDrawingCommandParams>(RemoveSheetDrawingCommand.id, params);
                }

                timer(200).pipe(takeUntil(this.dispose$)).subscribe(() => {
                    // Rewrite the clipboard content to prevent the default copy behavior, prevent to call the 'onPasteCells'
                    const dispose = focusDocument();
                    if (drawing.drawingType === DrawingTypeEnum.DRAWING_IMAGE
                        && (drawing as ISheetImage).imageSourceType === ImageSourceType.BASE64) {
                        copyBase64ToClipboard((drawing as ISheetImage).source);
                    } else {
                        this._clipboardInterfaceService.writeText('');
                    }
                    dispose();
                });

                this._copyInfo = {
                    unitId: drawing.unitId,
                    subUnitId: drawing.subUnitId,
                    drawings,
                };
                return true;
            },

            onBeforeCopy: (unitId, subUnitId, range) => {
                this._copyInfo = this._createDrawingsCopyInfoByRange(unitId, subUnitId, range);
            },

            onPasteCells: (pasteFrom, pasteTo, _data, payload) => {
                if (!payload.copyId) {
                    this._copyInfo = null;
                    return { redos: [], undos: [] };
                }

                if (!pasteFrom || !this._copyInfo) {
                    return { redos: [], undos: [] };
                }

                const { copyType = COPY_TYPE.COPY, pasteType } = payload;
                const { range: copyRange, unitId: fromUnitId, subUnitId: fromSubUnitId } = pasteFrom;
                const { range: pasteRange, unitId: toUnitId, subUnitId: toSubUnitId } = pasteTo;

                const mutations = this._copyInfo.copyRange
                    ? this._generateRangeDrawingsPasteMutations(
                        { unitId: toUnitId, subUnitId: toSubUnitId, pasteRange, pasteType },
                        { unitId: fromUnitId, subUnitId: fromSubUnitId, copyType, copyRange }
                    )
                    : this._generateSingleDrawingPasteMutations({ pasteTo, pasteType }, COPY_TYPE.COPY);

                return mutations;
            },

            onPastePlainText: (_pasteTo: ISheetDiscreteRangeLocation, _clipText: string) => {
                this._copyInfo = null;
                return { undos: [], redos: [] };
            },

            onPasteUnrecognized: (pasteTo: ISheetDiscreteRangeLocation) => {
                if (!this._copyInfo || this._copyInfo.copyRange) {
                    return { undos: [], redos: [] };
                }

                const mutations = this._generateSingleDrawingPasteMutations({ pasteTo, pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }, COPY_TYPE.COPY);
                this._copyInfo = null;
                return mutations;
            },

            onPasteFiles: (pasteTo: ISheetDiscreteRangeLocation, files) => {
                if (this._copyInfo && !this._copyInfo.copyRange) {
                    const mutations = this._generateSingleDrawingPasteMutations({ pasteTo, pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }, COPY_TYPE.COPY);
                    this._copyInfo = null;
                    return mutations;
                }

                const images = files.filter((file) => file.type.includes('image'));
                if (images.length) {
                    this._copyInfo = null;
                    return {
                        undos: [],
                        redos: [
                            {
                                id: InsertFloatImageCommand.id,
                                params: { files: images },
                            },
                        ],
                    };
                }

                return { undos: [], redos: [] };
            },
        }));
    }

    private _createDrawingsCopyInfoByRange(unitId: string, subUnitId: string, range: IRange) {
        const skeleton = this._sheetSkeletonService.getSkeleton(unitId, subUnitId);
        if (!skeleton) return;

        const selectionRect = attachRangeWithCoord(skeleton, range);
        if (!selectionRect) return;

        const { startX, endX, startY, endY } = selectionRect;
        const drawings = this._drawingService.getDrawingData(unitId, subUnitId);

        const containedDrawings: ISheetDrawing[] = [];

        Object.keys(drawings).forEach((drawingId) => {
            const drawing = drawings[drawingId];
            if (drawing.drawingType !== DrawingTypeEnum.DRAWING_IMAGE) {
                return;
            }
            const anchorType = (drawing as ISheetDrawing).anchorType ?? SheetDrawingAnchorType.Position;
            if (anchorType === SheetDrawingAnchorType.None) {
                return;
            }
            const { transform } = drawing;
            if (!transform) {
                return;
            }
            const { left = 0, top = 0, width = 0, height = 0 } = transform;
            const { drawingStartX, drawingEndX, drawingStartY, drawingEndY } = {
                drawingStartX: left,
                drawingEndX: left + width,
                drawingStartY: top,
                drawingEndY: top + height,
            };

            if (startX <= drawingStartX && drawingEndX <= endX && startY <= drawingStartY && drawingEndY <= endY) {
                containedDrawings.push(drawing as ISheetDrawing);
            }
        });

        if (containedDrawings.length) {
            return {
                copyRange: range,
                drawings: containedDrawings,
                unitId,
                subUnitId,
            };
        }
    }

    private _generateSingleDrawingPasteMutations(pasteContext: {
        pasteTo: ISheetDiscreteRangeLocation;
        pasteType: IPasteHookValueType;
    }, copyType: COPY_TYPE) {
        const { pasteType, pasteTo } = pasteContext;

        if (specialPastes.includes(pasteType)) {
            return { redos: [], undos: [] };
        }

        const { unitId, subUnitId, range } = pasteTo;
        const worksheet = this._instanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)?.getSheetBySheetId(subUnitId);
        if (worksheet?.getConfig().type === SheetTypes.CHART) {
            return this._generateChartSheetImagePasteMutations(pasteTo);
        }
        const pasteToSkeleton = this._sheetSkeletonService.getSkeleton(unitId, subUnitId);
        if (!pasteToSkeleton) {
            return { redos: [], undos: [] };
        }

        const copyInfo = this._copyInfo!;
        const { drawings } = copyInfo;
        const pasteRange = discreteRangeToRange(range);
        let drawingAnchor: { left: number; top: number } | undefined;
        if (drawings.length > 1) {
            let anchorLeft = Infinity;
            let anchorTop = Infinity;
            for (const drawing of drawings) {
                const { left = 0, top = 0 } = drawing.transform ?? {};
                anchorLeft = Math.min(anchorLeft, left);
                anchorTop = Math.min(anchorTop, top);
            }
            drawingAnchor = { left: anchorLeft, top: anchorTop };
        }

        return this._generateMutations(drawings, {
            unitId,
            subUnitId,
            isCut: copyType === COPY_TYPE.CUT,
            getTransform: (transform, sheetTransform) => {
                const pasteRect = attachRangeWithCoord(pasteToSkeleton, {
                    startRow: pasteRange.startRow,
                    endRow: pasteRange.endRow,
                    startColumn: pasteRange.startColumn,
                    endColumn: pasteRange.endColumn,
                });

                const newTransform = {
                    ...transform,
                    left: drawingAnchor && pasteRect
                        ? pasteRect.startX + (transform?.left ?? 0) - drawingAnchor.left
                        : pasteRect?.startX,
                    top: drawingAnchor && pasteRect
                        ? pasteRect.startY + (transform?.top ?? 0) - drawingAnchor.top
                        : pasteRect?.startY,
                };

                return {
                    transform: newTransform,
                    sheetTransform: transformToDrawingPosition(newTransform, pasteToSkeleton) ?? sheetTransform,
                    axisAlignSheetTransform: transformToAxisAlignPosition(newTransform, pasteToSkeleton) ?? sheetTransform,
                };
            },
        });
    }

    private _generateChartSheetImagePasteMutations(pasteTo: ISheetDiscreteRangeLocation) {
        const { unitId, subUnitId } = pasteTo;
        const worksheet = this._instanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)!.getSheetBySheetId(subUnitId)!;
        if (!this._permissions.permissionCheckWithoutRange({
            workbookTypes: [WorkbookEditablePermission],
            worksheetTypes: [WorksheetEditPermission],
        }, unitId, subUnitId)) {
            throw new CustomCommandExecutionError(SheetPasteCommand.id);
        }
        const source = this._copyInfo!;
        if (!this._permissions.permissionCheckWithoutRange({
            workbookTypes: [WorkbookCopyPermission],
            worksheetTypes: [WorksheetCopyPermission],
        }, source.unitId, source.subUnitId) || source.drawings.some((drawing) =>
            !Tools.diffValue(drawing, this._drawingService.getDrawingByParam(drawing)))) {
            throw new CustomCommandExecutionError(SheetPasteCommand.id);
        }
        const frame = this._renderManagerService.getRenderUnitById(unitId)?.components.get(SHEET_VIEW_KEY.CHART_PAGE);
        const originals = this._copyInfo!.drawings;
        const placed = frame && transformDrawingsToChartSheet(originals, {
            left: frame.left,
            top: frame.top,
            width: frame.width,
            height: frame.height,
        }, worksheet.getConfig().rightToLeft === BooleanNumber.TRUE);
        if (!placed) {
            return { redos: [], undos: [] };
        }
        const copies = new Map(placed.map((drawing) => [drawing.drawingId, drawing]));
        return this._generateMutations(originals, {
            unitId,
            subUnitId,
            isCut: false,
            getTransform: (_transform, _sheetTransform, drawing) => {
                const copy = copies.get(drawing.drawingId)!;
                return { anchorType: copy.anchorType, transform: copy.transform, sheetTransform: copy.sheetTransform, axisAlignSheetTransform: copy.axisAlignSheetTransform };
            },
        });
    }

    private _generateMutations(drawings: ISheetDrawing[], payload: {
        unitId: string;
        subUnitId: string;
        getTransform: (transform: ISheetDrawing['transform'], sheetTransform: ISheetDrawing['sheetTransform'], drawing: ISheetDrawing) => {
            anchorType?: SheetDrawingAnchorType;
            transform: ISheetDrawing['transform'];
            sheetTransform: ISheetDrawing['sheetTransform'];
            axisAlignSheetTransform: ISheetDrawing['sheetTransform'];
        };
        isCut: boolean;
    }) {
        const {
            unitId,
            subUnitId,
            getTransform,
            isCut,
        } = payload;

        const redos: IMutationInfo[] = [];
        const undos: IMutationInfo[] = [];

        const { _drawingService } = this;

        drawings.forEach((drawing) => {
            const { transform, sheetTransform } = drawing;
            if (!transform) {
                return;
            }
            const transformContext = getTransform(transform, sheetTransform, drawing);
            const drawingObject: Partial<ISheetDrawing> = {
                ...drawing,
                ...transformContext,
                drawingId: isCut ? drawing.drawingId : generateRandomId(),
                unitId,
                subUnitId,
            };

            if (isCut) {
                const { undo, redo, objects } = _drawingService.getBatchUpdateOp([drawingObject] as ISheetDrawing[]) as IDrawingJsonUndo1;
                redos.push({
                    id: SetDrawingApplyMutation.id,
                    params: {
                        unitId,
                        subUnitId,
                        type: DrawingApplyType.UPDATE,
                        op: redo,
                        objects,
                    },
                });
                undos.push({
                    id: SetDrawingApplyMutation.id,
                    params: {
                        unitId,
                        subUnitId,
                        type: DrawingApplyType.UPDATE,
                        op: undo,
                        objects,
                    },
                });
            } else {
                const { undo, redo, objects } = _drawingService.getBatchAddOp([drawingObject] as ISheetDrawing[]) as IDrawingJsonUndo1;
                redos.push({ id: SetDrawingApplyMutation.id, params: { op: redo, unitId, subUnitId, objects, type: DrawingApplyType.INSERT } });
                undos.push({ id: SetDrawingApplyMutation.id, params: { op: undo, unitId, subUnitId, objects, type: DrawingApplyType.REMOVE } });
            }
        });

        return { redos, undos };
    }

    private _generateRangeDrawingsPasteMutations(
        pasteContext: {
            unitId: string;
            subUnitId: string;
            pasteRange: IDiscreteRange;
            pasteType: IPasteHookValueType;
        },
        copyContext: {
            unitId?: string;
            subUnitId?: string;
            copyRange?: IDiscreteRange;
            copyType: COPY_TYPE;
        }
    ) {
        if (!this._copyInfo) {
            return { redos: [], undos: [] };
        }

        const { unitId: toUnitId, subUnitId: toSubUnitId, pasteRange, pasteType } = pasteContext;
        const { unitId: fromUnitId, subUnitId: fromSubUnitId, copyRange, copyType } = copyContext;

        if (specialPastes.includes(pasteType)) {
            return { redos: [], undos: [] };
        }

        const toSkeleton = this._sheetSkeletonService.getSkeleton(toUnitId, toSubUnitId);
        if (!toSkeleton) {
            return { redos: [], undos: [] };
        }

        const { drawings } = this._copyInfo;

        if (!copyRange) {
            return this._generateSingleDrawingPasteMutations({
                pasteTo: { unitId: toUnitId, subUnitId: toSubUnitId, range: discreteRangeToRange(pasteRange) as unknown as IDiscreteRange },
                pasteType,
            }, copyType);
        }

        const fromSkeleton = this._sheetSkeletonService.getSkeleton(fromUnitId!, fromSubUnitId!);
        if (!fromSkeleton) {
            return { redos: [], undos: [] };
        }

        const { ranges: [vCopyRange, vPastedRange], mapFunc } = virtualizeDiscreteRanges([copyRange, pasteRange]);
        const { row: copyRow, col: copyCol } = mapFunc(vCopyRange.startRow, vCopyRange.startColumn);
        const { row: pasteRow, col: pasteCol } = mapFunc(vPastedRange.startRow, vPastedRange.startColumn);

        const copyRect = attachRangeWithCoord(fromSkeleton, {
            startRow: copyRow,
            endRow: copyRow,
            startColumn: copyCol,
            endColumn: copyCol,
        });
        const pasteRect = attachRangeWithCoord(toSkeleton, {
            startRow: pasteRow,
            endRow: pasteRow,
            startColumn: pasteCol,
            endColumn: pasteCol,
        });

        if (!copyRect || !pasteRect) {
            return { redos: [], undos: [] };
        }

        const leftOffset = pasteRect.startX - copyRect.startX;
        const topOffset = pasteRect.startY - copyRect.startY;
        const rowOffset = pasteRow - copyRow;
        const columnOffset = pasteCol - copyCol;

        return this._generateMutations(drawings, {
            unitId: toUnitId,
            subUnitId: toSubUnitId,
            getTransform: (transform, sheetTransform) => {
                const newTransform = {
                    ...transform,
                    left: (transform?.left ?? 0) + leftOffset,
                    top: (transform?.top ?? 0) + topOffset,
                };

                return {
                    transform: newTransform,
                    sheetTransform: {
                        ...sheetTransform,
                        to: {
                            ...sheetTransform.to,
                            row: sheetTransform.to.row + rowOffset,
                            column: sheetTransform.to.column + columnOffset,
                        },
                        from: {
                            ...sheetTransform.from,
                            row: sheetTransform.from.row + rowOffset,
                            column: sheetTransform.from.column + columnOffset,
                        },
                    },
                    axisAlignSheetTransform: transformToAxisAlignPosition(newTransform, toSkeleton) ?? sheetTransform,
                };
            },
            isCut: copyType === COPY_TYPE.CUT,
        });
    }
}