Skip to content

Block Manager

The Block Manager is the main class for managing blocks in the editor. It is accessible from any part of the application and allows creating, deleting, moving blocks, as well as performing other operations on them.

BlockManager

A class for managing blocks in the editor.

BlockManager Methods

FunctionSignatureDescription
toaststoasts(): IToastsReturns the toast notification service instance.
refreshVirtualSelectionrefreshVirtualSelection(): IVirtualSelection | nullRefreshes the virtual selection and returns its instance.
getVirtualSelectiongetVirtualSelection(): IVirtualSelection | nullReturns the virtual selection instance or creates it if it doesn't exist.
clearVirtualSelectionclearVirtualSelection(): voidClears the virtual selection.
destroyVirtualSelectiondestroyVirtualSelection(): voidDestroys the virtual selection.
getBlocksContainergetBlocksContainer(): HTMLElement | nullReturns the blocks container.
focusfocus(index: number, startPos?: number, endPos?: number, itemIndex?: number): BlockElement | nullSets focus on a block by index.
getBlocksgetBlocks(): BlockElement[]Returns an array of all blocks.
getBlockgetBlock(index?: number): BlockElement | nullReturns the block by index or the active one.
getContentElementgetContentElement(blockElement?: BlockElement): HTMLElement | nullReturns the content element of a block.
getNextBlockElementgetNextBlockElement(): BlockElement | nullReturns the next block.
getPrevBlockElementgetPrevBlockElement(): BlockElement | nullReturns the previous block.
findParentfindParent(targetElement: EventTarget | BlockElement | HTMLElement): BlockElement | nullFinds the parent block for the given element.
useuse(index: number): voidSets the current active block by index.
getIndexgetIndex(el?: BlockElement | HTMLElement | EventTarget): numberReturns the index of a block.
countcount(): numberReturns the number of blocks.
isEmptyisEmpty(index?: number): booleanChecks if the block at the given index is empty.
detectEmptydetectEmpty(emptyAttr: boolean = true): voidDetects empty blocks and sets the data-empty attribute.
normalizenormalize(): voidNormalizes the content of all blocks.
getModelgetModel(index?: number): BlockModel | nullReturns the block model by index.
removeBlockremoveBlock(index: number | number[] = -1, skipEvents: boolean = false): number | nullRemoves block(s) by index(es).
createDefaultBlockcreateDefaultBlock(index: number = -1, options?: BlockCreateSchema): BlockElement | nullCreates a default block.
createBlockcreateBlock(name: string, index: number = -1, options?: BlockCreateSchema, skipEvents: boolean = false, scrollIntoView: boolean | ScrollIntoViewOptions = true): BlockElement | nullCreates a new block with the specified name.
rebuildrebuild(index: number): BlockElement | nullRebuilds the block at the given index, updating its entire structure.
moveBlockmoveBlock(index: number, targetIndex: number, skipEvents: boolean = false): voidMoves a block to a new position.
mergemerge(index: number, targetIndex: number, focus?: number, useItems?: boolean): voidMerges two editable blocks.
convertconvert(blockElement: BlockElement, targetModel: BlockModel): booleanConverts a block to another type (if supported).
getModelsgetModels(): BlockModel[]Returns an array of all block models.
getSchemasgetSchemas(): BlockModelSchema[]Returns an array of block schemas.
cleanupSchemascleanupSchemas(): voidClears the block schema cache.
getSchemagetSchema(name: string): BlockModelSchema | nullReturns the block schema by name.
getRealNamegetRealName(name: string): string | nullReturns the real block name by alias.
htmlToDatahtmlToData(html: string): Array<BlockSchema | string>Converts HTML to block data.
parseBlockparseBlock(blockSchema: BlockSchema, skipDecode: boolean = false): BlockElement | nullParses a block schema and returns a block element.
parseBlocksparseBlocks(data: BlockSchema[], skipDecode: boolean = false): BlockElement[]Parses an array of block schemas and returns block elements.
parseChildrenparseChildren(schema: BlockSchema | BlockChildSchema, skipDecode: boolean = false, returnElement: boolean = false): Node[]Parses the child elements of a block schema.
destroydestroy(): voidDestroys the BlockManager instance and cleans up resources.

Example of Using BlockManager

javascript
import { BlockModel } from "@/core/models";
import { Paragraph } from "texditor/entities/blocks";
import Texditor from "texditor";

// Example of a custom block and using the block manager inside it (this is for reference only, do not use this in real projects)
class CustomHeader extends BlockModel {
  onMount(evt) {
    const { blockManager } = this.editor;
    const currentBlock = blockManager.getBlock();
    const index = blockManager.getIndex();

    console.log(currentBlock, index)
  }
}

const editor = new Texditor({
  handle: "my-editor",
  blocks: [
    Paragraph,
    CustomHeader.setup({
      name: 'header',
      tagName: 'h2',
    })
  ]
});

// Removing a block by index
editor.blockManager.removeBlock(1);

// Creating a new block
editor.blockManager.createBlock('p', 0);

// Getting the current block
const currentBlock = editor.blockManager.getBlock();

// Focusing on a block
editor.blockManager.focus(2);

// Moving a block
editor.blockManager.moveBlock(0, 3);

// Merging blocks
editor.blockManager.merge(1, 2);

dev@priveted.com | priveted.com