add markInputRule function

This commit is contained in:
Philipp Kühn 2018-08-24 23:53:49 +02:00
parent 6f25288e47
commit b61f95a2a7
5 changed files with 43 additions and 3 deletions

View File

@ -0,0 +1,20 @@
import { InputRule } from 'prosemirror-inputrules'
export default function (regexp, markType, getAttrs) {
return new InputRule(regexp, (state, match, start, end) => {
let attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
let tr = state.tr
if (match[1]) {
let textStart = start + match[0].indexOf(match[1])
let textEnd = textStart + match[1].length
if (textEnd < end) tr.delete(textEnd, end)
if (textStart > start) tr.delete(start, textStart)
end = start + match[1].length
}
tr.addMark(start, end, markType.create(attrs))
tr.removeStoredMark(markType) // Do not continue with mark.
return tr
})
}

View File

@ -38,6 +38,7 @@ import {
textblockTypeInputRule,
} from 'prosemirror-inputrules'
import markInputRule from './commands/markInputRule'
import removeMark from './commands/removeMark'
import toggleBlockType from './commands/toggleBlockType'
import toggleList from './commands/toggleList'
@ -82,6 +83,7 @@ export {
textblockTypeInputRule,
// custom
markInputRule,
removeMark,
toggleBlockType,
toggleList,

View File

@ -1,5 +1,5 @@
import { Mark } from 'tiptap'
import { toggleMark } from 'tiptap-commands'
import { toggleMark, markInputRule } from 'tiptap-commands'
export default class BoldMark extends Mark {
@ -36,4 +36,10 @@ export default class BoldMark extends Mark {
return toggleMark(type)
}
inputRules({ type }) {
return [
markInputRule(/(?:\*\*|__)([^\*_]+)(?:\*\*|__)$/, type),
]
}
}

View File

@ -1,5 +1,5 @@
import { Mark } from 'tiptap'
import { toggleMark } from 'tiptap-commands'
import { toggleMark, markInputRule } from 'tiptap-commands'
export default class CodeMark extends Mark {
@ -26,4 +26,10 @@ export default class CodeMark extends Mark {
return toggleMark(type)
}
inputRules({ type }) {
return [
markInputRule(/(?:`)([^`]+)(?:`)$/, type),
]
}
}

View File

@ -1,5 +1,5 @@
import { Mark } from 'tiptap'
import { toggleMark } from 'tiptap-commands'
import { toggleMark, markInputRule } from 'tiptap-commands'
export default class ItalicMark extends Mark {
@ -28,4 +28,10 @@ export default class ItalicMark extends Mark {
return toggleMark(type)
}
inputRules({ type }) {
return [
markInputRule(/(?:^|[^\*_])(?:\*|_)([^\*_]+)(?:\*|_)$/, type),
]
}
}