editor.js icon indicating copy to clipboard operation
editor.js copied to clipboard

Entering text into empty first block on new document triggers 'block-changed' event rather than 'block-created'

Open alexanderjulmer opened this issue 3 years ago • 4 comments
trafficstars

If a new editor is instantiated and no initial data is provided, editorjs creates an empty block in which the cursor is set. If the user starts typing in this block, editorjs triggers the event block-changed.

Steps to reproduce:

  1. Initialise an empty editorjs instance
  2. Provide an onChange function and listen to it
  3. Start typing in the first empty block
  4. Check the console

Here's a demo implementation: https://6246d3405682fb7683a8b9be--editorjs-bug.netlify.app

Expected behavior: Editors should trigger the event block-created because before the first input no block exists that could be updated.

Device, Browser, OS: Safari and Chrome on Mac OS

Editor.js version: 2.23.2

alexanderjulmer avatar Apr 01 '22 10:04 alexanderjulmer

The editor always has at least one Block. So the empty editor has the only block. And when you started typing, it is actually changed, not created.

neSpecc avatar Apr 19 '22 14:04 neSpecc

Maybe we should consider triggering the block-created on the empty editor initialization. What do you think?

neSpecc avatar Apr 19 '22 14:04 neSpecc

I have the same issue with the latest version.

The editor always has at least one Block. So the empty editor has the only block. And when you started typing, it is actually changed, not created.

If you try to save the empty editor, you'll get an empty list of block. The only way I found to get the first block was to use Editor.blocks.getBlockByIndex(0), which returns the first block.

Maybe we should consider triggering the block-created on the empty editor initialization. What do you think?

That's the behavior I was expecting from editor.js but I don't know well enough the lib to say that triggering the block-created event is the way to go. Adding a note in the documentation about this behavior could be enough. The workaround is quite simple, using getBlockByIndex(0) in onReady gives the first block.

mwath avatar Aug 12 '22 12:08 mwath

Actually, clearing the editor does trigger the event block-created. It can be used to work around this issue.

mwath avatar Aug 16 '22 14:08 mwath