Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/plugins/searchDecorations.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,34 @@ export function runSearch(doc, query, options) {
}
}

const mentionQuery = query.trim().startsWith('@')
? query.trim().slice(1).toLowerCase()
: query.trim().toLowerCase()

doc.descendants((node, offset, _position) => {
if (!node.isText) {
// Add decorations for text matches
if (node.isText) {
const matches = node.text.matchAll(new RegExp(query, 'gi'))

for (const match of matches) {
results.push({
from: match.index + offset,
to: match.index + offset + query.length,
})
}

return
}

const matches = node.text.matchAll(new RegExp(query, 'gi'))

for (const match of matches) {
results.push({
from: match.index + offset,
to: match.index + offset + query.length,
})
// Add decorations for mention matches
if (node.type.name === 'mention' && mentionQuery !== '') {
if (node.attrs.label.toLowerCase().startsWith(mentionQuery)) {
results.push({
from: offset,
to: offset + node.nodeSize,
mention: true,
})
}
}
})

Expand Down Expand Up @@ -142,7 +158,9 @@ export function highlightResults(doc, results) {
decorations.push(
Decoration.inline(result.from, result.to, {
'data-text-el': 'search-decoration',
style: 'background-color: #ead637; color: black; border-radius: 2px;',
style: result.mention
? 'outline: 2px solid #ead637; background-color: #ead637; color: black; border-radius: 2px;'
: 'background-color: #ead637; color: black; border-radius: 2px;',
}),
)
})
Expand Down
27 changes: 26 additions & 1 deletion src/tests/plugins/searchDecorations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import Mentions from '../../extensions/Mention.js'
import { highlightResults, runSearch } from '../../plugins/searchDecorations.js'
import createCustomEditor from '../testHelpers/createCustomEditor.ts'

Expand Down Expand Up @@ -76,10 +77,34 @@ describe('search plugin', () => {

testSearch(doc, 'cat', expected)
})

it('finds matches with mentions', () => {
const doc =
'<p>janes task, mention <span class="mention" data-type="user" data-id="jane.doe" data-label="Jane Doe">Jane Doe</span></p>'

const expected = {
results: [
{ from: 1, to: 5 },
{ from: 21, to: 22, mention: true },
],
}

testSearch(doc, 'jane', expected)
})

it('finds matches with mentions with @', () => {
const doc =
'<p>janes task, mention <span class="mention" data-type="user" data-id="jane.doe" data-label="Jane Doe">Jane Doe</span></p>'

const expected = {
results: [{ from: 21, to: 22, mention: true }],
}
testSearch(doc, '@jane', expected)
})
})

const testSearch = (content, query, expectedSearchResults) => {
const doc = createCustomEditor(content).state.doc
const doc = createCustomEditor(content, [Mentions]).state.doc
const searched = runSearch(doc, query)
expect(searched).toHaveProperty('results', expectedSearchResults.results)
expect(highlightResults(doc, searched.results)).toEqual(
Expand Down
Loading