sachinraja/rodemirror

Selection points outside of document

sswatson opened this issue · 1 comments

If:

  1. you change the contents of the editor from outside the component changing the value prop, and
  2. you do so in a way that makes the document shorter, and
  3. if the cursor was at the end of the document originally,

...then the useEffect transaction that updates the editor's contents asks for a selection that falls outside the document. This crashes the editor component with an error message "Selection points outside of document".

I'm using the editor in a semi-controlled manner as illustrated in the documentation (separate read/write variables, so as to avoid re-rendering the whole document on every keystroke).

I don't have enough context to know what the best solution would be, but I thought I should mention this in case this is a bug in the package. Or even if I'm just doing something silly, hopefully this message will help the next person who makes a similar mistake.

You'll need to update the selection manually. Here's an old example which resets the selection to the start after each change. It probably does not completely work but the idea is the same: https://codesandbox.io/s/wizardly-elbakyan-rpojo?file=/src/App.js