I have a case where I am running the following code:
removeViewText({ from, to }: { from: number; to: number }) {
this.cm.dispatch(
this.cm.state.update({
changes: { from, to, insert: '' },
}),
);
}
And it is giving me the error:
Uncaught RangeError: Selection points outside of document
at checkSelection (index.js:1542:19)
at new Transaction (index.js:2243:13)
at Transaction.create (index.js:2251:16)
at resolveTransaction (index.js:2424:26)
at EditorState.update (index.js:2596:16)
at EditorView.dispatch (index.js:6358:26)
at MouseSelection.select (index.js:3676:23)
at new MouseSelection (index.js:3613:18)
at handlers.mousedown (index.js:3807:45)
at handleEvent (index.js:3420:17)
This happens when I am simultaneously:
Clicking at the end of my editor (So there is a MouseEvent being triggered)
AND this logic runs (this logic basically runs when I focus away from my codemirror widget)
But I realized this won’t stop the MouseEvent from triggering …
Is there a way for me to tell CodeMirror to process these events in the correct squence so that first the removeViewText is called and then the MouseEvent (by my mouse click) moves the cursor to the end of the new document ?
If you make them two separate extensions, then it should execute the first extension before the second extension. You can even make them appear in the same overall extension by putting them both in [ ]'s.
This is an example where I want to show a inputbox or dropdown for all the “valid variables” and if the user does not enter a valid variable - I want to remove that variable
This is possibly because you are storing the varRange in the widget, but comparing only by varName, allowing a widget that was created for another range to be reused when the widget’s position changes, and thus end up with an invalid range.
Found the issue, which was indeed a library problem.
The storing of positions in widgets wasn’t causing this crash, but it is definitely unsafe—even if there’s only a single widget. When the document changes while the widget is alive, that position will become invalid. You can use view.posAtDOM(this.dom) to find the position of the widget in the document in a reliable way.