{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rich-text-editor-file-handler",
  "title": "File Handler",
  "description": "Paste and drop files into the document as image nodes.",
  "dependencies": [
    "@tiptap/extension-file-handler@3.32.2",
    "@tiptap/react@3.32.2"
  ],
  "registryDependencies": [
    "litingyes/blocove/rich-text-editor",
    "litingyes/blocove/rich-text-editor-extension",
    "litingyes/blocove/rich-text-editor-image"
  ],
  "files": [
    {
      "path": "src/registry/blocove/ui/rich-text-editor/file-handler.tsx",
      "content": "import type { Editor } from '@tiptap/react'\nimport type { ExtensionComponent } from '@/lib/blocove/rich-text-editor-extension'\nimport { FileHandler } from '@tiptap/extension-file-handler'\nimport { defineExtensionComponent } from '@/lib/blocove/rich-text-editor-extension'\n\nconst defaultAllowedMimeTypes = [\n  'image/png',\n  'image/jpeg',\n  'image/gif',\n  'image/webp',\n]\n\n/**\n * Reads a file into a data URL so the document stays self-contained. Consumers\n * that upload the file first can replace this through the `onFile` prop.\n */\nexport function readFileAsDataUrl(file: File): Promise<string> {\n  return new Promise((resolve, reject) => {\n    const reader = new FileReader()\n\n    reader.addEventListener('load', () => resolve(String(reader.result)))\n    reader.addEventListener('error', () => reject(reader.error))\n    reader.readAsDataURL(file)\n  })\n}\n\nasync function insertFiles(\n  editor: Editor,\n  files: File[],\n  position: number | null,\n  onFile?: (file: File) => Promise<string> | string,\n) {\n  const uploaded = await Promise.all(\n    files.map(async (file) => {\n      const src = onFile\n        ? await onFile(file)\n        : await readFileAsDataUrl(file)\n      return { src, alt: file.name }\n    }),\n  )\n\n  const content = uploaded.map(({ src, alt }) => ({\n    type: 'image',\n    attrs: { src, alt },\n  }))\n\n  if (content.length === 0) {\n    return\n  }\n\n  const at = position ?? editor.state.selection.from\n\n  editor.chain().focus().insertContentAt(at, content).run()\n}\n\nexport interface EditorFileHandlerProps {\n  /** Mime types the handler reacts to. */\n  allowedMimeTypes?: string[]\n  /**\n   * Turns a file into the URL inserted into the document. The default reads the\n   * file as a data URL, which keeps the document self-contained.\n   */\n  onFile?: (file: File) => Promise<string> | string\n}\n\n/**\n * Registers the file handler for pasted and dropped files. Files are inserted\n * as image nodes, so the editor also needs the image node installed.\n */\nexport const EditorFileHandler: ExtensionComponent<EditorFileHandlerProps> = defineExtensionComponent(\n  'file-handler',\n  ({ allowedMimeTypes = defaultAllowedMimeTypes, onFile }: EditorFileHandlerProps) => [\n    FileHandler.configure({\n      allowedMimeTypes,\n      // Take over the paste event so the pasted HTML does not also land in the\n      // document next to the image.\n      consumePasteEvent: true,\n      onPaste: (currentEditor, files) => {\n        void insertFiles(currentEditor, files, null, onFile)\n      },\n      onDrop: (currentEditor, files, position) => {\n        void insertFiles(currentEditor, files, position, onFile)\n      },\n    }),\n  ],\n)\n",
      "type": "registry:ui",
      "target": "components/blocove/rich-text-editor/file-handler.tsx"
    }
  ],
  "categories": [
    "rich-text-editor",
    "extension"
  ],
  "type": "registry:ui"
}