Skip to content

Commit 3d9c6a2

Browse files
mejo-backportbot[bot]
authored andcommitted
fix(search): also search for user mentions
Adds user mentions to results if the display name starts with search query. If search query starts with `@`, strip it before checking if user mention matches. Contributes to nextcloud/collectives#2299 Signed-off-by: Jonas <jonas@freesources.org> [skip ci]
1 parent 7df2597 commit 3d9c6a2

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

src/plugins/searchDecorations.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,34 @@ export function runSearch(doc, query, options) {
9191
}
9292
}
9393

94+
const mentionQuery = query.trim().startsWith('@')
95+
? query.trim().slice(1).toLowerCase()
96+
: query.trim().toLowerCase()
97+
9498
doc.descendants((node, offset, _position) => {
95-
if (!node.isText) {
99+
// Add decorations for text matches
100+
if (node.isText) {
101+
const matches = node.text.matchAll(new RegExp(query, 'gi'))
102+
103+
for (const match of matches) {
104+
results.push({
105+
from: match.index + offset,
106+
to: match.index + offset + query.length,
107+
})
108+
}
109+
96110
return
97111
}
98112

99-
const matches = node.text.matchAll(new RegExp(query, 'gi'))
100-
101-
for (const match of matches) {
102-
results.push({
103-
from: match.index + offset,
104-
to: match.index + offset + query.length,
105-
})
113+
// Add decorations for mention matches
114+
if (node.type.name === 'mention' && mentionQuery !== '') {
115+
if (node.attrs.label.toLowerCase().startsWith(mentionQuery)) {
116+
results.push({
117+
from: offset,
118+
to: offset + node.nodeSize,
119+
mention: true,
120+
})
121+
}
106122
}
107123
})
108124

@@ -142,7 +158,9 @@ export function highlightResults(doc, results) {
142158
decorations.push(
143159
Decoration.inline(result.from, result.to, {
144160
'data-text-el': 'search-decoration',
145-
style: 'background-color: #ead637; color: black; border-radius: 2px;',
161+
style: result.mention
162+
? 'outline: 2px solid #ead637; background-color: #ead637; color: black; border-radius: 2px;'
163+
: 'background-color: #ead637; color: black; border-radius: 2px;',
146164
}),
147165
)
148166
})

src/tests/plugins/searchDecorations.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6+
import Mentions from '../../extensions/Mention.js'
67
import { highlightResults, runSearch } from '../../plugins/searchDecorations.js'
78
import createCustomEditor from '../testHelpers/createCustomEditor.ts'
89

@@ -76,6 +77,30 @@ describe('search plugin', () => {
7677

7778
testSearch(doc, 'cat', expected)
7879
})
80+
81+
it('finds matches with mentions', () => {
82+
const doc =
83+
'<p>janes task, mention <span class="mention" data-type="user" data-id="jane.doe" data-label="Jane Doe">Jane Doe</span></p>'
84+
85+
const expected = {
86+
results: [
87+
{ from: 1, to: 5 },
88+
{ from: 21, to: 22, mention: true },
89+
],
90+
}
91+
92+
testSearch(doc, 'jane', expected)
93+
})
94+
95+
it('finds matches with mentions with @', () => {
96+
const doc =
97+
'<p>janes task, mention <span class="mention" data-type="user" data-id="jane.doe" data-label="Jane Doe">Jane Doe</span></p>'
98+
99+
const expected = {
100+
results: [{ from: 21, to: 22, mention: true }],
101+
}
102+
testSearch(doc, '@jane', expected)
103+
})
79104
})
80105

81106
const testSearch = (content, query, expectedSearchResults) => {

0 commit comments

Comments
 (0)