@portabletext/plugin-input-rule 6.0.13 → 6.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +104 -238
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +361 -427
- package/dist/index.js.map +1 -1
- package/package.json +12 -10
package/dist/index.js
CHANGED
|
@@ -1,443 +1,377 @@
|
|
|
1
1
|
import { c } from "react/compiler-runtime";
|
|
2
2
|
import { useEditor } from "@portabletext/editor";
|
|
3
|
-
import { defineBehavior,
|
|
4
|
-
import {
|
|
5
|
-
import { blockOffsetToSpanSelectionPoint, childSelectionPointToBlockOffset,
|
|
3
|
+
import { defineBehavior, effect, forward, raise } from "@portabletext/editor/behaviors";
|
|
4
|
+
import { getBlockOffsets, getBlockTextBefore, getFocusBlock, getMarkState, getNextInlineObjects, getPreviousInlineObjects } from "@portabletext/editor/selectors";
|
|
5
|
+
import { blockOffsetToSpanSelectionPoint, childSelectionPointToBlockOffset, isEqualSelections, isKeyedSegment, isSelectionCollapsed } from "@portabletext/editor/utils";
|
|
6
6
|
import { useActorRef } from "@xstate/react";
|
|
7
|
-
import {
|
|
7
|
+
import { fromCallback, setup } from "xstate";
|
|
8
|
+
/**
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
8
11
|
function defineInputRule(config) {
|
|
9
|
-
|
|
12
|
+
return config;
|
|
10
13
|
}
|
|
11
|
-
function getInputRuleMatchLocation({
|
|
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
|
-
if (!inlineObjectOffset || !allowedInlineObjectRanges.some((range) => inlineObjectOffset.offset >= range.start + adjustIndexBy && inlineObjectOffset.offset <= range.end + adjustIndexBy))
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
return {
|
|
74
|
-
text,
|
|
75
|
-
selection,
|
|
76
|
-
targetOffsets
|
|
77
|
-
};
|
|
14
|
+
function getInputRuleMatchLocation({ match, adjustIndexBy, snapshot, focusBlock, originalTextBefore, allowedInlineObjectRanges }) {
|
|
15
|
+
let [text, start, end] = match, adjustedIndex = start + adjustIndexBy, targetOffsets = {
|
|
16
|
+
anchor: {
|
|
17
|
+
path: focusBlock.path,
|
|
18
|
+
offset: adjustedIndex
|
|
19
|
+
},
|
|
20
|
+
focus: {
|
|
21
|
+
path: focusBlock.path,
|
|
22
|
+
offset: adjustedIndex + end - start
|
|
23
|
+
},
|
|
24
|
+
backward: !1
|
|
25
|
+
}, normalizedOffsets = {
|
|
26
|
+
anchor: {
|
|
27
|
+
path: focusBlock.path,
|
|
28
|
+
offset: Math.min(targetOffsets.anchor.offset, originalTextBefore.length)
|
|
29
|
+
},
|
|
30
|
+
focus: {
|
|
31
|
+
path: focusBlock.path,
|
|
32
|
+
offset: Math.min(targetOffsets.focus.offset, originalTextBefore.length)
|
|
33
|
+
},
|
|
34
|
+
backward: !1
|
|
35
|
+
}, anchorBackwards = blockOffsetToSpanSelectionPoint({
|
|
36
|
+
snapshot,
|
|
37
|
+
blockOffset: normalizedOffsets.anchor,
|
|
38
|
+
direction: "backward"
|
|
39
|
+
}), focusForwards = blockOffsetToSpanSelectionPoint({
|
|
40
|
+
snapshot,
|
|
41
|
+
blockOffset: normalizedOffsets.focus,
|
|
42
|
+
direction: "forward"
|
|
43
|
+
});
|
|
44
|
+
if (!anchorBackwards || !focusForwards) return;
|
|
45
|
+
let selection = {
|
|
46
|
+
anchor: anchorBackwards,
|
|
47
|
+
focus: focusForwards
|
|
48
|
+
}, inlineObjectsAfterMatch = getNextInlineObjects({
|
|
49
|
+
...snapshot,
|
|
50
|
+
context: {
|
|
51
|
+
...snapshot.context,
|
|
52
|
+
selection: {
|
|
53
|
+
anchor: selection.anchor,
|
|
54
|
+
focus: selection.anchor
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}), inlineObjectsBefore = getPreviousInlineObjects(snapshot), inlineObjectsInMatch = inlineObjectsAfterMatch.filter((inlineObjectAfter) => inlineObjectsBefore.some((inlineObjectBefore) => inlineObjectAfter.node._key === inlineObjectBefore.node._key));
|
|
58
|
+
for (let inlineObject of inlineObjectsInMatch) {
|
|
59
|
+
let inlineObjectOffset = childSelectionPointToBlockOffset({
|
|
60
|
+
snapshot,
|
|
61
|
+
selectionPoint: {
|
|
62
|
+
path: inlineObject.path,
|
|
63
|
+
offset: 0
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
if (!inlineObjectOffset || !allowedInlineObjectRanges.some((range) => inlineObjectOffset.offset >= range.start + adjustIndexBy && inlineObjectOffset.offset <= range.end + adjustIndexBy)) return;
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
text,
|
|
70
|
+
selection,
|
|
71
|
+
targetOffsets
|
|
72
|
+
};
|
|
78
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
79
77
|
function defineInputRuleBehavior(config) {
|
|
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
|
-
foundMatches.push(match), textBefore = newText.slice(0, match.targetOffsets.focus.offset ?? 0), newText = originalNewText.slice(match.targetOffsets.focus.offset ?? 0);
|
|
181
|
-
} else
|
|
182
|
-
break;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return foundActions.length === 0 ? !1 : {
|
|
186
|
-
actions: foundActions
|
|
187
|
-
};
|
|
188
|
-
},
|
|
189
|
-
actions: [({
|
|
190
|
-
event
|
|
191
|
-
}) => [forward(event)], (_, {
|
|
192
|
-
actions
|
|
193
|
-
}) => actions, ({
|
|
194
|
-
snapshot
|
|
195
|
-
}) => [effect(() => {
|
|
196
|
-
const blockOffsets = getBlockOffsets(snapshot);
|
|
197
|
-
config.onApply?.({
|
|
198
|
-
endOffsets: blockOffsets,
|
|
199
|
-
endSelection: snapshot.context.selection
|
|
200
|
-
});
|
|
201
|
-
})]]
|
|
202
|
-
});
|
|
78
|
+
return defineBehavior({
|
|
79
|
+
on: "insert.text",
|
|
80
|
+
guard: ({ snapshot, event, dom }) => {
|
|
81
|
+
if (!snapshot.context.selection || !isSelectionCollapsed(snapshot.context.selection)) return !1;
|
|
82
|
+
let focusBlock = getFocusBlock(snapshot);
|
|
83
|
+
if (!focusBlock) return !1;
|
|
84
|
+
let originalTextBefore = getBlockTextBefore(snapshot), textBefore = originalTextBefore, originalNewText = textBefore + event.text, newText = originalNewText, foundMatches = [], foundActions = [];
|
|
85
|
+
for (let rule of config.rules) {
|
|
86
|
+
let safeUserFlags = rule.on.flags.replace(/[^imsu]/g, ""), matcher = new RegExp(rule.on.source, `gd${safeUserFlags}`);
|
|
87
|
+
for (;;) {
|
|
88
|
+
let ruleMatches = [...newText.matchAll(matcher)].flatMap((regExpMatch) => {
|
|
89
|
+
if (regExpMatch.indices === void 0) return [];
|
|
90
|
+
let match = regExpMatch.indices.at(0);
|
|
91
|
+
if (!match) return [];
|
|
92
|
+
let allowedInlineObjectRanges = (rule.inlineObjects?.allow ?? []).flatMap((groupName) => {
|
|
93
|
+
let span = regExpMatch.indices?.groups?.[groupName];
|
|
94
|
+
return span ? [{
|
|
95
|
+
start: span[0],
|
|
96
|
+
end: span[1]
|
|
97
|
+
}] : [];
|
|
98
|
+
}), matchLocation = getInputRuleMatchLocation({
|
|
99
|
+
match: [regExpMatch.at(0) ?? "", ...match],
|
|
100
|
+
adjustIndexBy: originalNewText.length - newText.length,
|
|
101
|
+
snapshot,
|
|
102
|
+
focusBlock,
|
|
103
|
+
originalTextBefore,
|
|
104
|
+
allowedInlineObjectRanges
|
|
105
|
+
});
|
|
106
|
+
if (!matchLocation || matchLocation.targetOffsets.focus.offset <= originalTextBefore.length || foundMatches.some((foundMatch) => foundMatch.targetOffsets.anchor.offset === matchLocation.targetOffsets.anchor.offset)) return [];
|
|
107
|
+
let groups = {};
|
|
108
|
+
for (let [groupName, span] of Object.entries(regExpMatch.indices.groups ?? {})) {
|
|
109
|
+
if (!span) continue;
|
|
110
|
+
let groupLocation = getInputRuleMatchLocation({
|
|
111
|
+
match: [
|
|
112
|
+
regExpMatch.groups?.[groupName] ?? "",
|
|
113
|
+
span[0],
|
|
114
|
+
span[1]
|
|
115
|
+
],
|
|
116
|
+
adjustIndexBy: originalNewText.length - newText.length,
|
|
117
|
+
snapshot,
|
|
118
|
+
focusBlock,
|
|
119
|
+
originalTextBefore,
|
|
120
|
+
allowedInlineObjectRanges
|
|
121
|
+
});
|
|
122
|
+
groupLocation && (groups[groupName] = groupLocation);
|
|
123
|
+
}
|
|
124
|
+
return [{
|
|
125
|
+
text: matchLocation.text,
|
|
126
|
+
selection: matchLocation.selection,
|
|
127
|
+
targetOffsets: matchLocation.targetOffsets,
|
|
128
|
+
groups
|
|
129
|
+
}];
|
|
130
|
+
});
|
|
131
|
+
if (ruleMatches.length > 0) {
|
|
132
|
+
let guardResult = rule.guard?.({
|
|
133
|
+
snapshot,
|
|
134
|
+
event: {
|
|
135
|
+
type: "custom.input rule",
|
|
136
|
+
matches: ruleMatches,
|
|
137
|
+
focusBlock,
|
|
138
|
+
textBefore: originalTextBefore,
|
|
139
|
+
textInserted: event.text
|
|
140
|
+
},
|
|
141
|
+
dom
|
|
142
|
+
}) ?? !0;
|
|
143
|
+
if (!guardResult) break;
|
|
144
|
+
let actionSets = rule.actions.map((action) => action({
|
|
145
|
+
snapshot,
|
|
146
|
+
event: {
|
|
147
|
+
type: "custom.input rule",
|
|
148
|
+
matches: ruleMatches,
|
|
149
|
+
focusBlock,
|
|
150
|
+
textBefore: originalTextBefore,
|
|
151
|
+
textInserted: event.text
|
|
152
|
+
},
|
|
153
|
+
dom
|
|
154
|
+
}, guardResult));
|
|
155
|
+
for (let actionSet of actionSets) for (let action of actionSet) foundActions.push(action);
|
|
156
|
+
let matches = ruleMatches.flatMap((match) => {
|
|
157
|
+
let groupLocations = Object.values(match.groups).filter((location) => location !== void 0);
|
|
158
|
+
return groupLocations.length === 0 ? [match] : groupLocations;
|
|
159
|
+
});
|
|
160
|
+
for (let match of matches) foundMatches.push(match), textBefore = newText.slice(0, match.targetOffsets.focus.offset ?? 0), newText = originalNewText.slice(match.targetOffsets.focus.offset ?? 0);
|
|
161
|
+
} else break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return foundActions.length !== 0 && { actions: foundActions };
|
|
165
|
+
},
|
|
166
|
+
actions: [
|
|
167
|
+
({ event }) => [forward(event)],
|
|
168
|
+
(_, { actions }) => actions,
|
|
169
|
+
({ snapshot }) => [effect(() => {
|
|
170
|
+
let blockOffsets = getBlockOffsets(snapshot);
|
|
171
|
+
config.onApply?.({
|
|
172
|
+
endOffsets: blockOffsets,
|
|
173
|
+
endSelection: snapshot.context.selection
|
|
174
|
+
});
|
|
175
|
+
})]
|
|
176
|
+
]
|
|
177
|
+
});
|
|
203
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Turn an array of `InputRule`s into a Behavior that can be used to apply the
|
|
181
|
+
* rules to the editor.
|
|
182
|
+
*
|
|
183
|
+
* The plugin handles undo/redo out of the box including smart undo with
|
|
184
|
+
* Backspace.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```tsx
|
|
188
|
+
* <InputRulePlugin rules={smartQuotesRules} />
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* @public
|
|
192
|
+
*/
|
|
204
193
|
function InputRulePlugin(props) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
rules: props.rules
|
|
211
|
-
}
|
|
212
|
-
}, $[0] = editor, $[1] = props.rules, $[2] = t0) : t0 = $[2], useActorRef(inputRuleMachine, t0), null;
|
|
194
|
+
let $ = c(3), editor = useEditor(), t0;
|
|
195
|
+
return $[0] !== editor || $[1] !== props.rules ? (t0 = { input: {
|
|
196
|
+
editor,
|
|
197
|
+
rules: props.rules
|
|
198
|
+
} }, $[0] = editor, $[1] = props.rules, $[2] = t0) : t0 = $[2], useActorRef(inputRuleMachine, t0), null;
|
|
213
199
|
}
|
|
214
|
-
const inputRuleListenerCallback = ({
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}) => {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
})]]
|
|
249
|
-
})
|
|
250
|
-
}), selectionListenerCallback = ({
|
|
251
|
-
sendBack,
|
|
252
|
-
input
|
|
253
|
-
}) => {
|
|
254
|
-
const subscription = input.editor.on("selection", (event) => {
|
|
255
|
-
const blockOffsets = getBlockOffsets({
|
|
256
|
-
...input.editor.getSnapshot(),
|
|
257
|
-
context: {
|
|
258
|
-
...input.editor.getSnapshot().context,
|
|
259
|
-
selection: event.selection
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
sendBack({
|
|
263
|
-
type: "selection changed",
|
|
264
|
-
blockOffsets,
|
|
265
|
-
selection: event.selection
|
|
266
|
-
});
|
|
267
|
-
});
|
|
268
|
-
return () => subscription.unsubscribe();
|
|
200
|
+
const inputRuleListenerCallback = ({ input, sendBack }) => {
|
|
201
|
+
let unregister = input.editor.registerBehavior({ behavior: defineInputRuleBehavior({
|
|
202
|
+
rules: input.rules,
|
|
203
|
+
onApply: ({ endOffsets, endSelection }) => {
|
|
204
|
+
sendBack({
|
|
205
|
+
type: "input rule raised",
|
|
206
|
+
endOffsets,
|
|
207
|
+
endSelection
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}) });
|
|
211
|
+
return () => {
|
|
212
|
+
unregister();
|
|
213
|
+
};
|
|
214
|
+
}, deleteBackwardListenerCallback = ({ input, sendBack }) => input.editor.registerBehavior({ behavior: defineBehavior({
|
|
215
|
+
on: "delete.backward",
|
|
216
|
+
actions: [() => [raise({ type: "history.undo" }), effect(() => {
|
|
217
|
+
sendBack({ type: "history.undo raised" });
|
|
218
|
+
})]]
|
|
219
|
+
}) }), selectionListenerCallback = ({ sendBack, input }) => {
|
|
220
|
+
let subscription = input.editor.on("selection", (event) => {
|
|
221
|
+
sendBack({
|
|
222
|
+
type: "selection changed",
|
|
223
|
+
blockOffsets: getBlockOffsets({
|
|
224
|
+
...input.editor.getSnapshot(),
|
|
225
|
+
context: {
|
|
226
|
+
...input.editor.getSnapshot().context,
|
|
227
|
+
selection: event.selection
|
|
228
|
+
}
|
|
229
|
+
}),
|
|
230
|
+
selection: event.selection
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
return () => subscription.unsubscribe();
|
|
269
234
|
}, inputRuleSetup = setup({
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
return !1;
|
|
291
|
-
const startChanged = contextStartBlock._key !== eventStartBlock._key || context.endOffsets.start.offset !== event.blockOffsets.start.offset, endChanged = contextEndBlock._key !== eventEndBlock._key || context.endOffsets.end.offset !== event.blockOffsets.end.offset;
|
|
292
|
-
return startChanged || endChanged;
|
|
293
|
-
}
|
|
294
|
-
return !isEqualSelections(context.endSelection, event.selection);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
235
|
+
types: {
|
|
236
|
+
context: {},
|
|
237
|
+
input: {},
|
|
238
|
+
events: {}
|
|
239
|
+
},
|
|
240
|
+
actors: {
|
|
241
|
+
"delete.backward listener": fromCallback(deleteBackwardListenerCallback),
|
|
242
|
+
"input rule listener": fromCallback(inputRuleListenerCallback),
|
|
243
|
+
"selection listener": fromCallback(selectionListenerCallback)
|
|
244
|
+
},
|
|
245
|
+
guards: { "selection changed": ({ context, event }) => {
|
|
246
|
+
if (event.type !== "selection changed") return !1;
|
|
247
|
+
if (event.blockOffsets && context.endOffsets) {
|
|
248
|
+
let contextStartBlock = context.endOffsets.start.path.at(-1), eventStartBlock = event.blockOffsets.start.path.at(-1), contextEndBlock = context.endOffsets.end.path.at(-1), eventEndBlock = event.blockOffsets.end.path.at(-1);
|
|
249
|
+
if (!isKeyedSegment(contextStartBlock) || !isKeyedSegment(eventStartBlock) || !isKeyedSegment(contextEndBlock) || !isKeyedSegment(eventEndBlock)) return !1;
|
|
250
|
+
let startChanged = contextStartBlock._key !== eventStartBlock._key || context.endOffsets.start.offset !== event.blockOffsets.start.offset, endChanged = contextEndBlock._key !== eventEndBlock._key || context.endOffsets.end.offset !== event.blockOffsets.end.offset;
|
|
251
|
+
return startChanged || endChanged;
|
|
252
|
+
}
|
|
253
|
+
return !isEqualSelections(context.endSelection, event.selection);
|
|
254
|
+
} }
|
|
297
255
|
}), assignEndState = inputRuleSetup.assign({
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
event
|
|
301
|
-
}) => event.type === "input rule raised" ? event.endOffsets : context.endOffsets,
|
|
302
|
-
endSelection: ({
|
|
303
|
-
context,
|
|
304
|
-
event
|
|
305
|
-
}) => event.type === "input rule raised" ? event.endSelection : context.endSelection
|
|
256
|
+
endOffsets: ({ context, event }) => event.type === "input rule raised" ? event.endOffsets : context.endOffsets,
|
|
257
|
+
endSelection: ({ context, event }) => event.type === "input rule raised" ? event.endSelection : context.endSelection
|
|
306
258
|
}), inputRuleMachine = inputRuleSetup.createMachine({
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
context
|
|
346
|
-
}) => ({
|
|
347
|
-
editor: context.editor
|
|
348
|
-
})
|
|
349
|
-
}],
|
|
350
|
-
on: {
|
|
351
|
-
"selection changed": {
|
|
352
|
-
target: "idle",
|
|
353
|
-
guard: "selection changed"
|
|
354
|
-
},
|
|
355
|
-
"history.undo raised": {
|
|
356
|
-
target: "idle"
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
}
|
|
259
|
+
id: "input rule",
|
|
260
|
+
context: ({ input }) => ({
|
|
261
|
+
editor: input.editor,
|
|
262
|
+
rules: input.rules,
|
|
263
|
+
endOffsets: void 0,
|
|
264
|
+
endSelection: null
|
|
265
|
+
}),
|
|
266
|
+
initial: "idle",
|
|
267
|
+
invoke: {
|
|
268
|
+
src: "input rule listener",
|
|
269
|
+
input: ({ context }) => ({
|
|
270
|
+
editor: context.editor,
|
|
271
|
+
rules: context.rules
|
|
272
|
+
})
|
|
273
|
+
},
|
|
274
|
+
on: { "input rule raised": {
|
|
275
|
+
target: ".input rule applied",
|
|
276
|
+
actions: assignEndState
|
|
277
|
+
} },
|
|
278
|
+
states: {
|
|
279
|
+
idle: {},
|
|
280
|
+
"input rule applied": {
|
|
281
|
+
invoke: [{
|
|
282
|
+
src: "delete.backward listener",
|
|
283
|
+
input: ({ context }) => ({ editor: context.editor })
|
|
284
|
+
}, {
|
|
285
|
+
src: "selection listener",
|
|
286
|
+
input: ({ context }) => ({ editor: context.editor })
|
|
287
|
+
}],
|
|
288
|
+
on: {
|
|
289
|
+
"selection changed": {
|
|
290
|
+
target: "idle",
|
|
291
|
+
guard: "selection changed"
|
|
292
|
+
},
|
|
293
|
+
"history.undo raised": { target: "idle" }
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
361
297
|
});
|
|
298
|
+
/**
|
|
299
|
+
* Define an `InputRule` specifically designed to transform matched text into
|
|
300
|
+
* some other text.
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```tsx
|
|
304
|
+
* const transformRule = defineTextTransformRule({
|
|
305
|
+
* on: /--/,
|
|
306
|
+
* transform: () => '—',
|
|
307
|
+
* })
|
|
308
|
+
* ```
|
|
309
|
+
*
|
|
310
|
+
* @public
|
|
311
|
+
*/
|
|
362
312
|
function defineTextTransformRule(config) {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
path: event.focusBlock.path,
|
|
425
|
-
offset: newText.length - textLengthDelta
|
|
426
|
-
};
|
|
427
|
-
return [...actions, raise({
|
|
428
|
-
type: "select",
|
|
429
|
-
at: {
|
|
430
|
-
anchor: endCaretPosition,
|
|
431
|
-
focus: endCaretPosition
|
|
432
|
-
}
|
|
433
|
-
})];
|
|
434
|
-
}]
|
|
435
|
-
};
|
|
313
|
+
let transformRecord = typeof config.transform == "function" ? void 0 : config.transform;
|
|
314
|
+
if (transformRecord) {
|
|
315
|
+
let probeFlags = config.on.flags.replace(/[gyd]/g, ""), namedGroups = Object.keys(RegExp(`${config.on.source}|`, probeFlags).exec("")?.groups ?? {});
|
|
316
|
+
for (let groupName of Object.keys(transformRecord)) if (!namedGroups.includes(groupName)) throw Error(`defineTextTransformRule: \`transform\` targets the group "${groupName}", but \`on\` (${config.on}) has no such named capture group` + (namedGroups.length > 0 ? `. Named groups: ${namedGroups.map((name) => `"${name}"`).join(", ")}` : ". The pattern has no named capture groups"));
|
|
317
|
+
}
|
|
318
|
+
return {
|
|
319
|
+
on: config.on,
|
|
320
|
+
guard: config.guard ?? (() => !0),
|
|
321
|
+
actions: [({ snapshot, event }, guardResponse) => {
|
|
322
|
+
let targets = event.matches.flatMap((match) => transformRecord ? Object.entries(transformRecord).flatMap(([groupName, groupTransform]) => {
|
|
323
|
+
let location = match.groups[groupName];
|
|
324
|
+
return location ? [{
|
|
325
|
+
location,
|
|
326
|
+
transform: groupTransform
|
|
327
|
+
}] : [];
|
|
328
|
+
}) : [{
|
|
329
|
+
location: match,
|
|
330
|
+
transform: config.transform
|
|
331
|
+
}]).sort((a, b) => a.location.targetOffsets.anchor.offset - b.location.targetOffsets.anchor.offset), newText = event.textBefore + event.textInserted, textLengthDelta = 0, actions = [];
|
|
332
|
+
for (let { location, transform } of targets.reverse()) {
|
|
333
|
+
let text = transform({ location }, guardResponse);
|
|
334
|
+
textLengthDelta -= text.length - (location.targetOffsets.focus.offset - location.targetOffsets.anchor.offset), actions.push(raise({
|
|
335
|
+
type: "select",
|
|
336
|
+
at: location.targetOffsets
|
|
337
|
+
})), actions.push(raise({
|
|
338
|
+
type: "delete",
|
|
339
|
+
at: location.targetOffsets
|
|
340
|
+
})), actions.push(raise({
|
|
341
|
+
type: "insert.child",
|
|
342
|
+
child: {
|
|
343
|
+
_type: snapshot.context.schema.span.name,
|
|
344
|
+
text,
|
|
345
|
+
marks: getMarkState({
|
|
346
|
+
...snapshot,
|
|
347
|
+
context: {
|
|
348
|
+
...snapshot.context,
|
|
349
|
+
selection: {
|
|
350
|
+
anchor: location.selection.anchor,
|
|
351
|
+
focus: {
|
|
352
|
+
path: location.selection.focus.path,
|
|
353
|
+
offset: Math.min(location.selection.focus.offset, event.textBefore.length)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
})?.marks ?? []
|
|
358
|
+
}
|
|
359
|
+
}));
|
|
360
|
+
}
|
|
361
|
+
let endCaretPosition = {
|
|
362
|
+
path: event.focusBlock.path,
|
|
363
|
+
offset: newText.length - textLengthDelta
|
|
364
|
+
};
|
|
365
|
+
return [...actions, raise({
|
|
366
|
+
type: "select",
|
|
367
|
+
at: {
|
|
368
|
+
anchor: endCaretPosition,
|
|
369
|
+
focus: endCaretPosition
|
|
370
|
+
}
|
|
371
|
+
})];
|
|
372
|
+
}]
|
|
373
|
+
};
|
|
436
374
|
}
|
|
437
|
-
export {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
defineInputRuleBehavior,
|
|
441
|
-
defineTextTransformRule
|
|
442
|
-
};
|
|
443
|
-
//# sourceMappingURL=index.js.map
|
|
375
|
+
export { InputRulePlugin, defineInputRule, defineInputRuleBehavior, defineTextTransformRule };
|
|
376
|
+
|
|
377
|
+
//# sourceMappingURL=index.js.map
|