I have hierarchical data which I’m letting users select from. The data selections need to be created in the editor as lists/nested lists and prepopulated with the data. I’m having a hard time conceptualizing the indexing in prosemirror and manipulating it to insert the proper node and text.
When data is selected, I have a handler that is called that should clear the document, create the structure for the selections, and then loop over the selections to 1)create the element and 2) insert the data.
//Clear Document
let docNode = state.doc
let start = 0
let end = start + docNode.content.size
tr.replaceWith(start,end, Fragment.empty)
//Create structure
let strut = this.createNodesStructure(paragraphs)
//Loop with tr
Object.keys(strut).forEach((key)=>{
let paragraph = strut[key]
tr.insert(tr.selection.anchor, paragraph.node)
//Insert text to the end of the doc, probably wrong, but works on top level lists
tr.insertText(paragraph.paragraph.paragraphText, tr.doc.resolve(0).end() - 2)
if(paragraph.children.length > 0){
//Set Selection to end of previously inserted node?
//Insert<ul>
//Insert child <li> node
}
})
Trying to move the selection as in How to Select a Node Immediately After Inserting It, I’ve been getting a number of position errors and resolve() on the inserted nodes typically resolves to 0, which I don’t understand. I also can’t use replaceSelectionWith as the selection seems to span the entire document.
Any help would be appreciated. Thanks.