import { Node, mergeAttributes } from "@tiptap/core" import { ReactNodeViewRenderer, NodeViewWrapper } from "@tiptap/react" import type { NodeViewProps } from "@tiptap/react" import { pb } from "../pb" import type { PbImage } from "../types" import { useState, useEffect } from "react" import { ImagePicker } from "../components/ImagePicker" import { BlockActions } from "../components/BlockActions" import { BlockInsert } from "../components/BlockInsert" function ImageBlockView({ node, updateAttributes, editor, getPos }: NodeViewProps) { const { imageId, wide, caption } = node.attrs const [thumbnail, setThumbnail] = useState(null) const [showPicker, setShowPicker] = useState(false) useEffect(() => { if (!imageId) return pb.collection("images").getOne(imageId).then((img) => { const url = pb.files.getURL(img, img.file, { thumb: "300x200" }) setThumbnail(url) }).catch(() => { setThumbnail(null) }) }, [imageId]) function handleSelect(newImageId: string) { updateAttributes({ imageId: newImageId }) setShowPicker(false) } return ( <>
Image
{thumbnail ? ( {caption setShowPicker(true)} style={{ cursor: "pointer" }} /> ) : (
setShowPicker(true)} style={{ cursor: "pointer" }} > {imageId ? "Loading..." : "Click to select image"}
)}
updateAttributes({ caption: e.target.value })} className="node-image-caption" />
{showPicker && ( setShowPicker(false)} /> )}
) } export const ImageBlock = Node.create({ name: "imageBlock", group: "block", atom: true, draggable: true, addAttributes() { return { id: { default: null }, imageId: { default: "" }, wide: { default: false }, caption: { default: "" }, } }, parseHTML() { return [{ tag: "div[data-image-block]" }] }, renderHTML({ HTMLAttributes }) { return ["div", mergeAttributes(HTMLAttributes, { "data-image-block": "" })] }, addNodeView() { return ReactNodeViewRenderer(ImageBlockView) }, })