* 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
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import { NodeType } from 'prosemirror-model'
|
|
import { InputRule, InputRuleFinder } from '../InputRule'
|
|
import { ExtendedRegExpMatchArray } from '../types'
|
|
import callOrReturn from '../utilities/callOrReturn'
|
|
|
|
/**
|
|
* 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
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
},
|
|
})
|
|
}
|