@kerebron/editor 0.2.1 → 0.3.1

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