From a9193fdcd4ad255e3343065d6e54e664ae823a32 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 29 Mar 2025 22:04:34 +0200 Subject: [PATCH] feat(autocomplete): display note icon --- src/becca/becca_service.ts | 28 ++++++++++++++++++++++++++ src/routes/api/autocomplete.ts | 6 +++--- src/services/search/services/search.ts | 7 ++++--- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/becca/becca_service.ts b/src/becca/becca_service.ts index ab6838a78..3304fcd1b 100644 --- a/src/becca/becca_service.ts +++ b/src/becca/becca_service.ts @@ -40,6 +40,33 @@ function getNoteTitle(childNoteId: string, parentNoteId?: string) { return `${branch && branch.prefix ? `${branch.prefix} - ` : ""}${title}`; } +/** + * Similar to {@link getNoteTitle}, but also returns the icon class of the note. + * + * @returns An object containing the title and icon class of the note. + */ +function getNoteTitleAndIcon(childNoteId: string, parentNoteId?: string) { + const childNote = becca.notes[childNoteId]; + const parentNote = parentNoteId ? becca.notes[parentNoteId] : null; + + if (!childNote) { + log.info(`Cannot find note '${childNoteId}'`); + return { + title: "[error fetching title]" + } + } + + const title = childNote.getTitleOrProtected(); + const icon = childNote.getLabelValue("iconClass"); + + const branch = parentNote ? becca.getBranchFromChildAndParent(childNote.noteId, parentNote.noteId) : null; + + return { + icon, + title: `${branch && branch.prefix ? `${branch.prefix} - ` : ""}${title}` + } +} + function getNoteTitleArrayForPath(notePathArray: string[]) { if (!notePathArray || !Array.isArray(notePathArray)) { throw new Error(`${notePathArray} is not an array.`); @@ -84,6 +111,7 @@ function getNoteTitleForPath(notePathArray: string[]) { export default { getNoteTitle, + getNoteTitleAndIcon, getNoteTitleForPath, isNotePathArchived }; diff --git a/src/routes/api/autocomplete.ts b/src/routes/api/autocomplete.ts index 27d2e69f3..a357ca4b3 100644 --- a/src/routes/api/autocomplete.ts +++ b/src/routes/api/autocomplete.ts @@ -67,14 +67,14 @@ function getRecentNotes(activeNoteId: string) { return recentNotes.map((rn) => { const notePathArray = rn.notePath.split("/"); - const noteTitle = beccaService.getNoteTitle(notePathArray[notePathArray.length - 1]); + const { title, icon } = beccaService.getNoteTitleAndIcon(notePathArray[notePathArray.length - 1]); const notePathTitle = beccaService.getNoteTitleForPath(notePathArray); return { notePath: rn.notePath, - noteTitle, + noteTitle: title, notePathTitle, - highlightedNotePathTitle: utils.escapeHtml(notePathTitle) + highlightedNotePathTitle: ` ${notePathTitle}` }; }); } diff --git a/src/services/search/services/search.ts b/src/services/search/services/search.ts index 745562cd7..5e9b4bb9d 100644 --- a/src/services/search/services/search.ts +++ b/src/services/search/services/search.ts @@ -354,11 +354,12 @@ function searchNotesForAutocomplete(query: string, fastSearch: boolean = true) { highlightSearchResults(trimmed, searchContext.highlightedTokens, searchContext.ignoreInternalAttributes); return trimmed.map((result) => { + const { title, icon } = beccaService.getNoteTitleAndIcon(result.noteId); return { notePath: result.notePath, - noteTitle: beccaService.getNoteTitle(result.noteId), - notePathTitle: result.notePathTitle, - highlightedNotePathTitle: result.highlightedNotePathTitle + noteTitle: title, + notePathTitle: `${icon} ${result.notePathTitle}`, + highlightedNotePathTitle: ` ${result.highlightedNotePathTitle}` }; }); }