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

NameTypeDescription
acceptstring[]Allowed MIME types. Defaults to png, jpeg, webp, gif.
maxSizenumberMaximum file size in bytes. Defaults to 5 MB.
upload(file) => Promise<UploadResult>Required. Returns { src, width?, height?, alt? }.
onError(error: Error) => voidCalled on validation or upload failure.

Commands

NameTypeDescription
command("pickImage")voidOpens the native file picker and uploads the choice.
command("uploadImage", file)voidUploads a File you already have.
command("setImageAlt", alt)voidSets alt text on the selected image.
command("deleteImage")voidRemoves 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));