@kerebron/editor 0.4.28 → 0.4.29
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/esm/CoreEditor.js +1 -0
- package/esm/CoreEditor.js.map +1 -0
- package/esm/DummyEditorView.js +1 -0
- package/esm/DummyEditorView.js.map +1 -0
- package/esm/Extension.js +1 -0
- package/esm/Extension.js.map +1 -0
- package/esm/ExtensionManager.js +1 -0
- package/esm/ExtensionManager.js.map +1 -0
- package/esm/Mark.js +1 -0
- package/esm/Mark.js.map +1 -0
- package/esm/Node.js +1 -0
- package/esm/Node.js.map +1 -0
- package/esm/commands/CommandManager.js +1 -0
- package/esm/commands/CommandManager.js.map +1 -0
- package/esm/commands/baseCommandFactories.js +1 -0
- package/esm/commands/baseCommandFactories.js.map +1 -0
- package/esm/commands/createChainableState.js +1 -0
- package/esm/commands/createChainableState.js.map +1 -0
- package/esm/commands/keyCommandFactories.js +1 -0
- package/esm/commands/keyCommandFactories.js.map +1 -0
- package/esm/commands/mod.js +1 -0
- package/esm/commands/mod.js.map +1 -0
- package/esm/commands/replaceCommandFactories.js +1 -0
- package/esm/commands/replaceCommandFactories.js.map +1 -0
- package/esm/commands/types.js +1 -0
- package/esm/commands/types.js.map +1 -0
- package/esm/mod.js +1 -0
- package/esm/mod.js.map +1 -0
- package/esm/nodeToTreeString.js +1 -0
- package/esm/nodeToTreeString.js.map +1 -0
- package/esm/plugins/TrackSelecionPlugin.js +1 -0
- package/esm/plugins/TrackSelecionPlugin.js.map +1 -0
- package/esm/plugins/input-rules/InputRulesPlugin.js +1 -0
- package/esm/plugins/input-rules/InputRulesPlugin.js.map +1 -0
- package/esm/plugins/input-rules/mod.js +1 -0
- package/esm/plugins/input-rules/mod.js.map +1 -0
- package/esm/plugins/input-rules/rulebuilders.js +1 -0
- package/esm/plugins/input-rules/rulebuilders.js.map +1 -0
- package/esm/plugins/keymap/keymap.js +1 -0
- package/esm/plugins/keymap/keymap.js.map +1 -0
- package/esm/plugins/keymap/mod.js +1 -0
- package/esm/plugins/keymap/mod.js.map +1 -0
- package/esm/plugins/keymap/w3c-keyname.js +1 -0
- package/esm/plugins/keymap/w3c-keyname.js.map +1 -0
- package/esm/search/mod.js +1 -0
- package/esm/search/mod.js.map +1 -0
- package/esm/search/query.js +1 -0
- package/esm/search/query.js.map +1 -0
- package/esm/search/search.js +1 -0
- package/esm/search/search.js.map +1 -0
- package/esm/types.js +1 -0
- package/esm/types.js.map +1 -0
- package/esm/ui.js +1 -0
- package/esm/ui.js.map +1 -0
- package/esm/utilities/SmartOutput.js +1 -0
- package/esm/utilities/SmartOutput.js.map +1 -0
- package/esm/utilities/createNodeFromContent.js +1 -0
- package/esm/utilities/createNodeFromContent.js.map +1 -0
- package/esm/utilities/getHtmlAttributes.js +1 -0
- package/esm/utilities/getHtmlAttributes.js.map +1 -0
- package/esm/utilities/getShadowRoot.js +1 -0
- package/esm/utilities/getShadowRoot.js.map +1 -0
- package/esm/utilities/mod.js +1 -0
- package/esm/utilities/mod.js.map +1 -0
- package/esm/utilities/toRawTextResult.js +1 -0
- package/esm/utilities/toRawTextResult.js.map +1 -0
- package/package.json +5 -1
- package/src/CoreEditor.ts +277 -0
- package/src/DummyEditorView.ts +403 -0
- package/src/Extension.ts +63 -0
- package/src/ExtensionManager.ts +328 -0
- package/src/Mark.ts +47 -0
- package/src/Node.ts +66 -0
- package/src/commands/CommandManager.ts +145 -0
- package/src/commands/baseCommandFactories.ts +1103 -0
- package/src/commands/createChainableState.ts +36 -0
- package/src/commands/keyCommandFactories.ts +26 -0
- package/src/commands/mod.ts +104 -0
- package/src/commands/replaceCommandFactories.ts +129 -0
- package/src/commands/types.ts +30 -0
- package/src/mod.ts +8 -0
- package/src/nodeToTreeString.ts +100 -0
- package/src/plugins/TrackSelecionPlugin.ts +27 -0
- package/src/plugins/input-rules/InputRulesPlugin.ts +242 -0
- package/src/plugins/input-rules/mod.ts +2 -0
- package/src/plugins/input-rules/rulebuilders.ts +88 -0
- package/src/plugins/keymap/keymap.ts +117 -0
- package/src/plugins/keymap/mod.ts +1 -0
- package/src/plugins/keymap/w3c-keyname.ts +123 -0
- package/src/search/mod.ts +2 -0
- package/src/search/query.ts +412 -0
- package/src/search/search.ts +284 -0
- package/src/types.ts +71 -0
- package/src/ui.ts +35 -0
- package/src/utilities/SmartOutput.ts +284 -0
- package/src/utilities/createNodeFromContent.ts +66 -0
- package/src/utilities/getHtmlAttributes.ts +68 -0
- package/src/utilities/getShadowRoot.ts +18 -0
- package/src/utilities/mod.ts +5 -0
- package/src/utilities/toRawTextResult.ts +27 -0
- package/assets/base.css +0 -114
- package/assets/content.css +0 -35
- package/assets/gapcursor.css +0 -25
- package/assets/index.css +0 -2
- package/assets/main.css +0 -8
- package/assets/mobile.css +0 -33
- package/assets/prosemirror.css +0 -20
- package/assets/search.css +0 -6
- package/assets/vars.css +0 -110
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import { EditorState } from 'prosemirror-state';
|
|
2
|
+
import { Fragment, Node, Slice } from 'prosemirror-model';
|
|
3
|
+
|
|
4
|
+
export class SearchQuery {
|
|
5
|
+
/// The search string (or regular expression).
|
|
6
|
+
readonly search: string;
|
|
7
|
+
/// Indicates whether the search is case-sensitive.
|
|
8
|
+
readonly caseSensitive: boolean;
|
|
9
|
+
/// By default, string search will replace `\n`, `\r`, and `\t` in
|
|
10
|
+
/// the query with newline, return, and tab characters. When this
|
|
11
|
+
/// is set to true, that behavior is disabled.
|
|
12
|
+
readonly literal: boolean;
|
|
13
|
+
/// When true, the search string is interpreted as a regular
|
|
14
|
+
/// expression.
|
|
15
|
+
readonly regexp: boolean;
|
|
16
|
+
/// The replace text, or the empty string if no replace text has
|
|
17
|
+
/// been given.
|
|
18
|
+
readonly replace: string;
|
|
19
|
+
/// Whether this query is non-empty and, in case of a regular
|
|
20
|
+
/// expression search, syntactically valid.
|
|
21
|
+
readonly valid: boolean;
|
|
22
|
+
/// When true, matches that contain words are ignored when there are
|
|
23
|
+
/// further word characters around them.
|
|
24
|
+
readonly wholeWord: boolean;
|
|
25
|
+
/// An optional filter that causes some results to be ignored.
|
|
26
|
+
readonly filter:
|
|
27
|
+
| ((state: EditorState, result: SearchResult) => boolean)
|
|
28
|
+
| null;
|
|
29
|
+
|
|
30
|
+
/// @internal
|
|
31
|
+
impl: QueryImpl;
|
|
32
|
+
|
|
33
|
+
/// Create a query object.
|
|
34
|
+
constructor(config: {
|
|
35
|
+
/// The search string.
|
|
36
|
+
search: string;
|
|
37
|
+
/// Controls whether the search should be case-sensitive.
|
|
38
|
+
caseSensitive?: boolean;
|
|
39
|
+
/// By default, string search will replace `\n`, `\r`, and `\t` in
|
|
40
|
+
/// the query with newline, return, and tab characters. When this
|
|
41
|
+
/// is set to true, that behavior is disabled.
|
|
42
|
+
literal?: boolean;
|
|
43
|
+
/// When true, interpret the search string as a regular expression.
|
|
44
|
+
regexp?: boolean;
|
|
45
|
+
/// The replace text.
|
|
46
|
+
replace?: string;
|
|
47
|
+
/// Enable whole-word matching.
|
|
48
|
+
wholeWord?: boolean;
|
|
49
|
+
/// Providing a filter causes results for which the filter returns
|
|
50
|
+
/// false to be ignored.
|
|
51
|
+
filter?: (state: EditorState, result: SearchResult) => boolean;
|
|
52
|
+
}) {
|
|
53
|
+
this.search = config.search;
|
|
54
|
+
this.caseSensitive = !!config.caseSensitive;
|
|
55
|
+
this.literal = !!config.literal;
|
|
56
|
+
this.regexp = !!config.regexp;
|
|
57
|
+
this.replace = config.replace || '';
|
|
58
|
+
this.valid = !!this.search && !(this.regexp && !validRegExp(this.search));
|
|
59
|
+
this.wholeWord = !!config.wholeWord;
|
|
60
|
+
this.filter = config.filter || null;
|
|
61
|
+
this.impl = !this.valid
|
|
62
|
+
? nullQuery
|
|
63
|
+
: this.regexp
|
|
64
|
+
? new RegExpQuery(this)
|
|
65
|
+
: new StringQuery(this);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/// Compare this query to another query.
|
|
69
|
+
eq(other: SearchQuery) {
|
|
70
|
+
return this.search == other.search && this.replace == other.replace &&
|
|
71
|
+
this.caseSensitive == other.caseSensitive &&
|
|
72
|
+
this.regexp == other.regexp &&
|
|
73
|
+
this.wholeWord == other.wholeWord;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/// Find the next occurrence of this query in the given range.
|
|
77
|
+
findNext(
|
|
78
|
+
state: EditorState,
|
|
79
|
+
from: number = 0,
|
|
80
|
+
to: number = state.doc.content.size,
|
|
81
|
+
) {
|
|
82
|
+
for (;;) {
|
|
83
|
+
if (from >= to) return null;
|
|
84
|
+
let result = this.impl.findNext(state, from, to);
|
|
85
|
+
if (!result || this.checkResult(state, result)) return result;
|
|
86
|
+
from = result.from + 1;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Find the previous occurrence of this query in the given range.
|
|
91
|
+
/// Note that, if `to` is given, it should be _less_ than `from`.
|
|
92
|
+
findPrev(
|
|
93
|
+
state: EditorState,
|
|
94
|
+
from: number = state.doc.content.size,
|
|
95
|
+
to: number = 0,
|
|
96
|
+
) {
|
|
97
|
+
for (;;) {
|
|
98
|
+
if (from <= to) return null;
|
|
99
|
+
let result = this.impl.findPrev(state, from, to);
|
|
100
|
+
if (!result || this.checkResult(state, result)) return result;
|
|
101
|
+
from = result.to - 1;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// @internal
|
|
106
|
+
checkResult(state: EditorState, result: SearchResult) {
|
|
107
|
+
return (!this.wholeWord ||
|
|
108
|
+
checkWordBoundary(state, result.from) &&
|
|
109
|
+
checkWordBoundary(state, result.to)) &&
|
|
110
|
+
(!this.filter || this.filter(state, result));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// @internal
|
|
114
|
+
unquote(string: string) {
|
|
115
|
+
return this.literal
|
|
116
|
+
? string
|
|
117
|
+
: string.replace(/\\([nrt\\])/g, (_, ch) =>
|
|
118
|
+
ch == 'n' ? '\n' : ch == 'r' ? '\r' : ch == 't' ? '\t' : '\\');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// Get the ranges that should be replaced for this result. This can
|
|
122
|
+
/// return multiple ranges when `this.replace` contains
|
|
123
|
+
/// `$1`/`$&`-style placeholders, in which case the preserved
|
|
124
|
+
/// content is skipped by the replacements.
|
|
125
|
+
///
|
|
126
|
+
/// Ranges are sorted by position, and `from`/`to` positions all
|
|
127
|
+
/// refer to positions in `state.doc`. When applying these, you'll
|
|
128
|
+
/// want to either apply them from back to front, or map these
|
|
129
|
+
/// positions through your transaction's current mapping.
|
|
130
|
+
getReplacements(
|
|
131
|
+
state: EditorState,
|
|
132
|
+
result: SearchResult,
|
|
133
|
+
): { from: number; to: number; insert: Slice }[] {
|
|
134
|
+
let $from = state.doc.resolve(result.from);
|
|
135
|
+
let marks = $from.marksAcross(state.doc.resolve(result.to));
|
|
136
|
+
let ranges: { from: number; to: number; insert: Slice }[] = [];
|
|
137
|
+
|
|
138
|
+
let frag = Fragment.empty, pos = result.from, { match } = result;
|
|
139
|
+
let groups = match
|
|
140
|
+
? getGroupIndices(match)
|
|
141
|
+
: [[0, result.to - result.from]];
|
|
142
|
+
let replParts = parseReplacement(this.unquote(this.replace)), groupSpan;
|
|
143
|
+
for (let part of replParts) {
|
|
144
|
+
if (typeof part == 'string') { // Replacement text
|
|
145
|
+
frag = frag.addToEnd(state.schema.text(part, marks));
|
|
146
|
+
} else {
|
|
147
|
+
groupSpan = groups[part.group];
|
|
148
|
+
if (groupSpan) {
|
|
149
|
+
let from = result.matchStart + groupSpan[0],
|
|
150
|
+
to = result.matchStart + groupSpan[1];
|
|
151
|
+
if (part.copy) { // Copied content
|
|
152
|
+
frag = frag.append(state.doc.slice(from, to).content);
|
|
153
|
+
} else { // Skipped content
|
|
154
|
+
if (frag != Fragment.empty || from > pos) {
|
|
155
|
+
ranges.push({
|
|
156
|
+
from: pos,
|
|
157
|
+
to: from,
|
|
158
|
+
insert: new Slice(frag, 0, 0),
|
|
159
|
+
});
|
|
160
|
+
frag = Fragment.empty;
|
|
161
|
+
}
|
|
162
|
+
pos = to;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (frag != Fragment.empty || pos < result.to) {
|
|
168
|
+
ranges.push({ from: pos, to: result.to, insert: new Slice(frag, 0, 0) });
|
|
169
|
+
}
|
|
170
|
+
return ranges;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/// A matched instance of a search query. `match` will be non-null
|
|
175
|
+
/// only for regular expression queries.
|
|
176
|
+
export interface SearchResult {
|
|
177
|
+
from: number;
|
|
178
|
+
to: number;
|
|
179
|
+
match: RegExpMatchArray | null;
|
|
180
|
+
matchStart: number;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
interface QueryImpl {
|
|
184
|
+
findNext(state: EditorState, from: number, to: number): SearchResult | null;
|
|
185
|
+
findPrev(state: EditorState, from: number, to: number): SearchResult | null;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const nullQuery = new class implements QueryImpl {
|
|
189
|
+
findNext() {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
findPrev() {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
}();
|
|
196
|
+
|
|
197
|
+
class StringQuery implements QueryImpl {
|
|
198
|
+
string: string;
|
|
199
|
+
|
|
200
|
+
constructor(readonly query: SearchQuery) {
|
|
201
|
+
let string = query.unquote(query.search);
|
|
202
|
+
if (!query.caseSensitive) string = string.toLowerCase();
|
|
203
|
+
this.string = string;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
findNext(state: EditorState, from: number, to: number) {
|
|
207
|
+
return scanTextblocks(state.doc, from, to, (node, start) => {
|
|
208
|
+
let off = Math.max(from, start);
|
|
209
|
+
let content = textContent(node).slice(
|
|
210
|
+
off - start,
|
|
211
|
+
Math.min(node.content.size, to - start),
|
|
212
|
+
);
|
|
213
|
+
let index = (this.query.caseSensitive ? content : content.toLowerCase())
|
|
214
|
+
.indexOf(this.string);
|
|
215
|
+
return index < 0 ? null : {
|
|
216
|
+
from: off + index,
|
|
217
|
+
to: off + index + this.string.length,
|
|
218
|
+
match: null,
|
|
219
|
+
matchStart: start,
|
|
220
|
+
};
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
findPrev(state: EditorState, from: number, to: number) {
|
|
225
|
+
return scanTextblocks(state.doc, from, to, (node, start) => {
|
|
226
|
+
let off = Math.max(start, to);
|
|
227
|
+
let content = textContent(node).slice(
|
|
228
|
+
off - start,
|
|
229
|
+
Math.min(node.content.size, from - start),
|
|
230
|
+
);
|
|
231
|
+
if (!this.query.caseSensitive) content = content.toLowerCase();
|
|
232
|
+
let index = content.lastIndexOf(this.string);
|
|
233
|
+
return index < 0 ? null : {
|
|
234
|
+
from: off + index,
|
|
235
|
+
to: off + index + this.string.length,
|
|
236
|
+
match: null,
|
|
237
|
+
matchStart: start,
|
|
238
|
+
};
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const baseFlags = 'g' + (/x/.unicode == null ? '' : 'u') +
|
|
244
|
+
((/x/ as any).hasIndices == null ? '' : 'd');
|
|
245
|
+
|
|
246
|
+
class RegExpQuery implements QueryImpl {
|
|
247
|
+
regexp: RegExp;
|
|
248
|
+
|
|
249
|
+
constructor(readonly query: SearchQuery) {
|
|
250
|
+
this.regexp = new RegExp(
|
|
251
|
+
query.search,
|
|
252
|
+
baseFlags + (query.caseSensitive ? '' : 'i'),
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
findNext(state: EditorState, from: number, to: number) {
|
|
257
|
+
return scanTextblocks(state.doc, from, to, (node, start) => {
|
|
258
|
+
let content = textContent(node).slice(
|
|
259
|
+
0,
|
|
260
|
+
Math.min(node.content.size, to - start),
|
|
261
|
+
);
|
|
262
|
+
this.regexp.lastIndex = from - start;
|
|
263
|
+
let match = this.regexp.exec(content);
|
|
264
|
+
return match
|
|
265
|
+
? {
|
|
266
|
+
from: start + match.index,
|
|
267
|
+
to: start + match.index + match[0].length,
|
|
268
|
+
match,
|
|
269
|
+
matchStart: start,
|
|
270
|
+
}
|
|
271
|
+
: null;
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
findPrev(state: EditorState, from: number, to: number) {
|
|
276
|
+
return scanTextblocks(state.doc, from, to, (node, start) => {
|
|
277
|
+
let content = textContent(node).slice(
|
|
278
|
+
0,
|
|
279
|
+
Math.min(node.content.size, from - start),
|
|
280
|
+
);
|
|
281
|
+
let match;
|
|
282
|
+
for (let off = 0;;) {
|
|
283
|
+
this.regexp.lastIndex = off;
|
|
284
|
+
let next = this.regexp.exec(content);
|
|
285
|
+
if (!next) break;
|
|
286
|
+
match = next;
|
|
287
|
+
off = next.index + 1;
|
|
288
|
+
}
|
|
289
|
+
return match
|
|
290
|
+
? {
|
|
291
|
+
from: start + match.index,
|
|
292
|
+
to: start + match.index + match[0].length,
|
|
293
|
+
match,
|
|
294
|
+
matchStart: start,
|
|
295
|
+
}
|
|
296
|
+
: null;
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function getGroupIndices(
|
|
302
|
+
match: RegExpMatchArray,
|
|
303
|
+
): ([number, number] | undefined)[] {
|
|
304
|
+
if ((match as any).indices) return (match as any).indices;
|
|
305
|
+
let result: ([number, number] | undefined)[] = [[0, match[0].length]];
|
|
306
|
+
for (let i = 1, pos = 0; i < match.length; i++) {
|
|
307
|
+
let found = match[i] ? match[0].indexOf(match[i], pos) : -1;
|
|
308
|
+
result.push(found < 0 ? undefined : [found, pos = found + match[i].length]);
|
|
309
|
+
}
|
|
310
|
+
return result;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function parseReplacement(
|
|
314
|
+
text: string,
|
|
315
|
+
): (string | { group: number; copy: boolean })[] {
|
|
316
|
+
let result: (string | { group: number; copy: boolean })[] = [],
|
|
317
|
+
highestSeen = -1;
|
|
318
|
+
function add(text: string) {
|
|
319
|
+
let last = result.length - 1;
|
|
320
|
+
if (last > -1 && typeof result[last] == 'string') result[last] += text;
|
|
321
|
+
else result.push(text);
|
|
322
|
+
}
|
|
323
|
+
while (text.length) {
|
|
324
|
+
let m = /\$([$&\d+])/.exec(text);
|
|
325
|
+
if (!m) {
|
|
326
|
+
add(text);
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
if (m.index > 0) add(text.slice(0, m.index + (m[1] == '$' ? 1 : 0)));
|
|
330
|
+
if (m[1] != '$') {
|
|
331
|
+
let n = m[1] == '&' ? 0 : +m[1];
|
|
332
|
+
if (highestSeen >= n) {
|
|
333
|
+
result.push({ group: n, copy: true });
|
|
334
|
+
} else {
|
|
335
|
+
highestSeen = n || 1000;
|
|
336
|
+
result.push({ group: n, copy: false });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
text = text.slice(m.index + m[0].length);
|
|
340
|
+
}
|
|
341
|
+
return result;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function validRegExp(source: string) {
|
|
345
|
+
try {
|
|
346
|
+
new RegExp(source, baseFlags);
|
|
347
|
+
return true;
|
|
348
|
+
} catch {
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const TextContentCache = new WeakMap<Node, string>();
|
|
354
|
+
|
|
355
|
+
function textContent(node: Node) {
|
|
356
|
+
let cached = TextContentCache.get(node);
|
|
357
|
+
if (cached) return cached;
|
|
358
|
+
|
|
359
|
+
let content = '';
|
|
360
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
361
|
+
let child = node.child(i);
|
|
362
|
+
if (child.isText) content += child.text!;
|
|
363
|
+
else if (child.isLeaf) content += '\ufffc';
|
|
364
|
+
else content += ' ' + textContent(child) + ' ';
|
|
365
|
+
}
|
|
366
|
+
TextContentCache.set(node, content);
|
|
367
|
+
return content;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function scanTextblocks<T>(
|
|
371
|
+
node: Node,
|
|
372
|
+
from: number,
|
|
373
|
+
to: number,
|
|
374
|
+
f: (node: Node, startPos: number) => T | null,
|
|
375
|
+
nodeStart: number = 0,
|
|
376
|
+
): T | null {
|
|
377
|
+
if (node.inlineContent) {
|
|
378
|
+
return f(node, nodeStart);
|
|
379
|
+
} else if (!node.isLeaf) {
|
|
380
|
+
if (from > to) {
|
|
381
|
+
for (
|
|
382
|
+
let i = node.childCount - 1, pos = nodeStart + node.content.size;
|
|
383
|
+
i >= 0 && pos > to;
|
|
384
|
+
i--
|
|
385
|
+
) {
|
|
386
|
+
let child = node.child(i);
|
|
387
|
+
pos -= child.nodeSize;
|
|
388
|
+
if (pos < from) {
|
|
389
|
+
let result = scanTextblocks(child, from, to, f, pos + 1);
|
|
390
|
+
if (result != null) return result;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
} else {
|
|
394
|
+
for (let i = 0, pos = nodeStart; i < node.childCount && pos < to; i++) {
|
|
395
|
+
let child = node.child(i), start = pos;
|
|
396
|
+
pos += child.nodeSize;
|
|
397
|
+
if (pos > from) {
|
|
398
|
+
let result = scanTextblocks(child, from, to, f, start + 1);
|
|
399
|
+
if (result != null) return result;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function checkWordBoundary(state: EditorState, pos: number) {
|
|
408
|
+
let $pos = state.doc.resolve(pos);
|
|
409
|
+
let before = $pos.nodeBefore, after = $pos.nodeAfter;
|
|
410
|
+
if (!before || !after || !before.isText || !after.isText) return true;
|
|
411
|
+
return !/\p{L}$/u.test(before.text!) || !/^\p{L}/u.test(after.text!);
|
|
412
|
+
}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EditorState,
|
|
3
|
+
Plugin,
|
|
4
|
+
PluginKey,
|
|
5
|
+
TextSelection,
|
|
6
|
+
Transaction,
|
|
7
|
+
} from 'prosemirror-state';
|
|
8
|
+
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
9
|
+
|
|
10
|
+
import { type Command } from '../commands/types.js';
|
|
11
|
+
import { SearchQuery, SearchResult } from './query.js';
|
|
12
|
+
|
|
13
|
+
class SearchState {
|
|
14
|
+
constructor(
|
|
15
|
+
readonly query: SearchQuery,
|
|
16
|
+
readonly range: { from: number; to: number } | null,
|
|
17
|
+
readonly deco: DecorationSet,
|
|
18
|
+
) {}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function buildMatchDeco(
|
|
22
|
+
state: EditorState,
|
|
23
|
+
query: SearchQuery,
|
|
24
|
+
range: { from: number; to: number } | null,
|
|
25
|
+
) {
|
|
26
|
+
if (!query.valid) return DecorationSet.empty;
|
|
27
|
+
let deco: Decoration[] = [];
|
|
28
|
+
let sel = state.selection;
|
|
29
|
+
for (
|
|
30
|
+
let pos = range ? range.from : 0,
|
|
31
|
+
end = range ? range.to : state.doc.content.size;;
|
|
32
|
+
) {
|
|
33
|
+
let next = query.findNext(state, pos, end);
|
|
34
|
+
if (!next) break;
|
|
35
|
+
let cls = next.from == sel.from && next.to == sel.to
|
|
36
|
+
? 'kb-active-search-match'
|
|
37
|
+
: 'kb-search-match';
|
|
38
|
+
deco.push(Decoration.inline(next.from, next.to, { class: cls }));
|
|
39
|
+
pos = next.to;
|
|
40
|
+
}
|
|
41
|
+
return DecorationSet.create(state.doc, deco);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const searchKey: PluginKey<SearchState> = new PluginKey('search');
|
|
45
|
+
|
|
46
|
+
/// Returns a plugin that stores a current search query and searched
|
|
47
|
+
/// range, and highlights matches of the query.
|
|
48
|
+
export function search(
|
|
49
|
+
options: {
|
|
50
|
+
initialQuery?: SearchQuery;
|
|
51
|
+
initialRange?: { from: number; to: number };
|
|
52
|
+
} = {},
|
|
53
|
+
): Plugin {
|
|
54
|
+
return new Plugin<SearchState>({
|
|
55
|
+
key: searchKey,
|
|
56
|
+
state: {
|
|
57
|
+
init(_config, state) {
|
|
58
|
+
let query = options.initialQuery || new SearchQuery({ search: '' });
|
|
59
|
+
let range = options.initialRange || null;
|
|
60
|
+
return new SearchState(
|
|
61
|
+
query,
|
|
62
|
+
range,
|
|
63
|
+
buildMatchDeco(state, query, range),
|
|
64
|
+
);
|
|
65
|
+
},
|
|
66
|
+
apply(tr, search, _oldState, state) {
|
|
67
|
+
let set = tr.getMeta(searchKey) as {
|
|
68
|
+
query: SearchQuery;
|
|
69
|
+
range: { from: number; to: number } | null;
|
|
70
|
+
} | undefined;
|
|
71
|
+
if (set) {
|
|
72
|
+
return new SearchState(
|
|
73
|
+
set.query,
|
|
74
|
+
set.range,
|
|
75
|
+
buildMatchDeco(state, set.query, set.range),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (tr.docChanged || tr.selectionSet) {
|
|
80
|
+
let range = search.range;
|
|
81
|
+
if (range) {
|
|
82
|
+
let from = tr.mapping.map(range.from, 1);
|
|
83
|
+
let to = tr.mapping.map(range.to, -1);
|
|
84
|
+
range = from < to ? { from, to } : null;
|
|
85
|
+
}
|
|
86
|
+
search = new SearchState(
|
|
87
|
+
search.query,
|
|
88
|
+
range,
|
|
89
|
+
buildMatchDeco(state, search.query, range),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return search;
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
props: {
|
|
96
|
+
decorations: (state) => searchKey.getState(state)!.deco,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/// Get the current active search query and searched range. Will
|
|
102
|
+
/// return `undefined` is the search plugin isn't active.
|
|
103
|
+
export function getSearchState(state: EditorState): {
|
|
104
|
+
query: SearchQuery;
|
|
105
|
+
range: { from: number; to: number } | null;
|
|
106
|
+
} | undefined {
|
|
107
|
+
return searchKey.getState(state);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/// Access the decoration set holding the currently highlighted search
|
|
111
|
+
/// matches in the document.
|
|
112
|
+
export function getMatchHighlights(state: EditorState) {
|
|
113
|
+
let search = searchKey.getState(state);
|
|
114
|
+
return search ? search.deco : DecorationSet.empty;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// Add metadata to a transaction that updates the active search query
|
|
118
|
+
/// and searched range, when dispatched.
|
|
119
|
+
export function setSearchState(
|
|
120
|
+
tr: Transaction,
|
|
121
|
+
query: SearchQuery,
|
|
122
|
+
range: { from: number; to: number } | null = null,
|
|
123
|
+
) {
|
|
124
|
+
return tr.setMeta(searchKey, { query, range });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function nextMatch(
|
|
128
|
+
search: SearchState,
|
|
129
|
+
state: EditorState,
|
|
130
|
+
wrap: boolean,
|
|
131
|
+
curFrom: number,
|
|
132
|
+
curTo: number,
|
|
133
|
+
) {
|
|
134
|
+
let range = search.range || { from: 0, to: state.doc.content.size };
|
|
135
|
+
let next = search.query.findNext(
|
|
136
|
+
state,
|
|
137
|
+
Math.max(curTo, range.from),
|
|
138
|
+
range.to,
|
|
139
|
+
);
|
|
140
|
+
if (!next && wrap) {
|
|
141
|
+
next = search.query.findNext(
|
|
142
|
+
state,
|
|
143
|
+
range.from,
|
|
144
|
+
Math.min(curFrom, range.to),
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return next;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function prevMatch(
|
|
151
|
+
search: SearchState,
|
|
152
|
+
state: EditorState,
|
|
153
|
+
wrap: boolean,
|
|
154
|
+
curFrom: number,
|
|
155
|
+
curTo: number,
|
|
156
|
+
) {
|
|
157
|
+
let range = search.range || { from: 0, to: state.doc.content.size };
|
|
158
|
+
let prev = search.query.findPrev(
|
|
159
|
+
state,
|
|
160
|
+
Math.min(curFrom, range.to),
|
|
161
|
+
range.from,
|
|
162
|
+
);
|
|
163
|
+
if (!prev && wrap) {
|
|
164
|
+
prev = search.query.findPrev(state, range.to, Math.max(curTo, range.from));
|
|
165
|
+
}
|
|
166
|
+
return prev;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function findCommand(wrap: boolean, dir: -1 | 1): Command {
|
|
170
|
+
return (state, dispatch) => {
|
|
171
|
+
let search = searchKey.getState(state);
|
|
172
|
+
if (!search || !search.query.valid) return false;
|
|
173
|
+
let { from, to } = state.selection;
|
|
174
|
+
let next = dir > 0
|
|
175
|
+
? nextMatch(search, state, wrap, from, to)
|
|
176
|
+
: prevMatch(search, state, wrap, from, to);
|
|
177
|
+
if (!next) return false;
|
|
178
|
+
let selection = TextSelection.create(state.doc, next.from, next.to);
|
|
179
|
+
if (dispatch) dispatch(state.tr.setSelection(selection).scrollIntoView());
|
|
180
|
+
return true;
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/// Find the next instance of the search query after the current
|
|
185
|
+
/// selection and move the selection to it.
|
|
186
|
+
export const findNext = findCommand(true, 1);
|
|
187
|
+
|
|
188
|
+
/// Find the next instance of the search query and move the selection
|
|
189
|
+
/// to it. Don't wrap around at the end of document or search range.
|
|
190
|
+
export const findNextNoWrap = findCommand(false, 1);
|
|
191
|
+
|
|
192
|
+
/// Find the previous instance of the search query and move the
|
|
193
|
+
/// selection to it.
|
|
194
|
+
export const findPrev = findCommand(true, -1);
|
|
195
|
+
|
|
196
|
+
/// Find the previous instance of the search query and move the
|
|
197
|
+
/// selection to it. Don't wrap at the start of the document or search
|
|
198
|
+
/// range.
|
|
199
|
+
export const findPrevNoWrap = findCommand(false, -1);
|
|
200
|
+
|
|
201
|
+
function replaceCommand(wrap: boolean, moveForward: boolean): Command {
|
|
202
|
+
return (state, dispatch) => {
|
|
203
|
+
let search = searchKey.getState(state);
|
|
204
|
+
if (!search || !search.query.valid) return false;
|
|
205
|
+
let { from } = state.selection;
|
|
206
|
+
let next = nextMatch(search, state, wrap, from, from);
|
|
207
|
+
if (!next) return false;
|
|
208
|
+
|
|
209
|
+
if (!dispatch) return true;
|
|
210
|
+
if (state.selection.from == next.from && state.selection.to == next.to) {
|
|
211
|
+
let tr = state.tr,
|
|
212
|
+
replacements = search.query.getReplacements(state, next);
|
|
213
|
+
for (let i = replacements.length - 1; i >= 0; i--) {
|
|
214
|
+
let { from, to, insert } = replacements[i];
|
|
215
|
+
tr.replace(from, to, insert);
|
|
216
|
+
}
|
|
217
|
+
let after = moveForward &&
|
|
218
|
+
nextMatch(search, state, wrap, next.from, next.to);
|
|
219
|
+
if (after) {
|
|
220
|
+
tr.setSelection(
|
|
221
|
+
TextSelection.create(
|
|
222
|
+
tr.doc,
|
|
223
|
+
tr.mapping.map(after.from, 1),
|
|
224
|
+
tr.mapping.map(after.to, -1),
|
|
225
|
+
),
|
|
226
|
+
);
|
|
227
|
+
} else {
|
|
228
|
+
tr.setSelection(
|
|
229
|
+
TextSelection.create(tr.doc, next.from, tr.mapping.map(next.to, 1)),
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
dispatch(tr.scrollIntoView());
|
|
233
|
+
} else if (!moveForward) {
|
|
234
|
+
return false;
|
|
235
|
+
} else {
|
|
236
|
+
dispatch(
|
|
237
|
+
state.tr.setSelection(
|
|
238
|
+
TextSelection.create(state.doc, next.from, next.to),
|
|
239
|
+
).scrollIntoView(),
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
return true;
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/// Replace the currently selected instance of the search query, and
|
|
247
|
+
/// move to the next one. Or select the next match, if none is already
|
|
248
|
+
/// selected.
|
|
249
|
+
export const replaceNext = replaceCommand(true, true);
|
|
250
|
+
|
|
251
|
+
/// Replace the next instance of the search query. Don't wrap around
|
|
252
|
+
/// at the end of the document.
|
|
253
|
+
export const replaceNextNoWrap = replaceCommand(false, true);
|
|
254
|
+
|
|
255
|
+
/// Replace the currently selected instance of the search query, if
|
|
256
|
+
/// any, and keep it selected.
|
|
257
|
+
export const replaceCurrent = replaceCommand(false, false);
|
|
258
|
+
|
|
259
|
+
/// Replace all instances of the search query.
|
|
260
|
+
export const replaceAll: Command = (state, dispatch) => {
|
|
261
|
+
let search = searchKey.getState(state);
|
|
262
|
+
if (!search) return false;
|
|
263
|
+
let matches: SearchResult[] = [],
|
|
264
|
+
range = search.range || { from: 0, to: state.doc.content.size };
|
|
265
|
+
for (let pos = range.from;;) {
|
|
266
|
+
let next = search.query.findNext(state, pos, range.to);
|
|
267
|
+
if (!next) break;
|
|
268
|
+
matches.push(next);
|
|
269
|
+
pos = next.to;
|
|
270
|
+
}
|
|
271
|
+
if (dispatch) {
|
|
272
|
+
let tr = state.tr;
|
|
273
|
+
for (let i = matches.length - 1; i >= 0; i--) {
|
|
274
|
+
let match = matches[i];
|
|
275
|
+
let replacements = search.query.getReplacements(state, match);
|
|
276
|
+
for (let j = replacements.length - 1; j >= 0; j--) {
|
|
277
|
+
let { from, to, insert } = replacements[j];
|
|
278
|
+
tr.replace(from, to, insert);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
dispatch(tr);
|
|
282
|
+
}
|
|
283
|
+
return true;
|
|
284
|
+
};
|