|
| 1 | +import { Plugin, PluginKey } from 'prosemirror-state'; |
| 2 | +import { Fragment } from 'prosemirror-model'; |
| 3 | +import { CommandService } from './CommandService.js'; |
| 4 | +import { chainableEditorState } from './helpers/chainableEditorState.js'; |
| 5 | +import { getHTMLFromFragment } from './helpers/getHTMLFromFragment.js'; |
| 6 | +import { getTextContentFromNodes } from './helpers/getTextContentFromNodes.js'; |
| 7 | +import { isRegExp } from './utilities/isRegExp.js'; |
| 8 | + |
| 9 | +export class InputRule { |
| 10 | + match; |
| 11 | + handler; |
| 12 | + |
| 13 | + constructor(config) { |
| 14 | + this.match = config.match; |
| 15 | + this.handler = config.handler; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +const inputRuleMatcherHandler = (text, match) => { |
| 20 | + if (isRegExp(match)) { |
| 21 | + return match.exec(text); |
| 22 | + } |
| 23 | + |
| 24 | + const inputRuleMatch = match(text); |
| 25 | + |
| 26 | + if (!inputRuleMatch) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + const result = [ inputRuleMatch.text ]; |
| 31 | + |
| 32 | + result.index = inputRuleMatch.index; |
| 33 | + result.input = text; |
| 34 | + result.data = inputRuleMatch.data; |
| 35 | + |
| 36 | + if (inputRuleMatch.replaceWith) { |
| 37 | + if (!inputRuleMatch.text.includes(inputRuleMatch.replaceWith)) { |
| 38 | + console.warn( |
| 39 | + '[super-editor warn]: "inputRuleMatch.replaceWith" must be part of "inputRuleMatch.text".', |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + result.push(inputRuleMatch.replaceWith); |
| 44 | + } |
| 45 | + |
| 46 | + return result; |
| 47 | +} |
| 48 | + |
| 49 | +const run = (config) => { |
| 50 | + const { |
| 51 | + editor, from, to, text, rules, plugin, |
| 52 | + } = config; |
| 53 | + const { view } = editor; |
| 54 | + |
| 55 | + if (view.composing) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + const $from = view.state.doc.resolve(from); |
| 60 | + |
| 61 | + if ( |
| 62 | + $from.parent.type.spec.code |
| 63 | + || !!($from.nodeBefore || $from.nodeAfter)?.marks.find(mark => mark.type.spec.code) |
| 64 | + ) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + let matched = false; |
| 69 | + const textBefore = getTextContentFromNodes($from) + text; |
| 70 | + |
| 71 | + rules.forEach(rule => { |
| 72 | + if (matched) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const match = inputRuleMatcherHandler(textBefore, rule.match) |
| 77 | + |
| 78 | + if (!match) { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + const tr = view.state.tr; |
| 83 | + const state = chainableEditorState(tr, view.state); |
| 84 | + const range = { |
| 85 | + from: from - (match[0].length - text.length), |
| 86 | + to, |
| 87 | + }; |
| 88 | + |
| 89 | + const { commands, chain, can } = new CommandService({ |
| 90 | + editor, |
| 91 | + state, |
| 92 | + }); |
| 93 | + |
| 94 | + const handler = rule.handler({ |
| 95 | + state, |
| 96 | + range, |
| 97 | + match, |
| 98 | + commands, |
| 99 | + chain, |
| 100 | + can, |
| 101 | + }); |
| 102 | + |
| 103 | + // stop if there are no changes |
| 104 | + if (handler === null || !tr.steps.length) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + // store transform as metadata |
| 109 | + // so we can undo input rules within the `undoInputRules` command |
| 110 | + tr.setMeta(plugin, { |
| 111 | + transform: tr, |
| 112 | + from, |
| 113 | + to, |
| 114 | + text, |
| 115 | + }); |
| 116 | + |
| 117 | + view.dispatch(tr); |
| 118 | + matched = true; |
| 119 | + }) |
| 120 | + |
| 121 | + return matched; |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Create an input rules plugin. When enabled, it will cause text |
| 126 | + * input that matches any of the given rules to trigger the rule’s |
| 127 | + * action. |
| 128 | + */ |
| 129 | +export const inputRulesPlugin = ({ editor, rules }) => { |
| 130 | + const plugin = new Plugin({ |
| 131 | + key: new PluginKey('inputRulesPlugin'), |
| 132 | + |
| 133 | + state: { |
| 134 | + init() { |
| 135 | + return null; |
| 136 | + }, |
| 137 | + |
| 138 | + apply(tr, prev, state) { |
| 139 | + const stored = tr.getMeta(plugin); |
| 140 | + |
| 141 | + if (stored) { |
| 142 | + return stored; |
| 143 | + } |
| 144 | + |
| 145 | + // if InputRule is triggered by insertContent() |
| 146 | + const simulatedInputMeta = tr.getMeta('applyInputRules'); |
| 147 | + const isSimulatedInput = !!simulatedInputMeta; |
| 148 | + |
| 149 | + if (isSimulatedInput) { |
| 150 | + setTimeout(() => { |
| 151 | + let { text } = simulatedInputMeta; |
| 152 | + |
| 153 | + if (typeof text !== 'string') { |
| 154 | + text = getHTMLFromFragment(Fragment.from(text), state.schema); |
| 155 | + } |
| 156 | + |
| 157 | + const { from } = simulatedInputMeta; |
| 158 | + const to = from + text.length; |
| 159 | + |
| 160 | + run({ |
| 161 | + editor, |
| 162 | + from, |
| 163 | + to, |
| 164 | + text, |
| 165 | + rules, |
| 166 | + plugin, |
| 167 | + }); |
| 168 | + }) |
| 169 | + } |
| 170 | + |
| 171 | + return tr.selectionSet || tr.docChanged ? null : prev; |
| 172 | + }, |
| 173 | + }, |
| 174 | + |
| 175 | + props: { |
| 176 | + handleTextInput(view, from, to, text) { |
| 177 | + return run({ |
| 178 | + editor, |
| 179 | + from, |
| 180 | + to, |
| 181 | + text, |
| 182 | + rules, |
| 183 | + plugin, |
| 184 | + }) |
| 185 | + }, |
| 186 | + |
| 187 | + // add support for input rules to trigger on enter |
| 188 | + // this is useful for example for code blocks |
| 189 | + handleKeyDown(view, event) { |
| 190 | + if (event.key !== 'Enter') { |
| 191 | + return false; |
| 192 | + } |
| 193 | + |
| 194 | + const { $cursor } = view.state.selection; |
| 195 | + |
| 196 | + if ($cursor) { |
| 197 | + return run({ |
| 198 | + editor, |
| 199 | + from: $cursor.pos, |
| 200 | + to: $cursor.pos, |
| 201 | + text: '\n', |
| 202 | + rules, |
| 203 | + plugin, |
| 204 | + }) |
| 205 | + } |
| 206 | + |
| 207 | + return false; |
| 208 | + }, |
| 209 | + }, |
| 210 | + |
| 211 | + isInputRules: true, |
| 212 | + }); |
| 213 | + return plugin; |
| 214 | +} |
0 commit comments