@tiptap/extension-find-and-replace 3.33.1 → 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.
@@ -1,29 +1,77 @@
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
+ import { AttrStep } from '@tiptap/pm/transform'
3
4
 
5
+ import { findAdjacentTextblock } from './findAdjacentTextblock.js'
6
+ import { findNearestTextblock } from './findNearestTextblock.js'
4
7
  import type { TextblockRange } from './types.js'
5
8
 
6
- export function getChangedTextblocks(doc: Node, mapping: Mapping): TextblockRange[] {
9
+ function addTextblock(
10
+ textblocks: Map<number, TextblockRange>,
11
+ node: PMNode,
12
+ pos: number,
13
+ ): TextblockRange {
14
+ const textblock = { node, pos, from: pos, to: pos + node.nodeSize }
15
+
16
+ textblocks.set(pos, textblock)
17
+
18
+ return textblock
19
+ }
20
+
21
+ // Attribute steps change no ranges, but can join or split a textblock and the one before it
22
+ function addAttributeChanges(
23
+ doc: PMNode,
24
+ transform: Transform,
25
+ textblocks: Map<number, TextblockRange>,
26
+ ): void {
27
+ transform.steps.forEach((step, index) => {
28
+ if (!(step instanceof AttrStep)) {
29
+ return
30
+ }
31
+
32
+ const pos = transform.mapping.slice(index + 1).map(step.pos)
33
+ const node = doc.nodeAt(pos)
34
+
35
+ if (!node?.isTextblock) {
36
+ return
37
+ }
38
+
39
+ const textblock = addTextblock(textblocks, node, pos)
40
+ const previous = findAdjacentTextblock(doc, textblock, -1)
41
+
42
+ if (previous) {
43
+ textblocks.set(previous.pos, previous)
44
+ }
45
+ })
46
+ }
47
+
48
+ export function getChangedTextblocks(doc: PMNode, transform: Transform): TextblockRange[] {
49
+ const { mapping } = transform
7
50
  const textblocks = new Map<number, TextblockRange>()
8
51
 
9
52
  mapping.maps.forEach((stepMap, index) => {
10
53
  const remaining = mapping.slice(index + 1)
11
54
 
12
55
  stepMap.forEach((_oldStart, _oldEnd, newStart, newEnd) => {
13
- const from = Math.max(0, remaining.map(newStart, -1) - 1)
14
- const to = Math.min(doc.content.size, remaining.map(newEnd, 1) + 1)
56
+ // A change between two parents can join or split the textblocks inside them
57
+ const before = findNearestTextblock(doc, remaining.map(newStart, -1), -1)
58
+ const after = findNearestTextblock(doc, remaining.map(newEnd, 1), 1)
59
+ const from = before?.from ?? 0
60
+ const to = after?.to ?? doc.content.size
15
61
 
16
62
  doc.nodesBetween(from, to, (node, pos) => {
17
63
  if (!node.isTextblock) {
18
64
  return true
19
65
  }
20
66
 
21
- textblocks.set(pos, { node, pos, from: pos, to: pos + node.nodeSize })
67
+ addTextblock(textblocks, node, pos)
22
68
 
23
69
  return false
24
70
  })
25
71
  })
26
72
  })
27
73
 
74
+ addAttributeChanges(doc, transform, textblocks)
75
+
28
76
  return [...textblocks.values()].sort((first, second) => first.pos - second.pos)
29
77
  }
@@ -1,9 +1,10 @@
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'
8
9
  import { hasCurrentIndex } from './hasCurrentIndex.js'
9
10
  import { keepsVisitedResult } from './keepsVisitedResult.js'
@@ -17,14 +18,19 @@ import { resultInTextblocks } from './resultInTextblocks.js'
17
18
  export function refreshDocumentState(
18
19
  previousState: FindAndReplacePluginState,
19
20
  state: FindAndReplacePluginState,
20
- doc: Node,
21
+ doc: PMNode,
21
22
  meta: FindAndReplaceMeta | undefined,
22
- mapping: Mapping,
23
+ transform: Transform,
23
24
  ): FindAndReplacePluginState {
24
- const textblocks = getChangedTextblocks(doc, mapping)
25
+ const { mapping } = transform
26
+ const textblocks = addContinuedTextblocks(
27
+ doc,
28
+ getChangedTextblocks(doc, transform),
29
+ state.continuesNode,
30
+ )
25
31
  const mappedResults = mapResults(previousState.results, mapping)
26
32
  const unaffectedResults = mappedResults.filter(result => !resultInTextblocks(result, textblocks))
27
- const changedResults = searchTextblocks(textblocks, state.searchTerm, state)
33
+ const changedResults = searchTextblocks(textblocks, state.searchTerm, state, doc)
28
34
  const results = mergeResults(unaffectedResults, changedResults)
29
35
  const currentIndex = resolveCurrentIndex(previousState, results, meta, true, mapping)
30
36
  const previousCurrent =
@@ -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)) {