Skip to content

Instantly share code, notes, and snippets.

@anarchang
Last active February 8, 2024 13:15
Show Gist options
  • Save anarchang/43e695be820198592641d8eb24df18b6 to your computer and use it in GitHub Desktop.
Save anarchang/43e695be820198592641d8eb24df18b6 to your computer and use it in GitHub Desktop.
Combine commands on the ProseMirror undo stack
/**
* Given two commands (commandA and commandB), returns a new command that when applied
* to a ProseMirror state that uses the prosemirror-history plugin applies commandA and
* then commandB so that both commands are undone with a single undo action.
**/
const combineCommands =
(commandA: Command, commandB: Command) =>
(state: EditorState, dispatch?: (tr: Transaction) => void): boolean => {
return commandA(state, (transactionA: Transaction) => {
const { state: stateA } = state.applyTransaction(transactionA)
commandB(stateA, (transactionB: Transaction) => {
transactionB.setMeta('appendedTransaction', transactionA)
if (dispatch) {
dispatch(transactionA)
dispatch(transactionB)
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment