I want set the initial cursor in the document at the end of the first paragraph. Here’s my code so far:
const doc = schema.nodeFromJSON({ type: "doc", content: [{ type: "paragraph" }] })
const state = EditorState.create({/* ... */})
const view = new EditorView<EditorSchema>(node, {/* ... */})
// Focus
node.childNodes[0].focus()
// Error: Selection passed to setSelection must point at the current document
view.dispatch(
view.state.tr.setSelection(
TextSelection.atEnd(view.state.tr.doc.firstChild)
)
)
I’m seeing a weird error having to do with the doc being used for the selection is not the same on for the transaction? It’s a bit unclear to me what I’m supposed to do differently though.
Does your state initialization set the state’s doc to doc? In any case, you’ll probably want to pass the new selection right away when creating the state, and call view.focus() instead of just the DOM focus method.
atEnd expects the entire document, not any node (it creates a selection relative to the document it was given, which as you found doesn’t work if it only uses a given node).