Skip to content
Open
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
21 changes: 13 additions & 8 deletions src/plugins/searchDecorations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { emit } from '@nextcloud/event-bus'
import { escapeForRegEx } from '@tiptap/core'
import { Plugin, PluginKey } from '@tiptap/pm/state'
import { Decoration, DecorationSet } from '@tiptap/pm/view'
import { searchQueryPluginKey } from './searchQuery.js'
Expand Down Expand Up @@ -95,19 +96,20 @@ export function runSearch(doc, query, options) {
? query.trim().slice(1).toLowerCase()
: query.trim().toLowerCase()

const regex = new RegExp(escapeForRegEx(query), 'gi')

doc.descendants((node, offset) => {
// Add decorations for text matches
if (node.isText) {
const matches = node.text.matchAll(new RegExp(query, 'gi'))
// Search the whole textblock so matches can sparn mark boundaries.
// Inline leaf nodes take one position each, so map them to one char.
if (node.isTextblock) {
const text = node.textBetween(0, node.content.size, undefined, '\uFFFC')

for (const match of matches) {
for (const match of text.matchAll(regex)) {
results.push({
from: match.index + offset,
to: match.index + offset + query.length,
from: offset + 1 + match.index,
to: offset + 1 + match.index + match[0].length,
})
}

return
}

// Add decorations for mention matches
Expand All @@ -122,6 +124,9 @@ export function runSearch(doc, query, options) {
}
})

// Text matches of a block are pushed before its mentions; restore document order
results.sort((a, b) => a.from - b.from)

if (options.matchAll) {
return {
results,
Expand Down
25 changes: 23 additions & 2 deletions src/tests/plugins/searchDecorations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ describe('search plugin', () => {
testSearch('<p>cat dinosaur bird dog cat</p>', 'cat', expected)
})

it('finds matches spanning different marks', () => {
const expected = {
results: [{ from: 1, to: 11 }],
total: 1,
index: 0,
}

testSearch('<p>test<strong>string</strong> other</p>', 'teststring', expected)
})

it('finds matches after an inline node', () => {
const doc
= '<p>a<span class="mention" data-type="user" data-id="jane.doe" data-label="Jane Doe">Jane Doe</span>teststring</p>'

const expected = {
results: [{ from: 3, to: 13 }],
}

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

it('finds matches in separate blocks', () => {
const doc
= '<p>cat dinosaur bird dog cat</p>'
Expand All @@ -71,7 +92,7 @@ describe('search plugin', () => {
{ from: 37, to: 40 },
{ from: 55, to: 58 },
],
total: 5,
total: 4,
index: 0,
}

Expand Down Expand Up @@ -107,7 +128,7 @@ function testSearch(content, query, expectedSearchResults) {
const editor = createCustomEditor(content, [Mentions])
const doc = editor.state.doc
const searched = runSearch(doc, query)
expect(searched).toHaveProperty('results', expectedSearchResults.results)
expect(searched).toMatchObject(expectedSearchResults)
expect(highlightResults(doc, searched.results)).toEqual(highlightResults(doc, expectedSearchResults.results))
editor.destroy()
}
Loading