Skip to content

Build your personal knowledge base with Trilium Notes

feat(quick_search): page through the results with PageDown and PageUp

Elian Doran46b35b0650fe
diff --git a/apps/client/src/widgets/quick_search.spec.tsx b/apps/client/src/widgets/quick_search.spec.tsx
index 793cc82a9b..18a9ab5a85 100644
--- a/apps/client/src/widgets/quick_search.spec.tsx
+++ b/apps/client/src/widgets/quick_search.spec.tsx
@@ -224,6 +224,42 @@ describe("QuickSearch", () => {
expect(menu()?.classList.contains("tn-menu-keyboard")).toBe(false);
});
+ it("moves a page of results at a time with PageDown and PageUp", async () => {
+ vi.spyOn(server, "get").mockResolvedValue(response(30, []));
+ const { editor } = await mount();
+
+ typeQuery(editor, "hello");
+ pressEnter(editor);
+ const rows = await waitForResults(30);
+
+ // Rows 50px tall in a scroller that shows four of them.
+ const scroller = menu()?.querySelector<HTMLElement>(".quick-search-results");
+ if (!scroller) throw new Error("The results did not render.");
+ Object.defineProperty(scroller, "clientHeight", { configurable: true, get: () => 200 });
+ for (const [ index, row ] of rows.entries()) {
+ Object.defineProperty(row, "offsetTop", { configurable: true, get: () => index * 50 });
+ }
+
+ const press = (key: string) => act(() => {
+ document.activeElement?.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true }));
+ });
+ rows[0].focus();
+ press("PageDown");
+ expect(document.activeElement).toBe(rows[4]);
+ expect(menu()?.classList.contains("tn-menu-keyboard")).toBe(true);
+ press("PageUp");
+ expect(document.activeElement).toBe(rows[0]);
+
+ // Held at either end rather than wrapping round.
+ press("PageUp");
+ expect(document.activeElement).toBe(rows[0]);
+ rows[27].focus();
+ press("PageDown");
+ expect(document.activeElement).toBe(rows[29]);
+ press("PageDown");
+ expect(document.activeElement).toBe(rows[29]);
+ });
+
it("leaves ArrowDown to the field while the results are closed or hold nothing to focus", async () => {
const get = vi.spyOn(server, "get").mockResolvedValue(response(0, []));
const { editor } = await mount();
diff --git a/apps/client/src/widgets/quick_search.tsx b/apps/client/src/widgets/quick_search.tsx
index c56b93ec46..40b04d5a1e 100644
--- a/apps/client/src/widgets/quick_search.tsx
+++ b/apps/client/src/widgets/quick_search.tsx
@@ -20,6 +20,9 @@ import Popup from "./react/Popup";
import RawHtml, { RawHtmlBlock } from "./react/RawHtml";
import SearchStringEditor from "./ribbon/SearchStringEditor";
+/** The keys that move focus through the results. */
+const NAVIGATION_KEYS = new Set([ "ArrowDown", "ArrowUp", "PageDown", "PageUp" ]);
+
type SearchState =
| { status: "searching" }
| { status: "done", results: SearchResultDetails[], viewScope: ViewScope | undefined };
@@ -181,22 +184,45 @@ function useResultNavigation(popupRef: RefObject<HTMLElement>, open: boolean, ca
const onKeyDown = (e: KeyboardEvent) => {
const popup = popupRef.current;
const target = e.target as Element;
- if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return;
- if (!popup?.contains(target)) return;
+ if (!NAVIGATION_KEYS.has(e.key) || !popup?.contains(target)) return;
e.preventDefault();
e.stopPropagation();
- if (e.key === "ArrowUp" && target.closest(".dropdown-item") === firstResult(popup)) {
+ const row = target.closest<HTMLElement>(".dropdown-item");
+ if (e.key === "ArrowUp" && row === firstResult(popup)) {
callbacksRef.current.focusField();
+ return;
+ }
+
+ if (e.key === "PageDown" || e.key === "PageUp") {
+ if (row) resultAPageFrom(popup, row, e.key === "PageDown" ? 1 : -1)?.focus();
} else {
focusListItem(popup, e.key === "ArrowDown" ? "next" : "previous", target);
- callbacksRef.current.onMoved();
}
+ callbacksRef.current.onMoved();
};
window.addEventListener("keydown", onKeyDown, true);
return () => window.removeEventListener("keydown", onKeyDown, true);
}, [ open ]);
}
+/**
+ * The result a page away from `row`: the farthest one that starts within the scroller's height of
+ * it, and at least the next one. Nothing past either end, so the keys stop there.
+ */
+function resultAPageFrom(popup: HTMLElement, row: HTMLElement, direction: 1 | -1) {
+ const rows = [ ...popup.querySelectorAll<HTMLElement>(".dropdown-item:not(.disabled)") ];
+ const index = rows.indexOf(row);
+ if (index < 0) return undefined;
+
+ const pageHeight = popup.querySelector(".quick-search-results")?.clientHeight ?? 0;
+ let found: HTMLElement | undefined = rows[index + direction];
+ for (let i = index + direction; i >= 0 && i < rows.length; i += direction) {
+ if (Math.abs(rows[i].offsetTop - row.offsetTop) > pageHeight) break;
+ found = rows[i];
+ }
+ return found;
+}
+
/** The first result focus can move to; the searching and no-results rows are disabled. */
function firstResult(popup: HTMLElement) {
return popup.querySelector(".dropdown-item:not(.disabled)");
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick search.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick search.html
index 287bf2c979..f4cfbfce3f 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick search.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick search.html
@@ -74,6 +74,8 @@
<li>Press <kbd>Down</kbd> in the search field to move to the first result, then <kbd>Up</kbd> and <kbd>Down</kbd> to
move between results. <kbd>Up</kbd> on the first result returns to the search
field.</li>
+ <li>Press <kbd>Page Down</kbd> and <kbd>Page Up</kbd> to move through the results
+ a page at a time.</li>
<li>Press <kbd>Enter</kbd> on a result to open it.</li>
<li>Press <kbd>Ctrl</kbd>+<kbd>Enter</kbd> to open the query in the full search.</li>
<li>Press <kbd>Escape</kbd> to close the results.</li>
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick search.md b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick search.md
index 4ec85b8bd0..81b449f327 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick search.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Quick search.md
@@ -58,6 +58,7 @@ To search the whole database while hoisted, use the full <a class="reference-li
* Press <kbd>Enter</kbd> in the search field to search, or to refresh the results already shown.
* Press <kbd>Down</kbd> in the search field to move to the first result, then <kbd>Up</kbd> and <kbd>Down</kbd> to move between results. <kbd>Up</kbd> on the first result returns to the search field.
+* Press <kbd>Page Down</kbd> and <kbd>Page Up</kbd> to move through the results a page at a time.
* Press <kbd>Enter</kbd> on a result to open it.
* Press <kbd>Ctrl</kbd>+<kbd>Enter</kbd> to open the query in the full search.
* Press <kbd>Escape</kbd> to close the results.