-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d49c2f
commit a202e6e
Showing
9 changed files
with
185 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { createImageUpload } from "novel/plugins"; | ||
import { toast } from "sonner"; | ||
|
||
const onUpload = (file: File) => { | ||
const promise = fetch("/api/upload", { | ||
method: "POST", | ||
headers: { | ||
"content-type": file?.type || "application/octet-stream", | ||
"x-vercel-filename": file?.name || "image.png", | ||
}, | ||
body: file, | ||
}); | ||
return new Promise((resolve) => { | ||
toast.promise( | ||
promise.then(async (res) => { | ||
// Successfully uploaded image | ||
if (res.status === 200) { | ||
const { url } = (await res.json()) as any; | ||
// preload the image | ||
let image = new Image(); | ||
image.src = url; | ||
image.onload = () => { | ||
resolve(url); | ||
}; | ||
// No blob store configured | ||
} else if (res.status === 401) { | ||
resolve(file); | ||
throw new Error( | ||
"`BLOB_READ_WRITE_TOKEN` environment variable not found, reading image locally instead.", | ||
); | ||
// Unknown error | ||
} else { | ||
throw new Error(`Error uploading image. Please try again.`); | ||
} | ||
}), | ||
{ | ||
loading: "Uploading image...", | ||
success: "Image uploaded successfully.", | ||
error: (e) => e.message, | ||
}, | ||
); | ||
}); | ||
}; | ||
|
||
export const uploadFn = createImageUpload({ | ||
onUpload, | ||
validateFn: (file) => { | ||
if (!file.type.includes("image/")) { | ||
toast.error("File type not supported."); | ||
return; | ||
} else if (file.size / 1024 / 1024 > 20) { | ||
toast.error("File size too big (max 20MB)."); | ||
return; | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
export { | ||
UploadImagesPlugin, | ||
startImageUpload, | ||
handleImageUpload, | ||
type UploadFn, | ||
type ImageUploadOptions, | ||
createImageUpload, | ||
handleImageDrop, | ||
handleImagePaste, | ||
} from "./upload-images"; |
Oops, something went wrong.