Last active
February 8, 2024 13:15
-
-
Save anarchang/43e695be820198592641d8eb24df18b6 to your computer and use it in GitHub Desktop.
Combine commands on the ProseMirror undo stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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