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