mirror of
https://github.com/codex-team/editor.js
synced 2026-03-14 14:45:47 +01:00
resolve "Can't find a Block to remove" error in renderFromHTML (#2941)
* fix(blocks):Error occurred when calling renderFromHTML: Can't find a Block to remove. * fix: resolve "Can't find a Block to remove" error in renderFromHTML - Make renderFromHTML async and await BlockManager.clear() to prevent race condition - Change removeBlock order: remove from array before destroy to prevent index invalidation - Fix clear() method to copy blocks array before iteration to avoid modification during loop Fixes issue where renderFromHTML would fail with "Can't find a Block to remove" error due to concurrent block removal operations and array modification during iteration. Resolves #2518
This commit is contained in:
parent
628f2188e7
commit
df7d3a7883
2 changed files with 7 additions and 4 deletions
|
|
@ -224,8 +224,8 @@ export default class BlocksAPI extends Module {
|
|||
* @param {string} data - HTML string to render
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public renderFromHTML(data: string): Promise<void> {
|
||||
this.Editor.BlockManager.clear();
|
||||
public async renderFromHTML(data: string): Promise<void> {
|
||||
await this.Editor.BlockManager.clear();
|
||||
|
||||
return this.Editor.Paste.processText(data, true);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<void> {
|
||||
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);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue