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
feat(options): show engine icons in the search engine dropdown
Elian Dorande846708549f
diff --git a/apps/client/src/widgets/type_widgets/options/other.spec.tsx b/apps/client/src/widgets/type_widgets/options/other.spec.tsxindex 4f0d17595d..87b409bb86 100644--- a/apps/client/src/widgets/type_widgets/options/other.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/other.spec.tsx@@ -86,28 +86,37 @@ function open() {const button = (name: string) => host.querySelector<HTMLButtonElement>(`button[name='${name}']`);describe("the search engine settings", () => {- const engine = () => host.querySelector<HTMLSelectElement>(".search-engine-choice select");+ const toggle = () => host.querySelector<HTMLElement>(".search-engine-picker > button");const urlBox = () => host.querySelector<HTMLInputElement>("input[type='url']");- function choose(value: string) {- const select = engine();- expect(select).not.toBeNull();- act(() => {- if (!select) return;- select.value = value;- select.dispatchEvent(new Event("change", { bubbles: true }));- });+ /** Opens the engine menu, which is placed in the body, and returns its rows. */+ function openMenu() {+ const button = toggle();+ expect(button).not.toBeNull();+ act(() => button?.click());+ return [ ...document.querySelectorAll<HTMLElement>(".dropdown-item") ];+ }+ function choose(title: string) {+ const item = openMenu().find((row) => row.textContent?.trim() === title);+ expect(item).toBeDefined();+ act(() => item?.click());}- it("offers none, the presets and custom, picking the entry from the stored URL", () => {+ it("offers none, the presets and custom, showing the entry for the stored URL", () => {open();- expect([ ...engine()?.options ?? [] ].map((option) => option.text)).toEqual([- "search_engine.none", "search_engine.duckduckgo", "search_engine.bing",- "search_engine.baidu", "search_engine.google", "search_engine.custom"- ]);+ expect(toggle()?.textContent?.trim()).toBe("search_engine.duckduckgo");expect(urlBox()).toBeNull();+ expect(openMenu().map((row) => [+ row.textContent?.trim(),+ [ ...row.querySelector(".bx")?.classList ?? [] ].find((name) => /^bxl?-/.test(name))+ ])).toEqual([+ [ "search_engine.none", "bx-block" ],+ [ "search_engine.duckduckgo", "bx-search-alt" ],+ [ "search_engine.bing", "bxl-bing" ],+ [ "search_engine.baidu", "bxl-baidu" ],+ [ "search_engine.google", "bxl-google" ],+ [ "search_engine.custom", "bx-edit-alt" ]+ ]);- // An empty URL is DuckDuckGo, as the context menu falls back to it.- expect(getSearchEngineChoice("")).toBe("https://duckduckgo.com/?q={keyword}");expect(getSearchEngineChoice("none")).toBe("none");expect(getSearchEngineChoice("https://www.bing.com/search?q={keyword}")).toBe("https://www.bing.com/search?q={keyword}");@@ -117,8 +126,8 @@ describe("the search engine settings", () => {it("stores the name and address of a preset, and the sentinel for none", () => {open();- choose("https://www.google.com/search?q={keyword}");- choose("none");+ choose("search_engine.google");+ choose("search_engine.none");expect(mocks.saved).toEqual([[ "customSearchEngineName", "search_engine.google" ],[ "customSearchEngineUrl", "https://www.google.com/search?q={keyword}" ],@@ -133,19 +142,20 @@ describe("the search engine settings", () => {};store("https://example.com/?s={keyword}");open();+ expect(toggle()?.textContent?.trim()).toBe("search_engine.custom");expect(urlBox()?.type).toBe("url");expect(urlBox()?.value).toBe("https://example.com/?s={keyword}");// Switching from a preset keeps its values to edit; from none, it clears the sentinel.store("https://www.bing.com/search?q={keyword}");open();- choose("custom");+ choose("search_engine.custom");expect(urlBox()).not.toBeNull();expect(mocks.saved).toEqual([]);store("none");open();- choose("custom");+ choose("search_engine.custom");expect(mocks.saved).toEqual([[ "customSearchEngineName", "" ],[ "customSearchEngineUrl", "" ]diff --git a/apps/client/src/widgets/type_widgets/options/other.tsx b/apps/client/src/widgets/type_widgets/options/other.tsxindex 5de33f45b9..166876f260 100644--- a/apps/client/src/widgets/type_widgets/options/other.tsx+++ b/apps/client/src/widgets/type_widgets/options/other.tsx@@ -10,7 +10,7 @@ import server from "../../../services/server";import toast from "../../../services/toast";import Button from "../../react/Button";import { Card, CardSection, OptionCardSection } from "../../react/Card";-import FormSelect from "../../react/FormSelect";+import FormDropdownList from "../../react/FormDropdownList";import FormTextBox, { FormTextBoxWithUnit } from "../../react/FormTextBox";import FormToggle from "../../react/FormToggle";import { useTriliumOption, useTriliumOptionBool, useTriliumOptionJson } from "../../react/hooks";@@ -65,9 +65,14 @@ function SearchEngineSettings() {const choice = customChosen ? CUSTOM_SEARCH_ENGINE : getSearchEngineChoice(url);const choices = useMemo(() => [- { value: NO_SEARCH_ENGINE, title: t("search_engine.none") },- ...SEARCH_ENGINE_PRESETS.map(({ url, nameKey }) => ({ value: url, title: t(nameKey) })),- { value: CUSTOM_SEARCH_ENGINE, title: t("search_engine.custom") }+ { value: NO_SEARCH_ENGINE, title: t("search_engine.none"), icon: "bx bx-block" },+ ...SEARCH_ENGINE_PRESETS.map(({ url, nameKey, icon }, index) => ({+ value: url, title: t(nameKey), icon, dividerBefore: index === 0+ })),+ {+ value: CUSTOM_SEARCH_ENGINE, title: t("search_engine.custom"),+ icon: "bx bx-edit-alt", dividerBefore: true+ }], []);function choose(value: string) {@@ -92,7 +97,6 @@ function SearchEngineSettings() {description={t("search_engine.custom_search_engine_info")}><OptionCardSection- className="search-engine-choice"name="search-engine"label={t("search_engine.engine_label")}subSectionsVisible={choice === CUSTOM_SEARCH_ENGINE}@@ -123,8 +127,10 @@ function SearchEngineSettings() {</OptionCardSection>]}>- <FormSelect+ <FormDropdownList+ className="search-engine-picker"values={choices} keyProperty="value" titleProperty="title"+ iconProperty="icon" dividerBeforeProperty="dividerBefore"currentValue={choice} onChange={choose}/></OptionCardSection>@@ -135,10 +141,22 @@ function SearchEngineSettings() {const CUSTOM_SEARCH_ENGINE = "custom";const SEARCH_ENGINE_PRESETS = [- { url: "https://duckduckgo.com/?q={keyword}", nameKey: "search_engine.duckduckgo" },- { url: "https://www.bing.com/search?q={keyword}", nameKey: "search_engine.bing" },- { url: "https://www.baidu.com/s?wd={keyword}", nameKey: "search_engine.baidu" },- { url: "https://www.google.com/search?q={keyword}", nameKey: "search_engine.google" }+ {+ url: "https://duckduckgo.com/?q={keyword}",+ nameKey: "search_engine.duckduckgo", icon: "bx bx-search-alt"+ },+ {+ url: "https://www.bing.com/search?q={keyword}",+ nameKey: "search_engine.bing", icon: "bx bxl-bing"+ },+ {+ url: "https://www.baidu.com/s?wd={keyword}",+ nameKey: "search_engine.baidu", icon: "bx bxl-baidu"+ },+ {+ url: "https://www.google.com/search?q={keyword}",+ nameKey: "search_engine.google", icon: "bx bxl-google"+ }] as const;/** The dropdown entry for a stored URL. An empty one is DuckDuckGo, as in the context menu. */