2021-10-08 21:02:09 +08:00
|
|
|
|
import { EditorState, Plugin, TextSelection } from 'prosemirror-state'
|
2021-10-14 17:56:40 +08:00
|
|
|
|
import { Editor } from './Editor'
|
2021-12-06 19:00:09 +08:00
|
|
|
|
import { CommandManager } from './CommandManager'
|
|
|
|
|
import { createChainableState } from './helpers/createChainableState'
|
|
|
|
|
import { isRegExp } from './utilities/isRegExp'
|
2021-10-14 17:56:40 +08:00
|
|
|
|
import {
|
|
|
|
|
Range,
|
|
|
|
|
ExtendedRegExpMatchArray,
|
|
|
|
|
SingleCommands,
|
|
|
|
|
ChainedCommands,
|
|
|
|
|
CanCommands,
|
|
|
|
|
} from './types'
|
2021-10-08 21:02:09 +08:00
|
|
|
|
|
|
|
|
|
export type InputRuleMatch = {
|
|
|
|
|
index: number,
|
|
|
|
|
text: string,
|
|
|
|
|
replaceWith?: string,
|
|
|
|
|
match?: RegExpMatchArray,
|
|
|
|
|
data?: Record<string, any>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type InputRuleFinder =
|
|
|
|
|
| RegExp
|
|
|
|
|
| ((text: string) => InputRuleMatch | null)
|
|
|
|
|
|
|
|
|
|
export class InputRule {
|
|
|
|
|
find: InputRuleFinder
|
|
|
|
|
|
|
|
|
|
handler: (props: {
|
|
|
|
|
state: EditorState,
|
|
|
|
|
range: Range,
|
|
|
|
|
match: ExtendedRegExpMatchArray,
|
2021-10-14 17:56:40 +08:00
|
|
|
|
commands: SingleCommands,
|
|
|
|
|
chain: () => ChainedCommands,
|
|
|
|
|
can: () => CanCommands,
|
2021-10-08 21:02:09 +08:00
|
|
|
|
}) => void
|
|
|
|
|
|
|
|
|
|
constructor(config: {
|
|
|
|
|
find: InputRuleFinder,
|
|
|
|
|
handler: (props: {
|
|
|
|
|
state: EditorState,
|
|
|
|
|
range: Range,
|
|
|
|
|
match: ExtendedRegExpMatchArray,
|
2021-10-14 17:56:40 +08:00
|
|
|
|
commands: SingleCommands,
|
|
|
|
|
chain: () => ChainedCommands,
|
|
|
|
|
can: () => CanCommands,
|
2021-10-08 21:02:09 +08:00
|
|
|
|
}) => void,
|
|
|
|
|
}) {
|
|
|
|
|
this.find = config.find
|
|
|
|
|
this.handler = config.handler
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const inputRuleMatcherHandler = (text: string, find: InputRuleFinder): ExtendedRegExpMatchArray | null => {
|
|
|
|
|
if (isRegExp(find)) {
|
|
|
|
|
return find.exec(text)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const inputRuleMatch = find(text)
|
|
|
|
|
|
|
|
|
|
if (!inputRuleMatch) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result: ExtendedRegExpMatchArray = []
|
|
|
|
|
|
|
|
|
|
result.push(inputRuleMatch.text)
|
|
|
|
|
result.index = inputRuleMatch.index
|
|
|
|
|
result.input = text
|
|
|
|
|
result.data = inputRuleMatch.data
|
|
|
|
|
|
|
|
|
|
if (inputRuleMatch.replaceWith) {
|
|
|
|
|
if (!inputRuleMatch.text.includes(inputRuleMatch.replaceWith)) {
|
|
|
|
|
console.warn('[tiptap warn]: "inputRuleMatch.replaceWith" must be part of "inputRuleMatch.text".')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.push(inputRuleMatch.replaceWith)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function run(config: {
|
2021-10-14 17:56:40 +08:00
|
|
|
|
editor: Editor,
|
2021-10-08 21:02:09 +08:00
|
|
|
|
from: number,
|
|
|
|
|
to: number,
|
|
|
|
|
text: string,
|
|
|
|
|
rules: InputRule[],
|
|
|
|
|
plugin: Plugin,
|
|
|
|
|
}): any {
|
|
|
|
|
const {
|
2021-10-14 17:56:40 +08:00
|
|
|
|
editor,
|
2021-10-08 21:02:09 +08:00
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
text,
|
|
|
|
|
rules,
|
|
|
|
|
plugin,
|
|
|
|
|
} = config
|
2021-10-14 17:56:40 +08:00
|
|
|
|
const { view } = editor
|
2021-10-08 21:02:09 +08:00
|
|
|
|
|
|
|
|
|
if (view.composing) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const $from = view.state.doc.resolve(from)
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
// check for code node
|
|
|
|
|
$from.parent.type.spec.code
|
|
|
|
|
// check for code mark
|
|
|
|
|
|| !!($from.nodeBefore || $from.nodeAfter)?.marks.find(mark => mark.type.spec.code)
|
|
|
|
|
) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let matched = false
|
|
|
|
|
const maxMatch = 500
|
|
|
|
|
const textBefore = $from.parent.textBetween(
|
|
|
|
|
Math.max(0, $from.parentOffset - maxMatch),
|
|
|
|
|
$from.parentOffset,
|
|
|
|
|
undefined,
|
|
|
|
|
'\ufffc',
|
|
|
|
|
) + text
|
|
|
|
|
|
|
|
|
|
rules.forEach(rule => {
|
|
|
|
|
if (matched) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const match = inputRuleMatcherHandler(textBefore, rule.find)
|
|
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tr = view.state.tr
|
|
|
|
|
const state = createChainableState({
|
|
|
|
|
state: view.state,
|
|
|
|
|
transaction: tr,
|
|
|
|
|
})
|
|
|
|
|
const range = {
|
|
|
|
|
from: from - (match[0].length - text.length),
|
|
|
|
|
to,
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 17:56:40 +08:00
|
|
|
|
const { commands, chain, can } = new CommandManager({
|
|
|
|
|
editor,
|
|
|
|
|
state,
|
|
|
|
|
})
|
|
|
|
|
|
2021-10-08 21:02:09 +08:00
|
|
|
|
rule.handler({
|
|
|
|
|
state,
|
|
|
|
|
range,
|
|
|
|
|
match,
|
2021-10-14 17:56:40 +08:00
|
|
|
|
commands,
|
|
|
|
|
chain,
|
|
|
|
|
can,
|
2021-10-08 21:02:09 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// stop if there are no changes
|
|
|
|
|
if (!tr.steps.length) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// store transform as meta data
|
|
|
|
|
// so we can undo input rules within the `undoInputRules` command
|
|
|
|
|
tr.setMeta(plugin, {
|
|
|
|
|
transform: tr,
|
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
text,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
view.dispatch(tr)
|
|
|
|
|
matched = true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return matched
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an input rules plugin. When enabled, it will cause text
|
|
|
|
|
* input that matches any of the given rules to trigger the rule’s
|
|
|
|
|
* action.
|
|
|
|
|
*/
|
2021-10-14 17:56:40 +08:00
|
|
|
|
export function inputRulesPlugin(props: { editor: Editor, rules: InputRule[] }): Plugin {
|
|
|
|
|
const { editor, rules } = props
|
2021-10-08 21:02:09 +08:00
|
|
|
|
const plugin = new Plugin({
|
|
|
|
|
state: {
|
|
|
|
|
init() {
|
|
|
|
|
return null
|
|
|
|
|
},
|
|
|
|
|
apply(tr, prev) {
|
|
|
|
|
const stored = tr.getMeta(this)
|
|
|
|
|
|
|
|
|
|
if (stored) {
|
|
|
|
|
return stored
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tr.selectionSet || tr.docChanged
|
|
|
|
|
? null
|
|
|
|
|
: prev
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
props: {
|
|
|
|
|
handleTextInput(view, from, to, text) {
|
|
|
|
|
return run({
|
2021-10-14 17:56:40 +08:00
|
|
|
|
editor,
|
2021-10-08 21:02:09 +08:00
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
text,
|
|
|
|
|
rules,
|
|
|
|
|
plugin,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleDOMEvents: {
|
|
|
|
|
compositionend: view => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
const { $cursor } = view.state.selection as TextSelection
|
|
|
|
|
|
|
|
|
|
if ($cursor) {
|
|
|
|
|
run({
|
2021-10-14 17:56:40 +08:00
|
|
|
|
editor,
|
2021-10-08 21:02:09 +08:00
|
|
|
|
from: $cursor.pos,
|
|
|
|
|
to: $cursor.pos,
|
|
|
|
|
text: '',
|
|
|
|
|
rules,
|
|
|
|
|
plugin,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// add support for input rules to trigger on enter
|
|
|
|
|
// this is useful for example for code blocks
|
|
|
|
|
handleKeyDown(view, event) {
|
|
|
|
|
if (event.key !== 'Enter') {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { $cursor } = view.state.selection as TextSelection
|
|
|
|
|
|
|
|
|
|
if ($cursor) {
|
|
|
|
|
return run({
|
2021-10-14 17:56:40 +08:00
|
|
|
|
editor,
|
2021-10-08 21:02:09 +08:00
|
|
|
|
from: $cursor.pos,
|
|
|
|
|
to: $cursor.pos,
|
|
|
|
|
text: '\n',
|
|
|
|
|
rules,
|
|
|
|
|
plugin,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
isInputRules: true,
|
|
|
|
|
}) as Plugin
|
|
|
|
|
|
|
|
|
|
return plugin
|
|
|
|
|
}
|