Prosemirror: New Error: "Selection passed to setSelection must point at the current document"

Created on 18 Aug 2019  路  11Comments  路  Source: ProseMirror/prosemirror

Issue details

starting with commit https://github.com/ProseMirror/prosemirror-state/commit/58772b0796d3ff5e7c77a5b5da2761cf15da67a5

transaction.setSelection(new TextSelection(state.selection.$from, transaction.selection.$to))

fails with RangeError: "Selection passed to setSelection must point at the current document"

how is this supposed to be handled now?

ProseMirror version

prosemirror-state 1.2.4

Affected platforms

most probably all platforms, but only tested on firefox

Most helpful comment

If the position is the same, you can use the create methods

tr.setSelection(TextSelection.create(tr.doc, state.selection.anchor, state.selection.head))

If the document did actually change, you can do selection mapping

tr.setSelection(state.selection.map(tr.doc, tr.mapping))

Second option is more future proof and you don't have to rely on knowing the selection type.

All 11 comments

The resolved position in state.selection.$from is resolved to the state document, not the transaction document. Now in setSelection, the resolved positions in the selection must point to the same document (transaction.doc). If you want to use state.selection.$from, you just have to resolve it to the transaction document.

thank you for clarifying. will try to fix it this way. already successfully tried to use TextSelection.create but will try to revert to previous version using state document.

closing as fixed. thx.

Yeah, I was afraid someone would harshly run into a hitherto unnoticed issue like this. Sorry for the inconvenience, but on the bright side, your old code was going to cause problems at some point too, and now you are aware of the issue.

Would someone be able to provide the recommended way to restore the selection after performing some transformations, instead of tr.setSelection(state.selection)?

If the position is the same, you can use the create methods

tr.setSelection(TextSelection.create(tr.doc, state.selection.anchor, state.selection.head))

If the document did actually change, you can do selection mapping

tr.setSelection(state.selection.map(tr.doc, tr.mapping))

Second option is more future proof and you don't have to rely on knowing the selection type.

(Yes, mapping is better since if you delete something the old anchor/head positions might not be valid anymore.)

sorry bothering ....

i am still have no idea about how refactor my code
from

const newSelection = TextSelection.create(tr.doc, from - 1)
dispatch(tr.setSelection(newSelection))

to something that match the prosemirror-state 1.2.4 rules .....

Those lines don't look like they would trigger this error (tr.doc will match the transaction's document at the point where you call setSelection).

oh, i see...
however, it get me more confused ....

const fromNodeParent = isGapCursor ? $from.node() : $from.node(-1)
 const newSelection = TextSelection.create(tr.doc, from - 1)
        // if (fromNode.childCount === 0) {
          // tr.delete(from - 1, to + 1)
        // }
 dispatch(tr.setSelection(newSelection))

if i remove the comment... i get the error "setSelection must point at the current document".

It's not that hard, really. Don't change the document between the point where you create the selection and call setSelection, or if you really must, map the selection through the changes you introduced.

ok, thanks a lot

Was this page helpful?
0 / 5 - 0 ratings