am-editor-002/docs/api/editor-block.md

6.0 KiB
Raw Blame History

BlockModel

Edit related operations of block-level nodes

Type: BlockModelInterface

Use

new Engine(...).block

Constructor

new (editor: EditorInterface): BlockModelInterface

Method

init

initialization

/**
 * Initialization
 */
init(): void;

findPlugin

Find the block plugin instance according to the node

/**
 * Find the block plugin instance according to the node
 * @param node node
 */
findPlugin(node: NodeInterface): BlockInterface | undefined;

findTop

Find the first-level node of the Block node. For example, div -> H2 returns H2 node

/**
 * Find the first level node of the Block node. For example, div -> H2 returns H2 node
 * @param parentNode parent node
 * @param childNode child node
 */
findTop(parentNode: NodeInterface, childNode: NodeInterface): NodeInterface;

closest

Get the nearest block node, can not find the return node

/**
 * Get the nearest block node, the return node cannot be found
 * @param node node
 */
closest(node: NodeInterface): NodeInterface;

wrap

Wrap a block node at the cursor position

/**
 * Wrap a block node at the cursor position
 * @param block node
 * @param range cursor
 */
wrap(block: NodeInterface | Node | string, range?: RangeInterface): void;

unwrap

Remove the package of the block node where the cursor is located

/**
 * Remove the package of the block node where the cursor is located
 * @param block node
 * @param range cursor
 */
unwrap(block: NodeInterface | Node | string, range?: RangeInterface): void;

getSiblings

Get the node's sibling node set relative to the cursor start position and end position

/**
 * Get the node's sibling node set relative to the cursor start position and end position
 * @param range cursor
 * @param block node
 */
getSiblings(
    range: RangeInterface,
    block: NodeInterface,
): Array<{ node: NodeInterface; position:'left' |'center' |'right' }>;

split

Split the block node selected by the current cursor

/**
 * Split the block node selected by the current cursor
 * @param range cursor
 */
split(range?: RangeInterface): void;

insert

Insert a block node at the current cursor position

/**
  * Insert a block node at the current cursor position
  * @param block node
  * @param range cursor
  * @param splitNode split node, the default is the block node at the beginning of the cursor
  */
insert(
     block: NodeInterface | Node | string,
     range?: RangeInterface,
     splitNode?: (node: NodeInterface) => NodeInterface,
): void;

setBlocks

Set all block nodes where the current cursor is located as new nodes or set new attributes

/**
 * Set all block nodes where the current cursor is located as new nodes or set new attributes
 * @param block The node or node attribute that needs to be set
 * @param range cursor
 */
setBlocks(
    block: string | {[k: string]: any },
    range?: RangeInterface,
): void;

merge

Merge blocks adjacent to the current cursor position

/**
 * Combine blocks adjacent to the current cursor position
 * @param range cursor
 */
merge(range?: RangeInterface): void;

findBlocks

Find all blocks that have an effect on the range

/**
 * Find all blocks that have an effect on the range
 * @param range
 */
findBlocks(range: RangeInterface): Array<NodeInterface>;

isFirstOffset

Determine whether the {Edge}Offset of the range is at the beginning of the Block

/**
 * Determine whether the {Edge}Offset of the range is at the beginning of the Block
 * @param range cursor
 * @param edge start  end
 */
isFirstOffset(range: RangeInterface, edge:'start' |'end'): boolean;

isLastOffset

Determine whether the {Edge}Offset of the range is at the last position of the Block

/**
 * Determine whether the {Edge}Offset of the range is at the last position of the Block
 * @param range cursor
 * @param edge start  end
 */
isLastOffset(range: RangeInterface, edge:'start' |'end'): boolean;

getBlocks

Get all blocks in the range

/**
 * Get all blocks in the range
 * @param range cursors
 */
getBlocks(range: RangeInterface): Array<NodeInterface>;

getLeftText

Get the left text of Block

/**
 * Get the left text of Block
 * @param block node
 */
getLeftText(block: NodeInterface | Node): string;

removeLeftText

Delete the left text of Block

/**
 * Delete the text on the left side of Block
 * @param block node
 */
removeLeftText(block: NodeInterface | Node): void;

getBlockByRange

Generate the node on the left or right side of the cursor and place it in the same container as the parent node

/**
 * Generate the node on the left or right side of the cursor and place it in the same container as the parent node
 * isLeft = true: left
 * isLeft = false: the right side
 * @param {block,range,isLeft,clone,keepID} node, cursor, left or right, whether to copy, whether to keep id
 *
 */
getBlockByRange({
    block,
    range,
    isLeft,
    clone,
    keepID,
}: {
    block: NodeInterface | Node;
    range: RangeInterface;
    isLeft: boolean;
    clone?: boolean;
    keepID?: boolean;
}): NodeInterface;

normal

Sort block-level nodes into standard editor values

/**
 * Sorting block-level nodes
 * @param node node
 * @param root root node
 */
normal(node: NodeInterface, root: NodeInterface): void;

brToBlock

br change line to paragraph

/**
 * br change lines to paragraphs
 * @param block node
 */
brToBlock(block: NodeInterface): void;

insertEmptyBlock

Insert an empty block node

/**
 * Insert an empty block node
 * @param range cursor position
 * @param block node
 * @returns
 */
insertEmptyBlock(range: RangeInterface, block: NodeInterface): void;

insertOrSplit

Insert or split node at cursor position

/**
 * Insert or split a node at the cursor position
 * @param range cursor position
 * @param block node
 */
insertOrSplit(range: RangeInterface, block: NodeInterface): void;