Block API
Read and change blocks by id, without ProseMirror positions.
editor.blocks is a small API over the document. Each method is one transaction, so one undo puts it back. Reading document does not write block ids. A block that has never been linked or passed through this API has id: null, and getJSON() stays the same.
const editor = useEditor({ extensions: createEditorExtensions() });
editor.blocks.document;
// [{ id: null, type: "paragraph", text: "Hello", props: {} }]
editor.blocks.insertBlocks(
[{ type: "paragraph", text: "After" }],
editor.blocks.document[0].id!,
"after",
);Give a block an id first (copy a block link, or set attrs.blockId when you create it) when you want to address a block that was already in the document.
Methods
| Method | What it does |
|---|---|
document | Top-level Block[]. Children of a container are on content. |
getBlock(id) | That block, or null. |
insertBlocks(blocks, ref, placement) | "before", "after", or "nested" (last child of ref). |
updateBlock(id, { text, props }) | Replaces the text of a text block and merges props. |
removeBlocks(ids) | Deletes those blocks. |
moveBlock(id, ref, placement) | Moves one block. |
replaceBlocks(ids, blocks) | Swaps those blocks for the new ones. |
placement: "nested" is how a paragraph goes inside a toggle or a column.
editor.blocks.insertBlocks([{ type: "paragraph", text: "Hidden" }], toggleId, "nested");
editor.blocks.updateBlock(id, { text: "Renamed", props: { textAlign: "center" } });
editor.commands.undo();Block
Block is a union of the core block types (paragraph, heading, blockquote, callout, toggle, lists, codeBlock, columns, column) with the props that type stores. Any other node is still a Block with open props. The list is maintained next to the schema in editor-core/blocks/api.ts.