ianstormtaylor / slate

A completely customizable framework for building rich text editors. (Currently in beta.)
http://slatejs.org
MIT License
29.71k stars 3.24k forks source link

Selecting text and deleting it will delete all preceding text under specific conditions #4121

Open matthewdiamant opened 3 years ago

matthewdiamant commented 3 years ago

Description When selecting text, if the selection starts before the first inserted node, and ends at the very beginning of another inserted node, when the selection is deleted, it will delete up to the beginning of the paragraph.

This is happening in my own project, and I can also replicate it on the Slate examples page. In my project, I'm also bolding and italicizing words and phrases, and the same selection conditions are overdeleting the text.

In the below example, you can start the selection anywhere before the word "rich", and the selection must end exactly before the word "much" or "<textarea>". As long as the selection includes the first inserted node (in this case rich) and ends before another formatted word, deleting the selection will remove all preceding text.

This happens only when pressing the Delete key. Highlight text, and then pressing "A" (for example), deletes all of the text and replaces it with the letter "a," which is expected. Pressing Return or pasting text also works as expected.

On other Slate examples (such as the link example, or the Mentions example), I could not replicate this behavior. Both the Rich Text example and my own project use bold and italicized elements, and the bug occurs on both of them.

Recording https://user-images.githubusercontent.com/806338/110873887-85af8300-8287-11eb-803d-03762b983526.mov

Sandbox https://www.slatejs.org/examples/richtext

Steps

  1. Go to https://www.slatejs.org/examples/richtext
  2. Start selecting text before the word rich.
  3. End selecting text either at the beginning of "much" or "<textarea>".
  4. Press the Delete key.

Expectation Pressing Delete should not delete text outside of the selection window.

Environment

williamstein commented 3 years ago

Thanks for reporting this. I'm working around it in CoCalc by marking the range with an arbitrary mark before deleting it: https://github.com/sagemathinc/cocalc/commit/1746d987a2e9aca0e2e798b9073bf9b9b5941e61

williamstein commented 3 years ago

My workaround for this serious bug ended up causing other unpleasant surprises, so today I came up with a different workaround involving extending the selection to offset 0 in the next text node: https://github.com/sagemathinc/cocalc/commit/6ca785abade52b22f854b391300ad8f176cc8099