Replicate extendSelection in v6


I’m currently trying to convert a code from CodeMirror v5 to v6.

How could I replicate the extendSelection function from v5 in v6.

More specifically, I have this v5 code:

    { ...from, ch: - 1 },
    {, ch: + 1 }

How could I accomplish that in v6?

There’s nothing that really works like extendSelection, but you can use SelectionRange.extend to extend a single range, and you do view.dispatch({selection}) to set a new selection.

Thank you very much @marijn !

After setting the selection, how could I extract the text of the selection?
In v5 if I do this, it would return me the string of the selection:


In v6, I can do this:


But it will return a object of type ITextSelection, but I’m not sure how to ge the content of the selection as a string.

… I don’t think you can.

You can read view.state.selection.ranges or view.state.selection.main to get the selected ranges (or the main range), and do view.state.sliceDoc(range.from, to get the part of the document covered by a given range.

1 Like