Skip to main content

Overview

The Blocks namespace provides methods for block-level operations in Yoopta Editor. Access it via:
import { Blocks } from '@yoopta/editor';

// Use with editor instance
Blocks.insertBlock(editor, 'Paragraph', { at: 0 });

// Or via editor methods
editor.insertBlock('Paragraph', { at: 0 });

Block Creation

insertBlock

Insert a new block at the specified position.
function insertBlock(
  editor: YooEditor,
  type: string,
  options?: InsertBlockOptions
): string
editor
YooEditor
required
The editor instance
type
string
required
Block type in PascalCase (e.g., “Paragraph”, “HeadingOne”, “Accordion”)
options
InsertBlockOptions
Insert options
return
string
The ID of the newly created block

Examples

// Insert paragraph at current position
const blockId = editor.insertBlock('Paragraph');

// Insert heading at specific position with focus
const headingId = editor.insertBlock('HeadingOne', {
  at: 0,
  focus: true
});

// Insert accordion with custom structure
const accordionId = editor.insertBlock('Accordion', {
  elements: editor.y('accordion-list', {
    children: [
      editor.y('accordion-list-item', {
        props: { isExpanded: false },
        children: [
          editor.y('accordion-list-item-heading'),
          editor.y('accordion-list-item-content')
        ]
      })
    ]
  })
});

duplicateBlock

Duplicate an existing block.
function duplicateBlock(
  editor: YooEditor,
  options?: DuplicateBlockOptions
): string | undefined
options
DuplicateBlockOptions

Examples

// Duplicate current block
const newId = editor.duplicateBlock();

// Duplicate specific block by ID
const newId = editor.duplicateBlock({ blockId: 'block-123' });

// Duplicate and insert at specific position
const newId = editor.duplicateBlock({ at: 3, insertAt: 0 });

buildBlockData

Build block data structure with defaults.
function buildBlockData(
  block?: BuildBlockDataOptions
): YooptaBlockData
block
BuildBlockDataOptions

Examples

// Build default paragraph block
const block = Blocks.buildBlockData();

// Build custom block
const block = Blocks.buildBlockData({
  type: 'HeadingOne',
  meta: { order: 0, depth: 0, align: 'center' }
});

Block Modification

updateBlock

Update block metadata or value.
function updateBlock(
  editor: YooEditor,
  blockId: string,
  newData: Omit<Partial<YooptaBlockData>, 'id' | 'type'>
): void
blockId
string
required
ID of the block to update
newData
Partial<YooptaBlockData>

Examples

// Update block alignment
editor.updateBlock('block-123', {
  meta: { align: 'center' }
});

// Update block depth
editor.updateBlock('block-123', {
  meta: { depth: 1 }
});

// Update block value
editor.updateBlock('block-123', {
  value: [newSlateElement]
});

toggleBlock

Toggle block type while preserving content.
function toggleBlock(
  editor: YooEditor,
  type: string,
  options?: ToggleBlockOptions
): string
type
string
required
Target block type in PascalCase
options
ToggleBlockOptions
return
string
The ID of the toggled/new block

Examples

// Transform paragraph to heading
editor.toggleBlock('HeadingOne', { preserveContent: true });

// Toggle back to paragraph (if already HeadingOne)
editor.toggleBlock('HeadingOne'); // Becomes Paragraph

// Insert element in current leaf
editor.toggleBlock('Paragraph', {
  scope: 'element',
  preserveContent: false
});

// With custom structure
editor.toggleBlock('Accordion', {
  elements: editor.y('accordion-list', { /* ... */ })
});

moveBlock

Move a block to a new position.
function moveBlock(
  editor: YooEditor,
  draggedBlockId: string,
  newPath: number
): void
draggedBlockId
string
required
ID of the block to move
newPath
number
required
New position (order) for the block

Examples

// Move block to position 0
editor.moveBlock('block-123', 0);

// Move block to end
const blockCount = Object.keys(editor.children).length;
editor.moveBlock('block-123', blockCount - 1);

increaseBlockDepth

Increase block nesting depth (indent).
function increaseBlockDepth(
  editor: YooEditor,
  options?: BlockDepthOptions
): void
options
BlockDepthOptions

Examples

// Increase depth of current block
editor.increaseBlockDepth();

// Increase depth of specific block
editor.increaseBlockDepth({ blockId: 'block-123' });

decreaseBlockDepth

Decrease block nesting depth (outdent).
function decreaseBlockDepth(
  editor: YooEditor,
  options?: BlockDepthOptions
): void
options
BlockDepthOptions
Same as increaseBlockDepth

Examples

// Decrease depth of current block
editor.decreaseBlockDepth();

// Decrease depth of specific block
editor.decreaseBlockDepth({ at: 3 });

Block Deletion

deleteBlock

Delete a block.
function deleteBlock(
  editor: YooEditor,
  options?: DeleteBlockOptions
): void
options
DeleteBlockOptions

Examples

// Delete current block
editor.deleteBlock();

// Delete specific block by path
editor.deleteBlock({ at: 3 });

// Delete by ID without focusing
editor.deleteBlock({ blockId: 'block-123', focus: false });

// Delete and focus next block
editor.deleteBlock({ at: 3, focusTarget: 'next' });

Block Splitting & Merging

splitBlock

Split a block at the selection point.
function splitBlock(
  editor: YooEditor,
  options?: SplitBlockOptions
): string | undefined
options
SplitBlockOptions
return
string | undefined
ID of the newly created block, or undefined if split failed

Examples

// Split current block at selection
const newBlockId = editor.splitBlock();

// Split specific block
const newBlockId = editor.splitBlock({ at: 3 });

// Split and focus original block
const newBlockId = editor.splitBlock({ focusTarget: 'original' });

mergeBlock

Merge a block into another block.
function mergeBlock(
  editor: YooEditor,
  options?: MergeBlockOptions
): void
options
MergeBlockOptions

Examples

// Merge current block into previous (default)
editor.mergeBlock();

// Merge specific block into previous
editor.mergeBlock({ at: 5 });

// Merge block at index 5 into block at index 3
editor.mergeBlock({ at: 5, targetAt: 3 });

// Merge without focusing
editor.mergeBlock({ focus: false });

Block Focus & Retrieval

focusBlock

Set focus to a specific block.
function focusBlock(
  editor: YooEditor,
  blockId: string,
  options?: FocusBlockOptions
): void
blockId
string
required
ID of the block to focus
options
FocusBlockOptions

Examples

// Focus block
editor.focusBlock('block-123');

// Focus at end of block
const lastPoint = Paths.getLastNodePoint(slate);
editor.focusBlock('block-123', { focusAt: lastPoint });

// Focus immediately without waiting
editor.focusBlock('block-123', { waitExecution: false });

getBlock

Retrieve a block by ID or position.
function getBlock(
  editor: YooEditor,
  options: GetBlockOptions
): YooptaBlockData | null
options
GetBlockOptions
return
YooptaBlockData | null
The block data or null if not found

Examples

// Get block by ID
const block = editor.getBlock({ id: 'block-123' });

// Get block by position
const block = editor.getBlock({ at: 0 });

if (block) {
  console.log(block.type, block.meta.order);
}

getBlockSlate

Retrieve the Slate editor instance for a block.
function getBlockSlate(
  editor: YooEditor,
  options: GetBlockSlateOptions
): SlateEditor | null
options
GetBlockSlateOptions
return
SlateEditor | null
The Slate editor instance or null if not found

Examples

// Get slate editor by block ID
const slate = Blocks.getBlockSlate(editor, { id: 'block-123' });

// Get slate editor by position
const slate = Blocks.getBlockSlate(editor, { at: 0 });

if (slate) {
  // Perform Slate operations
  const selection = slate.selection;
}

Type Definitions

type YooptaBlockData = {
  id: string;
  type: string;           // PascalCase: "Paragraph", "HeadingOne"
  value: SlateElement[];  // Slate elements
  meta: YooptaBlockBaseMeta;
};

type YooptaBlockBaseMeta = {
  order: number;          // Block position
  depth: number;          // Nesting depth
  align?: 'left' | 'center' | 'right';
};

type SlateElement = {
  id: string;
  type: string;           // kebab-case: "paragraph", "heading-one"
  children: Descendant[];
  props?: Record<string, unknown>;
};

Build docs developers (and LLMs) love