feat: Integrate input rules and paste rules into the core (#1997)
* refactoring * improve link regex * WIP: add new markPasteRule und linkify to image mark * move copy of inputrule to core * trigger codeblock inputrule on enter * refactoring * add regex match to markpasterulematch * refactoring * improve link regex * WIP: add new markPasteRule und linkify to image mark * move copy of inputrule to core * trigger codeblock inputrule on enter * refactoring * add regex match to markpasterulematch * update linkify * wip * wip * log * wip * remove debug code * wip * wip * wip * wip * wip * wip * wip * wip * rename matcher * add data to ExtendedRegExpMatchArray * remove logging * add code option to marks, prevent inputrules in code mark * remove link regex * fix codeblock inputrule on enter * refactoring * refactoring * refactoring * refactoring * fix position bug * add test * export InputRule and PasteRule * clean up link demo * fix types
This commit is contained in:
@@ -1,50 +1,70 @@
|
||||
import { InputRule } from 'prosemirror-inputrules'
|
||||
import { InputRule, InputRuleFinder } from '../InputRule'
|
||||
import { MarkType } from 'prosemirror-model'
|
||||
import getMarksBetween from '../helpers/getMarksBetween'
|
||||
import callOrReturn from '../utilities/callOrReturn'
|
||||
import { ExtendedRegExpMatchArray } from '../types'
|
||||
|
||||
export default function (regexp: RegExp, markType: MarkType, getAttributes?: Function): InputRule {
|
||||
return new InputRule(regexp, (state, match, start, end) => {
|
||||
const attributes = getAttributes instanceof Function
|
||||
? getAttributes(match)
|
||||
: getAttributes
|
||||
const { tr } = state
|
||||
const captureGroup = match[match.length - 1]
|
||||
const fullMatch = match[0]
|
||||
let markEnd = end
|
||||
/**
|
||||
* Build an input rule that adds a mark when the
|
||||
* matched text is typed into it.
|
||||
*/
|
||||
export default function markInputRule(config: {
|
||||
find: InputRuleFinder,
|
||||
type: MarkType,
|
||||
getAttributes?:
|
||||
| Record<string, any>
|
||||
| ((match: ExtendedRegExpMatchArray) => Record<string, any>)
|
||||
| false
|
||||
| null
|
||||
,
|
||||
}) {
|
||||
return new InputRule({
|
||||
find: config.find,
|
||||
handler: ({ state, range, match }) => {
|
||||
const attributes = callOrReturn(config.getAttributes, undefined, match)
|
||||
|
||||
if (captureGroup) {
|
||||
const startSpaces = fullMatch.search(/\S/)
|
||||
const textStart = start + fullMatch.indexOf(captureGroup)
|
||||
const textEnd = textStart + captureGroup.length
|
||||
|
||||
const excludedMarks = getMarksBetween(start, end, state)
|
||||
.filter(item => {
|
||||
// TODO: PR to add excluded to MarkType
|
||||
// @ts-ignore
|
||||
const { excluded } = item.mark.type
|
||||
return excluded.find((type: MarkType) => type.name === markType.name)
|
||||
})
|
||||
.filter(item => item.to > textStart)
|
||||
|
||||
if (excludedMarks.length) {
|
||||
return null
|
||||
if (attributes === false || attributes === null) {
|
||||
return
|
||||
}
|
||||
|
||||
if (textEnd < end) {
|
||||
tr.delete(textEnd, end)
|
||||
const { tr } = state
|
||||
const captureGroup = match[match.length - 1]
|
||||
const fullMatch = match[0]
|
||||
let markEnd = range.to
|
||||
|
||||
if (captureGroup) {
|
||||
const startSpaces = fullMatch.search(/\S/)
|
||||
const textStart = range.from + fullMatch.indexOf(captureGroup)
|
||||
const textEnd = textStart + captureGroup.length
|
||||
|
||||
const excludedMarks = getMarksBetween(range.from, range.to, state)
|
||||
.filter(item => {
|
||||
// TODO: PR to add excluded to MarkType
|
||||
// @ts-ignore
|
||||
const { excluded } = item.mark.type
|
||||
|
||||
return excluded.find((type: MarkType) => type.name === config.type.name)
|
||||
})
|
||||
.filter(item => item.to > textStart)
|
||||
|
||||
if (excludedMarks.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (textEnd < range.to) {
|
||||
tr.delete(textEnd, range.to)
|
||||
}
|
||||
|
||||
if (textStart > range.from) {
|
||||
tr.delete(range.from + startSpaces, textStart)
|
||||
}
|
||||
|
||||
markEnd = range.from + startSpaces + captureGroup.length
|
||||
|
||||
tr.addMark(range.from + startSpaces, markEnd, config.type.create(attributes || {}))
|
||||
|
||||
tr.removeStoredMark(config.type)
|
||||
}
|
||||
|
||||
if (textStart > start) {
|
||||
tr.delete(start + startSpaces, textStart)
|
||||
}
|
||||
|
||||
markEnd = start + startSpaces + captureGroup.length
|
||||
|
||||
tr.addMark(start + startSpaces, markEnd, markType.create(attributes))
|
||||
|
||||
tr.removeStoredMark(markType)
|
||||
}
|
||||
|
||||
return tr
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user