feat(autocomplete): display note icon

This commit is contained in:
Elian Doran 2025-03-29 22:04:34 +02:00
parent eb097ec1ea
commit a9193fdcd4
No known key found for this signature in database
3 changed files with 35 additions and 6 deletions

View File

@ -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
};

View File

@ -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: `<span class="${icon ?? "bx bx-note"}"></span> ${notePathTitle}`
};
});
}

View File

@ -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: `<span class="${icon ?? "bx bx-note"}"></span> ${result.highlightedNotePathTitle}`
};
});
}