blockcn

Block API

Read and edit blocks by id.

editor.blocks addresses blocks by blockId. Reading document does not assign ids. insertBlocks, updateBlock, removeBlocks, moveBlock, and replaceBlocks each run as one undo step. "nested" inserts inside a toggle or a column. See Block API.

This page's code block is examples/recipes/block-api.ts. pnpm --dir docs typecheck compiles that file and fails if this block drifts.

import "@/editor-core/blocks/extension";
import type { Editor } from "@tiptap/core";

/** Inserts a paragraph after the first block. One call is one undo step. */
export function appendParagraph(editor: Editor, text: string) {
  const first = editor.blocks.document[0];
  if (!first?.id) return false;
  return editor.blocks.insertBlocks([{ type: "paragraph", text }], first.id, "after");
}