-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Move search highlighting to editor API (#6199)
* feat(search): Move to editor API instead of event bus Signed-off-by: Elizabeth Danzberger <lizzy7128@tutanota.de>
- Loading branch information
Showing
6 changed files
with
148 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { Editor } from '@tiptap/core' | ||
import { Document } from '@tiptap/extension-document' | ||
import { Text } from '@tiptap/extension-text' | ||
import Search from '../../../src/extensions/Search.js' | ||
import Paragraph from '../../../src/nodes/Paragraph.js' | ||
import HardBreak from '../../../src/nodes/HardBreak.js' | ||
|
||
describe('editor search highlighting', () => { | ||
let editor = null | ||
|
||
before(() => { | ||
cy.fixture('lorem.txt').then((text) => { | ||
editor = new Editor({ | ||
element: document.querySelector('div[data-cy-root]'), | ||
content: text, | ||
extensions: [Document, Text, Search, Paragraph, HardBreak], | ||
}) | ||
}) | ||
}) | ||
|
||
it('can highlight a match', () => { | ||
const searchQuery = 'Lorem ipsum dolor sit amet' | ||
editor.commands.setSearchQuery(searchQuery) | ||
|
||
const highlightedElements = document.querySelectorAll('span[data-text-el="search-decoration"]') | ||
expect(highlightedElements).to.have.lengthOf(1) | ||
verifyHighlights(highlightedElements, searchQuery) | ||
}) | ||
|
||
it('can highlight multiple matches', () => { | ||
const searchQuery = 'quod' | ||
editor.commands.setSearchQuery(searchQuery) | ||
|
||
const highlightedElements = document.querySelectorAll('span[data-text-el="search-decoration"]') | ||
expect(highlightedElements).to.have.lengthOf(3) | ||
verifyHighlights(highlightedElements, searchQuery) | ||
}) | ||
|
||
it('can toggle highlight all', () => { | ||
const searchQuery = 'quod' | ||
let highlightedElements = [] | ||
|
||
// Highlight only first occurrence | ||
editor.commands.setSearchQuery(searchQuery, false) | ||
highlightedElements = document.querySelectorAll('span[data-text-el="search-decoration"]') | ||
|
||
expect(highlightedElements).to.have.lengthOf(1) | ||
verifyHighlights(highlightedElements, searchQuery) | ||
}) | ||
|
||
it('can move to next occurrence', () => { | ||
const searchQuery = 'quod' | ||
|
||
editor.commands.setSearchQuery(searchQuery, true) | ||
const allHighlightedElements = document.querySelectorAll('span[data-text-el="search-decoration"]') | ||
|
||
editor.commands.nextMatch() | ||
const currentlyHighlightedElement = document.querySelectorAll('span[data-text-el="search-decoration"]') | ||
|
||
expect(currentlyHighlightedElement).to.have.lengthOf(1) | ||
expect(allHighlightedElements[1]).to.deep.equal(currentlyHighlightedElement[0]) | ||
}) | ||
|
||
it('can move to previous occurrence', () => { | ||
const searchQuery = 'quod' | ||
|
||
editor.commands.setSearchQuery(searchQuery, true) | ||
const allHighlightedElements = document.querySelectorAll('span[data-text-el="search-decoration"]') | ||
|
||
editor.commands.previousMatch() | ||
const currentlyHighlightedElement = document.querySelectorAll('span[data-text-el="search-decoration"]') | ||
|
||
expect(currentlyHighlightedElement).to.have.lengthOf(1) | ||
expect(allHighlightedElements[0]).to.deep.equal(currentlyHighlightedElement[0]) | ||
}) | ||
}) | ||
|
||
/** | ||
* Verifies the Nodes in the given NodeList match the search query | ||
* @param {NodeList} highlightedElements - NodeList of highlighted elements | ||
* @param {string} searchQuery - search query | ||
*/ | ||
function verifyHighlights(highlightedElements, searchQuery) { | ||
for (const element of highlightedElements) { | ||
expect(element.innerText.toLowerCase()).to.equal(searchQuery.toLowerCase()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<p> | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim aeque doleamus animo, cum corpore dolemus, fieri tamen permagna accessio potest, si aliquod aeternum et infinitum impendere malum nobis opinemur. Quod idem licet transferre in voluptatem, ut postea variari voluptas distinguique possit, augeri amplificarique non possit. At. | ||
</p> | ||
|
||
<p> | ||
Ullus investigandi veri, nisi inveneris, et quaerendi defatigatio turpis est, cum esset accusata et vituperata ab Hortensio. Qui liber cum et mortem contemnit, qua qui est imbutus quietus esse numquam potest. Praeterea bona praeterita grata recordatione renovata delectant. Est autem situm in nobis ut et voluptates et dolores nasci fatemur e corporis voluptatibus et doloribus -- itaque concedo, quod modo dicebas, cadere. | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters