Insert, replace, etc


So, sometimes I really feel like an idiot while using PM. I get away so much richer after each post on here, but I always feel like I’m wasting people’s time, because most of the time my questions are so trivial. Anyway, here I am whining about wasting people’s time while I waste their time explaining how I feel I’m wasting their time… let’s move to the question.

Still working on implementing a custom solution for spellchecking. I’ve already posted about here, if you feel like checking it out. I also posted my solution at the end. So, now I’ve got the positions that match up with the PM document. Cool. But for the life of me, I can’t get PM to replace the old (misspelled) word with the new one. I’ve read all the posts on here about similar issues, I’ve read the source code to see what replace, replaceWith, and replaceRange all do, and still no luck. Here’s how I tackle the issue:

        const node = schema.text(w);
        const frag = Fragment.from(node);
        const slice = new Slice(frag, 0, 0);
        //suggestions is an array of objects describing each misspelled word and other useful properties
        //editor is the active PM editor instance
        const t =, + 1, slice);
        const st = editor.state.apply(t);

I’ve tried other variants of this code, and all give me the same result. The misspelled word gets deleted in the new document resulting from the transaction, but the new word is not inserted. I’ve even tried of splitting into two steps, delete and insert. When I do that, I get two steps on the transaction object, but still no insertion.

Can anybody point me in the right direction here?



You want editor.dispatch(editor.state.insertText(w, suggestions.position.from,, I think. Though I don’t know why your code above is failing – the only strange thing I spot is the + 1 in the to position.


That + 1 was there in the to position to account for the trailing space, though there isn’t any difference if I remove it. I thought that maybe this had to do with the fact that the lengths aren’t the same? For instance, if I replace the word “smidge” with “smidgen”, there is an extra character. I must confess I didn’t fully understand the openLeft and openRight properties of the new Slice constructor. Does that have anything to do with this?


After having gone through the doc to compare with this piece of text, it turns out @marijn, of course, was right. There’s just a small change to effect: if you’re faced with this, you want this slightly modified code: editor.dispatch(, suggestions.position.from, emphasis on the tr, because the insertText method is on the Transaction class and not on EditorState.

Thanks, @marijn for getting me unstuck. Again.


Nope, I was being sloppy. But looks like you found the need for adding .tr.