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
Block type in PascalCase (e.g., “Paragraph”, “HeadingOne”, “Accordion”)
Insert options Position to insert block. Defaults to editor.path.current
Focus the new block after insertion
Partial block data to merge with new block
Custom element structure created with editor.y(). If provided, used as the block’s value
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
Block to duplicate by path. Defaults to editor.path.current
Position to insert duplicate. Defaults to original.meta.order + 1
Focus the duplicated block after creation
Custom element structure for duplicate
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 ID. Auto-generated if not provided
type
string
default: "'Paragraph'"
Block type in PascalCase
meta
Partial<YooptaBlockBaseMeta>
Block metadata (order, depth, align)
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
ID of the block to update
meta
Partial<YooptaBlockBaseMeta>
Update block metadata (order, depth, align)
Update block’s Slate element structure
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
Target block type in PascalCase
Block position. Defaults to editor.path.current
scope
'auto' | 'block' | 'element'
default: "'auto'"
Toggle scope:
'auto': automatically determine from context
'block': transform entire block (Paragraph → Heading)
'element': insert element in current leaf with injectElementsFromPlugins
Preserve existing text content
Custom element structure created with editor.y()
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
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
Block position. Defaults to editor.path.current
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
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
Block position. Defaults to editor.path.current
focusTarget
'previous' | 'next' | 'none'
default: "'previous'"
Which block to focus after deletion:
'previous': focus previous block (default)
'next': focus next block
'none': don’t focus anything
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
Block to split. Defaults to editor.path.current
Split position (Slate selection point). Defaults to current selection
focusTarget
'new' | 'original' | 'none'
default: "'new'"
Which block to focus:
'new': focus the new block (default)
'original': focus the original block
'none': don’t focus anything
Preserve content in both blocks
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
Source block to merge. Defaults to editor.path.current
Target block position. Defaults to previous block
Preserve content from source block
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
Position to focus within block (Point or Path)
Wait before executing focus
Milliseconds to wait before focus
Update editor.path.current after focus
Provide slate editor instance directly
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
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
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 >;
};