The toggleBlock method transforms a block to a different type (e.g., Paragraph → Heading) or inserts an element in the current leaf, depending on the scope.
import { Blocks } from '@yoopta/editor';// Transform paragraph to headingBlocks.toggleBlock(editor, 'Heading', { preserveContent: true,});// Transform to same type (toggles back to Paragraph)Blocks.toggleBlock(editor, 'Paragraph', { scope: 'block',});
// Automatically determines scope based on context// If current element has injectElementsFromPlugins → element scope// Otherwise → block scopeBlocks.toggleBlock(editor, 'Heading', { scope: 'auto', // default});
When scope is 'block', the entire block is transformed. When scope is 'element', an element is inserted in the current leaf (only works if the current element has injectElementsFromPlugins).
If toggling to the same type in block scope, it will toggle back to the default block type (Paragraph).
The preserveContent option extracts text nodes and transfers them to the new block/element structure.
If the target plugin type doesn’t exist, an error will be thrown.
The method automatically determines the appropriate root element type for the target plugin if not specified in elements.