Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(jump-to-note): show the preview's path for the note it shows, as the row spells it
Elian Doran68476d37d6c5
diff --git a/apps/client/src/widgets/dialogs/jump_to_note.spec.tsx b/apps/client/src/widgets/dialogs/jump_to_note.spec.tsxindex 1eea7780bd..1e57feec12 100644--- a/apps/client/src/widgets/dialogs/jump_to_note.spec.tsx+++ b/apps/client/src/widgets/dialogs/jump_to_note.spec.tsx@@ -55,7 +55,7 @@ describe("JumpToNotePreview", () => {it("shows the highlighted note once the highlight settles, with its path, title and content", async () => {const host = renderInto(<div />);- await show({ notePath: "root/parent/plan", noteTitle: "Quarterly plan" }, host);+ await show({ notePath: "root/parent/plan", noteTitle: "Quarterly plan", highlightedParentPathTitle: "Projects" }, host);expect(host.querySelector(".jump-to-note-preview-title")).toBeNull();await wait(PREVIEW_DELAY_MS);@@ -64,6 +64,30 @@ describe("JumpToNotePreview", () => {expect(host.querySelector(".stub-content")?.textContent).toBe("content of plan");});+ it("shows the path the row carries, prefixes and ancestors froca does not hold included", async () => {+ const host = renderInto(<div />);+ await show({+ notePath: "root/uncached/parent/plan",+ highlightedParentPathTitle: "Unloaded › Work: <b>Pro</b>jects & more"+ }, host);+ await wait(PREVIEW_DELAY_MS);+ expect(host.querySelector(".jump-to-note-preview-path")?.textContent).toBe("Unloaded › Work: Projects & more");+ });++ it("keeps the path of the note it shows until the next one has loaded", async () => {+ const host = renderInto(<div />);+ await show({ notePath: "root/parent/plan", highlightedParentPathTitle: "Projects" }, host);+ await wait(PREVIEW_DELAY_MS);++ await show({ notePath: "root/other", highlightedParentPathTitle: "Elsewhere" }, host);+ expect(host.querySelector(".jump-to-note-preview-title")?.textContent).toBe("Quarterly plan");+ expect(host.querySelector(".jump-to-note-preview-path")?.textContent).toBe("Projects");++ await wait(PREVIEW_DELAY_MS);+ expect(host.querySelector(".jump-to-note-preview-title")?.textContent).toBe("Other");+ expect(host.querySelector(".jump-to-note-preview-path")?.textContent).toBe("Elsewhere");+ });+it("renders only the note the keys stop on, and nothing for a row that is no note", async () => {const host = renderInto(<div />);await show({ notePath: "root/other" }, host);diff --git a/apps/client/src/widgets/dialogs/jump_to_note.tsx b/apps/client/src/widgets/dialogs/jump_to_note.tsxindex da1d06c22a..e612c6cebd 100644--- a/apps/client/src/widgets/dialogs/jump_to_note.tsx+++ b/apps/client/src/widgets/dialogs/jump_to_note.tsx@@ -156,34 +156,37 @@ export default function JumpToNoteDialogComponent() {* empty, so the layout keeps still.*/export function JumpToNotePreview({ suggestion }: { suggestion: Suggestion | undefined }) {- const [ note, setNote ] = useState<FNote | null>(null);+ // The path is kept with the note it was loaded for, so the card never pairs one note's content+ // with the next one's path while that one loads.+ const [ previewed, setPreviewed ] = useState<{ note: FNote; parentPath: string } | null>(null);const notePath = !suggestion?.action ? suggestion?.notePath : undefined;+ // The path the row shows, as the server spelled it out: branch prefixes included, and ancestors+ // that froca has not loaded.+ const parentPathHtml = suggestion?.highlightedParentPathTitle ?? "";useEffect(() => {if (!notePath) {- setNote(null);+ setPreviewed(null);return;}let cancelled = false;const timeout = setTimeout(async () => {- const loaded = await froca.getNote(notePath.split("/").at(-1) ?? "", true);- if (!cancelled) setNote(loaded);+ const note = await froca.getNote(notePath.split("/").at(-1) ?? "", true);+ if (!cancelled) setPreviewed(note ? { note, parentPath: htmlToText(parentPathHtml) } : null);}, PREVIEW_DELAY_MS);return () => {cancelled = true;clearTimeout(timeout);};- }, [ notePath ]);+ }, [ notePath, parentPathHtml ]);- const parentTitles = note && notePath ? parentPathTitles(notePath) : [];+ const { note, parentPath } = previewed ?? {};return (<div className="jump-to-note-preview">{note && <div key={note.noteId} className="jump-to-note-preview-card">- {parentTitles.length > 0 && (- <div className="jump-to-note-preview-path">{parentTitles.join(" › ")}</div>- )}+ {parentPath && <div className="jump-to-note-preview-path">{parentPath}</div>}<h4 className="jump-to-note-preview-title"><Icon icon={note.getIcon()} /><span>{note.title}</span>@@ -195,9 +198,12 @@ export function JumpToNotePreview({ suggestion }: { suggestion: Suggestion | und);}-/** The titles of the notes above the last one of `notePath`, the root left out, as froca holds them. */-function parentPathTitles(notePath: string) {- return notePath.split("/").slice(0, -1)- .filter((noteId) => noteId !== "root")- .map((noteId) => froca.getNoteFromCache(noteId)?.title ?? "");+/**+ * The text of a highlighted path from the server, its `<b>` marks and entities resolved. Parsed in+ * a `<template>`, whose content runs no script and loads nothing.+ */+function htmlToText(html: string) {+ const template = document.createElement("template");+ template.innerHTML = html;+ return template.content.textContent ?? "";}