@tiptap/extension-find-and-replace 3.33.0 → 3.34.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.
Files changed (37) hide show
  1. package/dist/index.cjs +219 -79
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +25 -5
  4. package/dist/index.d.cts.map +1 -1
  5. package/dist/index.d.ts +25 -5
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +220 -80
  8. package/dist/index.js.map +1 -1
  9. package/package.json +5 -5
  10. package/src/find-and-replace.test.ts +293 -0
  11. package/src/find-and-replace.ts +52 -71
  12. package/src/plugin/plugin-state.ts +4 -0
  13. package/src/plugin/plugin.ts +1 -8
  14. package/src/search/group-continued-textblocks.ts +39 -0
  15. package/src/search/has-block-between.test.ts +27 -0
  16. package/src/search/has-block-between.ts +28 -0
  17. package/src/search/search.test.ts +100 -0
  18. package/src/search/search.ts +18 -9
  19. package/src/search/text-segments.ts +18 -11
  20. package/src/search/textblock-search.ts +13 -7
  21. package/src/search/types.ts +3 -2
  22. package/src/types.ts +16 -0
  23. package/src/utils/addContinuedTextblocks.test.ts +99 -0
  24. package/src/utils/addContinuedTextblocks.ts +74 -0
  25. package/src/utils/createState.ts +7 -2
  26. package/src/utils/findAdjacentTextblock.ts +14 -0
  27. package/src/utils/findNearestTextblock.ts +21 -0
  28. package/src/utils/getChangedTextblocks.test.ts +40 -0
  29. package/src/utils/getChangedTextblocks.ts +54 -6
  30. package/src/utils/getSearchCurrentIndex.ts +18 -1
  31. package/src/utils/keepsVisitedResult.ts +16 -0
  32. package/src/utils/refreshCurrentIndexState.ts +1 -0
  33. package/src/utils/refreshDocumentState.ts +17 -6
  34. package/src/utils/refreshState.ts +12 -0
  35. package/src/utils/resultInTextblocks.test.ts +25 -0
  36. package/src/utils/resultInTextblocks.ts +2 -2
  37. 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(textblock, pos) {
163
+ function getTextSegments(textblocks) {
119
164
  const segments = [];
120
165
  let textOffset = 0;
121
- textblock.forEach((child, offset) => {
122
- var _child$text;
123
- const text = child.isText ? (_child$text = child.text) !== null && _child$text !== void 0 ? _child$text : "" : "\n";
124
- segments.push({
125
- isText: child.isText,
126
- pos: pos + 1 + offset,
127
- length: child.nodeSize,
128
- text,
129
- textOffset
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 searchTextblock(regex, textblock, pos) {
157
- const segments = getTextSegments(textblock, pos);
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: offsetToPos(segments, matchEnd)
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
- * @param textblocks The textblocks and their document positions.
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, and whole word matching.
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 textblocks.flatMap(({ node, pos }) => searchTextblock(regex, node, pos));
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 never leave a textblock.
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, and whole word matching.
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,8 +290,10 @@ 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,
296
+ currentResultVisited: false,
242
297
  decorations: createDecorations(doc, results, currentIndex)
243
298
  };
244
299
  }
@@ -279,11 +334,11 @@ function resultInTextblocks(result, textblocks) {
279
334
  while (start <= end) {
280
335
  const index = Math.floor((start + end) / 2);
281
336
  const textblock = textblocks[index];
282
- if (result.from < textblock.from) {
337
+ if (result.to <= textblock.from) {
283
338
  end = index - 1;
284
339
  continue;
285
340
  }
286
- if (result.to > textblock.to) {
341
+ if (result.from >= textblock.to) {
287
342
  start = index + 1;
288
343
  continue;
289
344
  }
@@ -361,9 +416,16 @@ function isNewSearch(meta) {
361
416
  //#endregion
362
417
  //#region src/utils/getSearchCurrentIndex.ts
363
418
  function getSearchCurrentIndex(previousState, results, meta, docChanged, mapping) {
419
+ var _ref;
364
420
  if (isNewSearch(meta)) return 0;
365
421
  const mappedIndex = getMappedCurrentIndex(previousState, results, docChanged, mapping);
366
- return mappedIndex !== null && mappedIndex !== void 0 ? mappedIndex : getFallbackCurrentIndex(previousState, results.length);
422
+ return (_ref = mappedIndex !== null && mappedIndex !== void 0 ? mappedIndex : findPreviousCurrentIndex(previousState, results)) !== null && _ref !== void 0 ? _ref : getFallbackCurrentIndex(previousState, results.length);
423
+ }
424
+ function findPreviousCurrentIndex(previousState, results) {
425
+ var _getPreviousCurrentRe;
426
+ const previousCurrent = (_getPreviousCurrentRe = getPreviousCurrentResult(previousState)) !== null && _getPreviousCurrentRe !== void 0 ? _getPreviousCurrentRe : null;
427
+ const index = results.findIndex((result) => isCurrentResult(result, previousCurrent));
428
+ return index === -1 ? null : index;
367
429
  }
368
430
  //#endregion
369
431
  //#region src/utils/resolveCurrentIndex.ts
@@ -381,33 +443,115 @@ function refreshCurrentIndexState(previousState, state, doc, meta) {
381
443
  return {
382
444
  ...state,
383
445
  currentIndex,
446
+ currentResultVisited: true,
384
447
  decorations: refreshDecorations(previousState.decorations, doc, state.results, currentIndex, [], previousCurrent)
385
448
  };
386
449
  }
387
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
388
507
  //#region src/utils/getChangedTextblocks.ts
389
- function getChangedTextblocks(doc, mapping) {
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;
390
530
  const textblocks = /* @__PURE__ */ new Map();
391
531
  mapping.maps.forEach((stepMap, index) => {
392
532
  const remaining = mapping.slice(index + 1);
393
533
  stepMap.forEach((_oldStart, _oldEnd, newStart, newEnd) => {
394
- const from = Math.max(0, remaining.map(newStart, -1) - 1);
395
- const to = Math.min(doc.content.size, remaining.map(newEnd, 1) + 1);
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;
396
539
  doc.nodesBetween(from, to, (node, pos) => {
397
540
  if (!node.isTextblock) return true;
398
- textblocks.set(pos, {
399
- node,
400
- pos,
401
- from: pos,
402
- to: pos + node.nodeSize
403
- });
541
+ addTextblock(textblocks, node, pos);
404
542
  return false;
405
543
  });
406
544
  });
407
545
  });
546
+ addAttributeChanges(doc, transform, textblocks);
408
547
  return [...textblocks.values()].sort((first, second) => first.pos - second.pos);
409
548
  }
410
549
  //#endregion
550
+ //#region src/utils/keepsVisitedResult.ts
551
+ function keepsVisitedResult(previousState, current, previousCurrent) {
552
+ return previousState.currentResultVisited && current !== null && isCurrentResult(current, previousCurrent);
553
+ }
554
+ //#endregion
411
555
  //#region src/utils/mapResult.ts
412
556
  function mapResult(result, mapping) {
413
557
  const from = mapping.map(result.from, 1);
@@ -444,28 +588,36 @@ function mergeResults(first, second) {
444
588
  }
445
589
  //#endregion
446
590
  //#region src/utils/refreshDocumentState.ts
447
- function refreshDocumentState(previousState, state, doc, meta, mapping) {
448
- const textblocks = getChangedTextblocks(doc, mapping);
449
- const results = mergeResults(mapResults(previousState.results, mapping).filter((result) => !resultInTextblocks(result, textblocks)), searchTextblocks(textblocks, state.searchTerm, state));
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));
450
595
  const currentIndex = resolveCurrentIndex(previousState, results, meta, true, mapping);
451
596
  const previousCurrent = previousState.currentIndex === null ? null : mapResult(previousState.results[previousState.currentIndex], mapping);
597
+ const current = currentIndex === null ? null : results[currentIndex];
452
598
  const decorations = previousState.decorations.map(mapping, doc);
453
599
  return {
454
600
  ...state,
455
601
  results,
456
602
  currentIndex,
603
+ currentResultVisited: hasCurrentIndex(meta) || keepsVisitedResult(previousState, current, previousCurrent),
457
604
  decorations: refreshDecorations(decorations, doc, results, currentIndex, textblocks, previousCurrent)
458
605
  };
459
606
  }
460
607
  //#endregion
461
608
  //#region src/utils/refreshState.ts
462
609
  function refreshState(previousState, state, doc, meta, docChanged, mapping) {
610
+ var _getPreviousCurrentRe;
463
611
  const results = searchDocument(doc, state.searchTerm, state);
464
612
  const currentIndex = resolveCurrentIndex(previousState, results, meta, docChanged, mapping);
613
+ const current = currentIndex === null ? null : results[currentIndex];
614
+ const previousResult = (_getPreviousCurrentRe = getPreviousCurrentResult(previousState)) !== null && _getPreviousCurrentRe !== void 0 ? _getPreviousCurrentRe : null;
615
+ const previousCurrent = previousResult && mapping ? mapResult(previousResult, mapping) : previousResult;
465
616
  return {
466
617
  ...state,
467
618
  results,
468
619
  currentIndex,
620
+ currentResultVisited: hasCurrentIndex(meta) || !isNewSearch(meta) && keepsVisitedResult(previousState, current, previousCurrent),
469
621
  decorations: createDecorations(doc, results, currentIndex)
470
622
  };
471
623
  }
@@ -482,9 +634,9 @@ function touchesSearch(meta) {
482
634
  }
483
635
  //#endregion
484
636
  //#region src/utils/updateState.ts
485
- function updateState(previousState, state, doc, meta, docChanged, mapping) {
486
- if (touchesSearch(meta)) return refreshState(previousState, state, doc, meta, docChanged, mapping);
487
- if (docChanged) return refreshDocumentState(previousState, state, doc, meta, mapping);
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);
488
640
  if (hasCurrentIndex(meta)) return refreshCurrentIndexState(previousState, state, doc, meta);
489
641
  return state;
490
642
  }
@@ -507,7 +659,7 @@ const FindAndReplacePlugin = (options, onCreate) => {
507
659
  return updateState(state, {
508
660
  ...state,
509
661
  ...meta
510
- }, newState.doc, meta, tr.docChanged, tr.mapping);
662
+ }, newState.doc, meta, tr);
511
663
  }
512
664
  },
513
665
  props: { decorations(state) {
@@ -632,26 +784,30 @@ function getPendingAwarePluginState(state, pendingTerm) {
632
784
  const pluginState = FindAndReplacePluginKey.getState(state);
633
785
  if (!pluginState) return null;
634
786
  if (pendingTerm === null) return pluginState.results.length === 0 ? null : pluginState;
635
- const results = searchDocument(state.doc, pendingTerm, {
636
- caseSensitive: pluginState.caseSensitive,
637
- useRegex: pluginState.useRegex,
638
- wholeWord: pluginState.wholeWord
639
- });
787
+ const results = searchDocument(state.doc, pendingTerm, pluginState);
640
788
  if (results.length === 0) return null;
641
789
  return {
642
790
  ...pluginState,
643
791
  searchTerm: pendingTerm,
644
792
  results,
645
- currentIndex: 0
793
+ currentIndex: 0,
794
+ currentResultVisited: false
646
795
  };
647
796
  }
797
+ function flushPendingSearch(editor, tr, state, dispatch) {
798
+ const pendingTerm = dispatch ? consumePendingSearch(editor) : null;
799
+ if (dispatch && pendingTerm !== null) mergePluginMeta(tr, { searchTerm: pendingTerm });
800
+ return getPendingAwarePluginState(state, pendingTerm);
801
+ }
648
802
  function currentResult(pluginState) {
649
803
  var _pluginState$currentI;
650
804
  return pluginState.results[(_pluginState$currentI = pluginState.currentIndex) !== null && _pluginState$currentI !== void 0 ? _pluginState$currentI : 0];
651
805
  }
652
- function shiftIndex(currentIndex, length, direction) {
653
- if (currentIndex === null) return direction === 1 ? 0 : length - 1;
654
- return (currentIndex + direction + length) % length;
806
+ function shiftIndex(pluginState, direction) {
807
+ const { currentIndex, currentResultVisited, results } = pluginState;
808
+ if (currentIndex === null) return direction === 1 ? 0 : results.length - 1;
809
+ if (!currentResultVisited) return currentIndex;
810
+ return (currentIndex + direction + results.length) % results.length;
655
811
  }
656
812
  function selectResult(tr, result, index) {
657
813
  tr.setSelection(_tiptap_pm_state.TextSelection.create(tr.doc, result.from, result.to));
@@ -659,13 +815,9 @@ function selectResult(tr, result, index) {
659
815
  tr.scrollIntoView();
660
816
  }
661
817
  function replaceResult(tr, pluginState, result) {
662
- const { searchTerm, replaceTerm, caseSensitive, useRegex, wholeWord } = pluginState;
818
+ const { searchTerm, replaceTerm } = pluginState;
663
819
  tr.insertText(replaceTerm, result.from, result.to);
664
- const results = searchDocument(tr.doc, searchTerm, {
665
- caseSensitive,
666
- useRegex,
667
- wholeWord
668
- });
820
+ const results = searchDocument(tr.doc, searchTerm, pluginState);
669
821
  const nextIndex = findNextIndex(results, result.from + replaceTerm.length);
670
822
  if (nextIndex === null) {
671
823
  mergePluginMeta(tr, { currentIndex: null });
@@ -682,6 +834,7 @@ const FindAndReplace = _tiptap_core.Extension.create({
682
834
  caseSensitive: false,
683
835
  useRegex: false,
684
836
  wholeWord: false,
837
+ continuesNode: () => false,
685
838
  searchDebounceMs: 250,
686
839
  injectCSS: true,
687
840
  injectNonce: void 0
@@ -739,6 +892,13 @@ const FindAndReplace = _tiptap_core.Extension.create({
739
892
  }, this.options.searchDebounceMs);
740
893
  return true;
741
894
  };
895
+ const goToResult = (direction) => ({ tr, state, dispatch }) => {
896
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch);
897
+ if (!pluginState) return false;
898
+ const index = shiftIndex(pluginState, direction);
899
+ if (dispatch) selectResult(tr, pluginState.results[index], index);
900
+ return true;
901
+ };
742
902
  return {
743
903
  setSearchTerm: (term) => ({ dispatch }) => {
744
904
  if (!dispatch) return true;
@@ -759,9 +919,7 @@ const FindAndReplace = _tiptap_core.Extension.create({
759
919
  return true;
760
920
  },
761
921
  replace: () => ({ tr, state, dispatch }) => {
762
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null;
763
- if (dispatch && pendingTerm !== null) mergePluginMeta(tr, { searchTerm: pendingTerm });
764
- const pluginState = getPendingAwarePluginState(state, pendingTerm);
922
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch);
765
923
  if (!pluginState) return false;
766
924
  const result = currentResult(pluginState);
767
925
  if (!result) return false;
@@ -769,31 +927,13 @@ const FindAndReplace = _tiptap_core.Extension.create({
769
927
  return true;
770
928
  },
771
929
  replaceAll: () => ({ tr, state, dispatch }) => {
772
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null;
773
- if (dispatch && pendingTerm !== null) mergePluginMeta(tr, { searchTerm: pendingTerm });
774
- const pluginState = getPendingAwarePluginState(state, pendingTerm);
930
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch);
775
931
  if (!pluginState) return false;
776
932
  if (dispatch) replaceAllResults(tr, state, pluginState.results, pluginState.replaceTerm);
777
933
  return true;
778
934
  },
779
- goToNextResult: () => ({ tr, state, dispatch }) => {
780
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null;
781
- if (dispatch && pendingTerm !== null) mergePluginMeta(tr, { searchTerm: pendingTerm });
782
- const pluginState = getPendingAwarePluginState(state, pendingTerm);
783
- if (!pluginState) return false;
784
- const nextIndex = shiftIndex(pluginState.currentIndex, pluginState.results.length, 1);
785
- if (dispatch) selectResult(tr, pluginState.results[nextIndex], nextIndex);
786
- return true;
787
- },
788
- goToPreviousResult: () => ({ tr, state, dispatch }) => {
789
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null;
790
- if (dispatch && pendingTerm !== null) mergePluginMeta(tr, { searchTerm: pendingTerm });
791
- const pluginState = getPendingAwarePluginState(state, pendingTerm);
792
- if (!pluginState) return false;
793
- const previousIndex = shiftIndex(pluginState.currentIndex, pluginState.results.length, -1);
794
- if (dispatch) selectResult(tr, pluginState.results[previousIndex], previousIndex);
795
- return true;
796
- }
935
+ goToNextResult: () => goToResult(1),
936
+ goToPreviousResult: () => goToResult(-1)
797
937
  };
798
938
  },
799
939
  onDestroy() {