BlockNote icon indicating copy to clipboard operation
BlockNote copied to clipboard

Preserve block identity when editing from the block's start

Open horacioh opened this issue 1 year ago • 2 comments

Describe the bug At Mintter block Identity is really important. Specially because we let users create references to content inside blocks using its ID. I Notice that when I press Enter or Backspace in front of any block with content. I understand block identity might not be relevant to most use cases, but I find this could be a problem when you rely on content being accessible by a particular block ID.

To Reproduce

  1. write some content in a block and remember the block id
  2. move the cursor to the beginning of the block
  3. press Enter or Backspace (backspace if you are not on the first block of the document or of a nested list)
  4. see the blockID changed to the current block content.

Here's a video showing how the ID of the block changes when I press Enter in front of a block

https://github.com/TypeCellOS/BlockNote/assets/725120/cf5ba519-131a-4533-aed4-6471fa17331f

And here's what happens when pressing Backspace in front of a block

https://github.com/TypeCellOS/BlockNote/assets/725120/670d314a-7053-4ae0-a5db-fdfd33258ebd

How to solve this

I did some work to solve this on our side, and if this is relevant to the library I can share the solution with it.

horacioh avatar Jan 29 '24 12:01 horacioh

Hmm I think this is a slightly more complex issue than it seems at first:

Enter: I think generally, content right of the cursor should get moved into a new block with a new ID, while the original ID stays in the original block. This makes intuitive sense imo for each cursor position except for at the start of the block. When the cursor is at the start of the block, I can see why you might expect the block to just get moved down, but instead it gets split, since the behaviour is the same as the other scenarios. So I'm not 100% sure about changing the behaviour for when the cursor is at the start of the block since the logic would be inconsistent.

Backspace: This one is also tricky since you're merging 2 blocks, meaning you have to choose which ID to use. I would argue that it's the bottom block being merged into the block above, rather than the other way round, so I think it makes sense to use the ID of the previous block.

@YousefED Any thoughts on this?

matthewlipski avatar Jan 31 '24 03:01 matthewlipski

@matthewlipski I think you diagnosed the issue correctly; it sounds like it is because we have generic code for merging and splitting blocks. I think it would make sense to make some exceptions for when one of the blocks is empty.

@horacioh I'm also curious to hear your fix! thanks for reporting, I agree it's an important issue

YousefED avatar Jan 31 '24 09:01 YousefED