@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.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { Extension } from "@tiptap/core";
2
- import { Plugin, PluginKey, TextSelection } from "@tiptap/pm/state";
2
+ import { Plugin, PluginKey, Selection, TextSelection } from "@tiptap/pm/state";
3
3
  import { RE2JS } from "re2js";
4
4
  import { Decoration, DecorationSet } from "@tiptap/pm/view";
5
+ import { AttrStep } from "@tiptap/pm/transform";
5
6
  //#region src/state/debounced-search-state.ts
6
7
  const debouncedSearchState = /* @__PURE__ */ new WeakMap();
7
8
  function getDebouncedSearchState(editor) {
@@ -47,6 +48,50 @@ const style = `.find-and-replace-result {
47
48
  }
48
49
  `;
49
50
  //#endregion
51
+ //#region src/search/has-block-between.ts
52
+ /**
53
+ * Checks if a whole node lies between two positions.
54
+ * Nodes that only open or close in the range, like the parents of two textblocks, do not count.
55
+ * @param doc The document to check.
56
+ * @param from The start of the range.
57
+ * @param to The end of the range.
58
+ * @returns Whether a node lies fully inside the range.
59
+ */
60
+ function hasBlockBetween(doc, from, to) {
61
+ let found = false;
62
+ doc.nodesBetween(from, to, (node, pos) => {
63
+ if (found) return false;
64
+ if (pos >= from && pos + node.nodeSize <= to) {
65
+ found = true;
66
+ return false;
67
+ }
68
+ return true;
69
+ });
70
+ return found;
71
+ }
72
+ //#endregion
73
+ //#region src/search/group-continued-textblocks.ts
74
+ /**
75
+ * Groups textblocks that continue the textblock before them in the list.
76
+ * @param doc The document the textblocks are in.
77
+ * @param textblocks Whole chains of continued textblocks in document order.
78
+ * @param continuesNode Whether a textblock continues the one before it.
79
+ * @returns The groups to search as one text each.
80
+ */
81
+ function groupContinuedTextblocks(doc, textblocks, continuesNode = () => false) {
82
+ const groups = [];
83
+ textblocks.forEach((textblock) => {
84
+ const group = groups.at(-1);
85
+ const previous = group === null || group === void 0 ? void 0 : group.at(-1);
86
+ if (group && previous && continuesNode(textblock.node) && !hasBlockBetween(doc, previous.pos + previous.node.nodeSize, textblock.pos)) {
87
+ group.push(textblock);
88
+ return;
89
+ }
90
+ groups.push([textblock]);
91
+ });
92
+ return groups;
93
+ }
94
+ //#endregion
50
95
  //#region src/search/regex.ts
51
96
  const escapeRegExp = (value) => {
52
97
  return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -111,20 +156,22 @@ function findMatches(regex, text) {
111
156
  }
112
157
  //#endregion
113
158
  //#region src/search/text-segments.ts
114
- function getTextSegments(textblock, pos) {
159
+ function getTextSegments(textblocks) {
115
160
  const segments = [];
116
161
  let textOffset = 0;
117
- textblock.forEach((child, offset) => {
118
- var _child$text;
119
- const text = child.isText ? (_child$text = child.text) !== null && _child$text !== void 0 ? _child$text : "" : "\n";
120
- segments.push({
121
- isText: child.isText,
122
- pos: pos + 1 + offset,
123
- length: child.nodeSize,
124
- text,
125
- textOffset
162
+ textblocks.forEach(({ node, pos }) => {
163
+ node.forEach((child, offset) => {
164
+ var _child$text;
165
+ const text = child.isText ? (_child$text = child.text) !== null && _child$text !== void 0 ? _child$text : "" : "\n";
166
+ segments.push({
167
+ isText: child.isText,
168
+ pos: pos + 1 + offset,
169
+ length: child.nodeSize,
170
+ text,
171
+ textOffset
172
+ });
173
+ textOffset += text.length;
126
174
  });
127
- textOffset += text.length;
128
175
  });
129
176
  return segments;
130
177
  }
@@ -144,13 +191,16 @@ function offsetToPos(segments, offset) {
144
191
  const segment = findOffsetSegment(segments, offset);
145
192
  return segment ? segment.pos + Math.min(offset - segment.textOffset, segment.length) : 0;
146
193
  }
194
+ function endOffsetToPos(segments, offset) {
195
+ return offsetToPos(segments, offset - 1) + 1;
196
+ }
147
197
  function overlapsNonTextSegment(segments, from, to) {
148
198
  return segments.some((segment) => !segment.isText && from < segment.textOffset + segment.text.length && to > segment.textOffset);
149
199
  }
150
200
  //#endregion
151
201
  //#region src/search/textblock-search.ts
152
- function searchTextblock(regex, textblock, pos) {
153
- const segments = getTextSegments(textblock, pos);
202
+ function searchTextblockGroup(regex, textblocks) {
203
+ const segments = getTextSegments(textblocks);
154
204
  const text = segments.map((segment) => segment.text).join("");
155
205
  const results = [];
156
206
  for (const match of findMatches(regex, text)) {
@@ -159,7 +209,7 @@ function searchTextblock(regex, textblock, pos) {
159
209
  if (overlapsNonTextSegment(segments, match.index, matchEnd)) continue;
160
210
  results.push({
161
211
  from: offsetToPos(segments, match.index),
162
- to: offsetToPos(segments, matchEnd)
212
+ to: endOffsetToPos(segments, matchEnd)
163
213
  });
164
214
  }
165
215
  return results;
@@ -168,22 +218,25 @@ function searchTextblock(regex, textblock, pos) {
168
218
  //#region src/search/search.ts
169
219
  /**
170
220
  * Searches textblocks for a term and returns their matched ranges.
171
- * @param textblocks The textblocks and their document positions.
221
+ * Textblocks joined by `continuesNode` are searched as one text when `doc` is given.
222
+ * @param textblocks The textblocks and their document positions. Include every textblock of a continued chain.
172
223
  * @param term The search term, treated as regex source when `useRegex` is enabled.
173
- * @param options Case sensitivity, regex mode, and whole word matching.
224
+ * @param options Case sensitivity, regex mode, whole word matching, and continued textblocks.
225
+ * @param doc The document the textblocks are in. Needed to join continued textblocks.
174
226
  * @returns The list of matched ranges in textblock order.
175
227
  */
176
- function searchTextblocks(textblocks, term, options) {
228
+ function searchTextblocks(textblocks, term, options, doc) {
177
229
  const regex = createSearchRegex(term, options);
178
230
  if (!regex) return [];
179
- return textblocks.flatMap(({ node, pos }) => searchTextblock(regex, node, pos));
231
+ return (doc ? groupContinuedTextblocks(doc, textblocks, options.continuesNode) : textblocks.map((textblock) => [textblock])).flatMap((group) => searchTextblockGroup(regex, group));
180
232
  }
181
233
  /**
182
234
  * Searches all textblocks of a document for a term and returns the matched ranges.
183
- * Matches may span multiple text nodes (e.g. across marks), but never leave a textblock.
235
+ * Matches may span multiple text nodes (e.g. across marks), but only leave a textblock
236
+ * when `continuesNode` joins it with the next one.
184
237
  * @param doc The document to search in.
185
238
  * @param term The search term, treated as regex source when `useRegex` is enabled.
186
- * @param options Case sensitivity, regex mode, and whole word matching.
239
+ * @param options Case sensitivity, regex mode, whole word matching, and continued textblocks.
187
240
  * @returns The list of matched ranges in document order.
188
241
  */
189
242
  function searchDocument(doc, term, options) {
@@ -196,7 +249,7 @@ function searchDocument(doc, term, options) {
196
249
  });
197
250
  return false;
198
251
  });
199
- return searchTextblocks(textblocks, term, options);
252
+ return searchTextblocks(textblocks, term, options, doc);
200
253
  }
201
254
  /**
202
255
  * Finds the index of the first result at or after a position, wrapping around
@@ -233,8 +286,10 @@ function createState(doc, options) {
233
286
  caseSensitive: options.caseSensitive,
234
287
  useRegex: options.useRegex,
235
288
  wholeWord: options.wholeWord,
289
+ continuesNode: options.continuesNode,
236
290
  results,
237
291
  currentIndex,
292
+ currentResultVisited: false,
238
293
  decorations: createDecorations(doc, results, currentIndex)
239
294
  };
240
295
  }
@@ -275,11 +330,11 @@ function resultInTextblocks(result, textblocks) {
275
330
  while (start <= end) {
276
331
  const index = Math.floor((start + end) / 2);
277
332
  const textblock = textblocks[index];
278
- if (result.from < textblock.from) {
333
+ if (result.to <= textblock.from) {
279
334
  end = index - 1;
280
335
  continue;
281
336
  }
282
- if (result.to > textblock.to) {
337
+ if (result.from >= textblock.to) {
283
338
  start = index + 1;
284
339
  continue;
285
340
  }
@@ -357,9 +412,16 @@ function isNewSearch(meta) {
357
412
  //#endregion
358
413
  //#region src/utils/getSearchCurrentIndex.ts
359
414
  function getSearchCurrentIndex(previousState, results, meta, docChanged, mapping) {
415
+ var _ref;
360
416
  if (isNewSearch(meta)) return 0;
361
417
  const mappedIndex = getMappedCurrentIndex(previousState, results, docChanged, mapping);
362
- return mappedIndex !== null && mappedIndex !== void 0 ? mappedIndex : getFallbackCurrentIndex(previousState, results.length);
418
+ return (_ref = mappedIndex !== null && mappedIndex !== void 0 ? mappedIndex : findPreviousCurrentIndex(previousState, results)) !== null && _ref !== void 0 ? _ref : getFallbackCurrentIndex(previousState, results.length);
419
+ }
420
+ function findPreviousCurrentIndex(previousState, results) {
421
+ var _getPreviousCurrentRe;
422
+ const previousCurrent = (_getPreviousCurrentRe = getPreviousCurrentResult(previousState)) !== null && _getPreviousCurrentRe !== void 0 ? _getPreviousCurrentRe : null;
423
+ const index = results.findIndex((result) => isCurrentResult(result, previousCurrent));
424
+ return index === -1 ? null : index;
363
425
  }
364
426
  //#endregion
365
427
  //#region src/utils/resolveCurrentIndex.ts
@@ -377,33 +439,115 @@ function refreshCurrentIndexState(previousState, state, doc, meta) {
377
439
  return {
378
440
  ...state,
379
441
  currentIndex,
442
+ currentResultVisited: true,
380
443
  decorations: refreshDecorations(previousState.decorations, doc, state.results, currentIndex, [], previousCurrent)
381
444
  };
382
445
  }
383
446
  //#endregion
447
+ //#region src/utils/findNearestTextblock.ts
448
+ function findNearestTextblock(doc, pos, direction) {
449
+ const selection = Selection.findFrom(doc.resolve(pos), direction, true);
450
+ if (!selection) return null;
451
+ const node = selection.$from.parent;
452
+ const from = selection.$from.before();
453
+ return {
454
+ node,
455
+ pos: from,
456
+ from,
457
+ to: from + node.nodeSize
458
+ };
459
+ }
460
+ //#endregion
461
+ //#region src/utils/findAdjacentTextblock.ts
462
+ function findAdjacentTextblock(doc, textblock, direction) {
463
+ return findNearestTextblock(doc, direction === 1 ? textblock.to : textblock.from, direction);
464
+ }
465
+ //#endregion
466
+ //#region src/utils/addContinuedTextblocks.ts
467
+ function findJoinedTextblock(doc, textblock, direction, continuesNode) {
468
+ const adjacent = findAdjacentTextblock(doc, textblock, direction);
469
+ if (!adjacent) return null;
470
+ const [first, second] = direction === 1 ? [textblock, adjacent] : [adjacent, textblock];
471
+ if (!continuesNode(second.node) || hasBlockBetween(doc, first.to, second.from)) return null;
472
+ return adjacent;
473
+ }
474
+ function addChain(doc, textblock, continuesNode, found) {
475
+ let previous = findJoinedTextblock(doc, textblock, -1, continuesNode);
476
+ while (previous) {
477
+ found.set(previous.pos, previous);
478
+ previous = findJoinedTextblock(doc, previous, -1, continuesNode);
479
+ }
480
+ let next = findJoinedTextblock(doc, textblock, 1, continuesNode);
481
+ while (next) {
482
+ found.set(next.pos, next);
483
+ next = findJoinedTextblock(doc, next, 1, continuesNode);
484
+ }
485
+ }
486
+ /**
487
+ * Adds the textblocks that are joined with the given ones by `continuesNode`.
488
+ * @param doc The document the textblocks are in.
489
+ * @param textblocks The textblocks in document order.
490
+ * @param continuesNode Whether a textblock continues the one before it.
491
+ * @returns The textblocks with their whole chains, in document order.
492
+ */
493
+ function addContinuedTextblocks(doc, textblocks, continuesNode) {
494
+ const found = /* @__PURE__ */ new Map();
495
+ textblocks.forEach((textblock) => {
496
+ if (found.has(textblock.pos)) return;
497
+ found.set(textblock.pos, textblock);
498
+ addChain(doc, textblock, continuesNode, found);
499
+ });
500
+ return [...found.values()].sort((first, second) => first.pos - second.pos);
501
+ }
502
+ //#endregion
384
503
  //#region src/utils/getChangedTextblocks.ts
385
- function getChangedTextblocks(doc, mapping) {
504
+ function addTextblock(textblocks, node, pos) {
505
+ const textblock = {
506
+ node,
507
+ pos,
508
+ from: pos,
509
+ to: pos + node.nodeSize
510
+ };
511
+ textblocks.set(pos, textblock);
512
+ return textblock;
513
+ }
514
+ function addAttributeChanges(doc, transform, textblocks) {
515
+ transform.steps.forEach((step, index) => {
516
+ if (!(step instanceof AttrStep)) return;
517
+ const pos = transform.mapping.slice(index + 1).map(step.pos);
518
+ const node = doc.nodeAt(pos);
519
+ if (!(node === null || node === void 0 ? void 0 : node.isTextblock)) return;
520
+ const previous = findAdjacentTextblock(doc, addTextblock(textblocks, node, pos), -1);
521
+ if (previous) textblocks.set(previous.pos, previous);
522
+ });
523
+ }
524
+ function getChangedTextblocks(doc, transform) {
525
+ const { mapping } = transform;
386
526
  const textblocks = /* @__PURE__ */ new Map();
387
527
  mapping.maps.forEach((stepMap, index) => {
388
528
  const remaining = mapping.slice(index + 1);
389
529
  stepMap.forEach((_oldStart, _oldEnd, newStart, newEnd) => {
390
- const from = Math.max(0, remaining.map(newStart, -1) - 1);
391
- const to = Math.min(doc.content.size, remaining.map(newEnd, 1) + 1);
530
+ var _before$from, _after$to;
531
+ const before = findNearestTextblock(doc, remaining.map(newStart, -1), -1);
532
+ const after = findNearestTextblock(doc, remaining.map(newEnd, 1), 1);
533
+ const from = (_before$from = before === null || before === void 0 ? void 0 : before.from) !== null && _before$from !== void 0 ? _before$from : 0;
534
+ const to = (_after$to = after === null || after === void 0 ? void 0 : after.to) !== null && _after$to !== void 0 ? _after$to : doc.content.size;
392
535
  doc.nodesBetween(from, to, (node, pos) => {
393
536
  if (!node.isTextblock) return true;
394
- textblocks.set(pos, {
395
- node,
396
- pos,
397
- from: pos,
398
- to: pos + node.nodeSize
399
- });
537
+ addTextblock(textblocks, node, pos);
400
538
  return false;
401
539
  });
402
540
  });
403
541
  });
542
+ addAttributeChanges(doc, transform, textblocks);
404
543
  return [...textblocks.values()].sort((first, second) => first.pos - second.pos);
405
544
  }
406
545
  //#endregion
546
+ //#region src/utils/keepsVisitedResult.ts
547
+ function keepsVisitedResult(previousState, current, previousCurrent) {
548
+ return previousState.currentResultVisited && current !== null && isCurrentResult(current, previousCurrent);
549
+ }
550
+ //#endregion
407
551
  //#region src/utils/mapResult.ts
408
552
  function mapResult(result, mapping) {
409
553
  const from = mapping.map(result.from, 1);
@@ -440,28 +584,36 @@ function mergeResults(first, second) {
440
584
  }
441
585
  //#endregion
442
586
  //#region src/utils/refreshDocumentState.ts
443
- function refreshDocumentState(previousState, state, doc, meta, mapping) {
444
- const textblocks = getChangedTextblocks(doc, mapping);
445
- const results = mergeResults(mapResults(previousState.results, mapping).filter((result) => !resultInTextblocks(result, textblocks)), searchTextblocks(textblocks, state.searchTerm, state));
587
+ function refreshDocumentState(previousState, state, doc, meta, transform) {
588
+ const { mapping } = transform;
589
+ const textblocks = addContinuedTextblocks(doc, getChangedTextblocks(doc, transform), state.continuesNode);
590
+ const results = mergeResults(mapResults(previousState.results, mapping).filter((result) => !resultInTextblocks(result, textblocks)), searchTextblocks(textblocks, state.searchTerm, state, doc));
446
591
  const currentIndex = resolveCurrentIndex(previousState, results, meta, true, mapping);
447
592
  const previousCurrent = previousState.currentIndex === null ? null : mapResult(previousState.results[previousState.currentIndex], mapping);
593
+ const current = currentIndex === null ? null : results[currentIndex];
448
594
  const decorations = previousState.decorations.map(mapping, doc);
449
595
  return {
450
596
  ...state,
451
597
  results,
452
598
  currentIndex,
599
+ currentResultVisited: hasCurrentIndex(meta) || keepsVisitedResult(previousState, current, previousCurrent),
453
600
  decorations: refreshDecorations(decorations, doc, results, currentIndex, textblocks, previousCurrent)
454
601
  };
455
602
  }
456
603
  //#endregion
457
604
  //#region src/utils/refreshState.ts
458
605
  function refreshState(previousState, state, doc, meta, docChanged, mapping) {
606
+ var _getPreviousCurrentRe;
459
607
  const results = searchDocument(doc, state.searchTerm, state);
460
608
  const currentIndex = resolveCurrentIndex(previousState, results, meta, docChanged, mapping);
609
+ const current = currentIndex === null ? null : results[currentIndex];
610
+ const previousResult = (_getPreviousCurrentRe = getPreviousCurrentResult(previousState)) !== null && _getPreviousCurrentRe !== void 0 ? _getPreviousCurrentRe : null;
611
+ const previousCurrent = previousResult && mapping ? mapResult(previousResult, mapping) : previousResult;
461
612
  return {
462
613
  ...state,
463
614
  results,
464
615
  currentIndex,
616
+ currentResultVisited: hasCurrentIndex(meta) || !isNewSearch(meta) && keepsVisitedResult(previousState, current, previousCurrent),
465
617
  decorations: createDecorations(doc, results, currentIndex)
466
618
  };
467
619
  }
@@ -478,9 +630,9 @@ function touchesSearch(meta) {
478
630
  }
479
631
  //#endregion
480
632
  //#region src/utils/updateState.ts
481
- function updateState(previousState, state, doc, meta, docChanged, mapping) {
482
- if (touchesSearch(meta)) return refreshState(previousState, state, doc, meta, docChanged, mapping);
483
- if (docChanged) return refreshDocumentState(previousState, state, doc, meta, mapping);
633
+ function updateState(previousState, state, doc, meta, transform) {
634
+ if (touchesSearch(meta)) return refreshState(previousState, state, doc, meta, transform.docChanged, transform.mapping);
635
+ if (transform.docChanged) return refreshDocumentState(previousState, state, doc, meta, transform);
484
636
  if (hasCurrentIndex(meta)) return refreshCurrentIndexState(previousState, state, doc, meta);
485
637
  return state;
486
638
  }
@@ -503,7 +655,7 @@ const FindAndReplacePlugin = (options, onCreate) => {
503
655
  return updateState(state, {
504
656
  ...state,
505
657
  ...meta
506
- }, newState.doc, meta, tr.docChanged, tr.mapping);
658
+ }, newState.doc, meta, tr);
507
659
  }
508
660
  },
509
661
  props: { decorations(state) {
@@ -628,26 +780,30 @@ function getPendingAwarePluginState(state, pendingTerm) {
628
780
  const pluginState = FindAndReplacePluginKey.getState(state);
629
781
  if (!pluginState) return null;
630
782
  if (pendingTerm === null) return pluginState.results.length === 0 ? null : pluginState;
631
- const results = searchDocument(state.doc, pendingTerm, {
632
- caseSensitive: pluginState.caseSensitive,
633
- useRegex: pluginState.useRegex,
634
- wholeWord: pluginState.wholeWord
635
- });
783
+ const results = searchDocument(state.doc, pendingTerm, pluginState);
636
784
  if (results.length === 0) return null;
637
785
  return {
638
786
  ...pluginState,
639
787
  searchTerm: pendingTerm,
640
788
  results,
641
- currentIndex: 0
789
+ currentIndex: 0,
790
+ currentResultVisited: false
642
791
  };
643
792
  }
793
+ function flushPendingSearch(editor, tr, state, dispatch) {
794
+ const pendingTerm = dispatch ? consumePendingSearch(editor) : null;
795
+ if (dispatch && pendingTerm !== null) mergePluginMeta(tr, { searchTerm: pendingTerm });
796
+ return getPendingAwarePluginState(state, pendingTerm);
797
+ }
644
798
  function currentResult(pluginState) {
645
799
  var _pluginState$currentI;
646
800
  return pluginState.results[(_pluginState$currentI = pluginState.currentIndex) !== null && _pluginState$currentI !== void 0 ? _pluginState$currentI : 0];
647
801
  }
648
- function shiftIndex(currentIndex, length, direction) {
649
- if (currentIndex === null) return direction === 1 ? 0 : length - 1;
650
- return (currentIndex + direction + length) % length;
802
+ function shiftIndex(pluginState, direction) {
803
+ const { currentIndex, currentResultVisited, results } = pluginState;
804
+ if (currentIndex === null) return direction === 1 ? 0 : results.length - 1;
805
+ if (!currentResultVisited) return currentIndex;
806
+ return (currentIndex + direction + results.length) % results.length;
651
807
  }
652
808
  function selectResult(tr, result, index) {
653
809
  tr.setSelection(TextSelection.create(tr.doc, result.from, result.to));
@@ -655,13 +811,9 @@ function selectResult(tr, result, index) {
655
811
  tr.scrollIntoView();
656
812
  }
657
813
  function replaceResult(tr, pluginState, result) {
658
- const { searchTerm, replaceTerm, caseSensitive, useRegex, wholeWord } = pluginState;
814
+ const { searchTerm, replaceTerm } = pluginState;
659
815
  tr.insertText(replaceTerm, result.from, result.to);
660
- const results = searchDocument(tr.doc, searchTerm, {
661
- caseSensitive,
662
- useRegex,
663
- wholeWord
664
- });
816
+ const results = searchDocument(tr.doc, searchTerm, pluginState);
665
817
  const nextIndex = findNextIndex(results, result.from + replaceTerm.length);
666
818
  if (nextIndex === null) {
667
819
  mergePluginMeta(tr, { currentIndex: null });
@@ -678,6 +830,7 @@ const FindAndReplace = Extension.create({
678
830
  caseSensitive: false,
679
831
  useRegex: false,
680
832
  wholeWord: false,
833
+ continuesNode: () => false,
681
834
  searchDebounceMs: 250,
682
835
  injectCSS: true,
683
836
  injectNonce: void 0
@@ -735,6 +888,13 @@ const FindAndReplace = Extension.create({
735
888
  }, this.options.searchDebounceMs);
736
889
  return true;
737
890
  };
891
+ const goToResult = (direction) => ({ tr, state, dispatch }) => {
892
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch);
893
+ if (!pluginState) return false;
894
+ const index = shiftIndex(pluginState, direction);
895
+ if (dispatch) selectResult(tr, pluginState.results[index], index);
896
+ return true;
897
+ };
738
898
  return {
739
899
  setSearchTerm: (term) => ({ dispatch }) => {
740
900
  if (!dispatch) return true;
@@ -755,9 +915,7 @@ const FindAndReplace = Extension.create({
755
915
  return true;
756
916
  },
757
917
  replace: () => ({ tr, state, dispatch }) => {
758
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null;
759
- if (dispatch && pendingTerm !== null) mergePluginMeta(tr, { searchTerm: pendingTerm });
760
- const pluginState = getPendingAwarePluginState(state, pendingTerm);
918
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch);
761
919
  if (!pluginState) return false;
762
920
  const result = currentResult(pluginState);
763
921
  if (!result) return false;
@@ -765,31 +923,13 @@ const FindAndReplace = Extension.create({
765
923
  return true;
766
924
  },
767
925
  replaceAll: () => ({ tr, state, dispatch }) => {
768
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null;
769
- if (dispatch && pendingTerm !== null) mergePluginMeta(tr, { searchTerm: pendingTerm });
770
- const pluginState = getPendingAwarePluginState(state, pendingTerm);
926
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch);
771
927
  if (!pluginState) return false;
772
928
  if (dispatch) replaceAllResults(tr, state, pluginState.results, pluginState.replaceTerm);
773
929
  return true;
774
930
  },
775
- goToNextResult: () => ({ tr, state, dispatch }) => {
776
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null;
777
- if (dispatch && pendingTerm !== null) mergePluginMeta(tr, { searchTerm: pendingTerm });
778
- const pluginState = getPendingAwarePluginState(state, pendingTerm);
779
- if (!pluginState) return false;
780
- const nextIndex = shiftIndex(pluginState.currentIndex, pluginState.results.length, 1);
781
- if (dispatch) selectResult(tr, pluginState.results[nextIndex], nextIndex);
782
- return true;
783
- },
784
- goToPreviousResult: () => ({ tr, state, dispatch }) => {
785
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null;
786
- if (dispatch && pendingTerm !== null) mergePluginMeta(tr, { searchTerm: pendingTerm });
787
- const pluginState = getPendingAwarePluginState(state, pendingTerm);
788
- if (!pluginState) return false;
789
- const previousIndex = shiftIndex(pluginState.currentIndex, pluginState.results.length, -1);
790
- if (dispatch) selectResult(tr, pluginState.results[previousIndex], previousIndex);
791
- return true;
792
- }
931
+ goToNextResult: () => goToResult(1),
932
+ goToPreviousResult: () => goToResult(-1)
793
933
  };
794
934
  },
795
935
  onDestroy() {