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.

12.4 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 { IDisposable } from '@univerjs/core';
import type { IMenuSchema } from '@univerjs/ui';
import type { Root } from 'react-dom/client';
import {
    CommandService,
    ConfigService,
    ICommandService,
    IConfigService,
    ILogService,
    Injector,
    LocaleService,
    LocaleType,
    toDisposable,
} from '@univerjs/core';
import {
    ComponentManager,
    ContextMenuHostService,
    IconManager,
    IContextMenuHostService,
    ILayoutService,
    IMenuManagerService,
    IShortcutService,
    MenuItemType,
    MenuManagerPosition,
    MenuManagerService,
    RediContext,
} from '@univerjs/ui';
import { act } from 'react';
import { createRoot } from 'react-dom/client';
import { Subject } from 'rxjs';
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
    SetInlineFormatBoldCommand,
    SetInlineFormatTextBackgroundColorCommand,
} from '../../../commands/commands/inline-format.command';
import enUS from '../../../locale/en-US';
import { FLOAT_TEXT_STYLE_MENU_ID, FLOAT_TOOLBAR_MENU_POSITION } from '../../../menu/menu';
import { DocLayoutInteractionService } from '../../../services/doc-layout-interaction.service';
import { FloatToolbar, resolveFloatToolbarMenus } from '../FloatToolbar';

(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;

const HYPERLINK_MENU_ID = 'doc.operation.show-hyper-link-edit-popup';
const COMMENT_MENU_ID = 'docs.operation.start-add-comment';

class TestMenuManagerService {
    readonly menuChanged$ = new Subject<void>();
    private _floatToolbarMenus: IMenuSchema[] = [];
    private _ribbonMenus: IMenuSchema[] = [];

    setMenus(floatToolbarMenus: IMenuSchema[], ribbonMenus: IMenuSchema[] = []): void {
        this._floatToolbarMenus = floatToolbarMenus;
        this._ribbonMenus = ribbonMenus;
        this.menuChanged$.next();
    }

    mergeMenu(): void {}
    appendRootMenu(): void {}

    getMenuByPositionKey(position: string): IMenuSchema[] {
        return position === FLOAT_TOOLBAR_MENU_POSITION ? this._floatToolbarMenus : [];
    }

    getFlatMenuByPositionKey(position: string): IMenuSchema[] {
        if (position === FLOAT_TOOLBAR_MENU_POSITION) {
            return this._floatToolbarMenus;
        }
        if (position === MenuManagerPosition.RIBBON) {
            return this._ribbonMenus;
        }
        return [];
    }
}

class TestLayoutService {
    readonly rootContainerElement = document.body;
    private _focused = false;

    get isFocused() {
        return this._focused;
    }

    focus(): void {
        this._focused = true;
    }

    registerFocusHandler(): IDisposable {
        return toDisposable(() => undefined);
    }

    registerRootContainerElement(): IDisposable {
        return toDisposable(() => undefined);
    }

    registerContentElement(): IDisposable {
        return toDisposable(() => undefined);
    }

    registerContainerElement(): IDisposable {
        return toDisposable(() => undefined);
    }

    getContentElement(): HTMLElement {
        return document.body;
    }

    checkElementInCurrentContainers(): boolean {
        return true;
    }

    checkContentIsFocused(): boolean {
        return this._focused;
    }
}

class TestShortcutService {
    readonly shortcutChanged$ = new Subject<void>();

    forceEscape(): IDisposable {
        return toDisposable(() => undefined);
    }

    forceDisable(): IDisposable {
        return toDisposable(() => undefined);
    }

    dispatch(): undefined {
        return undefined;
    }

    registerShortcut(): IDisposable {
        return toDisposable(() => undefined);
    }

    getShortcutDisplay(): string | null {
        return null;
    }

    getShortcutDisplayOfCommand(): string | null {
        return null;
    }

    getAllShortcuts() {
        return [];
    }
}

class TestLogService {
    warn(): void {}
}

function createToolbarTestBed() {
    const injector = new Injector();
    injector.add([IMenuManagerService, { useClass: TestMenuManagerService as never }]);
    injector.add([ICommandService, { useClass: CommandService }]);
    injector.add([IConfigService, { useClass: ConfigService }]);
    injector.add([LocaleService]);
    injector.add([IContextMenuHostService, { useClass: ContextMenuHostService }]);
    injector.add([ILayoutService, { useClass: TestLayoutService as never }]);
    injector.add([IShortcutService, { useClass: TestShortcutService as never }]);
    injector.add([ILogService, { useClass: TestLogService as never }]);
    injector.add([ComponentManager]);
    injector.add([IconManager]);
    const localeService = injector.get(LocaleService);
    localeService.load({ [LocaleType.EN_US]: enUS });
    localeService.setLocale(LocaleType.EN_US);
    localeService.setDirection('ltr');

    return {
        injector,
        menuManagerService: injector.get(IMenuManagerService) as unknown as TestMenuManagerService,
    };
}

function renderToolbar(injector: Injector, avaliableMenus: string[], onDismiss?: () => void, onMount?: () => () => void) {
    const container = document.createElement('div');
    document.body.appendChild(container);
    const root = createRoot(container);

    act(() => {
        root.render(
            <RediContext.Provider value={{ injector }}>
                <FloatToolbar avaliableMenus={avaliableMenus} popup={{ extraProps: { onDismiss, onMount } }} />
            </RediContext.Provider>
        );
    });

    return { container, root };
}

function createMenuItem(key: string, title = key): IMenuSchema {
    return {
        key,
        order: 0,
        item: {
            id: key,
            title,
            type: MenuItemType.BUTTON,
        },
    };
}

describe('FloatToolbar', () => {
    let root: Root | undefined;
    let container: HTMLElement | undefined;

    afterEach(() => {
        if (root) {
            act(() => root!.unmount());
        }
        container?.remove();
        root = undefined;
        container = undefined;
    });

    it('holds layout only during the mounted toolbar lifetime', () => {
        const injector = new Injector();
        injector.add([IConfigService, { useClass: ConfigService }]);
        injector.add([IMenuManagerService, { useClass: MenuManagerService }]);
        injector.add([DocLayoutInteractionService]);
        const interaction = injector.get(DocLayoutInteractionService);
        try {
            expect(interaction.isActive).toBe(false);
            ({ root, container } = renderToolbar(injector, [], undefined, () => {
                const lock = interaction.beginInteraction();
                return () => lock.dispose();
            }));
            expect(interaction.isActive).toBe(true);
            act(() => root!.unmount());
            root = undefined;
            expect(interaction.isActive).toBe(false);
        } finally {
            if (root) {
                act(() => root!.unmount());
                root = undefined;
            }
            injector.dispose();
        }
    });

    it('fades outside the safe area and dismisses only once without prior hover', () => {
        const testBed = createToolbarTestBed();
        const onDismiss = vi.fn();
        ({ root, container } = renderToolbar(testBed.injector, [], onDismiss));
        const toolbar = container.querySelector<HTMLElement>('[data-doc-float-toolbar]')!;
        vi.spyOn(toolbar, 'getBoundingClientRect').mockReturnValue({ left: 100, right: 300, top: 100, bottom: 160 } as DOMRect);
        const move = (y: number) => act(() => document.dispatchEvent(new PointerEvent('pointermove', { clientX: 200, clientY: y })));

        move(170);
        expect(toolbar.style.opacity).toBe('1');
        move(210);
        expect(toolbar.style.opacity).toBe('0.5');
        move(170);
        expect(toolbar.style.opacity).toBe('1');
        move(250);
        move(260);
        expect(onDismiss).toHaveBeenCalledTimes(1);
        testBed.injector.dispose();
    });

    it('stays fully visible after hover even when the pointer leaves the menu', () => {
        const testBed = createToolbarTestBed();
        const onDismiss = vi.fn();
        ({ root, container } = renderToolbar(testBed.injector, [], onDismiss));
        const toolbar = container.querySelector<HTMLElement>('[data-doc-float-toolbar]')!;
        vi.spyOn(toolbar, 'getBoundingClientRect').mockReturnValue({ left: 100, right: 300, top: 100, bottom: 160 } as DOMRect);
        act(() => document.dispatchEvent(new PointerEvent('pointermove', { clientX: 200, clientY: 210 })));
        expect(toolbar.style.opacity).toBe('0.5');
        act(() => toolbar.dispatchEvent(new PointerEvent('pointerover', { bubbles: true })));
        act(() => document.dispatchEvent(new PointerEvent('pointermove', { clientX: 800, clientY: 800 })));
        expect(toolbar.style.opacity).toBe('1');
        expect(onDismiss).not.toHaveBeenCalled();
        act(() => root!.unmount());
        root = undefined;
        act(() => document.dispatchEvent(new PointerEvent('pointermove', { clientX: 1000, clientY: 1000 })));
        expect(onDismiss).not.toHaveBeenCalled();
        testBed.injector.dispose();
    });

    it('keeps whitelisted toolbar menus ordered and separates direct extension menus', () => {
        const textStyleItem = createMenuItem(FLOAT_TEXT_STYLE_MENU_ID);
        const boldItem = createMenuItem(SetInlineFormatBoldCommand.id);
        const hyperlinkItem = createMenuItem(HYPERLINK_MENU_ID);
        const commentItem = createMenuItem(COMMENT_MENU_ID);
        const menuManagerService = new TestMenuManagerService();
        menuManagerService.setMenus([textStyleItem, hyperlinkItem, commentItem], [boldItem]);

        const { menus, extraMenus } = resolveFloatToolbarMenus(menuManagerService as never, [
            FLOAT_TEXT_STYLE_MENU_ID,
            SetInlineFormatBoldCommand.id,
        ]);

        expect(menus.map((item) => item.key)).toEqual([
            FLOAT_TEXT_STYLE_MENU_ID,
            SetInlineFormatBoldCommand.id,
        ]);
        expect(extraMenus.map((item) => item.key)).toEqual([
            HYPERLINK_MENU_ID,
            COMMENT_MENU_ID,
        ]);
    });

    it('passes presentation config through to the resolved floating toolbar menu', () => {
        const backgroundItem = createMenuItem(SetInlineFormatTextBackgroundColorCommand.id);
        const menuManagerService = new TestMenuManagerService();
        menuManagerService.setMenus([backgroundItem]);

        const { menus, extraMenus } = resolveFloatToolbarMenus(menuManagerService as never, [{
            id: SetInlineFormatTextBackgroundColorCommand.id,
            iconColor: 'var(--univer-primary-600)',
        }]);

        expect(menus[0]?.iconColor).toBe('var(--univer-primary-600)');
        expect(extraMenus).toEqual([]);
    });

    it('renders available menus and refreshes when the menu service changes', async () => {
        const testBed = createToolbarTestBed();
        const textStyleItem = createMenuItem(FLOAT_TEXT_STYLE_MENU_ID, 'Text style');
        const boldItem = createMenuItem(SetInlineFormatBoldCommand.id, 'Bold');
        const hyperlinkItem = createMenuItem(HYPERLINK_MENU_ID, 'Link');
        const commentItem = createMenuItem(COMMENT_MENU_ID, 'Comment');
        testBed.menuManagerService.setMenus([textStyleItem, hyperlinkItem], [boldItem]);

        const rendered = renderToolbar(testBed.injector, [
            FLOAT_TEXT_STYLE_MENU_ID,
            SetInlineFormatBoldCommand.id,
        ]);
        root = rendered.root;
        container = rendered.container;

        expect(container.textContent).toContain('Text style');
        expect(container.textContent).toContain('Bold');
        expect(container.textContent).toContain('Link');
        expect(container.textContent).not.toContain('Comment');

        await act(async () => {
            testBed.menuManagerService.setMenus([textStyleItem, hyperlinkItem, commentItem], [boldItem]);
        });

        expect(container.textContent).toContain('Comment');
    });
});