Skip to main content

Blocks API

The Blocks API provides block-level operations: creation, deletion, movement, transformation, and navigation. It works above the Elements API, operating on entire blocks rather than elements within blocks.

Accessing the API

Import the Blocks namespace from @yoopta/editor:
Blocks API methods are not on the editor instance — use the Blocks namespace.

Type Definitions


Methods

insertBlock

Creates and inserts a new block of the given type.
Parameters:
  • editor — Editor instance (required).
  • type — Block type, e.g. 'Paragraph', 'Heading' (required).
  • options.at — Position to insert; default: editor.path.current.
  • options.focus — Whether to focus the block after insertion.
  • options.blockData — Custom block data (id, meta, value).
  • options.elements — Custom element structure via editor.y().
Returns: ID of the new block.

deleteBlock

Removes a block and optionally focuses another.
Parameters:
  • editor — Editor instance (required).
  • options.at — Position of the block to delete.
  • options.blockId — ID of the block to delete (overrides at if both set).
  • options.focus — Whether to focus a block after deletion; default: true.
  • options.focusTarget — Which block to focus: 'previous' | 'next' | 'none'; default: 'previous'.

updateBlock

Updates a block’s metadata and/or content.
Parameters:
  • editor — Editor instance (required).
  • blockId — ID of the block to update (required).
  • newData.meta — Metadata to update: order, depth, align.
  • newData.value — New block content (array of Slate elements).

getBlock

Retrieves block data by ID or path.
Parameters:
  • editor — Editor instance (required).
  • options.at — Position index of the block.
  • options.id — ID of the block (overrides at if both set). One of at or id is required.
Returns: Block data or null.

moveBlock

Moves a block to a new position.
Parameters:
  • editor — Editor instance (required).
  • draggedBlockId — ID of the block to move (required).
  • newPath — New position (order index) for the block (required).

focusBlock

Focuses a block and optionally sets cursor position.
Parameters:
  • editor — Editor instance (required).
  • blockId — ID of the block to focus (required).
  • options.waitExecution — Delay focus (e.g. until DOM is ready).
  • options.waitExecutionMs — Delay in milliseconds.
  • options.shouldUpdateBlockPath — Update editor.path.current; default: true.
  • options.focusAt — Cursor position: Point or Path; default: first node.
  • options.slate — Slate editor instance to use (optional).

duplicateBlock

Creates a copy of a block at a given position.
Parameters:
  • editor — Editor instance (required).
  • options.at — Position of the block to duplicate; default: editor.path.current.
  • options.blockId — ID of the block to duplicate (overrides at).
  • options.insertAt — Position to insert the duplicate; default: after original.
  • options.focus — Focus the new block after creation; default: true.
  • options.elements — Custom element structure for the duplicate.
Returns: New block ID or undefined.

splitBlock

Splits a block at the selection (or given position).
Parameters:
  • editor — Editor instance (required).
  • options.at — Position of the block to split; default: editor.path.current.
  • options.blockId — ID of the block to split.
  • options.splitAt — Location to split at; default: current selection.
  • options.focus — Focus after split; default: true.
  • options.focusTarget — Which block to focus: 'new' | 'original' | 'none'; default: 'new'.
  • options.preserveContent — Keep content in both blocks; default: true.
Returns: New block ID or undefined.

mergeBlock

Merges a block into another (default: into previous).
Parameters:
  • editor — Editor instance (required).
  • options.at — Position of the source block (the one merged into target).
  • options.blockId — ID of the source block.
  • options.targetAt — Position of the target block (receives content); default: previous block.
  • options.targetBlockId — ID of the target block.
  • options.focus — Focus the target block after merge; default: true.
  • options.preserveContent — Keep content from source block; default: true.

toggleBlock

Transforms block type (e.g. Paragraph → Heading) or inserts element in leaf.
Parameters:
  • editor — Editor instance (required).
  • type — Target block/element type, e.g. 'Heading', 'Paragraph' (required).
  • options.at — Position of the block; default: editor.path.current.
  • options.scope'auto' (default) | 'block' (transform block) | 'element' (insert in leaf).
  • options.preserveContent — Transfer text to new block/element; default: true.
  • options.focus — Focus after toggle; default: false.
  • options.elements — Custom element structure via editor.y().
Returns: Toggled block ID.

increaseBlockDepth

Increases block depth (indent).
Parameters:
  • editor — Editor instance (required).
  • options.at — Position of the block; default: editor.path.current.
  • options.blockId — ID of the block (overrides at).

decreaseBlockDepth

Decreases block depth (outdent).
Parameters:
  • editor — Editor instance (required).
  • options.at — Position of the block; default: editor.path.current.
  • options.blockId — ID of the block (overrides at).

getBlockSlate

Returns the Slate editor instance for a block.
Parameters:
  • editor — Editor instance (required).
  • options.at — Position of the block.
  • options.id — ID of the block (overrides at). One of at or id is required.
Returns: Slate editor or null.

buildBlockData

Builds a YooptaBlockData structure (does not insert into editor).
Parameters:
  • block.id — Block ID; if omitted, a unique ID is generated.
  • block.type — Block type (plugin name); default: 'Paragraph'.
  • block.value — Block content (array of Slate elements).
  • block.meta — Metadata: order, depth, align.
Returns: Complete YooptaBlockData object.