Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

1.4 KB
Raw Blame
import {
  getAttachmentFilenameFromLabel,
  PdfAttachment as BasePdfAttachment,
  resolvePdfDisplayMode,
} from "@edgeever/shared";
import { NodeViewWrapper, ReactNodeViewRenderer, type NodeViewProps } from "@tiptap/react";
import { PdfViewer } from "@/components/pdf/PdfViewer";

const PdfAttachmentNodeView = ({ node, updateAttributes }: NodeViewProps) => {
  const url = typeof node.attrs.url === "string" ? node.attrs.url : "";
  const label = typeof node.attrs.label === "string" ? node.attrs.label : "PDF";
  const filename = typeof node.attrs.filename === "string" && node.attrs.filename
    ? node.attrs.filename
    : getAttachmentFilenameFromLabel(label);
  const displayMode = resolvePdfDisplayMode(node.attrs.displayMode);

  return (
    <NodeViewWrapper
      as="span"
      className="edgeever-pdf-attachment-node"
      data-file-name={filename || label}
      data-file-url={url}
      contentEditable={false}
    >
      <PdfViewer
        url={url}
        label={label}
        filename={typeof node.attrs.filename === "string" ? node.attrs.filename : undefined}
        byteSize={node.attrs.byteSize}
        expanded={displayMode === "inline"}
        onExpandedChange={(expanded) => updateAttributes({ displayMode: expanded ? "inline" : "compact" })}
      />
    </NodeViewWrapper>
  );
};

export const PdfAttachment = BasePdfAttachment.extend({
  addNodeView() {
    return ReactNodeViewRenderer(PdfAttachmentNodeView);
  },
});