mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-08-06 13:38:49 +08:00
add markInputRule function
This commit is contained in:
parent
6f25288e47
commit
b61f95a2a7
20
packages/tiptap-commands/src/commands/markInputRule.js
Normal file
20
packages/tiptap-commands/src/commands/markInputRule.js
Normal 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
|
||||
})
|
||||
}
|
@ -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,
|
||||
|
@ -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),
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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),
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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),
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user