Skip to content

Build your personal knowledge base with Trilium Notes

fix(quick-search): let ArrowDown reach the results from the search box

BeatLink7ba35559ab47
diff --git a/apps/client/src/widgets/quick_search.spec.ts b/apps/client/src/widgets/quick_search.spec.ts
new file mode 100644
index 0000000000..030326d85d
--- /dev/null
+++ b/apps/client/src/widgets/quick_search.spec.ts
@@ -0,0 +1,72 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import server from "../services/server.js";
+import QuickSearchWidget from "./quick_search.js";
+
+describe("QuickSearchWidget", () => {
+ beforeEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ afterEach(() => {
+ document.body.innerHTML = "";
+ });
+
+ it("moves focus from the search box to the first result on ArrowDown", async () => {
+ const widget = await renderAndSearch();
+ const $input = widget.$widget.find(".search-string");
+ const $items = widget.$widget.find(".dropdown-menu .dropdown-item");
+
+ expect(pressArrowDown($input[0])).toBe(true);
+ expect(document.activeElement).toBe($items[0]);
+ });
+
+ it("leaves ArrowDown to the search box while the popup is closed or a modifier is held", async () => {
+ const widget = await renderAndSearch();
+ const $input = widget.$widget.find(".search-string");
+ const $firstItem = widget.$widget.find(".dropdown-menu .dropdown-item").first();
+
+ expect(pressArrowDown($input[0], { ctrlKey: true })).toBe(false);
+ expect(pressArrowDown($input[0], { shiftKey: true })).toBe(false);
+ expect(document.activeElement).not.toBe($firstItem[0]);
+
+ widget.$widget.find(".dropdown-menu").removeClass("show");
+ expect(pressArrowDown($input[0])).toBe(false);
+ expect(document.activeElement).not.toBe($firstItem[0]);
+ });
+});
+
+/** Returns whether the widget claimed the key, which is what keeps the caret from moving. */
+function pressArrowDown(element: HTMLElement, modifiers: KeyboardEventInit = {}) {
+ const event = new KeyboardEvent("keydown", {
+ key: "ArrowDown", code: "ArrowDown", bubbles: true, cancelable: true, ...modifiers
+ });
+ element.dispatchEvent(event);
+ return event.defaultPrevented;
+}
+
+async function renderAndSearch() {
+ const searchResults = Array.from({ length: 3 }, (_, index) => ({
+ notePath: `note${index}`,
+ noteTitle: `Note ${index}`,
+ notePathTitle: `Note ${index}`,
+ highlightedNotePathTitle: `Note ${index}`,
+ icon: "bx bx-note"
+ }));
+
+ vi.spyOn(server, "get").mockResolvedValue({
+ searchResultNoteIds: searchResults.map((result) => result.notePath),
+ searchResults,
+ error: ""
+ } as never);
+
+ const widget = new QuickSearchWidget();
+ widget.render();
+ // Focus only moves within the document, and `show` is the class Bootstrap opens the menu with.
+ widget.$widget.appendTo(document.body);
+ widget.$widget.find(".search-string").val("hello");
+ await widget.search();
+ widget.$widget.find(".dropdown-menu").addClass("show");
+
+ return widget;
+}
diff --git a/apps/client/src/widgets/quick_search.ts b/apps/client/src/widgets/quick_search.ts
index b4f28cd47f..e572f1b4d6 100644
--- a/apps/client/src/widgets/quick_search.ts
+++ b/apps/client/src/widgets/quick_search.ts
@@ -5,7 +5,7 @@ import froca from "../services/froca.js";
import { t } from "../services/i18n.js";
import linkService, { calculateHash, type ViewScope } from "../services/link.js";
import server from "../services/server.js";
-import shortcutService, { isIMEComposing } from "../services/shortcuts.js";
+import shortcutService, { isAppShortcutChord, isIMEComposing } from "../services/shortcuts.js";
import utils, { handleRightToLeftPlacement } from "../services/utils.js";
import BasicWidget from "./basic_widget.js";
@@ -229,8 +229,23 @@ export default class QuickSearchWidget extends BasicWidget {
this.$searchString.focus();
});
- shortcutService.bindElShortcut(this.$searchString, "down", () => {
- this.$dropdownMenu.find(".dropdown-item:not(.disabled):first").focus();
+ // Steps from the search box into the results. Bootstrap moves between the items from there,
+ // but it ignores key events on an input, and bindElShortcut() refuses a modifier-less
+ // ArrowDown, so this listener is what opens the way in.
+ this.$searchString.on("keydown", (e) => {
+ const event = e.originalEvent as KeyboardEvent | undefined;
+
+ if (!event || event.key !== "ArrowDown" || !this.isDropdownOpen()) {
+ return;
+ }
+
+ if (isIMEComposing(event) || isAppShortcutChord(event) || event.shiftKey) {
+ return;
+ }
+
+ e.preventDefault();
+ // first() rather than the `:first` selector, which jQuery 4 dropped.
+ this.$dropdownMenu.find(".dropdown-item:not(.disabled)").first().focus();
});
shortcutService.bindElShortcut(this.$searchString, "esc", () => {
@@ -420,11 +435,14 @@ export default class QuickSearchWidget extends BasicWidget {
shortcutService.bindElShortcut($showInFullButton, "return", () => this.showInFullSearch());
- shortcutService.bindElShortcut(this.$dropdownMenu.find(".dropdown-item:first"), "up", () => this.$searchString.focus());
-
this.dropdown.update();
}
+ /** Whether the results popup is open, which is what makes ArrowDown move focus into it. */
+ private isDropdownOpen() {
+ return this.$dropdownMenu.hasClass("show");
+ }
+
async showInFullSearch() {
this.dropdown.hide();