Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
* limitations under the License.
*/

import { DrawingTypeEnum, ImageSourceType } from '@univerjs/core';
import type { IRange } from '@univerjs/core';
import type { ISheetClipboardHook } from '@univerjs/sheets-ui';
import { DrawingTypeEnum, ICommandService, ImageSourceType, Injector, ObjectMatrix } from '@univerjs/core';
import { IDrawingManagerService } from '@univerjs/drawing';
import { IRenderManagerService } from '@univerjs/engine-render';
import { SheetSkeletonService } from '@univerjs/sheets';
import { RemoveSheetDrawingCommand, SheetDrawingAnchorType } from '@univerjs/sheets-drawing';
import { COPY_TYPE, PREDEFINED_HOOK_NAME_PASTE } from '@univerjs/sheets-ui';
import { COPY_TYPE, ISheetClipboardService, PREDEFINED_HOOK_NAME_PASTE } from '@univerjs/sheets-ui';
import { IClipboardInterfaceService } from '@univerjs/ui';
import { describe, expect, it, vi } from 'vitest';
import { InsertFloatImageCommand } from '../../commands/commands/insert-image.command';
import { SheetsDrawingCopyPasteController } from '../sheet-drawing-copy-paste.controller';

function createSkeleton() {
Expand Down Expand Up @@ -63,35 +70,56 @@ function createImageDrawing(overrides: Record<string, unknown> = {}) {
};
}

function createController(options?: { focusedDrawings?: any[]; drawingData?: Record<string, any> }) {
let hook: any;
type IImageDrawing = ReturnType<typeof createImageDrawing>;

interface IPrivateControllerAccess {
_copyInfo: {
unitId: string;
subUnitId: string;
copyRange?: IRange;
drawings: IImageDrawing[];
} | null;
}

interface ITestClipboardHook extends ISheetClipboardHook {
onBeforeCopy: NonNullable<ISheetClipboardHook['onBeforeCopy']>;
onBeforeCopyFocusedObject?: (unitId: string, subUnitId: string, copyType: COPY_TYPE) => boolean;
onPasteCells: NonNullable<ISheetClipboardHook['onPasteCells']>;
onPasteFiles: NonNullable<ISheetClipboardHook['onPasteFiles']>;
onPasteUnrecognized: NonNullable<ISheetClipboardHook['onPasteUnrecognized']>;
}

function createController(options?: { focusedDrawings?: IImageDrawing[]; drawingData?: Record<string, object> }) {
let hook: ISheetClipboardHook | undefined;
const skeleton = createSkeleton();
const drawingService = {
getFocusDrawings: vi.fn(() => options?.focusedDrawings ?? []),
getDrawingData: vi.fn(() => options?.drawingData ?? {}),
getBatchAddOp: vi.fn((drawings) => ({ undo: 'add-undo', redo: 'add-redo', objects: drawings })),
getBatchUpdateOp: vi.fn((drawings) => ({ undo: 'update-undo', redo: 'update-redo', objects: drawings })),
getBatchAddOp: vi.fn((drawings: IImageDrawing[]) => ({ undo: 'add-undo', redo: 'add-redo', objects: drawings })),
getBatchUpdateOp: vi.fn((drawings: IImageDrawing[]) => ({ undo: 'update-undo', redo: 'update-redo', objects: drawings })),
};
const commandService = {
executeCommand: vi.fn(),
};
const sheetClipboardService = {
addClipboardHook: vi.fn((config) => {
addClipboardHook: vi.fn((config: ISheetClipboardHook) => {
hook = config;
return { dispose: vi.fn() };
}),
};

const controller = new SheetsDrawingCopyPasteController(
sheetClipboardService as never,
{} as never,
{ getSkeleton: vi.fn(() => skeleton) } as never,
drawingService as never,
{ writeText: vi.fn() } as never,
commandService as never
);
const injector = new Injector([
[ISheetClipboardService, { useValue: sheetClipboardService }],
[IRenderManagerService, { useValue: {} }],
[SheetSkeletonService, { useValue: { getSkeleton: vi.fn(() => skeleton) } }],
[IDrawingManagerService, { useValue: drawingService }],
[IClipboardInterfaceService, { useValue: { writeText: vi.fn() } }],
[ICommandService, { useValue: commandService }],
[SheetsDrawingCopyPasteController],
]);
const controller = injector.get(SheetsDrawingCopyPasteController);

return { controller, hook, skeleton, drawingService, commandService };
return { controller, hook: hook as ITestClipboardHook, skeleton, drawingService, commandService };
}

describe('SheetsDrawingCopyPasteController', () => {
Expand All @@ -106,6 +134,7 @@ describe('SheetsDrawingCopyPasteController', () => {
anchorType: SheetDrawingAnchorType.Position,
});
const { controller, hook, drawingService } = createController({
focusedDrawings: [outsideDrawing],
drawingData: {
[containedDrawing.drawingId]: containedDrawing,
[outsideDrawing.drawingId]: outsideDrawing,
Expand All @@ -120,7 +149,6 @@ describe('SheetsDrawingCopyPasteController', () => {
startColumn: 0,
endColumn: 1,
}, COPY_TYPE.COPY);

const mutations = hook.onPasteCells(
{
unitId: 'unit-1',
Expand All @@ -132,8 +160,8 @@ describe('SheetsDrawingCopyPasteController', () => {
subUnitId: 'sheet-2',
range: { rows: [2, 3], cols: [3, 4] },
},
null,
{ copyType: COPY_TYPE.COPY, pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }
new ObjectMatrix(),
{ copyId: 'range-copy', copyType: COPY_TYPE.COPY, pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }
);

expect(drawingService.getBatchAddOp).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -161,27 +189,26 @@ describe('SheetsDrawingCopyPasteController', () => {
focusedDrawings: [focusedDrawing],
});

hook.onBeforeCopy('unit-1', 'sheet-1', {
startRow: 0,
endRow: 0,
startColumn: 0,
endColumn: 0,
}, COPY_TYPE.CUT);

expect(hook.onBeforeCopyFocusedObject).toBeTypeOf('function');
expect(hook.onBeforeCopyFocusedObject?.('unit-1', 'sheet-1', COPY_TYPE.CUT)).toBe(true);
expect(commandService.executeCommand).toHaveBeenCalledWith(RemoveSheetDrawingCommand.id, {
unitId: 'unit-1',
drawings: [focusedDrawing],
});

const mutations = hook.onPasteCells(
undefined,
{
unitId: 'unit-1',
subUnitId: 'sheet-1',
range: { rows: [0], cols: [0] },
},
{
unitId: 'unit-1',
subUnitId: 'sheet-1',
range: { rows: [3], cols: [4] },
},
null,
{ copyType: COPY_TYPE.CUT, pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }
new ObjectMatrix(),
{ copyId: 'image-cut', copyType: COPY_TYPE.CUT, pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }
);

expect(drawingService.getBatchAddOp).toHaveBeenCalledTimes(1);
Expand All @@ -198,4 +225,166 @@ describe('SheetsDrawingCopyPasteController', () => {

controller.dispose();
});

it('copies all focused image drawings', () => {
const firstDrawing = createImageDrawing({ drawingId: 'first-image' });
const secondDrawing = createImageDrawing({
drawingId: 'second-image',
transform: { left: 30, top: 40, width: 10, height: 20 },
});
const { controller, hook, drawingService } = createController({
focusedDrawings: [firstDrawing, secondDrawing],
});

expect(hook.onBeforeCopyFocusedObject?.('unit-1', 'sheet-1', COPY_TYPE.COPY)).toBe(true);

const mutations = hook.onPasteFiles(
{
unitId: 'unit-1',
subUnitId: 'sheet-1',
range: { rows: [3], cols: [4] },
},
[new File(['clipboard'], 'clipboard-image.png', { type: 'image/png' })],
{ pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }
);

expect(drawingService.getBatchAddOp).toHaveBeenCalledTimes(2);
expect(drawingService.getBatchAddOp.mock.calls.map(([drawings]) => drawings[0].transform)).toEqual([
expect.objectContaining({ left: 40, top: 60 }),
expect.objectContaining({ left: 65, top: 95 }),
]);
expect(mutations.redos).toHaveLength(2);

controller.dispose();
});

it('pastes external image files instead of the previous copied drawing', () => {
const focusedDrawing = createImageDrawing({ drawingId: 'focused-image' });
const { controller, hook } = createController({
focusedDrawings: [focusedDrawing],
});
const internalImage = new File(['internal'], 'clipboard-image.png', { type: 'image/png' });
const externalImage = new File(['external'], 'external.png', { type: 'image/png' });

(controller as unknown as IPrivateControllerAccess)._copyInfo = {
unitId: focusedDrawing.unitId,
subUnitId: focusedDrawing.subUnitId,
drawings: [focusedDrawing],
};

hook.onPasteFiles(
{
unitId: 'unit-1',
subUnitId: 'sheet-1',
range: { rows: [0], cols: [0] },
},
[internalImage],
{ pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }
);

const mutations = hook.onPasteFiles(
{
unitId: 'unit-1',
subUnitId: 'sheet-1',
range: { rows: [0], cols: [0] },
},
[externalImage],
{ pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }
);

expect(mutations).toEqual({
undos: [],
redos: [{
id: InsertFloatImageCommand.id,
params: { files: [externalImage] },
}],
});

controller.dispose();
});

it('clears a copied image after pasting external cell content', () => {
const focusedDrawing = createImageDrawing({ drawingId: 'focused-image' });
const { controller, hook } = createController({
focusedDrawings: [focusedDrawing],
});

(controller as unknown as IPrivateControllerAccess)._copyInfo = {
unitId: focusedDrawing.unitId,
subUnitId: focusedDrawing.subUnitId,
drawings: [focusedDrawing],
};

const mutations = hook.onPasteCells(
null,
{
unitId: 'unit-1',
subUnitId: 'sheet-1',
range: { rows: [0], cols: [0] },
},
new ObjectMatrix(),
{ copyType: COPY_TYPE.COPY, pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }
);

expect(mutations).toEqual({ undos: [], redos: [] });

const externalImage = new File(['external'], 'external.png', { type: 'image/png' });
expect(hook.onPasteFiles(
{
unitId: 'unit-1',
subUnitId: 'sheet-1',
range: { rows: [0], cols: [0] },
},
[externalImage],
{ pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }
).redos).toEqual([{
id: InsertFloatImageCommand.id,
params: { files: [externalImage] },
}]);

expect(hook.onPasteUnrecognized({
unitId: 'unit-1',
subUnitId: 'sheet-1',
range: { rows: [0], cols: [0] },
})).toEqual({ undos: [], redos: [] });

controller.dispose();
});

it('clears a copied image after an unrecognized internal paste', () => {
const focusedDrawing = createImageDrawing({ drawingId: 'focused-image' });
const { controller, hook } = createController({
focusedDrawings: [focusedDrawing],
});
const externalImage = new File(['external'], 'external.png', { type: 'image/png' });

(controller as unknown as IPrivateControllerAccess)._copyInfo = {
unitId: focusedDrawing.unitId,
subUnitId: focusedDrawing.subUnitId,
drawings: [focusedDrawing],
};

hook.onPasteUnrecognized({
unitId: 'unit-1',
subUnitId: 'sheet-1',
range: { rows: [0], cols: [0] },
});

const mutations = hook.onPasteFiles(
{
unitId: 'unit-1',
subUnitId: 'sheet-1',
range: { rows: [0], cols: [0] },
},
[externalImage],
{ pasteType: PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE }
);

expect(mutations.redos).toEqual([{
id: InsertFloatImageCommand.id,
params: { files: [externalImage] },
}]);

controller.dispose();
});
});
Loading