import { Extension } from "@tiptap/core" import { Plugin, PluginKey } from "@tiptap/pm/state" export const ImagePaste = Extension.create({ name: "imagePaste", addProseMirrorPlugins() { return [ new Plugin({ key: new PluginKey("imagePaste"), props: { handlePaste: (view, event, _slice) => { const items = Array.from(event.clipboardData?.items || []) const imageItems = items.filter(item => item.type.startsWith("image/")) if (imageItems.length > 0) { event.preventDefault() imageItems.forEach(item => { const file = item.getAsFile() if (file) { // Get the current position const { from } = view.state.selection // Insert ImageUploadNode at current position with pasted file data const imageUploadNode = view.state.schema.nodes.imageUpload?.create({ accept: "image/*", maxSize: 5 * 1024 * 1024, // 5MB limit limit: 1, pastedFile: file, // Pass the file directly as an attribute }) if (imageUploadNode) { const tr = view.state.tr.insert(from, imageUploadNode) view.dispatch(tr) } } }) return true } return false }, }, }), ] }, })