diff --git a/src/components/modules/api/blocks.ts b/src/components/modules/api/blocks.ts index 130311b48..68ca19132 100644 --- a/src/components/modules/api/blocks.ts +++ b/src/components/modules/api/blocks.ts @@ -1,9 +1,9 @@ import Module from '../../__module'; -import {Blocks} from '../../../../types/api'; -import {OutputData} from '../../../../types'; +import { Blocks } from '../../../../types/api'; +import { BlockToolData, OutputData, ToolConfig } from '../../../../types'; import Block from '../../block'; -import {ModuleConfig} from '../../../types-internal/module-config'; +import { ModuleConfig } from '../../../types-internal/module-config'; /** * @class BlocksAPI @@ -26,6 +26,12 @@ export default class BlocksAPI extends Module { getBlocksCount: () => this.getBlocksCount(), stretchBlock: (index: number, status: boolean = true) => this.stretchBlock(index, status), insertNewBlock: () => this.insertNewBlock(), + insert: ( + index: number, + toolName: string, + data: BlockToolData = {}, + settings: ToolConfig = {}, + ) => this.insert(index, toolName, data, settings), }; } @@ -147,4 +153,22 @@ export default class BlocksAPI extends Module { const newBlock = this.Editor.BlockManager.insert(); this.Editor.Caret.setToBlock(newBlock); } + + /** + * Insert new block at index with data + * + * @param {number} index — index of new block + * @param {string} toolName — plugin name + * @param {BlockToolData} data — plugin data + * @param {ToolConfig} settings - default settings + */ + public insert( + index: number, + toolName: string, + data: BlockToolData = {}, + settings: ToolConfig = {}, + ) { + const newBlock = this.Editor.BlockManager.insertAt(index, toolName, data, settings); + this.Editor.Caret.setToBlock(newBlock); + } } diff --git a/src/components/modules/blockManager.ts b/src/components/modules/blockManager.ts index 22e9cf269..fe85becad 100644 --- a/src/components/modules/blockManager.ts +++ b/src/components/modules/blockManager.ts @@ -233,10 +233,30 @@ export default class BlockManager extends Module { ): Block { // Increment index before construct, // because developers can use API/Blocks/getCurrentInputIndex on the render() method - const newIndex = ++this.currentBlockIndex; + const newIndex = this.currentBlockIndex + 1; + return this.insertAt(newIndex, toolName, data, settings); + } + + /** + * Insert new block into _blocks at index + * + * @param {Number} index — index of new block + * @param {String} toolName — plugin name, by default method inserts initial block type + * @param {Object} data — plugin data + * @param {Object} settings - default settings + * + * @return {Block} + */ + public insertAt( + index: number, + toolName: string = this.config.initialBlock, + data: BlockToolData = {}, + settings: ToolConfig = {}, + ): Block { + this.currentBlockIndex = index; const block = this.composeBlock(toolName, data, settings); - this._blocks[newIndex] = block; + this._blocks[index] = block; return block; } diff --git a/types/api/blocks.d.ts b/types/api/blocks.d.ts index 166c1f748..8ac611600 100644 --- a/types/api/blocks.d.ts +++ b/types/api/blocks.d.ts @@ -1,4 +1,6 @@ -import {OutputData} from '../data-formats/output-data'; +import { OutputData } from '../data-formats/output-data'; +import { BlockToolData } from '../tools/block-tool-data'; +import { ToolConfig } from '../tools/tool-config'; /** * Describes methods to manipulate with Editor`s blocks @@ -65,4 +67,18 @@ export interface Blocks { * Insert new Initial Block after current Block */ insertNewBlock(): void; + + /** + * Insert new block at index with data + * @param {number} index — index of new block + * @param {string} toolName — plugin name + * @param {BlockToolData} data — plugin data + * @param {ToolConfig} settings - default settings + */ + insert( + index: number, + toolName: string, + data?: BlockToolData, + settings?: ToolConfig, + ): void; }