@tiptap/extension-find-and-replace 3.33.1 → 3.35.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.
- package/dist/index.cjs +174 -50
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -5
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +23 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +175 -51
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/find-and-replace.test.ts +142 -0
- package/src/find-and-replace.ts +4 -7
- package/src/plugin/plugin-state.ts +2 -0
- package/src/plugin/plugin.ts +1 -8
- package/src/search/group-continued-textblocks.ts +39 -0
- package/src/search/has-block-between.test.ts +27 -0
- package/src/search/has-block-between.ts +28 -0
- package/src/search/search.test.ts +100 -0
- package/src/search/search.ts +18 -9
- package/src/search/text-segments.ts +18 -11
- package/src/search/textblock-search.ts +13 -7
- package/src/search/types.ts +3 -2
- package/src/types.ts +16 -0
- package/src/utils/addContinuedTextblocks.test.ts +99 -0
- package/src/utils/addContinuedTextblocks.ts +74 -0
- package/src/utils/createState.ts +6 -2
- package/src/utils/findAdjacentTextblock.ts +14 -0
- package/src/utils/findNearestTextblock.ts +21 -0
- package/src/utils/getChangedTextblocks.test.ts +40 -0
- package/src/utils/getChangedTextblocks.ts +54 -6
- package/src/utils/refreshDocumentState.ts +12 -6
- package/src/utils/resultInTextblocks.test.ts +25 -0
- package/src/utils/resultInTextblocks.ts +2 -2
- package/src/utils/updateState.ts +7 -8
|
@@ -1,29 +1,77 @@
|
|
|
1
|
-
import type { Node } from '@tiptap/pm/model'
|
|
2
|
-
import type {
|
|
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
|
-
|
|
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
|
-
|
|
14
|
-
const
|
|
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
|
|
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 {
|
|
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:
|
|
21
|
+
doc: PMNode,
|
|
21
22
|
meta: FindAndReplaceMeta | undefined,
|
|
22
|
-
|
|
23
|
+
transform: Transform,
|
|
23
24
|
): FindAndReplacePluginState {
|
|
24
|
-
const
|
|
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.
|
|
16
|
+
if (result.to <= textblock.from) {
|
|
17
17
|
end = index - 1
|
|
18
18
|
continue
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
if (result.
|
|
21
|
+
if (result.from >= textblock.to) {
|
|
22
22
|
start = index + 1
|
|
23
23
|
continue
|
|
24
24
|
}
|
package/src/utils/updateState.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Node } from '@tiptap/pm/model'
|
|
2
|
-
import type {
|
|
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:
|
|
15
|
+
doc: PMNode,
|
|
16
16
|
meta: FindAndReplaceMeta | undefined,
|
|
17
|
-
|
|
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,
|
|
23
|
+
if (transform.docChanged) {
|
|
24
|
+
return refreshDocumentState(previousState, state, doc, meta, transform)
|
|
26
25
|
}
|
|
27
26
|
|
|
28
27
|
if (hasCurrentIndex(meta)) {
|