codex-team / editor.js

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

[Bug] - Error during delete a block by index #1182

Closed nicolapagni closed 4 years ago

nicolapagni commented 4 years ago

I've installed the v2.18 to try the new feature about deleting specific block by index. I can notice that now I see a warning if I try to delete a block by an invalid index..ok, but now I see this error:

Instead, if I try to delete though the same code the firt element It seems work fine..

Cannot read property 'lastInput' of undefined Steps to reproduce:

  1. Create a new editor instance
  2. As first block (for example), insert a paragraph with something write
  3. As second one insert a paragraph too
  4. call after adding the second one the following code: this.api.blocks.getBlockByIndex(1);

Expected behavior: The block should be removed.

Screenshots:

editorjs-error

main.js is the file where I call the delete function.

Device, Browser, OS: Chrome v83.0.4103.61, Windows 10

Editor.js version: 2.18

stephenrichard commented 4 years ago

Hello @nicolapagni , I encountered the same issue working with the new delete feature.

After digging into the code, I found that after deleting a block this line is called : this.Editor.Caret.setToBlock(this.Editor.BlockManager.currentBlock, this.Editor.Caret.positions.END);

It's supposed to move the cursor focus after the current block. The issue is that this.Editor.BlockManager.currentBlock is undefined. In my case I'm not really surprised because I am adding/removing blocks without the need to interact with the editor manually.

2 options I see to fix this issue :

I can submit a PR with those changes any time but I would love some feedbacks from maintainers or other users ✌️

ranemihir commented 4 years ago

@nicolapagni We followed the steps as you have mentioned but we couldn't able to reproduce the issue. Can you provide any further details regarding this issue, maybe a GIF or a sall video to show the exact process of reproducing the issue?

stephenrichard commented 4 years ago

@ranemihir I reproduce the issue with those steps :

  1. Create a new editor instance (with sample data, 2 blocks is enough)
  2. Do not edit, focus or add a block in the editor
  3. Call editorInstance.blocks.delete(1) (I use a button next to the editor to delete a specific blocks) Note that any valid index triggers the error, an invalid or missing id triggers a simple warning
ranemihir commented 4 years ago

@stephenrichard Thank you for your help. I was able to reproduce the issue.

gohabereg commented 4 years ago

Resolved by #1218