Skip to content

Build your personal knowledge base with Trilium Notes

feat(jump-to-note): preview the highlighted note beside the results

Elian Doranf7c92d53ca4b
diff --git a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx
index 1c3700fe14..1658acaee0 100644
--- a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx
+++ b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx
@@ -273,7 +273,7 @@ function GridNoteCard(props: GridNoteCardProps) {
);
}
-function NoteAttributes({ note }: { note: FNote }) {
+export function NoteAttributes({ note }: { note: FNote }) {
const ref = useRef<HTMLSpanElement>(null);
useEffect(() => {
attribute_renderer.renderNormalAttributes(note).then(({$renderedAttributes}) => {
diff --git a/apps/client/src/widgets/dialogs/jump_to_note.css b/apps/client/src/widgets/dialogs/jump_to_note.css
index 3dcb7f5c74..647d2dd41f 100644
--- a/apps/client/src/widgets/dialogs/jump_to_note.css
+++ b/apps/client/src/widgets/dialogs/jump_to_note.css
@@ -54,6 +54,81 @@ body.desktop .jump-to-note-results {
}
}
+/*
+ * Room for a preview beside the results: the dialog widens into two columns, the results and the
+ * preview, with the footer across both. Matches `PREVIEW_MEDIA_QUERY` in jump_to_note.tsx. The
+ * results' container steps aside (`display: contents`), so its list and footer take the grid's cells.
+ */
+@media (min-width: 1100px) {
+ body.desktop .jump-to-note-dialog {
+ .modal-dialog {
+ max-width: 1200px;
+ }
+
+ .modal-body {
+ display: grid;
+ grid-template-columns: minmax(0, 1fr) 400px;
+ grid-template-rows: minmax(0, 1fr) auto;
+ column-gap: 16px;
+ height: calc(80vh - 150px);
+ }
+
+ .jump-to-note-results {
+ display: contents;
+ }
+
+ .jump-to-note-results .note-suggestion-list {
+ grid-area: 1 / 1;
+ margin-inline-end: 0;
+ }
+
+ .jump-to-note-results .note-suggestion-footer {
+ grid-area: 2 / 1 / 3 / 3;
+ }
+
+ .jump-to-note-preview {
+ grid-area: 1 / 2;
+ min-height: 0;
+ padding-block: 8px;
+ }
+ }
+}
+
+.jump-to-note-preview-card {
+ height: 100%;
+ overflow: hidden;
+ padding: 20px;
+ border: 1px solid var(--main-border-color);
+ border-radius: 8px;
+ background: var(--card-background-color, var(--main-background-color));
+ /* The content is cut off at the card's foot, fading out rather than ending on a sliced line. */
+ mask-image: linear-gradient(to bottom, black calc(100% - 40px), transparent);
+
+ .jump-to-note-preview-path {
+ margin-bottom: 4px;
+ font-size: 0.85em;
+ color: var(--muted-text-color);
+ }
+
+ .jump-to-note-preview-title {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ margin-bottom: 8px;
+ font-size: 1.4em;
+ overflow-wrap: anywhere;
+ }
+
+ .note-list-attributes {
+ display: block;
+ margin-bottom: 12px;
+ }
+
+ .note-book-content {
+ overflow: hidden;
+ }
+}
+
@media (max-width: 991px) {
body.mobile .jump-to-note-dialog {
.modal-header {
diff --git a/apps/client/src/widgets/dialogs/jump_to_note.spec.tsx b/apps/client/src/widgets/dialogs/jump_to_note.spec.tsx
new file mode 100644
index 0000000000..2d254aa8fe
--- /dev/null
+++ b/apps/client/src/widgets/dialogs/jump_to_note.spec.tsx
@@ -0,0 +1,70 @@
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const noteContent = vi.hoisted(() => ({ rendered: [] as string[] }));
+
+vi.mock("../collections/legacy/ListOrGridView", () => ({
+ NoteContent: ({ note }: { note: { noteId: string } }) => {
+ noteContent.rendered.push(note.noteId);
+ return <div className="stub-content">{`content of ${note.noteId}`}</div>;
+ },
+ NoteAttributes: () => <span className="stub-attributes" />
+}));
+
+import type { Suggestion } from "../../services/note_autocomplete";
+import { buildNote } from "../../test/easy-froca";
+import { renderInto } from "../../test/render";
+import { JumpToNotePreview, PREVIEW_DELAY_MS } from "./jump_to_note";
+
+describe("JumpToNotePreview", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ noteContent.rendered = [];
+ buildNote({ id: "parent", title: "Projects", children: [ { id: "plan", title: "Quarterly plan" } ] });
+ buildNote({ id: "other", title: "Other" });
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ async function show(suggestion: Suggestion | undefined, container?: HTMLElement) {
+ const host = container ?? document.createElement("div");
+ await act(async () => {
+ const { render } = await import("preact");
+ render(<JumpToNotePreview suggestion={suggestion} />, host);
+ });
+ return host;
+ }
+
+ async function wait(ms: number) {
+ await act(async () => { await vi.advanceTimersByTimeAsync(ms); });
+ }
+
+ 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);
+ expect(host.querySelector(".jump-to-note-preview-title")).toBeNull();
+
+ await wait(PREVIEW_DELAY_MS);
+ expect(host.querySelector(".jump-to-note-preview-path")?.textContent).toBe("Projects");
+ expect(host.querySelector(".jump-to-note-preview-title")?.textContent).toBe("Quarterly plan");
+ expect(host.querySelector(".stub-content")?.textContent).toBe("content of plan");
+ });
+
+ 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);
+ await wait(PREVIEW_DELAY_MS / 2);
+ await show({ notePath: "root/parent/plan" }, host);
+ await wait(PREVIEW_DELAY_MS);
+ expect(noteContent.rendered).toEqual([ "plan" ]);
+
+ for (const row of [ { action: "create-note", noteTitle: "New" }, { action: "command", commandId: "x" }, undefined ]) {
+ await show(row as Suggestion | undefined, host);
+ await wait(PREVIEW_DELAY_MS);
+ expect(host.querySelector(".jump-to-note-preview-title")).toBeNull();
+ expect(host.querySelector(".jump-to-note-preview")).not.toBeNull();
+ }
+ });
+});
diff --git a/apps/client/src/widgets/dialogs/jump_to_note.tsx b/apps/client/src/widgets/dialogs/jump_to_note.tsx
index 10e080e978..26e0216897 100644
--- a/apps/client/src/widgets/dialogs/jump_to_note.tsx
+++ b/apps/client/src/widgets/dialogs/jump_to_note.tsx
@@ -1,19 +1,26 @@
import "./jump_to_note.css";
-import { useRef, useState } from "preact/hooks";
+import { useEffect, useRef, useState } from "preact/hooks";
import appContext from "../../components/app_context";
+import type FNote from "../../entities/fnote";
import commandRegistry from "../../services/command_registry";
+import froca from "../../services/froca";
import { t } from "../../services/i18n";
import type { Suggestion } from "../../services/note_autocomplete";
import { isMobile } from "../../services/utils";
-import { useTriliumEvent } from "../react/hooks";
+import { NoteAttributes, NoteContent } from "../collections/legacy/ListOrGridView";
+import { useMediaQuery, useTriliumEvent } from "../react/hooks";
import Icon from "../react/Icon";
import Modal from "../react/Modal";
import NoteAutocomplete, { type NoteAutocompleteHandle } from "../react/NoteAutocomplete";
import { refToJQuerySelector } from "../react/react_utils";
const KEEP_LAST_SEARCH_FOR_X_SECONDS = 120;
+/** How long the highlight rests on a row before the preview renders it, so arrowing past skips it. */
+export const PREVIEW_DELAY_MS = 150;
+/** Wide enough for the results and the preview side by side; matches the media query in jump_to_note.css. */
+const PREVIEW_MEDIA_QUERY = "(min-width: 1100px)";
type Mode = "last-search" | "recent-notes" | "commands";
@@ -26,6 +33,8 @@ export default function JumpToNoteDialogComponent() {
const [ initialText, setInitialText ] = useState("");
const actualText = useRef<string>(initialText);
const [ shown, setShown ] = useState(false);
+ const [ highlighted, setHighlighted ] = useState<Suggestion>();
+ const showsPreview = useMediaQuery(PREVIEW_MEDIA_QUERY) && !isMobile();
async function openDialog(commandMode: boolean) {
let newMode: Mode;
@@ -119,6 +128,7 @@ export default function JumpToNoteDialogComponent() {
onTextChange={(text) => actualText.current = text}
onChange={onItemSelected}
searchFooter
+ onHighlight={showsPreview ? setHighlighted : undefined}
/>
</>}
onShown={onShown}
@@ -126,6 +136,59 @@ export default function JumpToNoteDialogComponent() {
show={shown}
>
<div className="jump-to-note-results" ref={containerRef} />
+ {showsPreview && <JumpToNotePreview suggestion={shown ? highlighted : undefined} />}
</Modal>
);
}
+
+/**
+ * The note a row of the results stands for, previewed beside them: its path, title, attributes and
+ * the start of its content. A row that is no note, such as a command or a creation row, leaves it
+ * empty, so the layout keeps still.
+ */
+export function JumpToNotePreview({ suggestion }: { suggestion: Suggestion | undefined }) {
+ const [ note, setNote ] = useState<FNote | null>(null);
+ const notePath = !suggestion?.action ? suggestion?.notePath : undefined;
+
+ useEffect(() => {
+ if (!notePath) {
+ setNote(null);
+ return;
+ }
+
+ let cancelled = false;
+ const timeout = setTimeout(async () => {
+ const loaded = await froca.getNote(notePath.split("/").at(-1) ?? "", true);
+ if (!cancelled) setNote(loaded);
+ }, PREVIEW_DELAY_MS);
+ return () => {
+ cancelled = true;
+ clearTimeout(timeout);
+ };
+ }, [ notePath ]);
+
+ const parentTitles = note && notePath ? parentPathTitles(notePath) : [];
+
+ 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>
+ )}
+ <h4 className="jump-to-note-preview-title">
+ <Icon icon={note.getIcon()} />
+ <span>{note.title}</span>
+ </h4>
+ <NoteAttributes note={note} />
+ <NoteContent note={note} trim highlightedTokens={null} includeArchivedNotes={false} />
+ </div>}
+ </div>
+ );
+}
+
+/** 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 ?? "");
+}
diff --git a/apps/client/src/widgets/react/hooks.spec.tsx b/apps/client/src/widgets/react/hooks.spec.tsx
index 5461ecfc46..4c0fcd6d00 100644
--- a/apps/client/src/widgets/react/hooks.spec.tsx
+++ b/apps/client/src/widgets/react/hooks.spec.tsx
@@ -11,6 +11,7 @@ import {
useDelayedVisibility,
useFocusWithin,
useImperativeSearchHighlighlighting,
+ useMediaQuery,
useNoteLabelBoolean,
useStaticTooltip,
useTooltip
@@ -638,3 +639,28 @@ describe("useNoteLabelBoolean", () => {
expect(draws).toEqual([ true ]);
});
});
+
+describe("useMediaQuery", () => {
+ it("follows whether the query matches, and stops listening once unmounted", async () => {
+ const listeners = new Set<() => void>();
+ const query = { matches: false, addEventListener: (_: string, cb: () => void) => listeners.add(cb),
+ removeEventListener: (_: string, cb: () => void) => listeners.delete(cb) };
+ vi.spyOn(window, "matchMedia").mockReturnValue(query as unknown as MediaQueryList);
+
+ function Consumer() {
+ return <span>{String(useMediaQuery("(min-width: 1100px)"))}</span>;
+ }
+ const host = document.body.appendChild(document.createElement("div"));
+ await act(async () => { render(<Consumer />, host); });
+ expect(host.textContent).toBe("false");
+
+ query.matches = true;
+ await act(async () => { for (const listener of listeners) listener(); });
+ expect(host.textContent).toBe("true");
+
+ await act(async () => { render(null, host); });
+ expect(listeners.size).toBe(0);
+ host.remove();
+ vi.restoreAllMocks();
+ });
+});
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 4ce82392b2..5d51634eb2 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -1432,6 +1432,21 @@ export function useLegacyComponentElement(elRef: RefObject<HTMLElement>) {
type ComponentElement = HTMLElement & { component?: Component };
+/** Whether the CSS media `query` matches, following it as the window changes. */
+export function useMediaQuery(query: string) {
+ const [ matches, setMatches ] = useState(() => window.matchMedia(query).matches);
+
+ useEffect(() => {
+ const mediaQuery = window.matchMedia(query);
+ const update = () => setMatches(mediaQuery.matches);
+ update();
+ mediaQuery.addEventListener("change", update);
+ return () => mediaQuery.removeEventListener("change", update);
+ }, [ query ]);
+
+ return matches;
+}
+
/**
* Registers this widget's contextual shortcut hints on its host component. When the user requests
* contextual shortcut help (Alt+F1 by default), the dispatcher walks up from the focused element
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html
index 347d98be74..61d3814d67 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html
@@ -54,6 +54,10 @@
<li>To see the keys the results answer to, click the <kbd>?</kbd> button below
the results, or press <kbd>Alt</kbd>+<kbd>F1</kbd> while the search field
is focused.</li>
+ <li>On a wide enough desktop window, the note highlighted in the results is
+ previewed on their right: its path, title, attributes and the start of
+ its content. The preview follows the highlight as it moves through the
+ results.</li>
<li>The options to create a note are listed after the notes. When no note
matches, the list says so above them. <kbd>Enter</kbd> opens the note that
matches best, or creates the note when none matches. Otherwise, press <kbd>↑</kbd> from
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md
index 399b9342f4..671e7cb180 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md
@@ -23,6 +23,7 @@ In addition to searching for notes, it is also possible to search for commands.
* The _Include note contents_ switch searches the content of the notes as well as their titles, and lists what it finds in place of the current results, without leaving the dialog. It stays on while the query changes, until it is switched off. Press <kbd>Shift</kbd>+<kbd>Enter</kbd> to switch it from the keyboard.
* _Show in full search_ opens the query in the full <a class="reference-link" href="Search.md">Search</a>, in a new tab, where its options can refine it. Press <kbd>Ctrl</kbd>+<kbd>Enter</kbd> to run it from the keyboard.
* To see the keys the results answer to, click the <kbd>?</kbd> button below the results, or press <kbd>Alt</kbd>+<kbd>F1</kbd> while the search field is focused.
+* On a wide enough desktop window, the note highlighted in the results is previewed on their right: its path, title, attributes and the start of its content. The preview follows the highlight as it moves through the results.
* The options to create a note are listed after the notes. When no note matches, the list says so above them. <kbd>Enter</kbd> opens the note that matches best, or creates the note when none matches. Otherwise, press <kbd>↑</kbd> from the first note to reach the options to create a note.
## Recent notes