Skip to content

Commit 9db02cb

Browse files
fix(notes): restore global search highlights (#903)
1 parent 482da05 commit 9db02cb

1 file changed

Lines changed: 58 additions & 15 deletions

File tree

‎src/renderer/components/notes/NotesEditor.vue‎

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
applyPendingNavigationUIStateForNote,
99
registerNavigationNoteUIState,
1010
useCopyToClipboard,
11+
useNoteSearch,
1112
useNotesEditor,
1213
useTheme,
1314
} from '@/composables'
@@ -101,6 +102,7 @@ const props = withDefaults(defineProps<Props>(), {
101102
const content = defineModel<string>('content', { default: '' })
102103
const { isDark } = useTheme()
103104
const { settings: notesSettings } = useNotesEditor()
105+
const { searchQuery: spaceSearchQuery } = useNoteSearch()
104106
const copyToClipboard = useCopyToClipboard()
105107
const isRawMode = computed(() => props.mode === 'raw')
106108
const isPreviewMode = computed(() => props.mode === 'preview')
@@ -357,7 +359,7 @@ function createEditorState(doc: string): EditorState {
357359
if (
358360
update.docChanged
359361
&& !isApplyingExternalContent
360-
&& contentSearchQuery.value
362+
&& getVisibleSearchQuery()
361363
) {
362364
const revision = ++contentSearchRevision
363365
const expectedState = update.state
@@ -367,13 +369,13 @@ function createEditorState(doc: string): EditorState {
367369
revision === contentSearchRevision
368370
&& view?.state === expectedState
369371
) {
370-
refreshContentSearch(false)
372+
refreshVisibleSearch(false)
371373
}
372374
})
373375
}
374376
375377
if (
376-
contentSearchQuery.value
378+
getVisibleSearchQuery()
377379
&& (update.viewportChanged
378380
|| update.focusChanged
379381
|| syntaxTree(update.startState) !== syntaxTree(update.state))
@@ -398,7 +400,40 @@ function applyExternalState(doc: string, selectFirstMatch = false) {
398400
isApplyingExternalContent = true
399401
view.setState(createEditorState(doc))
400402
isApplyingExternalContent = false
401-
refreshContentSearch(selectFirstMatch)
403+
refreshVisibleSearch(selectFirstMatch)
404+
}
405+
406+
function getVisibleSearchQuery() {
407+
return isContentSearchOpen.value
408+
? contentSearchQuery.value
409+
: spaceSearchQuery.value
410+
}
411+
412+
function refreshVisibleSearch(selectFirst = true) {
413+
if (isContentSearchOpen.value) {
414+
refreshContentSearch(selectFirst)
415+
return
416+
}
417+
418+
refreshSpaceSearchHighlights()
419+
}
420+
421+
function refreshSpaceSearchHighlights() {
422+
if (!view || isContentSearchOpen.value)
423+
return
424+
425+
contentSearchRevision += 1
426+
pendingTableSearchReveal = undefined
427+
const query = spaceSearchQuery.value
428+
const matches = getContentSearchMatches(view.state.doc.toString(), query)
429+
430+
view.dispatch({
431+
effects: setContentSearchMatches.of({
432+
matches,
433+
currentIndex: -1,
434+
}),
435+
})
436+
updateTableSearchHighlights(view, query)
402437
}
403438
404439
function refreshContentSearch(selectFirst = true) {
@@ -491,22 +526,20 @@ function scheduleTableSearchHighlights(expectedState: EditorState) {
491526
const revision = contentSearchRevision
492527
tableSearchFrame = requestAnimationFrame(() => {
493528
tableSearchFrame = undefined
529+
const query = getVisibleSearchQuery()
494530
if (
495531
!view
496532
|| view.state !== expectedState
497533
|| revision !== contentSearchRevision
498-
|| !contentSearchQuery.value
534+
|| !query
499535
) {
500536
return
501537
}
502538
503-
const matchFrom
504-
= contentSearchMatches.value[contentSearchIndex.value]?.from
505-
const tableMarker = updateTableSearchHighlights(
506-
view,
507-
contentSearchQuery.value,
508-
matchFrom,
509-
)
539+
const matchFrom = isContentSearchOpen.value
540+
? contentSearchMatches.value[contentSearchIndex.value]?.from
541+
: undefined
542+
const tableMarker = updateTableSearchHighlights(view, query, matchFrom)
510543
if (
511544
tableMarker
512545
&& pendingTableSearchReveal?.revision === revision
@@ -520,6 +553,7 @@ function scheduleTableSearchHighlights(expectedState: EditorState) {
520553
521554
function openContentSearch(focus = true) {
522555
isContentSearchOpen.value = true
556+
refreshContentSearch()
523557
if (!focus)
524558
return
525559
@@ -537,6 +571,7 @@ function closeContentSearch(focus = true) {
537571
pendingTableSearchReveal = undefined
538572
isContentSearchOpen.value = false
539573
contentSearchQuery.value = ''
574+
refreshSpaceSearchHighlights()
540575
if (focus)
541576
focusEditor()
542577
}
@@ -555,7 +590,14 @@ function onContentSearchPanelFocus() {
555590
})
556591
}
557592
558-
watch(contentSearchQuery, () => refreshContentSearch())
593+
watch(contentSearchQuery, () => {
594+
if (isContentSearchOpen.value)
595+
refreshContentSearch()
596+
})
597+
watch(spaceSearchQuery, () => {
598+
if (!isContentSearchOpen.value)
599+
refreshSpaceSearchHighlights()
600+
})
559601
560602
// noteId и content меняются согласованно (NotesEditorPane обновляет их
561603
// вместе, когда контент заметки загружен), поэтому один watcher.
@@ -589,8 +631,8 @@ watch([() => props.noteId, content], ([noteId, val]) => {
589631
changes: { from: 0, to: view.state.doc.length, insert: val },
590632
})
591633
isApplyingExternalContent = false
592-
if (contentSearchQuery.value)
593-
refreshContentSearch(false)
634+
if (getVisibleSearchQuery())
635+
refreshVisibleSearch(false)
594636
})
595637
596638
watch(
@@ -799,6 +841,7 @@ onMounted(() => {
799841
parent: editorContainer.value,
800842
})
801843
lastAppliedNoteId = props.noteId
844+
refreshVisibleSearch(false)
802845
803846
void syncNavigationNoteUIStateRegistration()
804847
})

0 commit comments

Comments
 (0)