I would like to repeat a command at two different positions in a document, but I’m unsure how to get the updated state after the first command.
Say I have a document that has two sections with the same structure. For example:
Question
(a)
(i)
(b)
(i)
Worked Solution
(a)
(i)
(b)
(i)
When a user hits backspace
and deletes any part/subpart from the top section, I would like to repeat the same command in the bottom section for the same part/subpart. I can already find the correct position in the bottom section, but I can’t seem to run a command multiple times because the command requires the current state and the state has changed after the first command.
Right now, when a user hits backspace
prosemirror calls
chainCommands(deleteSelection, joinBackward, selectNodeBackward)
What I’m trying to do is add a command before that which determines if a part/subpart is about to be deleted then calls the same sequence of commands in the bottom section:
chainCommands(customDelete, deleteSelection, joinBackward, selectNodeBackward)
function customDelete(state: EditorState, dispatch: (tr: Transaction) => void, view: EditorView): boolean {
let willDelete = //some stuff to determine if a part is about to be deleted
if(!willDelete) return false;
const { selection, doc } = state;
let pos = //some stuff to find the position in the bottom section
if(selection.empty) {
joinBackward(state, dispatch, view);
//I know this is wrong. How do I do it correctly?
let newSelection = TextSelection.create(doc, pos);
state.selection = newSelection;
joinBackwards(state, dispatch, view);
return true;
}
deleteSelection(state, dispatch, view);
//I know this is wrong. How do I do it correctly?
let newSelection = TextSelection.create(doc, pos);
state.selection = newSelection;
joinBackwards(state, dispatch, view);
return true;
}
I know the problem is after the first joinBackwards
or deleteSelection
the state has changed and I need the new, updated state. Is there a way I can get that updated state? Is there a better way to do this?
As an alternative, is there a way to add metadata to a transaction before calling joinBackwards
or deleteSelection
so I can find that transaction in a plugin’s appendTransaction
function?
Thanks a lot!