import { mergeAttributes, Node } from "@tiptap/core" import type { NodeViewProps } from "@tiptap/react" import { NodeViewWrapper, ReactNodeViewRenderer } from "@tiptap/react" import { useEffect, useState } from "react" import { BlockActions } from "../components/BlockActions" import { BlockInsert } from "../components/BlockInsert" import { ImagePicker } from "../components/ImagePicker" import { pb } from "../pb" import type { PbImage } from "../types" 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 ? ( ) : ( )}
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) }, })