improve code input rule
This commit is contained in:
@@ -26,7 +26,7 @@ export default function (regexp: RegExp, markType: MarkType, getAttrs?: Function
|
||||
|
||||
if (match[m]) {
|
||||
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 textEnd = textStart + match[m].length
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { undoInputRule } from 'prosemirror-inputrules'
|
||||
import { keymap } from 'prosemirror-keymap'
|
||||
import { baseKeymap } from 'prosemirror-commands'
|
||||
import { dropCursor } from 'prosemirror-dropcursor'
|
||||
import { gapCursor } from 'prosemirror-gapcursor'
|
||||
import editable from './editable'
|
||||
import focus from './focus'
|
||||
import undoInputRule from '../utils/undoInputRule'
|
||||
|
||||
export default [
|
||||
() => dropCursor(),
|
||||
|
||||
32
packages/core/src/utils/undoInputRule.ts
Normal file
32
packages/core/src/utils/undoInputRule.ts
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user