@tiptap/extension-find-and-replace 3.32.3 → 3.33.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.
@@ -0,0 +1,151 @@
1
+ import { Editor } from '@tiptap/core'
2
+ import Document from '@tiptap/extension-document'
3
+ import Paragraph from '@tiptap/extension-paragraph'
4
+ import Text from '@tiptap/extension-text'
5
+ import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
6
+
7
+ import { FindAndReplace } from './find-and-replace.js'
8
+ import type { FindAndReplaceOptions } from './types.js'
9
+
10
+ describe('result navigation', () => {
11
+ let editor: Editor
12
+
13
+ const createEditor = (content: string, options: Partial<FindAndReplaceOptions> = {}) => {
14
+ return new Editor({
15
+ extensions: [
16
+ Document,
17
+ Paragraph,
18
+ Text,
19
+ FindAndReplace.configure({ searchDebounceMs: 0, ...options }),
20
+ ],
21
+ content,
22
+ })
23
+ }
24
+
25
+ const selectedText = () => {
26
+ const { from, to } = editor.state.selection
27
+
28
+ return editor.state.doc.textBetween(from, to)
29
+ }
30
+
31
+ afterEach(() => {
32
+ editor.destroy()
33
+ vi.useRealTimers()
34
+ })
35
+
36
+ it('goes to the first result on the first next', () => {
37
+ editor = createEditor('<p>one two one</p>')
38
+ editor.commands.setSearchTerm('one')
39
+
40
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
41
+
42
+ editor.commands.goToNextResult()
43
+
44
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
45
+ expect(editor.state.selection.from).toBe(1)
46
+ expect(selectedText()).toBe('one')
47
+
48
+ editor.commands.goToNextResult()
49
+
50
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
51
+ expect(editor.state.selection.from).toBe(9)
52
+ })
53
+
54
+ it('goes to the first result on the first previous', () => {
55
+ editor = createEditor('<p>one two one</p>')
56
+ editor.commands.setSearchTerm('one')
57
+ editor.commands.goToPreviousResult()
58
+
59
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
60
+ expect(editor.state.selection.from).toBe(1)
61
+
62
+ editor.commands.goToPreviousResult()
63
+
64
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
65
+ expect(editor.state.selection.from).toBe(9)
66
+ })
67
+
68
+ it('goes to the first result again after the search term changes', () => {
69
+ editor = createEditor('<p>one two one two</p>')
70
+ editor.commands.setSearchTerm('one')
71
+ editor.commands.goToNextResult()
72
+ editor.commands.goToNextResult()
73
+ editor.commands.setSearchTerm('two')
74
+ editor.commands.goToNextResult()
75
+
76
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
77
+ expect(editor.state.selection.from).toBe(5)
78
+ })
79
+
80
+ it('goes to the first result of a pending search', () => {
81
+ vi.useFakeTimers()
82
+ editor = createEditor('<p>one two one</p>', { searchDebounceMs: 250 })
83
+ editor.commands.setSearchTerm('one')
84
+ editor.commands.goToNextResult()
85
+
86
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
87
+ expect(editor.state.selection.from).toBe(1)
88
+ })
89
+
90
+ it('keeps stepping forward after an edit outside the current result', () => {
91
+ editor = createEditor('<p>one two one two one</p>')
92
+ editor.commands.setSearchTerm('one')
93
+ editor.commands.goToNextResult()
94
+ editor.commands.goToNextResult()
95
+ editor.commands.insertContentAt(1, 'x')
96
+ editor.commands.goToNextResult()
97
+
98
+ expect(editor.storage.findAndReplace.currentIndex).toBe(2)
99
+ })
100
+
101
+ it('keeps stepping forward after a search option changes', () => {
102
+ editor = createEditor('<p>one two one</p>')
103
+ editor.commands.setSearchTerm('one')
104
+ editor.commands.goToNextResult()
105
+ editor.commands.setCaseSensitive(true)
106
+ editor.commands.goToNextResult()
107
+
108
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
109
+ expect(editor.state.selection.from).toBe(9)
110
+ })
111
+
112
+ it('stays on the visited result when a search option adds a result before it', () => {
113
+ editor = createEditor('<p>FOO foo</p>', { caseSensitive: true })
114
+ editor.commands.setSearchTerm('foo')
115
+ editor.commands.goToNextResult()
116
+ editor.commands.setCaseSensitive(false)
117
+
118
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
119
+
120
+ editor.commands.goToNextResult()
121
+
122
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
123
+ expect(editor.state.selection.from).toBe(1)
124
+ })
125
+
126
+ it('goes to a new current result after a search option removes the visited one', () => {
127
+ editor = createEditor('<p>One one</p>')
128
+ editor.commands.setSearchTerm('one')
129
+ editor.commands.goToNextResult()
130
+ editor.commands.setCaseSensitive(true)
131
+ editor.commands.goToNextResult()
132
+
133
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
134
+ expect(editor.state.selection.from).toBe(5)
135
+ })
136
+
137
+ it('goes to a new current result after the visited one is removed', () => {
138
+ editor = createEditor('<p>one two one two one</p>')
139
+ editor.commands.setSearchTerm('one')
140
+ editor.commands.goToNextResult()
141
+ editor.commands.goToNextResult()
142
+ editor.commands.deleteRange({ from: 9, to: 12 })
143
+
144
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
145
+
146
+ editor.commands.goToNextResult()
147
+
148
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
149
+ expect(selectedText()).toBe('one')
150
+ })
151
+ })
@@ -157,19 +157,42 @@ function getPendingAwarePluginState(
157
157
  searchTerm: pendingTerm,
158
158
  results,
159
159
  currentIndex: 0,
160
+ currentResultVisited: false,
160
161
  }
161
162
  }
162
163
 
164
+ function flushPendingSearch(
165
+ editor: Editor,
166
+ tr: Transaction,
167
+ state: EditorState,
168
+ dispatch: boolean,
169
+ ): FindAndReplacePluginState | null {
170
+ const pendingTerm = dispatch ? consumePendingSearch(editor) : null
171
+
172
+ if (dispatch && pendingTerm !== null) {
173
+ mergePluginMeta(tr, { searchTerm: pendingTerm })
174
+ }
175
+
176
+ return getPendingAwarePluginState(state, pendingTerm)
177
+ }
178
+
163
179
  function currentResult(pluginState: FindAndReplacePluginState): SearchResult | undefined {
164
180
  return pluginState.results[pluginState.currentIndex ?? 0]
165
181
  }
166
182
 
167
- function shiftIndex(currentIndex: number | null, length: number, direction: 1 | -1): number {
183
+ function shiftIndex(pluginState: FindAndReplacePluginState, direction: 1 | -1): number {
184
+ const { currentIndex, currentResultVisited, results } = pluginState
185
+
168
186
  if (currentIndex === null) {
169
- return direction === 1 ? 0 : length - 1
187
+ return direction === 1 ? 0 : results.length - 1
188
+ }
189
+
190
+ // Go to the highlighted result first instead of skipping it
191
+ if (!currentResultVisited) {
192
+ return currentIndex
170
193
  }
171
194
 
172
- return (currentIndex + direction + length) % length
195
+ return (currentIndex + direction + results.length) % results.length
173
196
  }
174
197
 
175
198
  function selectResult(tr: Transaction, result: SearchResult, index: number): void {
@@ -293,6 +316,24 @@ export const FindAndReplace = Extension.create<FindAndReplaceOptions, FindAndRep
293
316
  return true
294
317
  }
295
318
 
319
+ const goToResult =
320
+ (direction: 1 | -1): Command =>
321
+ ({ tr, state, dispatch }) => {
322
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch)
323
+
324
+ if (!pluginState) {
325
+ return false
326
+ }
327
+
328
+ const index = shiftIndex(pluginState, direction)
329
+
330
+ if (dispatch) {
331
+ selectResult(tr, pluginState.results[index], index)
332
+ }
333
+
334
+ return true
335
+ }
336
+
296
337
  return {
297
338
  setSearchTerm:
298
339
  term =>
@@ -329,13 +370,7 @@ export const FindAndReplace = Extension.create<FindAndReplaceOptions, FindAndRep
329
370
  replace:
330
371
  () =>
331
372
  ({ tr, state, dispatch }) => {
332
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null
333
-
334
- if (dispatch && pendingTerm !== null) {
335
- mergePluginMeta(tr, { searchTerm: pendingTerm })
336
- }
337
-
338
- const pluginState = getPendingAwarePluginState(state, pendingTerm)
373
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch)
339
374
 
340
375
  if (!pluginState) {
341
376
  return false
@@ -357,13 +392,7 @@ export const FindAndReplace = Extension.create<FindAndReplaceOptions, FindAndRep
357
392
  replaceAll:
358
393
  () =>
359
394
  ({ tr, state, dispatch }) => {
360
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null
361
-
362
- if (dispatch && pendingTerm !== null) {
363
- mergePluginMeta(tr, { searchTerm: pendingTerm })
364
- }
365
-
366
- const pluginState = getPendingAwarePluginState(state, pendingTerm)
395
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch)
367
396
 
368
397
  if (!pluginState) {
369
398
  return false
@@ -376,53 +405,8 @@ export const FindAndReplace = Extension.create<FindAndReplaceOptions, FindAndRep
376
405
  return true
377
406
  },
378
407
 
379
- goToNextResult:
380
- () =>
381
- ({ tr, state, dispatch }) => {
382
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null
383
-
384
- if (dispatch && pendingTerm !== null) {
385
- mergePluginMeta(tr, { searchTerm: pendingTerm })
386
- }
387
-
388
- const pluginState = getPendingAwarePluginState(state, pendingTerm)
389
-
390
- if (!pluginState) {
391
- return false
392
- }
393
-
394
- const nextIndex = shiftIndex(pluginState.currentIndex, pluginState.results.length, 1)
395
-
396
- if (dispatch) {
397
- selectResult(tr, pluginState.results[nextIndex], nextIndex)
398
- }
399
-
400
- return true
401
- },
402
-
403
- goToPreviousResult:
404
- () =>
405
- ({ tr, state, dispatch }) => {
406
- const pendingTerm = dispatch ? consumePendingSearch(this.editor) : null
407
-
408
- if (dispatch && pendingTerm !== null) {
409
- mergePluginMeta(tr, { searchTerm: pendingTerm })
410
- }
411
-
412
- const pluginState = getPendingAwarePluginState(state, pendingTerm)
413
-
414
- if (!pluginState) {
415
- return false
416
- }
417
-
418
- const previousIndex = shiftIndex(pluginState.currentIndex, pluginState.results.length, -1)
419
-
420
- if (dispatch) {
421
- selectResult(tr, pluginState.results[previousIndex], previousIndex)
422
- }
423
-
424
- return true
425
- },
408
+ goToNextResult: () => goToResult(1),
409
+ goToPreviousResult: () => goToResult(-1),
426
410
  }
427
411
  },
428
412
 
@@ -10,6 +10,8 @@ export interface FindAndReplacePluginState {
10
10
  wholeWord: boolean
11
11
  results: SearchResult[]
12
12
  currentIndex: number | null
13
+ /** Whether the current result was already selected by navigation. */
14
+ currentResultVisited: boolean
13
15
  decorations: DecorationSet
14
16
  }
15
17
 
@@ -18,6 +18,7 @@ export function createState(doc: Node, options: FindAndReplaceOptions): FindAndR
18
18
  wholeWord: options.wholeWord,
19
19
  results,
20
20
  currentIndex,
21
+ currentResultVisited: false,
21
22
  decorations: createDecorations(doc, results, currentIndex),
22
23
  }
23
24
  }
@@ -5,6 +5,8 @@ import type { SearchResult } from '../search/search.js'
5
5
 
6
6
  import { getFallbackCurrentIndex } from './getFallbackCurrentIndex.js'
7
7
  import { getMappedCurrentIndex } from './getMappedCurrentIndex.js'
8
+ import { getPreviousCurrentResult } from './getPreviousCurrentResult.js'
9
+ import { isCurrentResult } from './isCurrentResult.js'
8
10
  import { isNewSearch } from './isNewSearch.js'
9
11
 
10
12
  export function getSearchCurrentIndex(
@@ -20,5 +22,20 @@ export function getSearchCurrentIndex(
20
22
 
21
23
  const mappedIndex = getMappedCurrentIndex(previousState, results, docChanged, mapping)
22
24
 
23
- return mappedIndex ?? getFallbackCurrentIndex(previousState, results.length)
25
+ return (
26
+ mappedIndex ??
27
+ findPreviousCurrentIndex(previousState, results) ??
28
+ getFallbackCurrentIndex(previousState, results.length)
29
+ )
30
+ }
31
+
32
+ // Stay on the same match when an option adds or removes results before it
33
+ function findPreviousCurrentIndex(
34
+ previousState: FindAndReplacePluginState,
35
+ results: SearchResult[],
36
+ ): number | null {
37
+ const previousCurrent = getPreviousCurrentResult(previousState) ?? null
38
+ const index = results.findIndex(result => isCurrentResult(result, previousCurrent))
39
+
40
+ return index === -1 ? null : index
24
41
  }
@@ -0,0 +1,16 @@
1
+ import type { FindAndReplacePluginState } from '../plugin/plugin-state.js'
2
+ import type { SearchResult } from '../search/search.js'
3
+
4
+ import { isCurrentResult } from './isCurrentResult.js'
5
+
6
+ export function keepsVisitedResult(
7
+ previousState: FindAndReplacePluginState,
8
+ current: SearchResult | null,
9
+ previousCurrent: SearchResult | null,
10
+ ): boolean {
11
+ return (
12
+ previousState.currentResultVisited &&
13
+ current !== null &&
14
+ isCurrentResult(current, previousCurrent)
15
+ )
16
+ }
@@ -18,6 +18,7 @@ export function refreshCurrentIndexState(
18
18
  return {
19
19
  ...state,
20
20
  currentIndex,
21
+ currentResultVisited: true,
21
22
  decorations: refreshDecorations(
22
23
  previousState.decorations,
23
24
  doc,
@@ -5,6 +5,8 @@ import type { FindAndReplaceMeta, FindAndReplacePluginState } from '../plugin/pl
5
5
  import { searchTextblocks } from '../search/search.js'
6
6
 
7
7
  import { getChangedTextblocks } from './getChangedTextblocks.js'
8
+ import { hasCurrentIndex } from './hasCurrentIndex.js'
9
+ import { keepsVisitedResult } from './keepsVisitedResult.js'
8
10
  import { mapResult } from './mapResult.js'
9
11
  import { mapResults } from './mapResults.js'
10
12
  import { mergeResults } from './mergeResults.js'
@@ -29,12 +31,15 @@ export function refreshDocumentState(
29
31
  previousState.currentIndex === null
30
32
  ? null
31
33
  : mapResult(previousState.results[previousState.currentIndex], mapping)
34
+ const current = currentIndex === null ? null : results[currentIndex]
32
35
  const decorations = previousState.decorations.map(mapping, doc)
33
36
 
34
37
  return {
35
38
  ...state,
36
39
  results,
37
40
  currentIndex,
41
+ currentResultVisited:
42
+ hasCurrentIndex(meta) || keepsVisitedResult(previousState, current, previousCurrent),
38
43
  decorations: refreshDecorations(
39
44
  decorations,
40
45
  doc,
@@ -5,6 +5,11 @@ import type { FindAndReplaceMeta, FindAndReplacePluginState } from '../plugin/pl
5
5
  import { searchDocument } from '../search/search.js'
6
6
 
7
7
  import { createDecorations } from './createDecorations.js'
8
+ import { getPreviousCurrentResult } from './getPreviousCurrentResult.js'
9
+ import { hasCurrentIndex } from './hasCurrentIndex.js'
10
+ import { isNewSearch } from './isNewSearch.js'
11
+ import { keepsVisitedResult } from './keepsVisitedResult.js'
12
+ import { mapResult } from './mapResult.js'
8
13
  import { resolveCurrentIndex } from './resolveCurrentIndex.js'
9
14
 
10
15
  export function refreshState(
@@ -17,11 +22,18 @@ export function refreshState(
17
22
  ): FindAndReplacePluginState {
18
23
  const results = searchDocument(doc, state.searchTerm, state)
19
24
  const currentIndex = resolveCurrentIndex(previousState, results, meta, docChanged, mapping)
25
+ const current = currentIndex === null ? null : results[currentIndex]
26
+ const previousResult = getPreviousCurrentResult(previousState) ?? null
27
+ const previousCurrent =
28
+ previousResult && mapping ? mapResult(previousResult, mapping) : previousResult
20
29
 
21
30
  return {
22
31
  ...state,
23
32
  results,
24
33
  currentIndex,
34
+ currentResultVisited:
35
+ hasCurrentIndex(meta) ||
36
+ (!isNewSearch(meta) && keepsVisitedResult(previousState, current, previousCurrent)),
25
37
  decorations: createDecorations(doc, results, currentIndex),
26
38
  }
27
39
  }