codex-team / editor.js

A block-style editor with clean JSON output
https://editorjs.io
Apache License 2.0
27.77k stars 2.04k forks source link

Error occurred when calling renderFromHTML: Can't find a Block to remove. #2518

Open luichooy opened 9 months ago

luichooy commented 9 months ago

Describe a bug. I need to call the editor.clear() method to clear the existing content in the editor before calling the editor.blocks.renderFromHTML() method.

However, doing so results in the error "Uncaught (in promise) Error: Can't find a Block to remove. "

The clear method is supposed to remove all Blocks and create a new empty initial type Block. So, after calling the clear method, a new default block should have been created.

I'm wondering why calling renderFromHTML after clear is causing the error "Can't find a Block to remove".

Additionally, even if there is an error, why isn't it caught by using renderFromHTML().catch()? Instead, the error is thrown directly. In a React development environment, this would result in a runtime error.

Steps to reproduce:

  1. call editor.clear()
  2. call editor.blocks.renderFromHTML( '<p>call renderFromHTML after editor.clear()</p>')
  3. The error message states "Can't find a Block to remove" and appeared in the console.

Expected behavior: No error occur or some ways to avoid this, thanks!

Screenshots: image

Device, Browser, OS:

Editor.js version: https://cdn.jsdelivr.net/npm/@editorjs/editorjs@latest Plugins you use with their versions:

Marax commented 9 months ago

I had same issue when I called clear() twice in a row, while not waiting for first clear to finish.

renderFromHTML actually also calls internally clear(), so check that you wait for the first clear() to finish before calling the renderFromHTML either by using await before clear or by calling renderFromHTML inside .then() block.

kalebda commented 5 months ago

Hey @luichooy found a way? I am in a react dev and facing the same problem. wanted to clear and render a new block programmatically