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,32 +1,49 @@
|
||||
import { InputRule } from 'prosemirror-inputrules'
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { InputRule, InputRuleFinder } from '../InputRule'
|
||||
import { ExtendedRegExpMatchArray } from '../types'
|
||||
import callOrReturn from '../utilities/callOrReturn'
|
||||
|
||||
export default function (regexp: RegExp, type: NodeType, getAttributes?: (match: any) => any): InputRule {
|
||||
return new InputRule(regexp, (state, match, start, end) => {
|
||||
const attributes = getAttributes instanceof Function
|
||||
? getAttributes(match)
|
||||
: getAttributes
|
||||
const { tr } = state
|
||||
/**
|
||||
* Build an input rule that adds a node when the
|
||||
* matched text is typed into it.
|
||||
*/
|
||||
export default function nodeInputRule(config: {
|
||||
find: InputRuleFinder,
|
||||
type: NodeType,
|
||||
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) || {}
|
||||
const { tr } = state
|
||||
const start = range.from
|
||||
let end = range.to
|
||||
|
||||
if (match[1]) {
|
||||
const offset = match[0].lastIndexOf(match[1])
|
||||
let matchStart = start + offset
|
||||
if (matchStart > end) {
|
||||
matchStart = end
|
||||
} else {
|
||||
end = matchStart + match[1].length
|
||||
if (match[1]) {
|
||||
const offset = match[0].lastIndexOf(match[1])
|
||||
let matchStart = start + offset
|
||||
|
||||
if (matchStart > end) {
|
||||
matchStart = end
|
||||
} else {
|
||||
end = matchStart + match[1].length
|
||||
}
|
||||
|
||||
// insert last typed character
|
||||
const lastChar = match[0][match[0].length - 1]
|
||||
tr.insertText(lastChar, start + match[0].length - 1)
|
||||
|
||||
// insert node from input rule
|
||||
tr.replaceWith(matchStart, end, config.type.create(attributes))
|
||||
} else if (match[0]) {
|
||||
tr.replaceWith(start, end, config.type.create(attributes))
|
||||
}
|
||||
|
||||
// insert last typed character
|
||||
const lastChar = match[0][match[0].length - 1]
|
||||
tr.insertText(lastChar, start + match[0].length - 1)
|
||||
|
||||
// insert node from input rule
|
||||
tr.replaceWith(matchStart, end, type.create(attributes))
|
||||
} else if (match[0]) {
|
||||
tr.replaceWith(start, end, type.create(attributes))
|
||||
}
|
||||
|
||||
return tr
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user