@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
@@ -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,
@@ -1,10 +1,13 @@
1
- import type { Node } from '@tiptap/pm/model'
2
- import type { Mapping } from '@tiptap/pm/transform'
1
+ import type { Node as PMNode } from '@tiptap/pm/model'
2
+ import type { Transform } from '@tiptap/pm/transform'
3
3
 
4
4
  import type { FindAndReplaceMeta, FindAndReplacePluginState } from '../plugin/plugin-state.js'
5
5
  import { searchTextblocks } from '../search/search.js'
6
6
 
7
+ import { addContinuedTextblocks } from './addContinuedTextblocks.js'
7
8
  import { getChangedTextblocks } from './getChangedTextblocks.js'
9
+ import { hasCurrentIndex } from './hasCurrentIndex.js'
10
+ import { keepsVisitedResult } from './keepsVisitedResult.js'
8
11
  import { mapResult } from './mapResult.js'
9
12
  import { mapResults } from './mapResults.js'
10
13
  import { mergeResults } from './mergeResults.js'
@@ -15,26 +18,34 @@ import { resultInTextblocks } from './resultInTextblocks.js'
15
18
  export function refreshDocumentState(
16
19
  previousState: FindAndReplacePluginState,
17
20
  state: FindAndReplacePluginState,
18
- doc: Node,
21
+ doc: PMNode,
19
22
  meta: FindAndReplaceMeta | undefined,
20
- mapping: Mapping,
23
+ transform: Transform,
21
24
  ): FindAndReplacePluginState {
22
- const textblocks = getChangedTextblocks(doc, mapping)
25
+ const { mapping } = transform
26
+ const textblocks = addContinuedTextblocks(
27
+ doc,
28
+ getChangedTextblocks(doc, transform),
29
+ state.continuesNode,
30
+ )
23
31
  const mappedResults = mapResults(previousState.results, mapping)
24
32
  const unaffectedResults = mappedResults.filter(result => !resultInTextblocks(result, textblocks))
25
- const changedResults = searchTextblocks(textblocks, state.searchTerm, state)
33
+ const changedResults = searchTextblocks(textblocks, state.searchTerm, state, doc)
26
34
  const results = mergeResults(unaffectedResults, changedResults)
27
35
  const currentIndex = resolveCurrentIndex(previousState, results, meta, true, mapping)
28
36
  const previousCurrent =
29
37
  previousState.currentIndex === null
30
38
  ? null
31
39
  : mapResult(previousState.results[previousState.currentIndex], mapping)
40
+ const current = currentIndex === null ? null : results[currentIndex]
32
41
  const decorations = previousState.decorations.map(mapping, doc)
33
42
 
34
43
  return {
35
44
  ...state,
36
45
  results,
37
46
  currentIndex,
47
+ currentResultVisited:
48
+ hasCurrentIndex(meta) || keepsVisitedResult(previousState, current, previousCurrent),
38
49
  decorations: refreshDecorations(
39
50
  decorations,
40
51
  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
  }
@@ -0,0 +1,25 @@
1
+ import { describe, expect, it } from 'vite-plus/test'
2
+
3
+ import { createContinuedDoc, paragraph } from '../../tests/utils/createContinuedDoc.js'
4
+ import { resultInTextblocks } from './resultInTextblocks.js'
5
+ import type { TextblockRange } from './types.js'
6
+
7
+ describe('resultInTextblocks', () => {
8
+ const doc = createContinuedDoc([paragraph('one'), paragraph('two', true), paragraph('three')])
9
+ const textblocks: TextblockRange[] = [
10
+ { node: doc.child(0), pos: 0, from: 0, to: 5 },
11
+ { node: doc.child(2), pos: 10, from: 10, to: 17 },
12
+ ]
13
+
14
+ it('finds a result inside a textblock', () => {
15
+ expect(resultInTextblocks({ from: 11, to: 16 }, textblocks)).toBe(true)
16
+ })
17
+
18
+ it('finds a result that runs out of a textblock', () => {
19
+ expect(resultInTextblocks({ from: 2, to: 8 }, textblocks)).toBe(true)
20
+ })
21
+
22
+ it('ignores a result in another textblock', () => {
23
+ expect(resultInTextblocks({ from: 6, to: 9 }, textblocks)).toBe(false)
24
+ })
25
+ })
@@ -13,12 +13,12 @@ export function resultInTextblocks(
13
13
  const index = Math.floor((start + end) / 2)
14
14
  const textblock = textblocks[index]
15
15
 
16
- if (result.from < textblock.from) {
16
+ if (result.to <= textblock.from) {
17
17
  end = index - 1
18
18
  continue
19
19
  }
20
20
 
21
- if (result.to > textblock.to) {
21
+ if (result.from >= textblock.to) {
22
22
  start = index + 1
23
23
  continue
24
24
  }
@@ -1,5 +1,5 @@
1
- import type { Node } from '@tiptap/pm/model'
2
- import type { Mapping } from '@tiptap/pm/transform'
1
+ import type { Node as PMNode } from '@tiptap/pm/model'
2
+ import type { Transform } from '@tiptap/pm/transform'
3
3
 
4
4
  import type { FindAndReplaceMeta, FindAndReplacePluginState } from '../plugin/plugin-state.js'
5
5
 
@@ -12,17 +12,16 @@ import { touchesSearch } from './touchesSearch.js'
12
12
  export function updateState(
13
13
  previousState: FindAndReplacePluginState,
14
14
  state: FindAndReplacePluginState,
15
- doc: Node,
15
+ doc: PMNode,
16
16
  meta: FindAndReplaceMeta | undefined,
17
- docChanged: boolean,
18
- mapping: Mapping,
17
+ transform: Transform,
19
18
  ): FindAndReplacePluginState {
20
19
  if (touchesSearch(meta)) {
21
- return refreshState(previousState, state, doc, meta, docChanged, mapping)
20
+ return refreshState(previousState, state, doc, meta, transform.docChanged, transform.mapping)
22
21
  }
23
22
 
24
- if (docChanged) {
25
- return refreshDocumentState(previousState, state, doc, meta, mapping)
23
+ if (transform.docChanged) {
24
+ return refreshDocumentState(previousState, state, doc, meta, transform)
26
25
  }
27
26
 
28
27
  if (hasCurrentIndex(meta)) {