I want to create a content table, which updates itself if the headings have been changed in the document:
If an item in the content table is clicked, I want to scroll to the relevant heading.
My current code:
function scrollHeadingIntoViewById(id) {
for (let i = 0; i < window.editorView.state.doc.content.content.length; ++i) {
let node = window.editorView.state.doc.content.content[i];
if (node.type.name === 'heading' && node.attrs.id === id) {
let ns = NodeSelection.create(node,0);
let tr = window.editorView.state.tr.setSelection(ns).scrollIntoView();
window.editorView.dispatch(tr);
break;
}
}
}
basically, I enumerate all top-level blocks, and if I find a heading block, plus its id equals to the TOC item’s id, I will create a transaction such that the heading block will be scrolled into view.
But this doesn’t work, I’m getting:
Uncaught RangeError: Selection passed to setSelection must point at the current document
Well I understand the concept that prosemirror doc is immutable. A change will result in a new doc, which might invalidate a selection made on an older doc.
But in my code, I’m using the current doc to create a selection and I don’t mutate the doc. How come my selection is invalid?
In my code, window.editorView is a global variable that holds the prosemirror editor.