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.9 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 { ReactNode } from 'react';
import { LocaleService } from '@univerjs/core';
import { Button, ConfigContext, MobileActionRowGroup } from '@univerjs/design';
import { CloseIcon } from '@univerjs/icons';
import { useContext, useMemo, useRef, useState } from 'react';
import type { LocaleKey } from '../../../locale/types';
import type { MobilePanelLayout } from '../../mobile-workbench/MobileCanvasLayout';
import type { MobileDrawerSnap } from '../mobile-drawer/MobileDrawer';
import type { IDialogPartMethodOptions } from './interface';
import { IDialogService } from '../../../services/dialog/dialog.service';
import { useDependency, useObservable } from '../../../utils/di';
import { CustomLabel } from '../../custom-label/CustomLabel';
import { MobileDrawer } from '../mobile-drawer/MobileDrawer';

export interface IMobileDialogPartMethodOptions extends IDialogPartMethodOptions {
    layout?: MobilePanelLayout;
}

interface IMobileDialogOptions extends Omit<IMobileDialogPartMethodOptions, 'children' | 'title' | 'footer'> {
    children?: ReactNode;
    title?: ReactNode;
    footer?: ReactNode;
}

function toMobileDialogOptions(options: IMobileDialogPartMethodOptions): IMobileDialogOptions {
    const { children, title, footer, ...rest } = options;
    return {
        ...rest,
        children: children ? <CustomLabel {...children} /> : undefined,
        title: title ? <CustomLabel {...title} /> : undefined,
        footer: footer ? <CustomLabel {...footer} /> : undefined,
    };
}

export function MobileDialogPart() {
    const dialogService = useDependency(IDialogService);
    const localeService = useDependency(LocaleService);
    const { locale } = useContext(ConfigContext);
    const dialogOptions = useObservable(dialogService.getDialogs$(), []);
    const options = useMemo(() => {
        const activeDialogs = dialogOptions.filter((item) => item.open !== false);
        const active = activeDialogs[activeDialogs.length - 1] as IMobileDialogPartMethodOptions | undefined;
        return active ? toMobileDialogOptions(active) : null;
    }, [dialogOptions]);
    const [snapOverride, setSnapOverride] = useState<{ id: string; snap: MobileDrawerSnap } | null>(null);
    const layerRef = useRef<HTMLDivElement>(null);
    const backdropPointerIdRef = useRef<number | null>(null);

    if (!options) {
        return null;
    }
    const defaultSnap = options.layout === 'canvas' ? 'compact' : 'expanded';
    const footer = options.footer ?? (options.showOk || options.showCancel
        ? (
            <>
                {options.showCancel && <Button onClick={options.onCancel} size="middle">{locale?.Confirm.cancel}</Button>}
                {options.showOk && <Button variant="primary" onClick={options.onOk} size="middle">{locale?.Confirm.confirm}</Button>}
            </>
        )
        : null);

    const close = () => {
        dialogService.close(options.id);
        options.onClose?.();
        options.onOpenChange?.(false);
    };

    return (
        <div
            ref={layerRef}
            className="univer-pointer-events-none univer-fixed univer-inset-0 univer-z-[1200]"
            data-u-comp="mobile-dialog"
        >
            {options.layout !== 'canvas' && (
                <button
                    type="button"
                    aria-label={localeService.t<LocaleKey>('ui.sidebar.close')}
                    className="
                      univer-pointer-events-auto univer-absolute univer-inset-0 univer-m-0 univer-appearance-none
                      univer-rounded-none univer-border-0 univer-bg-black/35 univer-p-0
                    "
                    onPointerDown={options.maskClosable === false
                        ? undefined
                        : (event) => {
                            backdropPointerIdRef.current = event.pointerId;
                        }}
                    onPointerUp={options.maskClosable === false
                        ? undefined
                        : (event) => {
                            if (backdropPointerIdRef.current !== event.pointerId) {
                                return;
                            }

                            backdropPointerIdRef.current = null;
                            close();
                        }}
                    onPointerCancel={() => {
                        backdropPointerIdRef.current = null;
                    }}
                />
            )}
            <MobileDrawer
                layerRef={layerRef}
                layout={options.layout}
                autoHeight={options.layout !== 'canvas'}
                componentName="mobile-dialog-drawer"
                snap={snapOverride?.id === options.id ? snapOverride.snap : defaultSnap}
                expandLabel={localeService.t<LocaleKey>('ui.sidebar.resize')}
                collapseLabel={localeService.t<LocaleKey>('ui.sidebar.resize')}
                onSnapChange={(snap) => setSnapOverride({ id: options.id, snap })}
                onClose={close}
                role="dialog"
                panelClassName="
                      univer-bg-gray-0 univer-text-gray-900
                      dark:!univer-bg-gray-900 dark:!univer-text-gray-0
                    "
                contentClassName="univer-min-h-0 univer-px-4 univer-pb-4"
                header={(
                    <header
                        className="
                          univer-flex univer-h-12 univer-flex-1 univer-items-center univer-justify-between univer-px-4
                        "
                    >
                        <div className="univer-min-w-0 univer-truncate univer-text-base univer-font-semibold">{options.title}</div>
                        {options.closable !== false && (
                            <button
                                type="button"
                                className="
                                  univer-flex univer-size-10 univer-shrink-0 univer-items-center univer-justify-center
                                  univer-rounded-full univer-border-0 univer-bg-transparent univer-text-xl
                                  univer-text-gray-600 univer-outline-none
                                  active:univer-bg-gray-100
                                  dark:!univer-text-gray-300
                                  dark:active:!univer-bg-gray-700
                                "
                                onClick={close}
                                aria-label={localeService.t<LocaleKey>('ui.sidebar.close')}
                            >
                                <CloseIcon />
                            </button>
                        )}
                    </header>
                )}
                footer={footer
                    ? (
                        <footer
                            className="
                              univer-shrink-0 univer-border-0 univer-border-t univer-border-solid univer-border-gray-200
                              univer-p-4
                              dark:!univer-border-gray-700
                            "
                        >
                            <MobileActionRowGroup>{footer}</MobileActionRowGroup>
                        </footer>
                    )
                    : undefined}
            >
                {options.children}
            </MobileDrawer>
        </div>
    );
}