facebookarchive / draft-js

A React framework for building text editors.
https://draftjs.org/
MIT License
22.57k stars 2.64k forks source link

Deleting Custom Blocks throws errors when cursor position is on empty unstyled block #154

Open corbanbrook opened 8 years ago

corbanbrook commented 8 years ago

I ran into this bug on my own editor while working with custom blocks but it can also be reproduced on the TeX example.

When attempting to delete/backspace a custom block it will fail if your cursor position is on an unstyled empty text block. When adding custom blocks the tex example places unstyled empty text blocks after each media block so that you can continue adding more text content after.

Deleting these custom blocks from an empty block will work if the custom block is the last block but if you place your cursor on the empty unstyled text block between two custom blocks it will fail when backspacing. If the unstyled block contains text it will work correctly.

Steps to Reproduce

  1. Open TeX example
  2. Clear the content editable (not required but easier to see whats going on)
  3. Insert 2 new TeX custom blocks
  4. Press arrow-up OR click between the two TeX blocks to move your cursor
  5. Press delete

Expected result: The first TeX block should be removed.

Actual result: Cursor moves to top-right corner of screen and throws the error:

Uncaught Error: Got unexpected null or undefined

Fails in getPointForNonTextNode()

      if (!getSelectionOffsetKeyForNode(nodeBeforeCursor)) {
        // Our target node may be a leaf or a text node, in which case we're
        // already where we want to be and can just use the child's length as
        // our offset.
        leafKey = nullthrows(offsetKey);
        textLength = getTextContentLength(nodeBeforeCursor);
      }

Pressly delete again will then trigger another error:

Uncaught Invariant Violation: Unknown DraftEntity key.

Pressing delete a third time will remove the block.

Reproduced in Chrome 48.0.2564.116 and Firefox 44.0.2 on OS X Yosemite 10.10.5

hellendag commented 8 years ago

Thanks for the report. There might be some deletion management missing from the TeX example.

hellendag commented 8 years ago

I was thinking that #56 would be a fix for this, but I think it's not complete -- we want find the right place to move the cursor to as well.

Thanks for the details, by the way. Super useful.

TheoRyley commented 8 years ago

This bug still exists in 0.7.0. Is there any workaround for it?

Rokt33r commented 8 years ago

I got related problem but I can't reproduce corbanbrook 's way. Anyway, this is because the editor sometimes set entity offset to wrong location. As a temporary expedient, I'm going to insert a space intoto the missing entity range when the error found.