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

@@ -26,7 +26,7 @@ export default function (regexp: RegExp, markType: MarkType, getAttrs?: Function
if (match[m]) { if (match[m]) {
const matchStart = start + match[0].indexOf(match[m - 1]) const matchStart = start + match[0].indexOf(match[m - 1])
const matchEnd = matchStart + match[m - 1].length - 1 const matchEnd = matchStart + match[m - 1].length
const textStart = matchStart + match[m - 1].lastIndexOf(match[m]) const textStart = matchStart + match[m - 1].lastIndexOf(match[m])
const textEnd = textStart + match[m].length const textEnd = textStart + match[m].length

View File

@@ -1,10 +1,10 @@
import { undoInputRule } from 'prosemirror-inputrules'
import { keymap } from 'prosemirror-keymap' import { keymap } from 'prosemirror-keymap'
import { baseKeymap } from 'prosemirror-commands' import { baseKeymap } from 'prosemirror-commands'
import { dropCursor } from 'prosemirror-dropcursor' import { dropCursor } from 'prosemirror-dropcursor'
import { gapCursor } from 'prosemirror-gapcursor' import { gapCursor } from 'prosemirror-gapcursor'
import editable from './editable' import editable from './editable'
import focus from './focus' import focus from './focus'
import undoInputRule from '../utils/undoInputRule'
export default [ export default [
() => dropCursor(), () => dropCursor(),

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
}