diff --git a/src/components/modules/api/blocks.ts b/src/components/modules/api/blocks.ts index f9297d5d4..9ad22176a 100644 --- a/src/components/modules/api/blocks.ts +++ b/src/components/modules/api/blocks.ts @@ -224,8 +224,8 @@ export default class BlocksAPI extends Module { * @param {string} data - HTML string to render * @returns {Promise} */ - public renderFromHTML(data: string): Promise { - this.Editor.BlockManager.clear(); + public async renderFromHTML(data: string): Promise { + await this.Editor.BlockManager.clear(); return this.Editor.Paste.processText(data, true); } diff --git a/src/components/modules/blockManager.ts b/src/components/modules/blockManager.ts index 48fec0499..be8e1e247 100644 --- a/src/components/modules/blockManager.ts +++ b/src/components/modules/blockManager.ts @@ -533,8 +533,8 @@ export default class BlockManager extends Module { throw new Error('Can\'t find a Block to remove'); } - block.destroy(); this._blocks.remove(index); + block.destroy(); /** * Force call of didMutated event on Block removal @@ -894,7 +894,10 @@ export default class BlockManager extends Module { public async clear(needToAddDefaultBlock = false): Promise { const queue = new PromiseQueue(); - this.blocks.forEach((block) => { + // Create a copy of the blocks array to avoid issues with array modification during iteration + const blocksToRemove = [...this.blocks]; + + blocksToRemove.forEach((block) => { queue.add(async () => { await this.removeBlock(block, false); });