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 {
    CustomRangeType,
    DocumentDataModel,
    ICommandService,
    IPermissionService,
    IUniverInstanceService,
    LocaleService,
    UniverInstanceType,
} from '@univerjs/core';
import { borderClassName, Button, clsx, MessageType, Tooltip } from '@univerjs/design';
import { getDocumentPermissionValue } from '@univerjs/docs';
import { CopyIcon, LinkIcon, UnlinkIcon, WriteIcon } from '@univerjs/icons';
import { UnitAction } from '@univerjs/protocol';
import { IMessageService, useDependency, useObservable } from '@univerjs/ui';
import type { LocaleKey } from '../locale/types';
import { DeleteDocHyperLinkCommand } from '../commands/commands/delete-link.command';
import { ClickDocHyperLinkOperation, ShowDocHyperLinkEditPopupOperation } from '../commands/operations/popup.operation';
import { DocHyperLinkPopupService } from '../services/hyper-link-popup.service';

export const DocLinkPopup = () => {
    const hyperLinkService = useDependency(DocHyperLinkPopupService);
    const commandService = useDependency(ICommandService);
    const messageService = useDependency(IMessageService);
    const localeService = useDependency(LocaleService);
    const currentPopup = useObservable(hyperLinkService.showingLink$, hyperLinkService.showing);
    const univerInstanceService = useDependency(IUniverInstanceService);
    const permissionService = useDependency(IPermissionService);
    if (!currentPopup) {
        return null;
    }

    const { unitId, linkId, segmentId, startIndex, endIndex } = currentPopup;
    const doc = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
    const body = doc instanceof DocumentDataModel
        ? doc.getSelfOrHeaderFooterModel(segmentId)?.getBody()
        : undefined;
    const link = body?.customRanges?.find((range) => range.rangeId === linkId && range.rangeType === CustomRangeType.HYPERLINK && range.startIndex === startIndex && range.endIndex === endIndex);

    if (!link) {
        return null;
    }

    const target = link.properties?.url ?? `#${link.properties?.bookmarkId ?? link.properties?.headingId ?? ''}`;
    const canEdit = hyperLinkService.canEditLink(unitId, currentPopup);
    const canCopy = getDocumentPermissionValue(permissionService, unitId, unitId, UnitAction.Copy);
    return (
        <div
            className={clsx(`
              univer-box-border univer-flex univer-max-w-80 univer-items-center univer-justify-between
              univer-overflow-hidden univer-rounded-lg univer-bg-gray-0 univer-p-3 univer-shadow
              dark:!univer-bg-gray-900
            `, borderClassName)}
            onPointerEnter={() => {
                hyperLinkService.setInfoPopupHovered(true);
            }}
            onPointerLeave={() => {
                hyperLinkService.setInfoPopupHovered(false);
                hyperLinkService.scheduleHideInfoPopup();
            }}
            onClick={() => {
                hyperLinkService.hideInfoPopup();
            }}
        >
            <div
                className={`
                  univer-flex univer-h-6 univer-flex-1 univer-cursor-pointer univer-items-center univer-truncate
                  univer-text-sm univer-leading-5 univer-text-primary-500
                `}
                onClick={() => commandService.executeCommand(ClickDocHyperLinkOperation.id, { unitId, linkId, segmentId })}
            >
                <div
                    className={`
                      univer-mr-2 univer-flex univer-size-5 univer-flex-[0_0_auto] univer-items-center
                      univer-justify-center univer-text-base univer-text-gray-900
                      dark:!univer-text-gray-0
                    `}
                >
                    <LinkIcon />
                </div>
                <Tooltip showIfEllipsis title={target}>
                    <span className="univer-flex-1 univer-truncate">{target}</span>
                </Tooltip>
            </div>
            <div className="univer-flex univer-h-6 univer-flex-[0_0_auto] univer-items-center univer-justify-center">
                <Button
                    type="button"
                    variant="text"
                    size="small"
                    aria-label={localeService.t<LocaleKey>('docs-hyper-link-ui.info.copy')}
                    className="
                      univer-ml-2 !univer-flex univer-size-6 !univer-rounded !univer-border-0 !univer-bg-transparent
                      !univer-p-0 !univer-text-base !univer-font-normal
                      hover:!univer-bg-transparent
                      active:!univer-bg-transparent
                      disabled:!univer-pointer-events-auto disabled:!univer-opacity-40
                    "
                    disabled={!canCopy}
                    onClick={() => {
                        navigator.clipboard.writeText(target);
                        messageService.show({
                            content: localeService.t<LocaleKey>('docs-hyper-link-ui.info.coped'),
                            type: MessageType.Info,
                        });
                    }}
                >
                    <Tooltip placement="bottom" title={localeService.t<LocaleKey>('docs-hyper-link-ui.info.copy')}>
                        <CopyIcon />
                    </Tooltip>
                </Button>
                {canEdit && (
                    <>
                        <div
                            className={`
                              univer-ml-2 univer-flex univer-size-6 univer-cursor-pointer univer-items-center
                              univer-justify-center univer-rounded univer-text-base
                            `}
                            onClick={() => {
                                commandService.executeCommand(ShowDocHyperLinkEditPopupOperation.id, {
                                    link: currentPopup,
                                });
                            }}
                        >
                            <Tooltip placement="bottom" title={localeService.t<LocaleKey>('docs-hyper-link-ui.info.edit')}>
                                <WriteIcon />
                            </Tooltip>
                        </div>
                        <div
                            className={`
                              univer-ml-2 univer-flex univer-size-6 univer-cursor-pointer univer-items-center
                              univer-justify-center univer-rounded univer-text-base
                            `}
                            onClick={() => {
                                commandService.executeCommand(DeleteDocHyperLinkCommand.id, {
                                    unitId,
                                    linkId: link.rangeId,
                                    segmentId,
                                });
                            }}
                        >
                            <Tooltip placement="bottom" title={localeService.t<LocaleKey>('docs-hyper-link-ui.info.cancel')}>
                                <UnlinkIcon />
                            </Tooltip>
                        </div>
                    </>
                )}
            </div>
        </div>
    );
};

DocLinkPopup.componentKey = 'univer.doc.link-info-popup';