Skip to content

Build your personal knowledge base with Trilium Notes

Merge remote-tracking branch 'origin/main' into fix/quick-search-slash-11204

Elian Doranda5a3e570793
diff --cc apps/client/src/widgets/quick_search.spec.tsx
index 0000000000,d876706ade..6a3c74f057
mode 000000,100644..100644
--- a/apps/client/src/widgets/quick_search.spec.tsx
+++ b/apps/client/src/widgets/quick_search.spec.tsx
@@@ -1,0 -1,579 +1,589 @@@
+ import type { EditorView } from "@codemirror/view";
+ import type { QuickSearchResponse } from "@triliumnext/commons";
+ import { options, type VNode } from "preact";
+ import { act } from "preact/test-utils";
+ import { afterEach, describe, expect, it, vi } from "vitest";
+
+ import appContext from "../components/app_context";
+ import Component from "../components/component";
+ import { calculateHash } from "../services/link";
+ import server from "../services/server";
+ import { renderInto } from "../test/render";
+ import QuickSearch from "./quick_search";
+ import { ParentComponent } from "./react/react_utils";
+ import type { SearchCompletion } from "./ribbon/search_completions";
+
+ // The completions fetch attribute names and values through the server, so a spec that opens them
+ // supplies its own.
+ const completions = vi.hoisted(() => ({
+ at: null as ((before: string, explicit: boolean) => SearchCompletion | null) | null
+ }));
+ // Echoes the key and its interpolation, so a spec can tell which message a row shows.
+ vi.mock("../services/i18n", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../services/i18n")>()),
+ t: (key: string, options?: object) => (options ? `${key} ${JSON.stringify(options)}` : key)
+ }));
+
+ vi.mock("./ribbon/search_completions", () => ({
+ searchCompletionAt: (before: string, explicit: boolean) => completions.at?.(before, explicit) ?? null
+ }));
+
+ describe("QuickSearch", () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ completions.at = null;
+ });
+
+ it("renders the search field, focuses it on the quickSearch shortcut and marks a typed query", async () => {
+ const host = new Component();
+ const { container, editor } = await mount(host);
+ const root = container.querySelector(".quick-search");
+
+ expect(root?.querySelector(".search-string .cm-editor")).not.toBeNull();
+
+ expect(editor.hasFocus).toBe(false);
+ await act(async () => {
+ await host.handleEvent("quickSearch", {});
+ });
+ expect(editor.hasFocus).toBe(true);
+
+ expect(root?.classList.contains("has-query")).toBe(false);
+ typeQuery(editor, "hello");
+ expect(root?.classList.contains("has-query")).toBe(true);
+ typeQuery(editor, "");
+ expect(root?.classList.contains("has-query")).toBe(false);
+ });
+
+ it("searches on Enter and lists every result with its highlighted snippets", async () => {
+ const get = vi.spyOn(server, "get").mockResolvedValue(response(30, [ "hello" ]));
+ const { editor } = await mount();
+
+ typeQuery(editor, " hello ");
+ pressEnter(editor);
+
- expect(get).toHaveBeenCalledWith("quick-search/hello");
++ expect(get).toHaveBeenCalledWith("quick-search?searchString=hello");
+ const [ first ] = await waitForResults(30);
+
+ expect(first.getAttribute("href")).toBe(calculateHash({
+ notePath: "root/note0",
+ viewScope: { searchTerms: [ "hello" ] }
+ }));
+ // The row already shows the note's path and snippets, so the hover preview stays away.
+ expect(first.classList.contains("no-tooltip-preview")).toBe(true);
+ expect(first.querySelector(".quick-search-item-icon")?.classList.contains("bx-note")).toBe(true);
+ expect(first.querySelector(".search-result-title")?.innerHTML).toBe("<b>Note</b> 0");
+ const badges = first.querySelectorAll(".search-result-attributes > .ext-badge.outline");
+ const values = [ ...badges ].map((badge) => badge.querySelector(".attribute-badge-value")?.textContent);
+ expect(values).toEqual([ "1954", "tolkien" ]);
+ expect(first.querySelector(".search-result-content")?.innerHTML).toBe("about <b>hello</b>");
+ });
+
++ it("sends a query with a slash as a query parameter", async () => {
++ const get = vi.spyOn(server, "get").mockResolvedValue(response(0, []));
++ const { editor } = await mount();
++
++ typeQuery(editor, "中/英");
++ pressEnter(editor);
++
++ expect(get).toHaveBeenCalledWith(`quick-search?searchString=${encodeURIComponent("中/英")}`);
++ });
++
+ it("links to the plain note path when the search highlighted nothing", async () => {
+ const plain = response(1, []);
+ delete plain.searchResults[0].highlightedNotePathTitle;
+ vi.spyOn(server, "get").mockResolvedValue(plain);
+ const { editor } = await mount();
+
+ typeQuery(editor, "#book");
+ pressEnter(editor);
+
+ const [ first ] = await waitForResults(1);
+ expect(first.getAttribute("href")).toBe(calculateHash({ notePath: "root/note0" }));
+ // Nor its title, which then shows as it stands.
+ expect(first.querySelector(".search-result-title")?.innerHTML).toBe("Note 0");
+ });
+
+ it("shows progress while searching, then says when nothing matches", async () => {
+ const pending = deferred<QuickSearchResponse>();
+ vi.spyOn(server, "get").mockReturnValue(pending.promise as never);
+ const { editor } = await mount();
+
+ typeQuery(editor, "nothing");
+ pressEnter(editor);
+
+ await vi.waitFor(() => expect(placeholderItems()).toHaveLength(1));
+ expect(placeholderItems()[0].querySelector(".bx-loader")).not.toBeNull();
+
+ await act(async () => pending.resolve(response(0, [])));
+ await vi.waitFor(() => expect(placeholderItems()[0]?.querySelector(".bx-loader")).toBeNull());
+ expect(placeholderItems()).toHaveLength(1);
+ expect(resultItems()).toHaveLength(0);
+ });
+
+ it("shows the error the search ran into, above whatever it still found", async () => {
+ const failed = { ...response(0, []), error: "Note content can be searched only with operators" };
+ const get = vi.spyOn(server, "get").mockResolvedValue(failed);
+ const { editor } = await mount();
+
+ typeQuery(editor, "note.content > 3");
+ pressEnter(editor);
+
+ // In place of "No results found", which would hide why.
+ const error = await vi.waitFor(() => {
+ const row = menu()?.querySelector(".quick-search-error");
+ expect(row).not.toBeNull();
+ return row;
+ });
+ // A caution card titled as an error, with the server's message as its body.
+ expect(error?.classList.contains("extended-admonition")).toBe(true);
+ expect(error?.classList.contains("caution")).toBe(true);
+ expect(error?.querySelector(".admonition-title")?.textContent).toBe("search_string.error_title");
+ expect(error?.querySelector(".admonition-body")?.textContent)
+ .toBe("Note content can be searched only with operators");
+ expect(placeholderItems()).toEqual([]);
+
+ get.mockResolvedValue({ ...response(2, []), error: "Note content can be searched only with operators" });
+ pressEnter(editor);
+ await waitForResults(2);
+ expect(menu()?.querySelector(".quick-search-results")?.firstElementChild?.classList.contains("quick-search-error")).toBe(true);
+ });
+
+ it("keeps the results of the latest search when an earlier one answers last", async () => {
+ const earlier = deferred<QuickSearchResponse>();
+ const later = deferred<QuickSearchResponse>();
+ vi.spyOn(server, "get")
+ .mockReturnValueOnce(earlier.promise as never)
+ .mockReturnValueOnce(later.promise as never);
+ const { editor } = await mount();
+
+ typeQuery(editor, "first");
+ pressEnter(editor);
+ await vi.waitFor(() => expect(menu()).not.toBeNull());
+
+ // Enter on open results runs the search again.
+ typeQuery(editor, "second");
+ pressEnter(editor);
+
+ await act(async () => later.resolve(response(2, [])));
+ await act(async () => earlier.resolve(response(5, [])));
+ await waitForResults(2);
+ });
+
+ it("leaves the results closed for an empty query, and closes them when one is opened", async () => {
+ const get = vi.spyOn(server, "get").mockResolvedValue(response(3, []));
+ const { editor } = await mount();
+
+ typeQuery(editor, " ");
+ pressEnter(editor);
+ await act(async () => {});
+ expect(get).not.toHaveBeenCalled();
+ expect(menu()).toBeNull();
+
+ typeQuery(editor, "hello");
+ pressEnter(editor);
+ const [ first ] = await waitForResults(3);
+
+ await act(async () => first.click());
+ await vi.waitFor(() => expect(menu()).toBeNull());
+ });
+
+ it("opens and closes the results from the search button", async () => {
+ const get = vi.spyOn(server, "get").mockResolvedValue(response(3, []));
+ const { container, editor } = await mount();
+ const button = container.querySelector<HTMLButtonElement>(".search-button");
+ if (!button) throw new Error("The search button did not render.");
+
+ typeQuery(editor, "hello");
+ await act(async () => button.click());
+ await waitForResults(3);
- expect(get).toHaveBeenCalledWith("quick-search/hello");
++ expect(get).toHaveBeenCalledWith("quick-search?searchString=hello");
+ expect(button.classList.contains("active")).toBe(true);
+
+ await act(async () => button.click());
+ await vi.waitFor(() => expect(menu()).toBeNull());
+ expect(button.classList.contains("active")).toBe(false);
+ });
+
+ it("moves between the results with Up and Down, and gives the field focus back on Escape", async () => {
+ vi.spyOn(server, "get").mockResolvedValue(response(3, []));
+ const { editor } = await mount();
+
+ typeQuery(editor, "hello");
+ pressEnter(editor);
+ const [ first, second ] = await waitForResults(3);
+
+ const press = (target: HTMLElement, key: string) => act(() => {
+ target.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true }));
+ });
+ first.focus();
+ // A key the results do not answer stays where it was pressed.
+ expect(first.dispatchEvent(new KeyboardEvent("keydown", { key: "a", bubbles: true, cancelable: true })))
+ .toBe(true);
+ expect(document.activeElement).toBe(first);
+
+ press(first, "ArrowDown");
+ expect(document.activeElement).toBe(second);
+ press(second, "ArrowUp");
+ expect(document.activeElement).toBe(first);
+
+ press(first, "Escape");
+ await vi.waitFor(() => expect(menu()).toBeNull());
+ expect(editor.hasFocus).toBe(true);
+ });
+
+ it("steps from the field into the first result on ArrowDown, and back on ArrowUp", async () => {
+ vi.spyOn(server, "get").mockResolvedValue(response(3, []));
+ const { editor } = await mount();
+
+ typeQuery(editor, "hello");
+ pressEnter(editor);
+ const [ first ] = await waitForResults(3);
+ editor.focus();
+
+ // A modifier leaves the key to the field.
+ pressArrowDown(editor, { ctrlKey: true });
+ pressArrowDown(editor, { shiftKey: true });
+ expect(editor.hasFocus).toBe(true);
+
+ pressArrowDown(editor);
+ expect(document.activeElement).toBe(first);
+
+ act(() => {
+ first.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowUp", bubbles: true, cancelable: true }));
+ });
+ expect(editor.hasFocus).toBe(true);
+ expect(menu()).not.toBeNull();
+ });
+
+ it("lets the keys and the pointer take turns marking a result", async () => {
+ vi.spyOn(server, "get").mockResolvedValue(response(3, []));
+ const { editor } = await mount();
+
+ typeQuery(editor, "hello");
+ pressEnter(editor);
+ const [ first, , third ] = await waitForResults(3);
+ editor.focus();
+
+ // With focus in the field, a pointer moving over a result leaves the focus there.
+ movePointer(third, 4);
+ expect(editor.hasFocus).toBe(true);
+
+ // The keys mark their row and silence the hover a resting pointer would add.
+ pressArrowDown(editor);
+ expect(document.activeElement).toBe(first);
+ expect(menu()?.classList.contains("tn-menu-keyboard")).toBe(true);
+
+ // A pointer that does not move, as when a row slides under it, changes nothing.
+ movePointer(third, 0);
+ expect(document.activeElement).toBe(first);
+
+ // A pointer that moves takes over: its row gets the focus, so the keys go on from there.
+ movePointer(third, 4);
+ expect(document.activeElement).toBe(third);
+ 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("drops an open completion list on Escape before the results", async () => {
+ vi.spyOn(server, "get").mockResolvedValue(response(3, []));
+ completions.at = (before, explicit) => (explicit ? {
+ kind: "entries", key: "keywords", from: before.length, query: "", preselect: true,
+ entries: () => [ { id: "and", title: "and", insert: "and" }, { id: "asc", title: "asc", insert: "asc" } ]
+ } : null);
+ const { editor } = await mount();
+
+ typeQuery(editor, "a");
+ pressEnter(editor);
+ await waitForResults(3);
+ editor.focus();
+
+ act(() => {
+ editor.contentDOM.dispatchEvent(new KeyboardEvent("keydown", { key: " ", ctrlKey: true, bubbles: true, cancelable: true }));
+ });
+ const completionList = () => document.querySelector(".note-autocomplete-menu:not(.quick-search-menu)");
+ await vi.waitFor(() => expect(completionList()).not.toBeNull());
+
+ pressKey(editor.contentDOM, "Escape");
+ await vi.waitFor(() => expect(completionList()).toBeNull());
+ expect(menu()).not.toBeNull();
+
+ pressKey(editor.contentDOM, "Escape");
+ await vi.waitFor(() => expect(menu()).toBeNull());
+ expect(editor.hasFocus).toBe(true);
+
+ // With the results closed, Escape is left to whatever the field sits in.
+ expect(editor.contentDOM.dispatchEvent(new KeyboardEvent("keydown", {
+ key: "Escape", bubbles: true, cancelable: true
+ }))).toBe(true);
+ });
+
+ 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();
+
+ typeQuery(editor, "nothing");
+ editor.focus();
+ pressArrowDown(editor);
+ expect(editor.hasFocus).toBe(true);
+ expect(menu()).toBeNull();
+
+ pressEnter(editor);
+ await vi.waitFor(() => expect(get).toHaveBeenCalled());
+ await vi.waitFor(() => expect(placeholderItems()[0]?.querySelector(".bx-loader")).toBeNull());
+ pressArrowDown(editor);
+ expect(editor.hasFocus).toBe(true);
+ });
+
+ it("hands the query to the full search from a footer below the results, and on Ctrl+Enter", async () => {
+ vi.spyOn(server, "get").mockResolvedValue(response(30, []));
+ const triggerCommand = vi.spyOn(appContext, "triggerCommand").mockResolvedValue(undefined);
+ const { editor } = await mount();
+
+ typeQuery(editor, " #book AND tolkien ");
+ pressEnter(editor);
+ await waitForResults(30);
+
+ // Pinned below the scroller rather than at the end of the list, so it is always in reach.
+ const footer = menu()?.lastElementChild;
+ expect(footer?.classList.contains("quick-search-footer")).toBe(true);
+ const button = footer?.querySelector<HTMLButtonElement>(".show-in-full-search");
+ if (!button) throw new Error("The footer has no full search button.");
+ // Its key is in the shortcut hints, not on the button.
+ expect(button.querySelector("kbd")).toBeNull();
+
+ await act(async () => button.click());
+ expect(triggerCommand).toHaveBeenCalledWith("searchNotes", { searchString: "#book AND tolkien" });
+ await vi.waitFor(() => expect(menu()).toBeNull());
+
+ // From the field too, with the results closed.
+ triggerCommand.mockClear();
+ act(() => {
+ editor.contentDOM.dispatchEvent(new KeyboardEvent("keydown", {
+ key: "Enter", ctrlKey: true, bubbles: true, cancelable: true
+ }));
+ });
+ expect(triggerCommand).toHaveBeenCalledWith("searchNotes", { searchString: "#book AND tolkien" });
+ expect(menu()).toBeNull();
+ });
+
+ it("opens the shortcut hints from the footer, and stays open for a press in them", async () => {
+ vi.spyOn(server, "get").mockResolvedValue(response(3, []));
+ const triggerEvent = vi.spyOn(appContext, "triggerEvent").mockResolvedValue(undefined);
+ const { editor } = await mount();
+
+ typeQuery(editor, "hello");
+ pressEnter(editor);
+ await waitForResults(3);
+
+ const button = menu()?.querySelector<HTMLButtonElement>(".quick-search-footer .shortcut-hint-button");
+ if (!button) throw new Error("The footer has no shortcut hints button.");
+ await act(async () => button.click());
+
+ const [ name, data ] = triggerEvent.mock.calls.find(([ event ]) => event === "shortcutHintsRequested") ?? [];
+ expect(name).toBe("shortcutHintsRequested");
+ const titles = (data as { sections: { titleKey?: string }[] }).sections.map((section) => section.titleKey);
+ expect(titles).toEqual(expect.arrayContaining([ "search_string.hints.title", "quick-search.hints.title" ]));
+
+ const pane = document.createElement("div");
+ pane.className = "shortcut-hints-panel";
+ document.body.append(pane);
+ act(() => {
+ pane.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
+ });
+ expect(menu()).not.toBeNull();
+ pane.remove();
+ });
+
+ it("leaves the open results alone while the query is typed", async () => {
+ vi.spyOn(server, "get").mockResolvedValue(response(15, []));
+ const { editor } = await mount();
+
+ typeQuery(editor, "h");
+ pressEnter(editor);
+ await waitForResults(15);
+
+ let resultRenders = 0;
+ const previousDiffed = options.diffed;
+ options.diffed = (vnode: VNode) => {
+ if (typeof vnode.type === "function" && vnode.type.name === "QuickSearchResult") resultRenders++;
+ previousDiffed?.(vnode);
+ };
+ try {
+ typeQuery(editor, "he");
+ typeQuery(editor, "hel");
+ typeQuery(editor, "hell");
+ } finally {
+ options.diffed = previousDiffed;
+ }
+ expect(resultRenders).toBe(0);
+ });
+
+ it("stands the results under the search box, which a press does not close them from", async () => {
+ vi.spyOn(server, "get").mockResolvedValue(response(3, []));
+ // happy-dom lays nothing out, so the viewport is given a size to place the popup in.
+ vi.spyOn(document.documentElement, "clientWidth", "get").mockReturnValue(1000);
+ vi.spyOn(document.documentElement, "clientHeight", "get").mockReturnValue(800);
+ const { container, editor } = await mount();
+ const box = container.querySelector<HTMLElement>(".quick-search-box");
+ if (!box) throw new Error("The search box did not render.");
+ vi.spyOn(box, "getBoundingClientRect").mockReturnValue(DOMRect.fromRect({ x: 20, y: 40, width: 300, height: 30 }));
+
+ typeQuery(editor, "hello");
+ pressEnter(editor);
+ await waitForResults(3);
+ await vi.waitFor(() => expect(menu()?.style.left).toBe("20px"));
+
+ await act(async () => {
+ editor.contentDOM.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
+ });
+ expect(menu()).not.toBeNull();
+ });
+ });
+
+ /** Renders the component and waits for the CodeMirror modules it imports on demand. */
+ async function mount(host = new Component()) {
+ let container: HTMLDivElement | undefined;
+ act(() => {
+ container = renderInto(
+ <ParentComponent.Provider value={host}>
+ <QuickSearch />
+ </ParentComponent.Provider>
+ );
+ });
+ if (!container) {
+ throw new Error("The component did not render.");
+ }
+
+ const { EditorView } = await import("@codemirror/view");
+ let editor: EditorView | null = null;
+ for (let attempt = 0; attempt < 50 && !editor; attempt++) {
+ await new Promise((resolve) => setTimeout(resolve, 1));
+ const dom = container.querySelector(".cm-editor");
+ editor = dom instanceof HTMLElement ? EditorView.findFromDOM(dom) : null;
+ }
+
+ if (!editor) {
+ throw new Error(`The editor did not mount: ${container.innerHTML}`);
+ }
+
+ return { container, editor };
+ }
+
+ function typeQuery(editor: EditorView, value: string) {
+ act(() => {
+ editor.dispatch({ changes: { from: 0, to: editor.state.doc.length, insert: value } });
+ });
+ }
+
+ function pressEnter(editor: EditorView) {
+ act(() => {
+ editor.contentDOM.dispatchEvent(new KeyboardEvent("keydown", {
+ key: "Enter", code: "Enter", bubbles: true, cancelable: true
+ }));
+ });
+ }
+
+ function pressArrowDown(editor: EditorView, modifiers: KeyboardEventInit = {}) {
+ act(() => {
+ editor.contentDOM.dispatchEvent(new KeyboardEvent("keydown", {
+ key: "ArrowDown", code: "ArrowDown", bubbles: true, cancelable: true, ...modifiers
+ }));
+ });
+ }
+
+ function pressKey(target: Element, key: string) {
+ act(() => {
+ target.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true }));
+ });
+ }
+
+ function movePointer(target: HTMLElement, movement: number) {
+ act(() => {
+ target.dispatchEvent(new PointerEvent("pointermove", { bubbles: true, movementX: movement }));
+ });
+ }
+
+ /** The popup is portaled to `<body>`, outside the rendered container. */
+ function menu() {
+ return document.querySelector<HTMLElement>(".quick-search-menu");
+ }
+
+ function resultItems() {
+ return Array.from(menu()?.querySelectorAll<HTMLAnchorElement>(".quick-search-results > a.dropdown-item") ?? []);
+ }
+
+ async function waitForResults(count: number) {
+ return vi.waitFor(() => {
+ const items = resultItems();
+ expect(items).toHaveLength(count);
+ return items;
+ });
+ }
+
+ /** The disabled row that stands in for results while searching, or when nothing matches. */
+ function placeholderItems() {
+ return Array.from(menu()?.querySelectorAll(".dropdown-item.disabled") ?? []);
+ }
+
+ function response(count: number, highlightedTokens: string[]): QuickSearchResponse {
+ const searchResults = Array.from({ length: count }, (_, index) => ({
+ noteId: `note${index}`,
+ notePath: `root/note${index}`,
+ noteTitle: `Note ${index}`,
+ notePathTitle: `Note ${index}`,
+ highlightedNotePathTitle: `<b>Note</b> ${index}`,
+ highlightedAttributeSnippet: "#year=&quot;1954&quot;<br>#author=&quot;tolkien&quot;",
+ highlightedContentSnippet: "about <b>hello</b>",
+ icon: "bx bx-note"
+ }));
+
+ return {
+ searchResultNoteIds: searchResults.map((result) => result.noteId),
+ searchResults,
+ highlightedTokens,
+ error: null
+ };
+ }
+
+ function deferred<T>() {
+ let resolve: (value: T) => void = () => {};
+ const promise = new Promise<T>((r) => {
+ resolve = r;
+ });
+ return { promise, resolve };
+ }
diff --cc apps/client/src/widgets/quick_search.tsx
index 0000000000,a57abadde6..bfff1ef458
mode 000000,100644..100644
--- a/apps/client/src/widgets/quick_search.tsx
+++ b/apps/client/src/widgets/quick_search.tsx
@@@ -1,0 -1,364 +1,364 @@@
+ import "./quick_search.css";
+
+ import type { FieldEditor } from "@triliumnext/codemirror/src/field_editor";
+ import type { QuickSearchResponse, SearchResultDetails } from "@triliumnext/commons";
+ import clsx from "clsx";
+ import type { RefObject } from "preact";
+ import { useEffect, useRef, useState } from "preact/hooks";
+
+ import appContext from "../components/app_context";
+ import { t } from "../services/i18n";
+ import { calculateHash, type ViewScope } from "../services/link";
+ import server from "../services/server";
+ import type { ShortcutHintDefinition } from "../services/shortcut_hints";
+ import { isMobile } from "../services/utils";
+ import ActionButton from "./react/ActionButton";
+ import { ExtendedAdmonition } from "./react/Admonition";
+ import { AttributeSnippetBadges } from "./react/Badge";
+ import Button from "./react/Button";
+ import { focusListItem } from "./react/FormList";
+ import { useTriliumEvent } from "./react/hooks";
+ import Icon from "./react/Icon";
+ import { pointerMoved } from "./react/menu_context";
+ import OverlayControlGroup from "./react/OverlayControlGroup";
+ import Popup from "./react/Popup";
+ import RawHtml, { RawHtmlBlock } from "./react/RawHtml";
+ import SearchStringEditor from "./ribbon/SearchStringEditor";
+ import { ShortcutHintOverlayButton } from "./shortcut_hints/shortcut_hint_button";
+
+ /** The keys the results answer, beside the field's own, for the shortcut-hints pane. */
+ const QUICK_SEARCH_HINTS: ShortcutHintDefinition = [
+ {
+ titleKey: "quick-search.hints.title",
+ hints: [
+ { keys: [ "Down" ], labelKey: "quick-search.hints.to_results" },
+ { keys: [ "PageDown", "PageUp" ], labelKey: "quick-search.hints.page" },
+ { keys: [ "Ctrl+Enter" ], labelKey: "quick-search.show-in-full-search" },
+ { keys: [ "Escape" ], labelKey: "quick-search.hints.close" }
+ ]
+ }
+ ];
+
+ /** 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;
+ error: string | null;
+ };
+
+ export default function QuickSearch() {
+ // In a ref rather than state, so typing re-renders nothing; only its emptiness is state.
+ const searchStringRef = useRef("");
+ const [ hasQuery, setHasQuery ] = useState(false);
+ const [ searchState, setSearchState ] = useState<SearchState>();
+ const [ open, setOpen ] = useState(false);
+ // Set while the keys move through the results; see `.tn-menu-keyboard` in Menu.css.
+ const [ keyboardDriven, setKeyboardDriven ] = useState(false);
+ const editorRef = useRef<FieldEditor | undefined>(undefined);
+ const boxRef = useRef<HTMLDivElement>(null);
+ const popupRef = useRef<HTMLDivElement | null>(null);
+ // Each search takes a number, so a slower earlier response cannot overwrite a later one.
+ const requestIdRef = useRef(0);
+
+ useTriliumEvent("quickSearch", () => editorRef.current?.focus());
+ useResultNavigation(popupRef, open, {
+ focusField: () => editorRef.current?.focus(),
+ onMoved: () => setKeyboardDriven(true),
+ onEscape: close
+ });
+
+ /** Opens the results with a search for the query, or refreshes the open results. */
+ async function search() {
+ const query = searchStringRef.current.trim();
+ if (!query) {
+ close();
+ return;
+ }
+
+ setOpen(true);
+ const requestId = ++requestIdRef.current;
+ setSearchState({ status: "searching" });
+ const { searchResults, highlightedTokens, error } = await server.get<QuickSearchResponse>(
- `quick-search/${encodeURIComponent(query)}`
++ `quick-search?searchString=${encodeURIComponent(query)}`
+ );
+ if (requestId !== requestIdRef.current) return;
+
+ setSearchState({
+ status: "done",
+ results: searchResults,
+ // Opens the note at its first match; with nothing highlighted, at the plain path.
+ viewScope: highlightedTokens.length ? { searchTerms: highlightedTokens } : undefined,
+ error
+ });
+ }
+
+ function showInFullSearch() {
+ close();
+ const searchString = searchStringRef.current.trim();
+ void appContext.triggerCommand("searchNotes", { searchString });
+ }
+
+ function close() {
+ // Focus on a result goes back to the field, rather than to the page's body.
+ if (popupRef.current?.contains(document.activeElement)) editorRef.current?.focus();
+ setOpen(false);
+ setKeyboardDriven(false);
+ }
+
+ return (
+ <div className={clsx("quick-search", hasQuery && "has-query")}>
+ <div
+ ref={boxRef}
+ className="quick-search-box"
+ // Captured, so the field never takes the Enter for its own search.
+ onKeyDownCapture={(e) => {
+ if (e.key !== "Enter" || !e.ctrlKey) return;
+ e.preventDefault();
+ e.stopPropagation();
+ showInFullSearch();
+ }}
+ >
+ <ActionButton
+ className="search-button"
+ icon="bx bx-search"
+ text={t("quick-search.placeholder")}
+ noIconActionClass
+ active={open}
+ onClick={() => (open ? close() : void search())}
+ />
+
+ <SearchStringEditor
+ className="search-string"
+ placeholder={t("quick-search.placeholder")}
+ singleLine
+ extraShortcutHints={QUICK_SEARCH_HINTS}
+ editorRef={editorRef}
+ onChange={(value) => {
+ searchStringRef.current = value;
+ setHasQuery(value.length > 0);
+ }}
+ onEnter={() => void search()}
+ // Reached only once the field's completion list, if open, has dropped the key.
+ onEscape={() => {
+ if (!open) return false;
+ close();
+ return true;
+ }}
+ onArrowDown={() => {
+ const popup = popupRef.current;
+ if (!popup || !firstResult(popup)) return false;
+ focusListItem(popup, "first");
+ setKeyboardDriven(true);
+ return true;
+ }}
+ />
+ </div>
+
+ {open && boxRef.current && (
+ <Popup
+ anchor={boxRef.current}
+ placement="bottom-start"
+ offset={2}
+ capHeight={false}
+ className={clsx("dropdown-menu show tn-dropdown-menu quick-search-menu",
+ keyboardDriven && "tn-menu-keyboard")}
+ elementRef={popupRef}
+ escapeDismisses={false}
+ keepOpenSelector=".shortcut-hints-panel"
+ onPointerMove={(e) => {
+ if (!pointerMoved(e)) return;
+ setKeyboardDriven(false);
+ // With focus in the results, the pointer's row takes it, so the keys go on
+ // from there; with focus in the field, the field keeps it.
+ const row = (e.target as Element).closest<HTMLElement>(".dropdown-item");
+ if (row && !row.classList.contains("disabled")
+ && popupRef.current?.contains(document.activeElement)) {
+ row.focus({ preventScroll: true });
+ }
+ }}
+ onDismiss={close}
+ >
+ <div className="quick-search-results">
+ <QuickSearchResults searchState={searchState} onOpenResult={close} />
+ </div>
+ <div className="quick-search-footer">
+ {!isMobile() && (
+ <OverlayControlGroup>
+ <ShortcutHintOverlayButton />
+ </OverlayControlGroup>
+ )}
+ <Button
+ className="show-in-full-search"
+ text={t("quick-search.show-in-full-search")}
+ size="small"
+ onClick={showInFullSearch}
+ />
+ </div>
+ </Popup>
+ )}
+ </div>
+ );
+ }
+
+ /**
+ * Moves focus between the results with the arrow and page keys, and from the first result up to
+ * the field, and closes them on Escape. Captured at the window, as Bootstrap's handler for keys in
+ * a `.dropdown-menu` crashes on one with no toggle beside it.
+ */
+ function useResultNavigation(popupRef: RefObject<HTMLElement | null>, open: boolean, callbacks: {
+ /** Runs for ArrowUp on the first result. */
+ focusField(): void;
+ /** Runs once the keys have moved focus to another result. */
+ onMoved(): void;
+ /** Runs for Escape on a result. */
+ onEscape(): void;
+ }) {
+ const callbacksRef = useRef(callbacks);
+ callbacksRef.current = callbacks;
+
+ useEffect(() => {
+ if (!open) return;
+ const onKeyDown = (e: KeyboardEvent) => {
+ const popup = popupRef.current;
+ const target = e.target as Element;
+ if (!popup?.contains(target)) return;
+ if (e.key === "Escape") {
+ e.preventDefault();
+ e.stopPropagation();
+ callbacksRef.current.onEscape();
+ return;
+ }
+
+ if (!NAVIGATION_KEYS.has(e.key)) return;
+ e.preventDefault();
+ e.stopPropagation();
+ 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();
+ };
+ 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)");
+ }
+
+ function QuickSearchResults({ searchState, onOpenResult }: {
+ searchState: SearchState | undefined;
+ onOpenResult(): void;
+ }) {
+ if (!searchState) return null;
+
+ if (searchState.status === "searching") {
+ return (
+ <span className="dropdown-item disabled">
+ <Icon icon="bx bx-loader bx-spin" />
+ {t("quick-search.searching")}
+ </span>
+ );
+ }
+
+ // What the search ran into while running; the field's linter shows a query that fails to parse
+ // before it is run.
+ const error = searchState.error && (
+ <ExtendedAdmonition
+ type="caution"
+ icon="bx bx-error-circle"
+ title={t("search_string.error_title")}
+ className="quick-search-error"
+ >
+ {searchState.error}
+ </ExtendedAdmonition>
+ );
+
+ if (!searchState.results.length) {
+ return error || (
+ <span className="dropdown-item disabled">{t("quick-search.no-results")}</span>
+ );
+ }
+
+ return <>
+ {error}
+ {searchState.results.map((result) => (
+ <QuickSearchResult
+ key={result.notePath}
+ result={result}
+ viewScope={searchState.viewScope}
+ onOpen={onOpenResult}
+ />
+ ))}
+ </>;
+ }
+
+ function QuickSearchResult({ result, viewScope, onOpen }: {
+ result: SearchResultDetails;
+ viewScope: ViewScope | undefined;
+ onOpen(): void;
+ }) {
+ return (
+ <a
+ className="dropdown-item no-tooltip-preview"
+ tabIndex={0}
+ href={calculateHash({ notePath: result.notePath, viewScope })}
+ onClick={onOpen}
+ onAuxClick={onOpen}
+ >
+ <div className="quick-search-item">
+ <div className="quick-search-item-header">
+ <Icon icon={result.icon} className="quick-search-item-icon" />
+ <RawHtml
+ className="search-result-title"
+ html={result.highlightedNotePathTitle ?? result.notePathTitle}
+ />
+ </div>
+
+ <AttributeSnippetBadges
+ snippet={result.highlightedAttributeSnippet}
+ className="search-result-attributes"
+ />
+
+ {result.highlightedContentSnippet && (
+ <RawHtmlBlock
+ className="search-result-content"
+ html={result.highlightedContentSnippet}
+ />
+ )}
+ </div>
+ </a>
+ );
+ }
diff --cc apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/2. Reverse proxy/Traefik.html
index a1a5623f47,7ffb71c09e..b4702abe78
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/2. Reverse proxy/Traefik.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Server Installation/2. Reverse proxy/Traefik.html
@@@ -3,11 -3,9 +3,11 @@@
reference.</p>
<h2>Breaking change in Traefik 3.6.4</h2>
<p>Traefik 3.6.4 introduced a <a href="https://doc.traefik.io/traefik/migrate/v3/#encoded-characters-in-request-path">breaking change</a> regarding
- how percent-encoded characters are handled in URLs. More specifically some
- URLs used by Trilium (such as <code>search/%23workspace%20%23!template</code>)
- are automatically rejected by Traefik, resulting in HTTP 400 errors.</p>
+ how percent-encoded characters are handled in URLs. Earlier Trilium clients
+ sent search text in paths such as <code>/api/search/%23workspace%20%23!template</code>.
- Traefik automatically rejects these paths and returns HTTP 400. Updated clients
- send search text in a query parameter, but a cached older client can still use
- the old path.</p>
++ Traefik automatically rejects these paths and returns HTTP 400. Updated
++ clients send search text in a query parameter, but a cached older client
++ can still use the old path.</p>
<p>The easiest solution is to update to Traefik 3.6.7 or later which reverts
this configuration change. Alternatively, the configuration can be changed
according to the section below.</p>
@@@ -21,9 -19,9 +21,8 @@@
allowEncodedSlash: true
allowEncodedHash: true</code></pre>
<aside class="admonition tip">
- <p>If you still have issues, depending on how Trilium is used (especially
- regarding search), you might need to enable more encoded character groups.
- For more information, see <a href="https://github.com/TriliumNext/Trilium/issues/7968">the relevant GitHub issue</a>;
+ <p>If issues continue with an older client, you might need to enable more
- encoded character groups.
- For more information, see <a href="https://github.com/TriliumNext/Trilium/issues/7968">the relevant GitHub issue</a>;
++ encoded character groups. For more information, see <a href="https://github.com/TriliumNext/Trilium/issues/7968">the relevant GitHub issue</a>;
feel free to report your findings.</p>
</aside>
</details>
diff --cc packages/trilium-core/src/routes/api/search.ts
index 918f8af3b1,e4bf33883a..6200430ae5
--- a/packages/trilium-core/src/routes/api/search.ts
+++ b/packages/trilium-core/src/routes/api/search.ts
@@@ -110,8 -111,23 +111,23 @@@ function getSearchResultDetails(req: Re
};
}
- function quickSearch(req: Request<{ searchString?: string }, { searchString?: string }>) {
+ /**
+ * Reads a search string without running it, so the editor can report what is wrong with a query
+ * while it is being typed rather than once it is run. Carries the message alone: `SearchContext`
+ * records no offsets, so the caller cannot mark where the fault is.
+ */
+ function lintSearchString(req: Request): SearchLintResponse {
+ const { searchString } = (req.body ?? {}) as Partial<SearchLintRequest>;
+
+ if (typeof searchString !== "string") {
+ throw new ValidationError("searchString must be a string.");
+ }
+
+ return { error: searchService.validateSearchQuery(searchString) };
+ }
+
-function quickSearch(req: Request<{ searchString: string }>): QuickSearchResponse {
- const { searchString } = req.params;
++function quickSearch(req: Request<{ searchString?: string }, { searchString?: string }>): QuickSearchResponse {
+ const searchString = getSearchString(req);
const searchContext = new SearchContext({
fastSearch: false,
diff --cc packages/trilium-core/src/routes/index.ts
index e04cf2b52e,fca9a74bb4..74075f43b3
--- a/packages/trilium-core/src/routes/index.ts
+++ b/packages/trilium-core/src/routes/index.ts
@@@ -241,7 -243,9 +244,10 @@@ export function buildSharedApiRoutes({
apiRoute(PST, "/api/search-note/:noteId/result-details", searchRoute.getSearchResultDetails);
apiRoute(PST, "/api/search-and-execute-note/:noteId", searchRoute.searchAndExecute);
apiRoute(PST, "/api/search-related", searchRoute.getRelatedNotes);
+ // Ahead of the `:searchString` catch-all below, which would otherwise claim the literal path
+ // were it ever given a POST handler too.
+ apiRoute(PST, "/api/search/lint", searchRoute.lintSearchString);
+ apiRoute(GET, "/api/search", searchRoute.search);
apiRoute(GET, "/api/search/:searchString", searchRoute.search);
apiRoute(GET, "/api/search-templates", searchRoute.searchTemplates);