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.

7.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 {
    ContextService,
    DrawingTypeEnum,
    ICommandService,
    IContextService,
    IImageIoService,
    Injector,
    IPermissionService,
    IUniverInstanceService,
    LocaleService,
    LocaleType,
    toDisposable,
} from '@univerjs/core';
import { IDrawingManagerService } from '@univerjs/drawing';
import { OleUIService } from '@univerjs/drawing-ui';
import { IRenderManagerService } from '@univerjs/engine-render';
import { SheetCanvasPopManagerService } from '@univerjs/sheets-ui';
import { IDialogService, IMenuManagerService, IMessageService, MenuItemType } from '@univerjs/ui';
import { Subject } from 'rxjs';
import { describe, expect, it, vi } from 'vitest';
import { DrawingPopupMenuController } from '../drawing-popup-menu.controller';

function createControllerHarness(initialDrawingType: DrawingTypeEnum, icon?: string) {
    const injector = new Injector();
    injector.add([IPermissionService, { useValue: { getPermissionPoint: () => undefined } as never }]);
    injector.add([OleUIService, { useValue: { registerHost: () => toDisposable(() => undefined) } as never }]);
    const createControl$ = new Subject<void>();
    const clearControl$ = new Subject<void>();
    const changing$ = new Subject<void>();
    const imageIoChange$ = new Subject<number>();
    const currentWorkbook$ = new Subject<never>();
    const disposedWorkbook$ = new Subject<never>();
    const dialogs$ = new Subject<never>();
    const drawingObject = { oKey: 'drawing-1' };
    const popupDisposable = {
        dispose: vi.fn(),
        canDispose: () => true,
    };
    let drawingType = initialDrawingType;
    let attachedPopup: { extraProps?: Record<string, unknown> } | undefined;
    const attachPopupToObject = vi.fn((_targetObject: unknown, popup: { extraProps?: Record<string, unknown> }) => {
        attachedPopup = popup;
        return popupDisposable;
    });

    injector.add([LocaleService]);
    injector.add([IDialogService, {
        useValue: {
            open: () => toDisposable(() => undefined),
            close: () => undefined,
            closeAll: () => undefined,
            getDialogs$: () => dialogs$,
        },
    }]);
    injector.add([IDrawingManagerService, {
        useValue: {
            getDrawingOKey: () => ({
                unitId: 'unit-1',
                subUnitId: 'sheet-1',
                drawingId: 'drawing-1',
                drawingType,
            }),
        } as never,
    }]);
    const getFeatureMenuIcon = vi.fn(() => icon);
    injector.add([SheetCanvasPopManagerService, {
        useValue: {
            attachPopupToObject,
            getFeatureMenu: () => undefined,
            getFeatureMenuIcon,
        } as never,
    }]);
    injector.add([IRenderManagerService, {
        useValue: {
            has: () => true,
            getRenderUnitById: () => ({
                scene: {
                    getAllObjectsByOrder: () => [],
                    getTransformerByCreate: () => ({
                        createControl$,
                        clearControl$,
                        changing$,
                        getSelectedObjectMap: () => new Map([['drawing-1', drawingObject]]),
                    }),
                },
            }),
        } as never,
    }]);
    injector.add([IUniverInstanceService, {
        useValue: {
            getCurrentTypeOfUnit$: () => currentWorkbook$,
            getTypeOfUnitDisposed$: () => disposedWorkbook$,
            getAllUnitsForType: () => [{ getUnitId: () => 'unit-1' }],
        } as never,
    }]);
    injector.add([IMessageService, { useValue: { show: () => toDisposable(() => {}) } as never }]);
    injector.add([IMenuManagerService, {
        useValue: {
            getFlatMenuByPositionKey: () => [{
                key: 'add-comment',
                order: 0,
                item: {
                    id: 'sheet.operation.add-drawing-comment',
                    type: MenuItemType.BUTTON,
                    title: 'sheets-thread-comment-ui.menu.addComment',
                },
            }],
        } as never,
    }]);
    injector.add([IContextService, { useClass: ContextService }]);
    injector.add([IImageIoService, { useValue: { change$: imageIoChange$ } as never }]);
    injector.add([ICommandService, { useValue: { syncExecuteCommand: vi.fn() } as never }]);
    injector.add([DrawingPopupMenuController]);

    return {
        attachPopupToObject,
        createControl$,
        drawingObject,
        getAttachedPopup: () => attachedPopup,
        getFeatureMenuIcon,
        injector,
        popupDisposable,
        setDrawingType: (nextDrawingType: DrawingTypeEnum) => {
            drawingType = nextDrawingType;
        },
    };
}

describe('DrawingPopupMenuController', () => {
    it.each([
        { drawingType: DrawingTypeEnum.DRAWING_IMAGE, label: 'image', icon: 'AddImageIcon' },
        { drawingType: DrawingTypeEnum.DRAWING_DOM, label: 'Float DOM', icon: undefined },
        { drawingType: DrawingTypeEnum.DRAWING_CHART, label: 'chart', icon: 'CustomChartIcon' },
        { drawingType: DrawingTypeEnum.DRAWING_CHART, label: 'chart without an icon', icon: undefined },
    ])('passes the $label popup icon and constrains its placement in RTL', ({ drawingType, icon }) => {
        const { attachPopupToObject, createControl$, drawingObject, getAttachedPopup, getFeatureMenuIcon, injector } = createControllerHarness(drawingType, icon);
        const locale = injector.get(LocaleService);
        locale.load({ [LocaleType.EN_US]: {} });
        locale.setLocale(LocaleType.EN_US);
        locale.setDirection('rtl');
        const controller = injector.get(DrawingPopupMenuController);
        createControl$.next();

        expect(getFeatureMenuIcon).toHaveBeenCalledWith(drawingType);
        expect(attachPopupToObject).toHaveBeenCalledWith(
            drawingObject,
            expect.objectContaining({
                constrainToCanvas: true,
                direction: 'left',
                extraProps: expect.objectContaining({ icon }),
            })
        );
        expect(getAttachedPopup()?.extraProps?.menuItems).toEqual(expect.arrayContaining([
            expect.objectContaining({
                commandId: 'sheet.operation.add-drawing-comment',
                label: 'sheets-thread-comment-ui.menu.addComment',
            }),
        ]));

        controller.dispose();
        injector.dispose();
    });

    it('removes a chart popup when focus moves to a shape', () => {
        const {
            attachPopupToObject,
            createControl$,
            injector,
            popupDisposable,
            setDrawingType,
        } = createControllerHarness(DrawingTypeEnum.DRAWING_CHART);
        const locale = injector.get(LocaleService);
        locale.load({ [LocaleType.EN_US]: {} });
        locale.setLocale(LocaleType.EN_US);
        locale.setDirection('ltr');
        const controller = injector.get(DrawingPopupMenuController);
        createControl$.next();

        setDrawingType(DrawingTypeEnum.DRAWING_SHAPE);
        createControl$.next();

        expect(popupDisposable.dispose).toHaveBeenCalledOnce();
        expect(attachPopupToObject).toHaveBeenCalledOnce();

        controller.dispose();
        injector.dispose();
    });
});