diff --git a/src/components/block/api.ts b/src/components/block/api.ts index d760ab63e..c6fff254e 100644 --- a/src/components/block/api.ts +++ b/src/components/block/api.ts @@ -104,6 +104,16 @@ function BlockAPI( return block.save(); }, + /** + * Update Block data + * + * @param {BlockToolData} data - data to update + * @returns {Promise} + */ + update(data: BlockToolData): Promise { + return block.update(data); + }, + /** * Validate Block data * diff --git a/src/components/block/index.ts b/src/components/block/index.ts index 642ed4830..fbbb4e6ad 100644 --- a/src/components/block/index.ts +++ b/src/components/block/index.ts @@ -78,6 +78,7 @@ export enum BlockToolAPI { RENDERED = 'rendered', MOVED = 'moved', UPDATED = 'updated', + UPDATE = 'update', REMOVED = 'removed', // eslint-disable-next-line @typescript-eslint/naming-convention ON_PASTE = 'onPaste', @@ -600,6 +601,21 @@ export default class Block extends EventsDispatcher { await this.toolInstance.merge(data); } + /** + * Call plugins update method + * + * @param {BlockToolData} data - data to update + */ + public async update(data: BlockToolData): Promise { + let updated = false; + + if (this.toolInstance.update instanceof Function) { + updated = await this.toolInstance.update(data); + } + + return updated; + } + /** * Extracts data from Block * Groups Tool's save processing time diff --git a/src/components/modules/api/blocks.ts b/src/components/modules/api/blocks.ts index a711d52fd..fa01e8faa 100644 --- a/src/components/modules/api/blocks.ts +++ b/src/components/modules/api/blocks.ts @@ -298,15 +298,9 @@ export default class BlocksAPI extends Module { return; } - const blockIndex = BlockManager.getBlockIndex(block); - - BlockManager.insert({ + BlockManager.update({ id: block.id, - tool: block.name, data, - index: blockIndex, - replace: true, - tunes: block.tunes, }); }; } diff --git a/src/components/modules/blockManager.ts b/src/components/modules/blockManager.ts index fb5f30399..c012e2c0a 100644 --- a/src/components/modules/blockManager.ts +++ b/src/components/modules/blockManager.ts @@ -321,6 +321,62 @@ export default class BlockManager extends Module { return block; } + /** + * Update block by id + * + * keep the tool name + * only update data of block + * + * @param {object} options - insert options + * @param {string} [options.id] - block's unique id + * @param {object} [options.data] - plugin data + * @returns {void} + */ + public update({ + id = undefined, + data = {}, + }: { + id?: string; + data?: BlockToolData; + } = {}): void { + const block = this.blocks.find(item => item.id === id); + + if (block.update(data)) { + this.blockDidMutated(BlockMutationType.Changed, block, { + id, + }); + + return; + } + + const index = this._blocks.array.findIndex(item => item.id === id); + + if (index === -1) { + _.log(`blockManamger.update(): invalid index of block id: ${id}`, 'warn'); + + return; + } + + const tool = this._blocks[index].tool.name; + const newBlock = this.composeBlock({ + id, + tool, + data, + tunes: {}, + }); + + this._blocks.insert(index, newBlock, true); + + /** + * Force call of didMutated event on Block changed + */ + this.blockDidMutated(BlockMutationType.Changed, newBlock, { + id, + }); + + return; + } + /** * Replace current working block * diff --git a/types/api/block.d.ts b/types/api/block.d.ts index c20e46222..97f174f94 100644 --- a/types/api/block.d.ts +++ b/types/api/block.d.ts @@ -59,6 +59,15 @@ export interface BlockAPI { */ save(): Promise; + /** + * Update Block data + * + * @param {BlockToolData} data + * + * @return {Promise} + */ + update(data: BlockToolData): Promise; + /** * Validate Block data * diff --git a/types/tools/block-tool.d.ts b/types/tools/block-tool.d.ts index 8c9bb858a..d6e94bfb6 100644 --- a/types/tools/block-tool.d.ts +++ b/types/tools/block-tool.d.ts @@ -43,6 +43,12 @@ export interface BlockTool extends BaseTool { */ merge?(blockData: BlockToolData): void; + /** + * Method that specified how to update Block's data. + * @param {BlockToolData} blockData + */ + update?(blockData: BlockToolData): boolean; + /** * On paste callback. Fired when pasted content can be substituted by a Tool * @param {PasteEvent} event