Editor API

Everything createEditor gives you.

Options

NameTypeDescription
elementHTMLElementHost element made contenteditable.
contentstring | BabloomDocInitial HTML or JSON document.
extensionsExtension[]Extensions to register.
editablebooleanDefaults to true.
autofocusbooleanFocus the editor on mount.
onUpdate({ editor }) => voidFired after every document change.
onSelectionUpdate({ editor }) => voidFired when the selection moves.
onFocus / onBlur({ editor }) => voidFocus lifecycle callbacks.

Commands

NameTypeDescription
toggleBold / Italic / Underline / Strike / CodeEditorToggle inline marks.
setBlock(tag)Editorp, h1, h2, h3 or blockquote.
toggleBulletList / toggleOrderedListEditorList toggles.
setHorizontalRule()EditorInsert a divider.
setLink(href) / unsetLink()EditorLink marks.
insertImage({ src, alt, width })EditorInsert an image node.
insertHTML(html)EditorInsert raw HTML at the caret.
clearFormatting()EditorRemove all marks from the selection.
undo() / redo() / can(action)Editor | booleanHistory.
command(name, ...args)EditorRun an extension command.

State

NameTypeDescription
isActive(name)booleanbold, italic, underline, strike, code, link, heading1-3, blockquote, bulletList, orderedList.
getHTML() / getJSON() / getText()string | BabloomDocSerialize the document.
setContent(content)EditorReplace the document.
setEditable(boolean)EditorToggle read-only mode.
focus() / destroy()voidLifecycle.

Events

ts
const off = editor.on("update", ({ editor }) => save(editor.getJSON()));
editor.on("selection", () => refreshToolbar());
editor.on("image:upload:error", ({ error }) => toast.error(error.message));
off(); // unsubscribe