deleteBackwardAtRange with empty initial value
See original GitHub issueDo you want to request a feature or report a bug?
Bug
What’s the current behavior?
My editor has a character limit of 1000 characters. When pasting four blocks of 251, the text gets trimmed properly using deleteBackward
. However, when pasting 1000+ characters at once, the editor crashes.
- A JSFiddle that minimally reproduces the issue. https://codesandbox.io/s/crazy-feistel-9cmjw
(See browser console for the error, codesandbox does not display the error)
-
A GIF showing the issue in action. Pasting more than 1000 chars
-
Information about your OS, browser, Slate version, etc. OS X Mojave, Chrome 78, Slate 0.47.8, Slate-React 0.22.9
What’s the expected behavior?
I expect the editor to trim the pasted text to 1000 characters without crashing.
Note that pasting 1000+ characters works after typing a couple of characters or setting the initial value to some text or a space. The crash only happens when the initial value is an empty string.
It seems like there’s a check missing in deleteBackwardAtRange
where it attempts to access node.key
while node
is null.
I’m not completely sure if the way we impose the character limit is the correct way to do so. If possible, any pointers to improve the code (while avoiding the “bug”) are welcome. If not, I am certainly willing to work with you in fixing the issue in the Slate code.
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
The default “empty” state for this example is a single paragraph block with an empty text node inside. I think that is a reasonable state to have an editor in and expect this to work.
At the time of the paste in the steps of this bug, the selection anchor and focus are both at path [0,0], offset 0, which is the text node within the paragraph block.
It seems the issue is because this condition is causing the node at the current selection to be removed, which invalidates the selection, and then the pasted text replaces it. Therefore, when the schema normalization happens, it’s deleting backwards from a selection where the path and offset of the anchor and focus are now null.
I’m not quite sure what that condition is preventing.
I don’t know what OPs use case is, but maybe an onPaste listener can be used and if the paste is simply text, handle the insertion oneself?
Does Slate have to insert a fragment when the paste is only text? Or can it be handled as an insertText?
I believe that this may be fixed by https://github.com/ianstormtaylor/slate/pull/3093, which has changed a lot of the logic in Slate and
slate-react
especially. I’m going to close this out, but as always, feel free to open a new issue if it persists for you. Thanks for understanding.