import { NodeViewProps, NodeViewWrapper } from "@tiptap/react"; import { ActionIcon, Group, Loader, Text, Tooltip } from "@mantine/core"; import { useCallback, useMemo, useState } from "react"; import { getFileUrl } from "@/lib/config.ts"; import { ResizableWrapper } from "../common/resizable-wrapper"; import clsx from "clsx"; import classes from "./pdf-view.module.css"; import { useTranslation } from "react-i18next"; import { isInternalFileUrl } from "@docmost/editor-ext"; import { IconFileTypePdf, IconPaperclip, IconTrash, } from "@tabler/icons-react"; export default function PdfView(props: NodeViewProps) { const { t } = useTranslation(); const { editor, node, getPos, selected, updateAttributes } = props; const { src, placeholder, width: nodeWidth, height: nodeHeight } = node.attrs; const [hasError, setHasError] = useState(false); const safeSrc = useMemo(() => { if (!src || !isInternalFileUrl(src)) return null; return getFileUrl(src); }, [src]); const handleSelect = useCallback(() => { const pos = getPos(); if (pos !== undefined) { editor.commands.setNodeSelection(pos); } }, [editor, getPos]); const handleResize = useCallback( (newWidth: number, newHeight: number) => { updateAttributes({ width: newWidth, height: newHeight }); }, [updateAttributes], ); const handleConvertToAttachment = useCallback(() => { if (!src) return; const pos = getPos(); if (pos === undefined) return; const currentNode = editor.state.doc.nodeAt(pos); if (!currentNode || currentNode.type.name !== "pdf") return; editor .chain() .insertContentAt( { from: pos, to: pos + currentNode.nodeSize }, { type: "attachment", attrs: { url: currentNode.attrs.src, name: currentNode.attrs.name, attachmentId: currentNode.attrs.attachmentId, size: currentNode.attrs.size, mime: "application/pdf", }, }, ) .run(); }, [editor, src, getPos]); const handleDelete = useCallback(() => { const pos = getPos(); if (pos === undefined) return; editor.commands.setNodeSelection(pos); editor.commands.deleteSelection(); }, [editor, getPos]); if (!src || !safeSrc) { return (
{placeholder?.name ? t("Uploading {{name}}", { name: placeholder.name }) : t("Uploading file")}
); } if (hasError) { return (
{t("Failed to load PDF")}
); } return (