Updating the text of many nodes at once

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)

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?

state.tr is a getter function and it returns a new Transaction object every time you call it. I don’t know about other problems in your code, but I would recommend extracting the tr and then mutating it.

@kepta Oh! I should’ve known. Not sure why I didn’t see that before. Thanks!