|
| 1 | +import { FilePanelProps, useBlockNoteEditor } from "@blocknote/react"; |
| 2 | +import Uppy, { UploadSuccessCallback } from "@uppy/core"; |
| 3 | +import "@uppy/core/dist/style.min.css"; |
| 4 | +import "@uppy/dashboard/dist/style.min.css"; |
| 5 | +import { Dashboard } from "@uppy/react"; |
| 6 | +import XHR from "@uppy/xhr-upload"; |
| 7 | +import { useEffect } from "react"; |
| 8 | + |
| 9 | +// Image editor plugin |
| 10 | +import ImageEditor from "@uppy/image-editor"; |
| 11 | +import "@uppy/image-editor/dist/style.min.css"; |
| 12 | + |
| 13 | +// Screen capture plugin |
| 14 | +import ScreenCapture from "@uppy/screen-capture"; |
| 15 | +import "@uppy/screen-capture/dist/style.min.css"; |
| 16 | + |
| 17 | +// Webcam plugin |
| 18 | +import Webcam from "@uppy/webcam"; |
| 19 | +import "@uppy/webcam/dist/style.min.css"; |
| 20 | + |
| 21 | +// Configure your Uppy instance here. |
| 22 | +const uppy = new Uppy() |
| 23 | + // Enabled plugins - you probably want to customize this |
| 24 | + // See https://uppy.io/examples/ for all the integrations like Google Drive, |
| 25 | + // Instagram Dropbox etc. |
| 26 | + .use(Webcam) |
| 27 | + .use(ScreenCapture) |
| 28 | + .use(ImageEditor) |
| 29 | + |
| 30 | + // Uses an XHR upload plugin to upload files to tmpfiles.org. |
| 31 | + // You want to replace this with your own upload endpoint or Uppy Companion |
| 32 | + // server. |
| 33 | + .use(XHR, { |
| 34 | + endpoint: "https://tmpfiles.org/api/v1/upload", |
| 35 | + getResponseData(text, resp) { |
| 36 | + return { |
| 37 | + url: JSON.parse(text).data.url.replace( |
| 38 | + "tmpfiles.org/", |
| 39 | + "tmpfiles.org/dl/" |
| 40 | + ), |
| 41 | + }; |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | +export function UppyFilePanel(props: FilePanelProps) { |
| 46 | + const { block } = props; |
| 47 | + const editor = useBlockNoteEditor(); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + // Listen for successful tippy uploads, and then update the Block with the |
| 51 | + // uploaded URL. |
| 52 | + const handler: UploadSuccessCallback<Record<string, unknown>> = ( |
| 53 | + file, |
| 54 | + response |
| 55 | + ) => { |
| 56 | + if (!file) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (file.source === "uploadFile") { |
| 61 | + // Didn't originate from Dashboard, should be handled by `uploadFile` |
| 62 | + return; |
| 63 | + } |
| 64 | + if (response.status === 200) { |
| 65 | + const updateData = { |
| 66 | + props: { |
| 67 | + name: file?.name, |
| 68 | + url: response.uploadURL, |
| 69 | + }, |
| 70 | + }; |
| 71 | + editor.updateBlock(block, updateData); |
| 72 | + |
| 73 | + // File should be removed from the Uppy instance after upload. |
| 74 | + uppy.removeFile(file.id); |
| 75 | + } |
| 76 | + }; |
| 77 | + uppy.on("upload-success", handler); |
| 78 | + return () => { |
| 79 | + uppy.off("upload-success", handler); |
| 80 | + }; |
| 81 | + }, [block, editor]); |
| 82 | + |
| 83 | + // set up dashboard as in https://uppy.io/examples/ |
| 84 | + return <Dashboard uppy={uppy} width={400} height={500} />; |
| 85 | +} |
| 86 | + |
| 87 | +// Implementation for the BlockNote `uploadFile` function. |
| 88 | +// This function is used when for example, files are dropped into the editor. |
| 89 | +export async function uploadFile(file: File) { |
| 90 | + const id = uppy.addFile({ |
| 91 | + id: file.name, |
| 92 | + name: file.name, |
| 93 | + type: file.type, |
| 94 | + data: file, |
| 95 | + source: "uploadFile", |
| 96 | + }); |
| 97 | + |
| 98 | + try { |
| 99 | + const result = await uppy.upload(); |
| 100 | + return result.successful[0].response!.uploadURL!; |
| 101 | + } finally { |
| 102 | + uppy.removeFile(id); |
| 103 | + } |
| 104 | +} |
0 commit comments