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.

11.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 { ComponentProps } from 'react';
import { ICommandService, LocaleService } from '@univerjs/core';
import { ConfigContext } from '@univerjs/design';
import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import type { LocaleKey } from '../../../locale/types';
import type { ContextMenuEvent, IContextMenuTriggerContext } from '../../../services/contextmenu/contextmenu.service';
import { IContextMenuHostService } from '../../../services/contextmenu/contextmenu-host.service';
import { IContextMenuService } from '../../../services/contextmenu/contextmenu.service';
import { ILayoutService } from '../../../services/layout/layout.service';
import { IMenuManagerService } from '../../../services/menu/menu-manager.service';
import { ContextMenuPosition } from '../../../services/menu/types';
import { IUIRuntimeScopeService } from '../../../services/runtime-scope/ui-runtime-scope.service';
import { useDependency } from '../../../utils/di';
import { MobileMenu } from '../../menu/mobile/MobileMenu';
import { MobileMenuDrawer } from '../../menu/mobile/MobileMenuDrawer';

const MOBILE_CONTEXT_MENU_HOST_ID = 'mobile-context-menu';
const MOBILE_CONTEXT_MENU_POINTER_SIZE = 8;
const MOBILE_CONTEXT_MENU_POINTER_EDGE_GAP = 12;

export function resolveMobileCaretMenuPlacement(anchorX: number, containerLeft: number, containerWidth: number, menuWidth: number) {
    const relativeAnchorX = anchorX - containerLeft;
    const menuLeft = Math.min(
        Math.max(relativeAnchorX - menuWidth / 2, 0),
        Math.max(containerWidth - menuWidth, 0)
    );
    const pointerLeft = Math.min(
        Math.max(relativeAnchorX - menuLeft - MOBILE_CONTEXT_MENU_POINTER_SIZE / 2, MOBILE_CONTEXT_MENU_POINTER_EDGE_GAP),
        Math.max(menuWidth - MOBILE_CONTEXT_MENU_POINTER_EDGE_GAP - MOBILE_CONTEXT_MENU_POINTER_SIZE, MOBILE_CONTEXT_MENU_POINTER_EDGE_GAP)
    );

    return { menuLeft, pointerLeft };
}

export function shouldUseFloatingMobileContextMenu(_menuType: string, context?: IContextMenuTriggerContext): boolean {
    return context?.caretAnchor === true;
}

export function MobileContextMenu() {
    const [visible, setVisible] = useState(false);
    const [menuType, setMenuType] = useState('');
    const [anchor, setAnchor] = useState({ x: 0, y: 0 });
    const floatingMenuRef = useRef<HTMLDivElement>(null);
    const [menuContext, setMenuContext] = useState<IContextMenuTriggerContext | undefined>();
    const visibleRef = useRef(visible);
    const contextMenuHostService = useDependency(IContextMenuHostService);
    const contextMenuService = useDependency(IContextMenuService);
    const commandService = useDependency(ICommandService);
    const layoutService = useDependency(ILayoutService);
    const menuManagerService = useDependency(IMenuManagerService);
    const runtimeScopeService = useDependency(IUIRuntimeScopeService);
    const localeService = useDependency(LocaleService);
    const { mountContainer } = useContext(ConfigContext);
    const isCaretAnchor = menuContext?.caretAnchor === true;
    const isFloatingContextMenu = shouldUseFloatingMobileContextMenu(menuType, menuContext);

    visibleRef.current = visible;

    const handleContextMenu = useCallback((
        event: ContextMenuEvent,
        nextMenuType: string,
        context?: IContextMenuTriggerContext
    ) => {
        if (contextMenuHostService.suspended) return;
        contextMenuHostService.activateMenu(MOBILE_CONTEXT_MENU_HOST_ID);
        setMenuType(nextMenuType);
        setMenuContext(context);
        setAnchor({ x: event.clientX, y: event.clientY });
        setVisible(true);
    }, [contextMenuHostService]);

    const handleClose = useCallback(() => {
        setVisible(false);
        contextMenuHostService.deactivateMenu(MOBILE_CONTEXT_MENU_HOST_ID);
    }, [contextMenuHostService]);

    useEffect(() => {
        const hostDisposable = contextMenuHostService.registerMenu(MOBILE_CONTEXT_MENU_HOST_ID, () => {
            setVisible(false);
        });

        const disposables = contextMenuService.registerContextMenuHandler({
            handleContextMenu,
            hideContextMenu() {
                handleClose();
            },
            get visible() {
                return visibleRef.current;
            },
        });

        return () => {
            disposables.dispose();
            hostDisposable.dispose();
            contextMenuHostService.deactivateMenu(MOBILE_CONTEXT_MENU_HOST_ID);
        };
    }, [contextMenuHostService, contextMenuService, handleClose, handleContextMenu]);

    useEffect(() => {
        if (!visible || !isFloatingContextMenu || !mountContainer) {
            return undefined;
        }

        const ownerDocument = mountContainer.ownerDocument;
        const handleOutsidePointerDown = (event: PointerEvent) => {
            if (!(event.target instanceof Node) || !floatingMenuRef.current?.contains(event.target)) {
                setVisible(false);
                contextMenuHostService.deactivateMenu(MOBILE_CONTEXT_MENU_HOST_ID);
            }
        };
        ownerDocument.addEventListener('pointerdown', handleOutsidePointerDown, true);
        return () => ownerDocument.removeEventListener('pointerdown', handleOutsidePointerDown, true);
    }, [contextMenuHostService, isFloatingContextMenu, mountContainer, visible]);

    useLayoutEffect(() => {
        if (!visible || !isCaretAnchor) {
            return undefined;
        }

        const menu = floatingMenuRef.current;
        const container = menu?.parentElement;
        if (!menu || !container) {
            return undefined;
        }

        const updatePlacement = () => {
            const containerRect = container.getBoundingClientRect();
            const menuWidth = menu.getBoundingClientRect().width;
            const { menuLeft, pointerLeft } = resolveMobileCaretMenuPlacement(
                anchor.x,
                containerRect.left,
                containerRect.width,
                menuWidth
            );

            menu.style.left = `${menuLeft}px`;
            menu.style.setProperty('--univer-mobile-context-menu-pointer-left', `${pointerLeft}px`);
        };

        updatePlacement();
        const resizeObserver = new ResizeObserver(updatePlacement);
        resizeObserver.observe(container);
        resizeObserver.observe(menu);

        return () => {
            resizeObserver.disconnect();
            menu.style.removeProperty('left');
            menu.style.removeProperty('--univer-mobile-context-menu-pointer-left');
        };
    }, [anchor.x, isCaretAnchor, visible]);

    const sheetTitle = useMemo(() => {
        switch (menuType) {
            case ContextMenuPosition.ROW_HEADER:
                return localeService.t<LocaleKey>('ui.row');
            case ContextMenuPosition.COL_HEADER:
                return localeService.t<LocaleKey>('ui.column');
            default:
                return '';
        }
    }, [localeService, menuType]);

    if (!mountContainer || !visible) {
        return null;
    }

    const activeScope = runtimeScopeService.get(menuContext?.unitId);
    const activeCommandService = activeScope?.has(ICommandService)
        ? activeScope.get<ICommandService>(ICommandService)
        : commandService;
    const activeLayoutService = activeScope?.has(ILayoutService)
        ? activeScope.get<ILayoutService>(ILayoutService)
        : layoutService;
    const activeMenuManagerService = activeScope?.has(IMenuManagerService)
        ? activeScope.get<IMenuManagerService>(IMenuManagerService)
        : menuManagerService;
    const handleOptionSelect = (params: Parameters<NonNullable<ComponentProps<typeof MobileMenu>['onOptionSelect']>>[0]) => {
        const commandId = params.commandId ?? params.id ?? (typeof params.label === 'string' ? params.label : undefined);
        const fallbackParams = typeof params.params === 'function' ? params.params() : params.params;
        const optionParams = typeof params.value === 'undefined' ? fallbackParams : { value: params.value };
        const commandParams = menuContext
            ? { ...menuContext, ...optionParams }
            : optionParams;

        if (!commandId) {
            return;
        }

        activeLayoutService.focus();
        activeCommandService.executeCommand(commandId, commandParams);
        handleClose();
    };
    if (isFloatingContextMenu) {
        const viewportHeight = mountContainer.ownerDocument.defaultView?.innerHeight ?? 0;
        const placeBelow = anchor.y < 72;
        const top = placeBelow
            ? anchor.y + 12
            : Math.min(anchor.y - 56, Math.max(8, viewportHeight - 56));
        const pointerLeft = `clamp(20px, ${anchor.x - 8}px, calc(100% - 20px))`;

        return createPortal(
            <div
                className="
                  univer-pointer-events-none univer-fixed univer-inset-x-3 univer-z-[1080] univer-flex
                  univer-justify-center
                "
                style={{ top }}
            >
                <div
                    ref={floatingMenuRef}
                    className={`
                      univer-pointer-events-auto univer-min-w-0 univer-max-w-[560px]
                      ${isCaretAnchor
                            ? 'univer-absolute univer-left-0 univer-top-0 univer-w-fit'
                            : 'univer-relative univer-flex-1'}
                    `}
                >
                    <MobileMenu
                        menuType={menuType}
                        menuManagerService={activeMenuManagerService}
                        presentation="context-bar"
                        onOptionSelect={handleOptionSelect}
                    />
                    <div
                        aria-hidden="true"
                        className={placeBelow
                            ? `
                              univer-absolute -univer-top-1 univer-size-2 univer-rotate-45 univer-bg-gray-0
                              dark:!univer-bg-gray-700
                            `
                            : `
                              univer-absolute -univer-bottom-1 univer-size-2 univer-rotate-45 univer-bg-gray-0
                              dark:!univer-bg-gray-700
                            `}
                        style={{
                            left: isCaretAnchor
                                ? 'var(--univer-mobile-context-menu-pointer-left, calc(50% - 4px))'
                                : pointerLeft,
                        }}
                    />
                </div>
            </div>,
            mountContainer
        );
    }
    return (
        <MobileMenuDrawer
            visible
            title={sheetTitle}
            menuType={menuType}
            menuManagerService={activeMenuManagerService}
            onClose={handleClose}
            onOptionSelect={handleOptionSelect}
        />
    );
}