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.

10.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 { ICustomRange, Nullable, Workbook } from '@univerjs/core';
import type { LocaleKey } from '../locale/types';
import { ICommandService, IUniverInstanceService, LocaleService, UniverInstanceType } from '@univerjs/core';
import { borderClassName, Button, clsx, MessageType, Tooltip } from '@univerjs/design';
import { AllBorderIcon, CopyIcon, LinkIcon, SheetsMultiIcon, UnlinkIcon, WriteIcon } from '@univerjs/icons';
import {
    CancelHyperLinkCommand,
    CancelRichHyperLinkCommand,
    SheetHyperLinkType,
    SheetsHyperLinkParserService,
} from '@univerjs/sheets-hyper-link';
import { IEditorBridgeService } from '@univerjs/sheets-ui';
import { IMessageService, useDependency, useObservable } from '@univerjs/ui';
import { OpenHyperLinkEditPanelOperation } from '../commands/operations/popup.operations';
import { ISheetsHyperLinkPopupService } from '../services/popup.service';
import { SheetsHyperLinkResolverService } from '../services/resolver.service';
import { HyperLinkEditSourceType } from '../types/enums/edit-source';

const iconsMap = {
    [SheetHyperLinkType.URL]: <LinkIcon />,
    [SheetHyperLinkType.SHEET]: <SheetsMultiIcon className="univer-text-green-500" />,
    [SheetHyperLinkType.RANGE]: <AllBorderIcon />,
    [SheetHyperLinkType.DEFINE_NAME]: <AllBorderIcon />,
    [SheetHyperLinkType.INVALID]: <AllBorderIcon />,
};

interface ICellLinkPopupPureProps {
    customRange?: Nullable<ICustomRange>;
    row: number;
    col: number;
    unitId: string;
    subUnitId: string;
    editPermission?: boolean;
    copyPermission?: boolean;
    type: HyperLinkEditSourceType;
}

export const CellLinkPopupPure = (props: ICellLinkPopupPureProps) => {
    const popupService = useDependency(ISheetsHyperLinkPopupService);
    const commandService = useDependency(ICommandService);
    const messageService = useDependency(IMessageService);
    const localeService = useDependency(LocaleService);
    const resolverService = useDependency(SheetsHyperLinkResolverService);
    const editorBridgeService = useDependency(IEditorBridgeService);
    const parserHyperLinkService = useDependency(SheetsHyperLinkParserService);
    const { customRange, row, col, unitId, subUnitId, editPermission, copyPermission, type } = props;

    if (!customRange?.properties?.url) {
        return null;
    }
    const linkObj = parserHyperLinkService.parseHyperLink(customRange.properties.url ?? '');
    const tooltip = customRange.properties.tooltip ?? customRange.properties._xlsxHyperlinkTooltip;
    const isError = linkObj.type === SheetHyperLinkType.INVALID;

    return (
        <div
            className={clsx(`
              univer-mb-1 univer-grid univer-max-w-80 univer-grid-cols-[minmax(0,1fr)_auto] univer-items-center
              univer-gap-2 univer-overflow-hidden univer-rounded-lg univer-bg-gray-0 univer-p-2 univer-shadow-md
              dark:!univer-bg-gray-900
            `, borderClassName)}
            onClick={() => popupService.hideCurrentPopup()}
        >
            <div
                className={clsx(`
                  univer-flex univer-h-6 univer-flex-1 univer-cursor-pointer univer-flex-row univer-items-center
                  univer-gap-1 univer-truncate univer-text-sm univer-leading-5 univer-text-primary-600
                `, { 'univer-text-red-500': isError })}
                onClick={() => {
                    if (isError) {
                        return;
                    }

                    resolverService.navigate(linkObj);
                }}
            >
                <div
                    className={`
                      univer-flex univer-size-5 univer-flex-none univer-items-center univer-justify-center
                      univer-text-base univer-text-gray-900
                      dark:!univer-text-gray-0
                    `}
                >
                    {iconsMap[linkObj.type]}
                </div>
                <Tooltip showIfEllipsis title={linkObj.name} asChild>
                    <span className="univer-flex-1 univer-truncate">{linkObj.name}</span>
                </Tooltip>
            </div>
            <div
                className={`
                  univer-flex univer-h-6 univer-flex-none univer-flex-row univer-items-center univer-justify-center
                  univer-gap-2
                `}
            >
                <Button
                    type="button"
                    variant="text"
                    size="small"
                    data-u-comp="cell-link-popup-copy"
                    aria-label={localeService.t<LocaleKey>('sheets-hyper-link-ui.popup.copy')}
                    className={clsx(`
                      !univer-flex univer-size-6 !univer-rounded !univer-border-0 !univer-p-0 !univer-text-base
                      !univer-font-normal
                      disabled:!univer-pointer-events-auto
                    `, { 'univer-text-red-500': isError })}
                    disabled={!copyPermission || isError}
                    onClick={async () => {
                        if (linkObj.type !== SheetHyperLinkType.URL) {
                            const url = new URL(window.location.href);
                            url.hash = linkObj.url.slice(1);
                            await navigator.clipboard.writeText(url.href);
                        } else {
                            await navigator.clipboard.writeText(linkObj.url);
                        }
                        messageService.show({
                            content: localeService.t<LocaleKey>('sheets-hyper-link-ui.message.coped'),
                            type: MessageType.Info,
                        });
                    }}
                >
                    <Tooltip placement="bottom" title={localeService.t<LocaleKey>('sheets-hyper-link-ui.popup.copy')}>
                        <CopyIcon className="dark:!univer-text-gray-0" />
                    </Tooltip>
                </Button>
                {editPermission && (
                    <>
                        <div
                            data-u-comp="cell-link-popup-edit"
                            className={`
                              univer-flex univer-size-6 univer-cursor-pointer univer-flex-row univer-items-center
                              univer-justify-center univer-rounded univer-text-base
                              hover:univer-bg-gray-100
                              dark:hover:!univer-bg-gray-700
                            `}
                            onClick={() => {
                                return commandService.executeCommand(OpenHyperLinkEditPanelOperation.id, {
                                    unitId,
                                    subUnitId,
                                    row,
                                    col,
                                    customRangeId: customRange.rangeId,
                                    type,
                                });
                            }}
                        >
                            <Tooltip placement="bottom" title={localeService.t<LocaleKey>('sheets-hyper-link-ui.popup.edit')}>
                                <WriteIcon className="dark:!univer-text-gray-0" />
                            </Tooltip>
                        </div>
                        <div
                            data-u-comp="cell-link-popup-remove"
                            className={`
                              univer-flex univer-size-6 univer-cursor-pointer univer-flex-row univer-items-center
                              univer-justify-center univer-rounded univer-text-base
                              hover:univer-bg-gray-100
                              dark:hover:!univer-bg-gray-700
                            `}
                            onClick={() => {
                                const commandId = type === HyperLinkEditSourceType.EDITING ? CancelRichHyperLinkCommand.id : CancelHyperLinkCommand.id;
                                if (commandService.syncExecuteCommand(commandId, {
                                    unitId,
                                    subUnitId,
                                    id: customRange.rangeId,
                                    row,
                                    column: col,
                                    documentId: editorBridgeService.getCurrentEditorId(),
                                })) {
                                    popupService.hideCurrentPopup(undefined, true);
                                }
                            }}
                        >
                            <Tooltip placement="bottom" title={localeService.t<LocaleKey>('sheets-hyper-link-ui.popup.cancel')}>
                                <UnlinkIcon className="dark:!univer-text-gray-0" />
                            </Tooltip>
                        </div>
                    </>
                )}
            </div>
            {typeof tooltip === 'string' && tooltip && (
                <div
                    className="
                      univer-col-span-2 univer-max-h-32 univer-overflow-y-auto univer-whitespace-pre-wrap
                      univer-break-words univer-text-sm univer-text-gray-500
                    "
                >
                    {tooltip}
                </div>
            )}
        </div>
    );
};

export const CellLinkPopup = () => {
    const popupService = useDependency(ISheetsHyperLinkPopupService);
    const currentPopup = useObservable(popupService.currentPopup$, popupService.currentPopup);
    const univerInstanceService = useDependency(IUniverInstanceService);

    if (!currentPopup) {
        return null;
    }
    if (currentPopup.showAll) {
        const workbook = univerInstanceService.getUnit<Workbook>(currentPopup.unitId, UniverInstanceType.UNIVER_SHEET);
        const worksheet = workbook?.getSheetBySheetId(currentPopup.subUnitId);
        const cell = worksheet?.getCell(currentPopup.row, currentPopup.col);
        const customRanges = cell?.p?.body?.customRanges;
        return customRanges?.length
            ? (
                <div>
                    {customRanges.map((customRange) => {
                        return <CellLinkPopupPure key={customRange.rangeId} {...currentPopup} customRange={customRange} />;
                    })}
                </div>
            )
            : null;
    }
    return <CellLinkPopupPure {...currentPopup} />;
};

CellLinkPopup.componentKey = 'univer.sheet.cell-link-popup';