@tiptap/extension-find-and-replace 3.33.1 → 3.35.0
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.cjs +174 -50
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -5
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +23 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +175 -51
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/find-and-replace.test.ts +142 -0
- package/src/find-and-replace.ts +4 -7
- package/src/plugin/plugin-state.ts +2 -0
- package/src/plugin/plugin.ts +1 -8
- package/src/search/group-continued-textblocks.ts +39 -0
- package/src/search/has-block-between.test.ts +27 -0
- package/src/search/has-block-between.ts +28 -0
- package/src/search/search.test.ts +100 -0
- package/src/search/search.ts +18 -9
- package/src/search/text-segments.ts +18 -11
- package/src/search/textblock-search.ts +13 -7
- package/src/search/types.ts +3 -2
- package/src/types.ts +16 -0
- package/src/utils/addContinuedTextblocks.test.ts +99 -0
- package/src/utils/addContinuedTextblocks.ts +74 -0
- package/src/utils/createState.ts +6 -2
- package/src/utils/findAdjacentTextblock.ts +14 -0
- package/src/utils/findNearestTextblock.ts +21 -0
- package/src/utils/getChangedTextblocks.test.ts +40 -0
- package/src/utils/getChangedTextblocks.ts +54 -6
- package/src/utils/refreshDocumentState.ts +12 -6
- package/src/utils/resultInTextblocks.test.ts +25 -0
- package/src/utils/resultInTextblocks.ts +2 -2
- package/src/utils/updateState.ts +7 -8
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,7 @@ let _tiptap_core = require("@tiptap/core");
|
|
|
6
6
|
let _tiptap_pm_state = require("@tiptap/pm/state");
|
|
7
7
|
let re2js = require("re2js");
|
|
8
8
|
let _tiptap_pm_view = require("@tiptap/pm/view");
|
|
9
|
+
let _tiptap_pm_transform = require("@tiptap/pm/transform");
|
|
9
10
|
//#region src/state/debounced-search-state.ts
|
|
10
11
|
const debouncedSearchState = /* @__PURE__ */ new WeakMap();
|
|
11
12
|
function getDebouncedSearchState(editor) {
|
|
@@ -51,6 +52,50 @@ const style = `.find-and-replace-result {
|
|
|
51
52
|
}
|
|
52
53
|
`;
|
|
53
54
|
//#endregion
|
|
55
|
+
//#region src/search/has-block-between.ts
|
|
56
|
+
/**
|
|
57
|
+
* Checks if a whole node lies between two positions.
|
|
58
|
+
* Nodes that only open or close in the range, like the parents of two textblocks, do not count.
|
|
59
|
+
* @param doc The document to check.
|
|
60
|
+
* @param from The start of the range.
|
|
61
|
+
* @param to The end of the range.
|
|
62
|
+
* @returns Whether a node lies fully inside the range.
|
|
63
|
+
*/
|
|
64
|
+
function hasBlockBetween(doc, from, to) {
|
|
65
|
+
let found = false;
|
|
66
|
+
doc.nodesBetween(from, to, (node, pos) => {
|
|
67
|
+
if (found) return false;
|
|
68
|
+
if (pos >= from && pos + node.nodeSize <= to) {
|
|
69
|
+
found = true;
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
});
|
|
74
|
+
return found;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/search/group-continued-textblocks.ts
|
|
78
|
+
/**
|
|
79
|
+
* Groups textblocks that continue the textblock before them in the list.
|
|
80
|
+
* @param doc The document the textblocks are in.
|
|
81
|
+
* @param textblocks Whole chains of continued textblocks in document order.
|
|
82
|
+
* @param continuesNode Whether a textblock continues the one before it.
|
|
83
|
+
* @returns The groups to search as one text each.
|
|
84
|
+
*/
|
|
85
|
+
function groupContinuedTextblocks(doc, textblocks, continuesNode = () => false) {
|
|
86
|
+
const groups = [];
|
|
87
|
+
textblocks.forEach((textblock) => {
|
|
88
|
+
const group = groups.at(-1);
|
|
89
|
+
const previous = group === null || group === void 0 ? void 0 : group.at(-1);
|
|
90
|
+
if (group && previous && continuesNode(textblock.node) && !hasBlockBetween(doc, previous.pos + previous.node.nodeSize, textblock.pos)) {
|
|
91
|
+
group.push(textblock);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
groups.push([textblock]);
|
|
95
|
+
});
|
|
96
|
+
return groups;
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
54
99
|
//#region src/search/regex.ts
|
|
55
100
|
const escapeRegExp = (value) => {
|
|
56
101
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -115,20 +160,22 @@ function findMatches(regex, text) {
|
|
|
115
160
|
}
|
|
116
161
|
//#endregion
|
|
117
162
|
//#region src/search/text-segments.ts
|
|
118
|
-
function getTextSegments(
|
|
163
|
+
function getTextSegments(textblocks) {
|
|
119
164
|
const segments = [];
|
|
120
165
|
let textOffset = 0;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
166
|
+
textblocks.forEach(({ node, pos }) => {
|
|
167
|
+
node.forEach((child, offset) => {
|
|
168
|
+
var _child$text;
|
|
169
|
+
const text = child.isText ? (_child$text = child.text) !== null && _child$text !== void 0 ? _child$text : "" : "\n";
|
|
170
|
+
segments.push({
|
|
171
|
+
isText: child.isText,
|
|
172
|
+
pos: pos + 1 + offset,
|
|
173
|
+
length: child.nodeSize,
|
|
174
|
+
text,
|
|
175
|
+
textOffset
|
|
176
|
+
});
|
|
177
|
+
textOffset += text.length;
|
|
130
178
|
});
|
|
131
|
-
textOffset += text.length;
|
|
132
179
|
});
|
|
133
180
|
return segments;
|
|
134
181
|
}
|
|
@@ -148,13 +195,16 @@ function offsetToPos(segments, offset) {
|
|
|
148
195
|
const segment = findOffsetSegment(segments, offset);
|
|
149
196
|
return segment ? segment.pos + Math.min(offset - segment.textOffset, segment.length) : 0;
|
|
150
197
|
}
|
|
198
|
+
function endOffsetToPos(segments, offset) {
|
|
199
|
+
return offsetToPos(segments, offset - 1) + 1;
|
|
200
|
+
}
|
|
151
201
|
function overlapsNonTextSegment(segments, from, to) {
|
|
152
202
|
return segments.some((segment) => !segment.isText && from < segment.textOffset + segment.text.length && to > segment.textOffset);
|
|
153
203
|
}
|
|
154
204
|
//#endregion
|
|
155
205
|
//#region src/search/textblock-search.ts
|
|
156
|
-
function
|
|
157
|
-
const segments = getTextSegments(
|
|
206
|
+
function searchTextblockGroup(regex, textblocks) {
|
|
207
|
+
const segments = getTextSegments(textblocks);
|
|
158
208
|
const text = segments.map((segment) => segment.text).join("");
|
|
159
209
|
const results = [];
|
|
160
210
|
for (const match of findMatches(regex, text)) {
|
|
@@ -163,7 +213,7 @@ function searchTextblock(regex, textblock, pos) {
|
|
|
163
213
|
if (overlapsNonTextSegment(segments, match.index, matchEnd)) continue;
|
|
164
214
|
results.push({
|
|
165
215
|
from: offsetToPos(segments, match.index),
|
|
166
|
-
to:
|
|
216
|
+
to: endOffsetToPos(segments, matchEnd)
|
|
167
217
|
});
|
|
168
218
|
}
|
|
169
219
|
return results;
|
|
@@ -172,22 +222,25 @@ function searchTextblock(regex, textblock, pos) {
|
|
|
172
222
|
//#region src/search/search.ts
|
|
173
223
|
/**
|
|
174
224
|
* Searches textblocks for a term and returns their matched ranges.
|
|
175
|
-
*
|
|
225
|
+
* Textblocks joined by `continuesNode` are searched as one text when `doc` is given.
|
|
226
|
+
* @param textblocks The textblocks and their document positions. Include every textblock of a continued chain.
|
|
176
227
|
* @param term The search term, treated as regex source when `useRegex` is enabled.
|
|
177
|
-
* @param options Case sensitivity, regex mode,
|
|
228
|
+
* @param options Case sensitivity, regex mode, whole word matching, and continued textblocks.
|
|
229
|
+
* @param doc The document the textblocks are in. Needed to join continued textblocks.
|
|
178
230
|
* @returns The list of matched ranges in textblock order.
|
|
179
231
|
*/
|
|
180
|
-
function searchTextblocks(textblocks, term, options) {
|
|
232
|
+
function searchTextblocks(textblocks, term, options, doc) {
|
|
181
233
|
const regex = createSearchRegex(term, options);
|
|
182
234
|
if (!regex) return [];
|
|
183
|
-
return
|
|
235
|
+
return (doc ? groupContinuedTextblocks(doc, textblocks, options.continuesNode) : textblocks.map((textblock) => [textblock])).flatMap((group) => searchTextblockGroup(regex, group));
|
|
184
236
|
}
|
|
185
237
|
/**
|
|
186
238
|
* Searches all textblocks of a document for a term and returns the matched ranges.
|
|
187
|
-
* Matches may span multiple text nodes (e.g. across marks), but
|
|
239
|
+
* Matches may span multiple text nodes (e.g. across marks), but only leave a textblock
|
|
240
|
+
* when `continuesNode` joins it with the next one.
|
|
188
241
|
* @param doc The document to search in.
|
|
189
242
|
* @param term The search term, treated as regex source when `useRegex` is enabled.
|
|
190
|
-
* @param options Case sensitivity, regex mode,
|
|
243
|
+
* @param options Case sensitivity, regex mode, whole word matching, and continued textblocks.
|
|
191
244
|
* @returns The list of matched ranges in document order.
|
|
192
245
|
*/
|
|
193
246
|
function searchDocument(doc, term, options) {
|
|
@@ -200,7 +253,7 @@ function searchDocument(doc, term, options) {
|
|
|
200
253
|
});
|
|
201
254
|
return false;
|
|
202
255
|
});
|
|
203
|
-
return searchTextblocks(textblocks, term, options);
|
|
256
|
+
return searchTextblocks(textblocks, term, options, doc);
|
|
204
257
|
}
|
|
205
258
|
/**
|
|
206
259
|
* Finds the index of the first result at or after a position, wrapping around
|
|
@@ -237,6 +290,7 @@ function createState(doc, options) {
|
|
|
237
290
|
caseSensitive: options.caseSensitive,
|
|
238
291
|
useRegex: options.useRegex,
|
|
239
292
|
wholeWord: options.wholeWord,
|
|
293
|
+
continuesNode: options.continuesNode,
|
|
240
294
|
results,
|
|
241
295
|
currentIndex,
|
|
242
296
|
currentResultVisited: false,
|
|
@@ -280,11 +334,11 @@ function resultInTextblocks(result, textblocks) {
|
|
|
280
334
|
while (start <= end) {
|
|
281
335
|
const index = Math.floor((start + end) / 2);
|
|
282
336
|
const textblock = textblocks[index];
|
|
283
|
-
if (result.
|
|
337
|
+
if (result.to <= textblock.from) {
|
|
284
338
|
end = index - 1;
|
|
285
339
|
continue;
|
|
286
340
|
}
|
|
287
|
-
if (result.
|
|
341
|
+
if (result.from >= textblock.to) {
|
|
288
342
|
start = index + 1;
|
|
289
343
|
continue;
|
|
290
344
|
}
|
|
@@ -394,26 +448,102 @@ function refreshCurrentIndexState(previousState, state, doc, meta) {
|
|
|
394
448
|
};
|
|
395
449
|
}
|
|
396
450
|
//#endregion
|
|
451
|
+
//#region src/utils/findNearestTextblock.ts
|
|
452
|
+
function findNearestTextblock(doc, pos, direction) {
|
|
453
|
+
const selection = _tiptap_pm_state.Selection.findFrom(doc.resolve(pos), direction, true);
|
|
454
|
+
if (!selection) return null;
|
|
455
|
+
const node = selection.$from.parent;
|
|
456
|
+
const from = selection.$from.before();
|
|
457
|
+
return {
|
|
458
|
+
node,
|
|
459
|
+
pos: from,
|
|
460
|
+
from,
|
|
461
|
+
to: from + node.nodeSize
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
//#endregion
|
|
465
|
+
//#region src/utils/findAdjacentTextblock.ts
|
|
466
|
+
function findAdjacentTextblock(doc, textblock, direction) {
|
|
467
|
+
return findNearestTextblock(doc, direction === 1 ? textblock.to : textblock.from, direction);
|
|
468
|
+
}
|
|
469
|
+
//#endregion
|
|
470
|
+
//#region src/utils/addContinuedTextblocks.ts
|
|
471
|
+
function findJoinedTextblock(doc, textblock, direction, continuesNode) {
|
|
472
|
+
const adjacent = findAdjacentTextblock(doc, textblock, direction);
|
|
473
|
+
if (!adjacent) return null;
|
|
474
|
+
const [first, second] = direction === 1 ? [textblock, adjacent] : [adjacent, textblock];
|
|
475
|
+
if (!continuesNode(second.node) || hasBlockBetween(doc, first.to, second.from)) return null;
|
|
476
|
+
return adjacent;
|
|
477
|
+
}
|
|
478
|
+
function addChain(doc, textblock, continuesNode, found) {
|
|
479
|
+
let previous = findJoinedTextblock(doc, textblock, -1, continuesNode);
|
|
480
|
+
while (previous) {
|
|
481
|
+
found.set(previous.pos, previous);
|
|
482
|
+
previous = findJoinedTextblock(doc, previous, -1, continuesNode);
|
|
483
|
+
}
|
|
484
|
+
let next = findJoinedTextblock(doc, textblock, 1, continuesNode);
|
|
485
|
+
while (next) {
|
|
486
|
+
found.set(next.pos, next);
|
|
487
|
+
next = findJoinedTextblock(doc, next, 1, continuesNode);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Adds the textblocks that are joined with the given ones by `continuesNode`.
|
|
492
|
+
* @param doc The document the textblocks are in.
|
|
493
|
+
* @param textblocks The textblocks in document order.
|
|
494
|
+
* @param continuesNode Whether a textblock continues the one before it.
|
|
495
|
+
* @returns The textblocks with their whole chains, in document order.
|
|
496
|
+
*/
|
|
497
|
+
function addContinuedTextblocks(doc, textblocks, continuesNode) {
|
|
498
|
+
const found = /* @__PURE__ */ new Map();
|
|
499
|
+
textblocks.forEach((textblock) => {
|
|
500
|
+
if (found.has(textblock.pos)) return;
|
|
501
|
+
found.set(textblock.pos, textblock);
|
|
502
|
+
addChain(doc, textblock, continuesNode, found);
|
|
503
|
+
});
|
|
504
|
+
return [...found.values()].sort((first, second) => first.pos - second.pos);
|
|
505
|
+
}
|
|
506
|
+
//#endregion
|
|
397
507
|
//#region src/utils/getChangedTextblocks.ts
|
|
398
|
-
function
|
|
508
|
+
function addTextblock(textblocks, node, pos) {
|
|
509
|
+
const textblock = {
|
|
510
|
+
node,
|
|
511
|
+
pos,
|
|
512
|
+
from: pos,
|
|
513
|
+
to: pos + node.nodeSize
|
|
514
|
+
};
|
|
515
|
+
textblocks.set(pos, textblock);
|
|
516
|
+
return textblock;
|
|
517
|
+
}
|
|
518
|
+
function addAttributeChanges(doc, transform, textblocks) {
|
|
519
|
+
transform.steps.forEach((step, index) => {
|
|
520
|
+
if (!(step instanceof _tiptap_pm_transform.AttrStep)) return;
|
|
521
|
+
const pos = transform.mapping.slice(index + 1).map(step.pos);
|
|
522
|
+
const node = doc.nodeAt(pos);
|
|
523
|
+
if (!(node === null || node === void 0 ? void 0 : node.isTextblock)) return;
|
|
524
|
+
const previous = findAdjacentTextblock(doc, addTextblock(textblocks, node, pos), -1);
|
|
525
|
+
if (previous) textblocks.set(previous.pos, previous);
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
function getChangedTextblocks(doc, transform) {
|
|
529
|
+
const { mapping } = transform;
|
|
399
530
|
const textblocks = /* @__PURE__ */ new Map();
|
|
400
531
|
mapping.maps.forEach((stepMap, index) => {
|
|
401
532
|
const remaining = mapping.slice(index + 1);
|
|
402
533
|
stepMap.forEach((_oldStart, _oldEnd, newStart, newEnd) => {
|
|
403
|
-
|
|
404
|
-
const
|
|
534
|
+
var _before$from, _after$to;
|
|
535
|
+
const before = findNearestTextblock(doc, remaining.map(newStart, -1), -1);
|
|
536
|
+
const after = findNearestTextblock(doc, remaining.map(newEnd, 1), 1);
|
|
537
|
+
const from = (_before$from = before === null || before === void 0 ? void 0 : before.from) !== null && _before$from !== void 0 ? _before$from : 0;
|
|
538
|
+
const to = (_after$to = after === null || after === void 0 ? void 0 : after.to) !== null && _after$to !== void 0 ? _after$to : doc.content.size;
|
|
405
539
|
doc.nodesBetween(from, to, (node, pos) => {
|
|
406
540
|
if (!node.isTextblock) return true;
|
|
407
|
-
textblocks
|
|
408
|
-
node,
|
|
409
|
-
pos,
|
|
410
|
-
from: pos,
|
|
411
|
-
to: pos + node.nodeSize
|
|
412
|
-
});
|
|
541
|
+
addTextblock(textblocks, node, pos);
|
|
413
542
|
return false;
|
|
414
543
|
});
|
|
415
544
|
});
|
|
416
545
|
});
|
|
546
|
+
addAttributeChanges(doc, transform, textblocks);
|
|
417
547
|
return [...textblocks.values()].sort((first, second) => first.pos - second.pos);
|
|
418
548
|
}
|
|
419
549
|
//#endregion
|
|
@@ -458,9 +588,10 @@ function mergeResults(first, second) {
|
|
|
458
588
|
}
|
|
459
589
|
//#endregion
|
|
460
590
|
//#region src/utils/refreshDocumentState.ts
|
|
461
|
-
function refreshDocumentState(previousState, state, doc, meta,
|
|
462
|
-
const
|
|
463
|
-
const
|
|
591
|
+
function refreshDocumentState(previousState, state, doc, meta, transform) {
|
|
592
|
+
const { mapping } = transform;
|
|
593
|
+
const textblocks = addContinuedTextblocks(doc, getChangedTextblocks(doc, transform), state.continuesNode);
|
|
594
|
+
const results = mergeResults(mapResults(previousState.results, mapping).filter((result) => !resultInTextblocks(result, textblocks)), searchTextblocks(textblocks, state.searchTerm, state, doc));
|
|
464
595
|
const currentIndex = resolveCurrentIndex(previousState, results, meta, true, mapping);
|
|
465
596
|
const previousCurrent = previousState.currentIndex === null ? null : mapResult(previousState.results[previousState.currentIndex], mapping);
|
|
466
597
|
const current = currentIndex === null ? null : results[currentIndex];
|
|
@@ -503,9 +634,9 @@ function touchesSearch(meta) {
|
|
|
503
634
|
}
|
|
504
635
|
//#endregion
|
|
505
636
|
//#region src/utils/updateState.ts
|
|
506
|
-
function updateState(previousState, state, doc, meta,
|
|
507
|
-
if (touchesSearch(meta)) return refreshState(previousState, state, doc, meta, docChanged, mapping);
|
|
508
|
-
if (docChanged) return refreshDocumentState(previousState, state, doc, meta,
|
|
637
|
+
function updateState(previousState, state, doc, meta, transform) {
|
|
638
|
+
if (touchesSearch(meta)) return refreshState(previousState, state, doc, meta, transform.docChanged, transform.mapping);
|
|
639
|
+
if (transform.docChanged) return refreshDocumentState(previousState, state, doc, meta, transform);
|
|
509
640
|
if (hasCurrentIndex(meta)) return refreshCurrentIndexState(previousState, state, doc, meta);
|
|
510
641
|
return state;
|
|
511
642
|
}
|
|
@@ -528,7 +659,7 @@ const FindAndReplacePlugin = (options, onCreate) => {
|
|
|
528
659
|
return updateState(state, {
|
|
529
660
|
...state,
|
|
530
661
|
...meta
|
|
531
|
-
}, newState.doc, meta, tr
|
|
662
|
+
}, newState.doc, meta, tr);
|
|
532
663
|
}
|
|
533
664
|
},
|
|
534
665
|
props: { decorations(state) {
|
|
@@ -653,11 +784,7 @@ function getPendingAwarePluginState(state, pendingTerm) {
|
|
|
653
784
|
const pluginState = FindAndReplacePluginKey.getState(state);
|
|
654
785
|
if (!pluginState) return null;
|
|
655
786
|
if (pendingTerm === null) return pluginState.results.length === 0 ? null : pluginState;
|
|
656
|
-
const results = searchDocument(state.doc, pendingTerm,
|
|
657
|
-
caseSensitive: pluginState.caseSensitive,
|
|
658
|
-
useRegex: pluginState.useRegex,
|
|
659
|
-
wholeWord: pluginState.wholeWord
|
|
660
|
-
});
|
|
787
|
+
const results = searchDocument(state.doc, pendingTerm, pluginState);
|
|
661
788
|
if (results.length === 0) return null;
|
|
662
789
|
return {
|
|
663
790
|
...pluginState,
|
|
@@ -688,13 +815,9 @@ function selectResult(tr, result, index) {
|
|
|
688
815
|
tr.scrollIntoView();
|
|
689
816
|
}
|
|
690
817
|
function replaceResult(tr, pluginState, result) {
|
|
691
|
-
const { searchTerm, replaceTerm
|
|
818
|
+
const { searchTerm, replaceTerm } = pluginState;
|
|
692
819
|
tr.insertText(replaceTerm, result.from, result.to);
|
|
693
|
-
const results = searchDocument(tr.doc, searchTerm,
|
|
694
|
-
caseSensitive,
|
|
695
|
-
useRegex,
|
|
696
|
-
wholeWord
|
|
697
|
-
});
|
|
820
|
+
const results = searchDocument(tr.doc, searchTerm, pluginState);
|
|
698
821
|
const nextIndex = findNextIndex(results, result.from + replaceTerm.length);
|
|
699
822
|
if (nextIndex === null) {
|
|
700
823
|
mergePluginMeta(tr, { currentIndex: null });
|
|
@@ -711,6 +834,7 @@ const FindAndReplace = _tiptap_core.Extension.create({
|
|
|
711
834
|
caseSensitive: false,
|
|
712
835
|
useRegex: false,
|
|
713
836
|
wholeWord: false,
|
|
837
|
+
continuesNode: () => false,
|
|
714
838
|
searchDebounceMs: 250,
|
|
715
839
|
injectCSS: true,
|
|
716
840
|
injectNonce: void 0
|