Image upload
V1 media support: images only. Storage is entirely up to you.
Setup
ts
import { ImageExtension, type UploadResult } from "@tlob/babloom-core";
async function uploadImage(file: File): Promise<UploadResult> {
const body = new FormData();
body.append("file", file);
const res = await fetch("/api/upload", { method: "POST", body });
if (!res.ok) throw new Error("Upload failed");
const { url, width, height } = await res.json();
return { src: url, width, height, alt: file.name };
}
ImageExtension({
accept: ["image/png", "image/jpeg", "image/webp"],
maxSize: 5 * 1024 * 1024,
upload: uploadImage,
onError: (error) => toast.error(error.message),
});Options
| Name | Type | Description |
|---|---|---|
| accept | string[] | Allowed MIME types. Defaults to png, jpeg, webp, gif. |
| maxSize | number | Maximum file size in bytes. Defaults to 5 MB. |
| upload | (file) => Promise<UploadResult> | Required. Returns { src, width?, height?, alt? }. |
| onError | (error: Error) => void | Called on validation or upload failure. |
Commands
| Name | Type | Description |
|---|---|---|
| command("pickImage") | void | Opens the native file picker and uploads the choice. |
| command("uploadImage", file) | void | Uploads a File you already have. |
| command("setImageAlt", alt) | void | Sets alt text on the selected image. |
| command("deleteImage") | void | Removes the selected image. |
Drag, drop and paste
Dropping or pasting an image file into the editor runs the same validation and upload pipeline. Upload progress is reported through the image:upload:start, image:upload:done and image:upload:error events.
ts
editor.on("image:upload:start", ({ file }) => setBusy(file.name));