improve code input rule

This commit is contained in:
Philipp Kühn
2020-09-10 13:50:40 +02:00
parent 49593ac4fc
commit 17a6d56a31
3 changed files with 34 additions and 2 deletions

View File

@@ -0,0 +1,32 @@
import { EditorState, Transaction } from 'prosemirror-state'
// source: https://github.com/ProseMirror/prosemirror-inputrules/blob/2d3ae3abe3428a1b4d343808915aafeff8563371/src/inputrules.js#L101
export default function undoInputRule(state: EditorState, dispatch?: (tr: Transaction<any>) => void) {
let plugins = state.plugins
for (let i = 0; i < plugins.length; i++) {
let plugin = plugins[i], undoable
// @ts-ignore
if (plugin.spec.isInputRules && (undoable = plugin.getState(state))) {
if (dispatch) {
let tr = state.tr, toUndo = undoable.transform
for (let j = toUndo.steps.length - 1; j >= 0; j--) {
tr.step(toUndo.steps[j].invert(toUndo.docs[j]))
}
let marks = tr.doc.resolve(undoable.from).marks()
if (!undoable.text) {
return false
}
dispatch(tr.replaceWith(undoable.from, undoable.to, state.schema.text(undoable.text, marks)))
}
return true
}
}
return false
}