question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ctrl+Z to undo on empty editor crashes the editor

See original GitHub issue

Creating an empty editor with html.deserialize('<p></p>'), and then pressing Ctrl+Z crashes the editor.

Instead initialize with a raw value:

Value.fromJSON({
  document: {
    nodes: [
      {
        kind: 'block',
        type: 'paragraph',
        nodes: [
          {
            kind: 'text',
            leaves: [{ text: '' }]
          }
        ]
      }
    ]
  }
})

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ianstormtaylorcommented, Dec 3, 2017

Hey @conorcussell thanks for the super clear writeup. I’d love a PR that fixes the getSelectionIndexes case.

For the empty nodes case, Slate itself shouldn’t add a node, but I’d definitely be open to a PR that threw a more helpful error when that impossible case is run into. (Ideally in a single, or few places, wherever makes the most sense in that code path.)

Thank you!

2reactions
conorcussellcommented, Dec 3, 2017

I may just be repeating things people already know, but here are my findings from digging into this today.

When you create an empty editor the selection is unset, and an invalid selection is created because text is undefined.

https://github.com/ianstormtaylor/slate/blob/199c32a2b5f776c586e7202cfa3183e551153fe4/packages/slate/src/models/value.js#L117-L120

This also happens with copy and paste because the onPaste plugin invokes Plain.deserialize which in turn invokes Value.fromJSON

In the render method of slate-react’s content component getSelectionIndexes is called with a startKey and endKey of null, which calls getFurthestAncestor with null and gives us the error.

https://github.com/ianstormtaylor/slate/blob/199c32a2b5f776c586e7202cfa3183e551153fe4/packages/slate/src/models/node.js#L1410-L1414

Simply returning null in getSelectionIndexes when startKey and endKey are null will fix this case, and copy + pasting but it will not fix the case mentioned in https://github.com/ianstormtaylor/slate/issues/1161. If the editor won’t work without any nodes in it, should we ensure there is at least one node by default?

// if we've been passed an invalid range, we can exit early
if (!startKey && !endKey) {
  return null;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Editor crashes when executing Undo action after Audio ...
Editor crashes when executing Undo action after Audio Source component is added to Prefab ... Execute undo action (CTRL + Z)
Read more >
Bug - Undo crashes Unity 2021.3.9+
Here's a repro case. Drag a prefab into the prefab field in the inspector, click "Add Prefab" and hit ctrl+z => Unity will...
Read more >
Crash on undo (ctrl+z) in Filter Editor (#1688) · Issues · Inkscape ...
Steps to reproduce: open Inkscape; draw a shape; apply a filter on it, say 'Blur...' open 'filter editor'; Use slider to change standard...
Read more >
[4.9.0] - Undo In Certain Case Causes Crash - C++
Without thinking, I accidently hit Ctrl+Z to undo and it caused an editor crash.
Read more >
Undo crashes the Piskel editor - Bugs reports - GDevelop Forum
the error occurs not only for me alone. I am teaching one guy and he also wrote that the entire editor disappears when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found