I’ve done something like this outside of prosemirror before applying the new state
dispatchTransaction (tr) {
if (this.interceptTransaction) tr = this.interceptTransaction(tr)
if (!tr) return // leave unchanged
this.editor.state = this.editor.state.apply(tr)
this.editor.view.updateState(this.editor.state)
}