how to get selected content in v6

when we use codemirror in v5,we can use doc.getSelection to get the currently selected code,
but how to get currently selected code in codemirror v6?

Something like v.state.sliceDoc(v.state.selection.main.from, v.state.selection.main.to).

thanks,it does work.

for example :

1 Like

Hello! How to get all the content of the selected line and replace it no matter where the cursor is in the line?

// Exchange `from` with `to`, `anchor`, or `head`, depending on your needs
const line = view.state.doc.lineAt(view.state.selection.main.from)
// Get line contents
const lineContents = view.state.sliceDoc(line.from, line.to)
// Exchange line contents
view.dispatch({ changes: { from: line.from, to: line.to, insert: 'New text for the line' } })