@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
@@ -0,0 +1,293 @@
1
+ import type { JSONContent } from '@tiptap/core'
2
+ import { Editor } from '@tiptap/core'
3
+ import Blockquote from '@tiptap/extension-blockquote'
4
+ import Document from '@tiptap/extension-document'
5
+ import HorizontalRule from '@tiptap/extension-horizontal-rule'
6
+ import Paragraph from '@tiptap/extension-paragraph'
7
+ import Text from '@tiptap/extension-text'
8
+ import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
9
+
10
+ import { ContinuedParagraph } from '../tests/utils/ContinuedParagraph.js'
11
+ import { continuesNode, paragraph } from '../tests/utils/createContinuedDoc.js'
12
+ import { FindAndReplace } from './find-and-replace.js'
13
+ import type { FindAndReplaceOptions } from './types.js'
14
+
15
+ describe('result navigation', () => {
16
+ let editor: Editor
17
+
18
+ const createEditor = (content: string, options: Partial<FindAndReplaceOptions> = {}) => {
19
+ return new Editor({
20
+ extensions: [
21
+ Document,
22
+ Paragraph,
23
+ Text,
24
+ FindAndReplace.configure({ searchDebounceMs: 0, ...options }),
25
+ ],
26
+ content,
27
+ })
28
+ }
29
+
30
+ const selectedText = () => {
31
+ const { from, to } = editor.state.selection
32
+
33
+ return editor.state.doc.textBetween(from, to)
34
+ }
35
+
36
+ afterEach(() => {
37
+ editor.destroy()
38
+ vi.useRealTimers()
39
+ })
40
+
41
+ it('goes to the first result on the first next', () => {
42
+ editor = createEditor('<p>one two one</p>')
43
+ editor.commands.setSearchTerm('one')
44
+
45
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
46
+
47
+ editor.commands.goToNextResult()
48
+
49
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
50
+ expect(editor.state.selection.from).toBe(1)
51
+ expect(selectedText()).toBe('one')
52
+
53
+ editor.commands.goToNextResult()
54
+
55
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
56
+ expect(editor.state.selection.from).toBe(9)
57
+ })
58
+
59
+ it('goes to the first result on the first previous', () => {
60
+ editor = createEditor('<p>one two one</p>')
61
+ editor.commands.setSearchTerm('one')
62
+ editor.commands.goToPreviousResult()
63
+
64
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
65
+ expect(editor.state.selection.from).toBe(1)
66
+
67
+ editor.commands.goToPreviousResult()
68
+
69
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
70
+ expect(editor.state.selection.from).toBe(9)
71
+ })
72
+
73
+ it('goes to the first result again after the search term changes', () => {
74
+ editor = createEditor('<p>one two one two</p>')
75
+ editor.commands.setSearchTerm('one')
76
+ editor.commands.goToNextResult()
77
+ editor.commands.goToNextResult()
78
+ editor.commands.setSearchTerm('two')
79
+ editor.commands.goToNextResult()
80
+
81
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
82
+ expect(editor.state.selection.from).toBe(5)
83
+ })
84
+
85
+ it('goes to the first result of a pending search', () => {
86
+ vi.useFakeTimers()
87
+ editor = createEditor('<p>one two one</p>', { searchDebounceMs: 250 })
88
+ editor.commands.setSearchTerm('one')
89
+ editor.commands.goToNextResult()
90
+
91
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
92
+ expect(editor.state.selection.from).toBe(1)
93
+ })
94
+
95
+ it('keeps stepping forward after an edit outside the current result', () => {
96
+ editor = createEditor('<p>one two one two one</p>')
97
+ editor.commands.setSearchTerm('one')
98
+ editor.commands.goToNextResult()
99
+ editor.commands.goToNextResult()
100
+ editor.commands.insertContentAt(1, 'x')
101
+ editor.commands.goToNextResult()
102
+
103
+ expect(editor.storage.findAndReplace.currentIndex).toBe(2)
104
+ })
105
+
106
+ it('keeps stepping forward after a search option changes', () => {
107
+ editor = createEditor('<p>one two one</p>')
108
+ editor.commands.setSearchTerm('one')
109
+ editor.commands.goToNextResult()
110
+ editor.commands.setCaseSensitive(true)
111
+ editor.commands.goToNextResult()
112
+
113
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
114
+ expect(editor.state.selection.from).toBe(9)
115
+ })
116
+
117
+ it('stays on the visited result when a search option adds a result before it', () => {
118
+ editor = createEditor('<p>FOO foo</p>', { caseSensitive: true })
119
+ editor.commands.setSearchTerm('foo')
120
+ editor.commands.goToNextResult()
121
+ editor.commands.setCaseSensitive(false)
122
+
123
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
124
+
125
+ editor.commands.goToNextResult()
126
+
127
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
128
+ expect(editor.state.selection.from).toBe(1)
129
+ })
130
+
131
+ it('goes to a new current result after a search option removes the visited one', () => {
132
+ editor = createEditor('<p>One one</p>')
133
+ editor.commands.setSearchTerm('one')
134
+ editor.commands.goToNextResult()
135
+ editor.commands.setCaseSensitive(true)
136
+ editor.commands.goToNextResult()
137
+
138
+ expect(editor.storage.findAndReplace.currentIndex).toBe(0)
139
+ expect(editor.state.selection.from).toBe(5)
140
+ })
141
+
142
+ it('goes to a new current result after the visited one is removed', () => {
143
+ editor = createEditor('<p>one two one two one</p>')
144
+ editor.commands.setSearchTerm('one')
145
+ editor.commands.goToNextResult()
146
+ editor.commands.goToNextResult()
147
+ editor.commands.deleteRange({ from: 9, to: 12 })
148
+
149
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
150
+
151
+ editor.commands.goToNextResult()
152
+
153
+ expect(editor.storage.findAndReplace.currentIndex).toBe(1)
154
+ expect(selectedText()).toBe('one')
155
+ })
156
+ })
157
+
158
+ describe('continued textblocks', () => {
159
+ let editor: Editor
160
+
161
+ const createEditor = (paragraphs: JSONContent[]) => {
162
+ return new Editor({
163
+ extensions: [
164
+ Document,
165
+ ContinuedParagraph,
166
+ Text,
167
+ Blockquote,
168
+ HorizontalRule,
169
+ FindAndReplace.configure({ searchDebounceMs: 0, continuesNode }),
170
+ ],
171
+ content: { type: 'doc', content: paragraphs },
172
+ })
173
+ }
174
+
175
+ afterEach(() => {
176
+ editor.destroy()
177
+ })
178
+
179
+ it('highlights a result in both textblocks', () => {
180
+ editor = createEditor([paragraph('find the '), paragraph('phrase here', true)])
181
+ editor.commands.setSearchTerm('the phrase')
182
+
183
+ const highlights = editor.view.dom.querySelectorAll('.find-and-replace-result')
184
+
185
+ expect(editor.storage.findAndReplace.results).toEqual([{ from: 6, to: 18 }])
186
+ expect([...highlights].map(highlight => highlight.textContent)).toEqual(['the ', 'phrase'])
187
+ })
188
+
189
+ it('selects a result across the boundary', () => {
190
+ editor = createEditor([paragraph('find the '), paragraph('phrase here', true)])
191
+ editor.commands.setSearchTerm('the phrase')
192
+ editor.commands.goToNextResult()
193
+
194
+ expect(editor.state.selection.from).toBe(6)
195
+ expect(editor.state.selection.to).toBe(18)
196
+ })
197
+
198
+ it('replaces a result across the boundary', () => {
199
+ editor = createEditor([paragraph('find the '), paragraph('phrase here', true)])
200
+ editor.commands.setSearchTerm('the phrase')
201
+ editor.commands.setReplaceTerm('a word')
202
+ editor.commands.replace()
203
+
204
+ expect(editor.getText()).toBe('find a word here')
205
+ expect(editor.storage.findAndReplace.results).toEqual([])
206
+ })
207
+
208
+ it('replaces all results across the boundary', () => {
209
+ editor = createEditor([paragraph('one the '), paragraph('phrase the phrase', true)])
210
+ editor.commands.setSearchTerm('the phrase')
211
+ editor.commands.setReplaceTerm('it')
212
+ editor.commands.replaceAll()
213
+
214
+ expect(editor.getText()).toBe('one it it')
215
+ })
216
+
217
+ it('updates a result when the continued textblock changes', () => {
218
+ editor = createEditor([paragraph('find the '), paragraph('phrase here', true)])
219
+ editor.commands.setSearchTerm('the phrase')
220
+ editor.commands.insertContentAt(14, 'x')
221
+
222
+ expect(editor.storage.findAndReplace.results).toEqual([])
223
+
224
+ editor.commands.deleteRange({ from: 14, to: 15 })
225
+
226
+ expect(editor.storage.findAndReplace.results).toEqual([{ from: 6, to: 18 }])
227
+ expect(editor.view.dom.querySelectorAll('.find-and-replace-result')).toHaveLength(2)
228
+ })
229
+
230
+ it('keeps one result when the first textblock changes', () => {
231
+ editor = createEditor([paragraph('find the '), paragraph('phrase here', true)])
232
+ editor.commands.setSearchTerm('the phrase')
233
+ editor.commands.insertContentAt(1, 'x')
234
+
235
+ expect(editor.storage.findAndReplace.results).toEqual([{ from: 7, to: 19 }])
236
+ expect(editor.view.dom.querySelectorAll('.find-and-replace-result')).toHaveLength(2)
237
+ })
238
+
239
+ it('drops a result when the textblock stops continuing', () => {
240
+ editor = createEditor([paragraph('find the '), paragraph('phrase here', true)])
241
+ editor.commands.setSearchTerm('the phrase')
242
+ editor.view.dispatch(editor.state.tr.setNodeAttribute(11, 'continued', false))
243
+
244
+ expect(editor.storage.findAndReplace.results).toEqual([])
245
+ expect(editor.view.dom.querySelectorAll('.find-and-replace-result')).toHaveLength(0)
246
+ })
247
+
248
+ it('finds a whole word at the end of a textblock that is no longer continued', () => {
249
+ editor = createEditor([paragraph('hel'), paragraph('lo', true)])
250
+ editor.commands.setWholeWord(true)
251
+ editor.commands.setSearchTerm('hel')
252
+
253
+ expect(editor.storage.findAndReplace.results).toEqual([])
254
+
255
+ editor.view.dispatch(editor.state.tr.setNodeAttribute(5, 'continued', false))
256
+
257
+ expect(editor.storage.findAndReplace.results).toEqual([{ from: 1, to: 4 }])
258
+ })
259
+
260
+ it('finds a result when a textblock starts continuing', () => {
261
+ editor = createEditor([paragraph('find the '), paragraph('phrase here')])
262
+ editor.commands.setSearchTerm('the phrase')
263
+ editor.view.dispatch(editor.state.tr.setNodeAttribute(11, 'continued', true))
264
+
265
+ expect(editor.storage.findAndReplace.results).toEqual([{ from: 6, to: 18 }])
266
+ })
267
+
268
+ it('drops a result when a block is inserted between the textblocks', () => {
269
+ editor = createEditor([paragraph('find the '), paragraph('phrase here', true)])
270
+ editor.commands.setSearchTerm('the phrase')
271
+ editor.commands.insertContentAt(11, { type: 'horizontalRule' })
272
+
273
+ expect(editor.storage.findAndReplace.results).toEqual([])
274
+ })
275
+
276
+ it('updates a result when a block between two parents is inserted or removed', () => {
277
+ editor = createEditor([
278
+ { type: 'blockquote', content: [paragraph('hel')] },
279
+ { type: 'blockquote', content: [paragraph('lo', true)] },
280
+ ])
281
+ editor.commands.setSearchTerm('hello')
282
+
283
+ expect(editor.storage.findAndReplace.results).toEqual([{ from: 2, to: 11 }])
284
+
285
+ editor.view.dispatch(editor.state.tr.insert(7, editor.schema.nodes.horizontalRule.create()))
286
+
287
+ expect(editor.storage.findAndReplace.results).toEqual([])
288
+
289
+ editor.view.dispatch(editor.state.tr.delete(7, 8))
290
+
291
+ expect(editor.storage.findAndReplace.results).toEqual([{ from: 2, to: 11 }])
292
+ })
293
+ })
@@ -142,11 +142,7 @@ function getPendingAwarePluginState(
142
142
  return pluginState.results.length === 0 ? null : pluginState
143
143
  }
144
144
 
145
- const results = searchDocument(state.doc, pendingTerm, {
146
- caseSensitive: pluginState.caseSensitive,
147
- useRegex: pluginState.useRegex,
148
- wholeWord: pluginState.wholeWord,
149
- })
145
+ const results = searchDocument(state.doc, pendingTerm, pluginState)
150
146
 
151
147
  if (results.length === 0) {
152
148
  return null
@@ -157,19 +153,42 @@ function getPendingAwarePluginState(
157
153
  searchTerm: pendingTerm,
158
154
  results,
159
155
  currentIndex: 0,
156
+ currentResultVisited: false,
160
157
  }
161
158
  }
162
159
 
160
+ function flushPendingSearch(
161
+ editor: Editor,
162
+ tr: Transaction,
163
+ state: EditorState,
164
+ dispatch: boolean,
165
+ ): FindAndReplacePluginState | null {
166
+ const pendingTerm = dispatch ? consumePendingSearch(editor) : null
167
+
168
+ if (dispatch && pendingTerm !== null) {
169
+ mergePluginMeta(tr, { searchTerm: pendingTerm })
170
+ }
171
+
172
+ return getPendingAwarePluginState(state, pendingTerm)
173
+ }
174
+
163
175
  function currentResult(pluginState: FindAndReplacePluginState): SearchResult | undefined {
164
176
  return pluginState.results[pluginState.currentIndex ?? 0]
165
177
  }
166
178
 
167
- function shiftIndex(currentIndex: number | null, length: number, direction: 1 | -1): number {
179
+ function shiftIndex(pluginState: FindAndReplacePluginState, direction: 1 | -1): number {
180
+ const { currentIndex, currentResultVisited, results } = pluginState
181
+
168
182
  if (currentIndex === null) {
169
- return direction === 1 ? 0 : length - 1
183
+ return direction === 1 ? 0 : results.length - 1
184
+ }
185
+
186
+ // Go to the highlighted result first instead of skipping it
187
+ if (!currentResultVisited) {
188
+ return currentIndex
170
189
  }
171
190
 
172
- return (currentIndex + direction + length) % length
191
+ return (currentIndex + direction + results.length) % results.length
173
192
  }
174
193
 
175
194
  function selectResult(tr: Transaction, result: SearchResult, index: number): void {
@@ -183,13 +202,13 @@ function replaceResult(
183
202
  pluginState: FindAndReplacePluginState,
184
203
  result: SearchResult,
185
204
  ): void {
186
- const { searchTerm, replaceTerm, caseSensitive, useRegex, wholeWord } = pluginState
205
+ const { searchTerm, replaceTerm } = pluginState
187
206
 
188
207
  tr.insertText(replaceTerm, result.from, result.to)
189
208
 
190
209
  // Jump to the first result behind the inserted text, so a replacement
191
210
  // that still matches the search (e.g. "foo" -> "foobar") is skipped.
192
- const results = searchDocument(tr.doc, searchTerm, { caseSensitive, useRegex, wholeWord })
211
+ const results = searchDocument(tr.doc, searchTerm, pluginState)
193
212
  const nextIndex = findNextIndex(results, result.from + replaceTerm.length)
194
213
 
195
214
  if (nextIndex === null) {
@@ -210,6 +229,7 @@ export const FindAndReplace = Extension.create<FindAndReplaceOptions, FindAndRep
210
229
  caseSensitive: false,
211
230
  useRegex: false,
212
231
  wholeWord: false,
232
+ continuesNode: () => false,
213
233
  searchDebounceMs: 250,
214
234
  injectCSS: true,
215
235
  injectNonce: undefined,
@@ -293,6 +313,24 @@ export const FindAndReplace = Extension.create<FindAndReplaceOptions, FindAndRep
293
313
  return true
294
314
  }
295
315
 
316
+ const goToResult =
317
+ (direction: 1 | -1): Command =>
318
+ ({ tr, state, dispatch }) => {
319
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch)
320
+
321
+ if (!pluginState) {
322
+ return false
323
+ }
324
+
325
+ const index = shiftIndex(pluginState, direction)
326
+
327
+ if (dispatch) {
328
+ selectResult(tr, pluginState.results[index], index)
329
+ }
330
+
331
+ return true
332
+ }
333
+
296
334
  return {
297
335
  setSearchTerm:
298
336
  term =>
@@ -329,13 +367,7 @@ export const FindAndReplace = Extension.create<FindAndReplaceOptions, FindAndRep
329
367
  replace:
330
368
  () =>
331
369
  ({ 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)
370
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch)
339
371
 
340
372
  if (!pluginState) {
341
373
  return false
@@ -357,13 +389,7 @@ export const FindAndReplace = Extension.create<FindAndReplaceOptions, FindAndRep
357
389
  replaceAll:
358
390
  () =>
359
391
  ({ 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)
392
+ const pluginState = flushPendingSearch(this.editor, tr, state, !!dispatch)
367
393
 
368
394
  if (!pluginState) {
369
395
  return false
@@ -376,53 +402,8 @@ export const FindAndReplace = Extension.create<FindAndReplaceOptions, FindAndRep
376
402
  return true
377
403
  },
378
404
 
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
- },
405
+ goToNextResult: () => goToResult(1),
406
+ goToPreviousResult: () => goToResult(-1),
426
407
  }
427
408
  },
428
409
 
@@ -1,3 +1,4 @@
1
+ import type { Node as PMNode } from '@tiptap/pm/model'
1
2
  import type { DecorationSet } from '@tiptap/pm/view'
2
3
 
3
4
  import type { SearchResult } from '../search/search.js'
@@ -8,8 +9,11 @@ export interface FindAndReplacePluginState {
8
9
  caseSensitive: boolean
9
10
  useRegex: boolean
10
11
  wholeWord: boolean
12
+ continuesNode: (node: PMNode) => boolean
11
13
  results: SearchResult[]
12
14
  currentIndex: number | null
15
+ /** Whether the current result was already selected by navigation. */
16
+ currentResultVisited: boolean
13
17
  decorations: DecorationSet
14
18
  }
15
19
 
@@ -38,14 +38,7 @@ export const FindAndReplacePlugin = (
38
38
  return state
39
39
  }
40
40
 
41
- return updateState(
42
- state,
43
- { ...state, ...meta },
44
- newState.doc,
45
- meta,
46
- tr.docChanged,
47
- tr.mapping,
48
- )
41
+ return updateState(state, { ...state, ...meta }, newState.doc, meta, tr)
49
42
  },
50
43
  },
51
44
 
@@ -0,0 +1,39 @@
1
+ import type { Node as PMNode } from '@tiptap/pm/model'
2
+
3
+ import { hasBlockBetween } from './has-block-between.js'
4
+ import type { TextblockSearchTarget } from './types.js'
5
+
6
+ /**
7
+ * Groups textblocks that continue the textblock before them in the list.
8
+ * @param doc The document the textblocks are in.
9
+ * @param textblocks Whole chains of continued textblocks in document order.
10
+ * @param continuesNode Whether a textblock continues the one before it.
11
+ * @returns The groups to search as one text each.
12
+ */
13
+ export function groupContinuedTextblocks(
14
+ doc: PMNode,
15
+ textblocks: readonly TextblockSearchTarget[],
16
+ continuesNode: (node: PMNode) => boolean = () => false,
17
+ ): TextblockSearchTarget[][] {
18
+ const groups: TextblockSearchTarget[][] = []
19
+
20
+ textblocks.forEach(textblock => {
21
+ const group = groups.at(-1)
22
+ const previous = group?.at(-1)
23
+
24
+ // A block between them would be deleted when a match across them is replaced
25
+ if (
26
+ group &&
27
+ previous &&
28
+ continuesNode(textblock.node) &&
29
+ !hasBlockBetween(doc, previous.pos + previous.node.nodeSize, textblock.pos)
30
+ ) {
31
+ group.push(textblock)
32
+ return
33
+ }
34
+
35
+ groups.push([textblock])
36
+ })
37
+
38
+ return groups
39
+ }
@@ -0,0 +1,27 @@
1
+ import { describe, expect, it } from 'vite-plus/test'
2
+
3
+ import { createContinuedDoc, paragraph } from '../../tests/utils/createContinuedDoc.js'
4
+ import { hasBlockBetween } from './has-block-between.js'
5
+
6
+ describe('hasBlockBetween', () => {
7
+ it('ignores nothing between two textblocks', () => {
8
+ const doc = createContinuedDoc([paragraph('one'), paragraph('two')])
9
+
10
+ expect(hasBlockBetween(doc, 5, 5)).toBe(false)
11
+ })
12
+
13
+ it('ignores parents that close and open between two textblocks', () => {
14
+ const doc = createContinuedDoc([
15
+ { type: 'blockquote', content: [paragraph('one')] },
16
+ { type: 'blockquote', content: [paragraph('two')] },
17
+ ])
18
+
19
+ expect(hasBlockBetween(doc, 6, 8)).toBe(false)
20
+ })
21
+
22
+ it('finds a block between two textblocks', () => {
23
+ const doc = createContinuedDoc([paragraph('one'), { type: 'horizontalRule' }, paragraph('two')])
24
+
25
+ expect(hasBlockBetween(doc, 5, 6)).toBe(true)
26
+ })
27
+ })
@@ -0,0 +1,28 @@
1
+ import type { Node as PMNode } from '@tiptap/pm/model'
2
+
3
+ /**
4
+ * Checks if a whole node lies between two positions.
5
+ * Nodes that only open or close in the range, like the parents of two textblocks, do not count.
6
+ * @param doc The document to check.
7
+ * @param from The start of the range.
8
+ * @param to The end of the range.
9
+ * @returns Whether a node lies fully inside the range.
10
+ */
11
+ export function hasBlockBetween(doc: PMNode, from: number, to: number): boolean {
12
+ let found = false
13
+
14
+ doc.nodesBetween(from, to, (node, pos) => {
15
+ if (found) {
16
+ return false
17
+ }
18
+
19
+ if (pos >= from && pos + node.nodeSize <= to) {
20
+ found = true
21
+ return false
22
+ }
23
+
24
+ return true
25
+ })
26
+
27
+ return found
28
+ }