diff --git a/.changeset/thirty-frogs-repeat.md b/.changeset/thirty-frogs-repeat.md new file mode 100644 index 0000000000..f631cd2318 --- /dev/null +++ b/.changeset/thirty-frogs-repeat.md @@ -0,0 +1,5 @@ +--- +"@tiptap/extension-find-and-replace": patch +--- + +Ensure only the active find-and-replace result keeps the current-result highlight while navigating matches. \ No newline at end of file diff --git a/packages/extension-find-and-replace/__tests__/find-and-replace.spec.ts b/packages/extension-find-and-replace/__tests__/find-and-replace.spec.ts index 58ed36b4e7..7b1b540d0f 100644 --- a/packages/extension-find-and-replace/__tests__/find-and-replace.spec.ts +++ b/packages/extension-find-and-replace/__tests__/find-and-replace.spec.ts @@ -288,19 +288,31 @@ describe('FindAndReplace', () => { editor = createEditor('
one two one two one
') editor.commands.setSearchTerm('one') + const currentResultPositions = () => { + return Array.from(editor.view.dom.querySelectorAll('.find-and-replace-result-current')).map( + element => editor.view.posAtDOM(element, 0), + ) + } + expect(editor.storage.findAndReplace.currentIndex).toBe(0) + expect(currentResultPositions()).toEqual([1]) editor.commands.goToNextResult() expect(editor.storage.findAndReplace.currentIndex).toBe(1) expect(editor.state.selection.from).toBe(9) expect(editor.state.selection.to).toBe(12) + expect(currentResultPositions()).toEqual([9]) editor.commands.goToNextResult() + expect(currentResultPositions()).toEqual([17]) + editor.commands.goToNextResult() expect(editor.storage.findAndReplace.currentIndex).toBe(0) + expect(currentResultPositions()).toEqual([1]) editor.commands.goToPreviousResult() expect(editor.storage.findAndReplace.currentIndex).toBe(2) + expect(currentResultPositions()).toEqual([17]) }) it('replaces the current result and jumps to the next one', () => { diff --git a/packages/extension-find-and-replace/src/utils/findDecorations.ts b/packages/extension-find-and-replace/src/utils/findDecorations.ts index dd6ba47600..a021c4c9b7 100644 --- a/packages/extension-find-and-replace/src/utils/findDecorations.ts +++ b/packages/extension-find-and-replace/src/utils/findDecorations.ts @@ -17,7 +17,8 @@ export function findDecorations( results.forEach(result => { decorations - .find(result.from, result.to, decoration => { + .find(result.from, result.to) + .filter(decoration => { return decoration.from === result.from && decoration.to === result.to }) .forEach(decoration => found.add(decoration))