I have the following editor command that I want to run to update all the child text nodes of a node type named, “node_name”:
updateNodes: () => (state: any, dispatch: any) => {
const text = 'test'
const results = []
state.doc.descendants((node: any, pos: any, parent: any) => {
if (parent.type.name === 'node_name')
results.push({ node, pos, parent })
})
results.forEach(({ node, pos }, index) => {
const textNode = schema.text(text)
state.tr.replaceWith(pos, pos + node.nodeSize, textNode)
})
dispatch(state.tr)
}
When I run it, nothing happens. But if I run it with the dispatch() inside the forEach loop, then it will work as expected a single time before giving the error: “RangeError: Applying a mismatched transaction” which makes sense because the transactions aren’t batched up. I know I’ll need to rebase after each selection, but just wanted to get it working in some form first.
Any ideas why nothing seems to happen when batching the transactions?