Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): show the engine's icon on the context-menu web search

Elian Doranbd7275b6e036
diff --git a/apps/client/src/menus/note_context_menu.spec.ts b/apps/client/src/menus/note_context_menu.spec.ts
index 8ec37e37bf..b1e92b29eb 100644
--- a/apps/client/src/menus/note_context_menu.spec.ts
+++ b/apps/client/src/menus/note_context_menu.spec.ts
@@ -277,15 +277,28 @@ describe("buildNoteContextMenuItems", () => {
});
});
- it("searches with the configured engine, and leaves the web search out when it is none", async () => {
+ it("searches with the configured engine and its icon, leaving it out for none", async () => {
const host = browserLikeHost();
+ const webSearchIcon = async () =>
+ findItem(await build(), "electron_context_menu.search_online")?.uiIcon;
h.options = {
customSearchEngineName: "Google",
customSearchEngineUrl: "https://www.google.com/search?q={keyword}"
};
- run(await build({ selectionText: "trilium" }, host), "electron_context_menu.search_online");
+ const items = await build({ selectionText: "trilium" }, host);
+ expect(await webSearchIcon()).toBe("bx bxl-google");
+ run(items, "electron_context_menu.search_online");
expect(host.openExternal).toHaveBeenCalledWith("https://www.google.com/search?q=trilium");
+ // A custom engine has no logo of its own, and nothing stored means DuckDuckGo.
+ h.options = {
+ customSearchEngineName: "Example",
+ customSearchEngineUrl: "https://example.com/?s={keyword}"
+ };
+ expect(await webSearchIcon()).toBe("bx bx-search-alt");
+ h.options = {};
+ expect(await webSearchIcon()).toBe("bx bx-search-alt");
+
h.options = { customSearchEngineName: "", customSearchEngineUrl: "none" };
expect(titles(await build())).toEqual([
"electron_context_menu.copy",
diff --git a/apps/client/src/menus/note_context_menu.ts b/apps/client/src/menus/note_context_menu.ts
index 71d45de577..31149734b0 100644
--- a/apps/client/src/menus/note_context_menu.ts
+++ b/apps/client/src/menus/note_context_menu.ts
@@ -4,7 +4,7 @@ import appContext, { type CommandNames } from "../components/app_context.js";
import { buildBlockReferenceMenuItems } from "../services/block_reference.js";
import { copyHtml, copyTextWithToast } from "../services/clipboard_ext.js";
import { t } from "../services/i18n.js";
-import options from "../services/options.js";
+import { getSearchEngine } from "../services/search_engine.js";
import server from "../services/server.js";
import utils from "../services/utils.js";
import { buildCodeEditorContextMenuItems } from "./code_editor_context_menu.js";
@@ -13,9 +13,6 @@ import { splitMenuItem } from "./context_menu_utils.js";
import { buildTableContextMenuSections, hasTableCellSelection } from "./table_context_menu.js";
import { buildAiActionsMenuItem, getTextEditorAtSelection } from "./text_editor_context_menu.js";
-/** The `customSearchEngineUrl` that turns off searching the web from the context menu. */
-export const NO_SEARCH_ENGINE = "none";
-
/** What the pointer was over when the menu was summoned. */
export interface ContextMenuTarget {
/** URL of the link under the pointer, or an empty string. */
@@ -278,7 +275,7 @@ export async function buildNoteContextMenuItems(
term: utils.escapeHtml(shortenedSelection),
searchEngine: utils.escapeHtml(searchEngine.name)
}),
- uiIcon: "bx bx-search-alt",
+ uiIcon: searchEngine.icon,
handler: () => host.openExternal(searchUrl)
});
}
@@ -295,14 +292,6 @@ export async function buildNoteContextMenuItems(
return items;
}
-function getSearchEngine(): { name: string; url: string } | null {
- const name = options.get("customSearchEngineName");
- const url = options.get("customSearchEngineUrl");
- if (url === NO_SEARCH_ENGINE) return null;
- if (name && url) return { name, url };
- return { name: "DuckDuckGo", url: "https://duckduckgo.com/?q={keyword}" };
-}
-
/**
* Returns the HTML to feed to the Markdown converter for the "Copy as Markdown" action.
*
diff --git a/apps/client/src/services/search_engine.ts b/apps/client/src/services/search_engine.ts
new file mode 100644
index 0000000000..043ad1e953
--- /dev/null
+++ b/apps/client/src/services/search_engine.ts
@@ -0,0 +1,41 @@
+import options from "./options.js";
+
+/** The `customSearchEngineUrl` that turns off searching the web from the context menu. */
+export const NO_SEARCH_ENGINE = "none";
+
+/** The search engines offered in the options, the first of which is used while none is set. */
+export const SEARCH_ENGINE_PRESETS = [
+ {
+ 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 engine the context menu searches the web with: the one in the options, DuckDuckGo while none
+ * is set, or `null` when searching the web is turned off.
+ */
+export function getSearchEngine(): { name: string; url: string; icon: string } | null {
+ const name = options.get("customSearchEngineName");
+ const url = options.get("customSearchEngineUrl");
+ if (url === NO_SEARCH_ENGINE) return null;
+ if (!name || !url) {
+ const [ fallback ] = SEARCH_ENGINE_PRESETS;
+ return { name: "DuckDuckGo", url: fallback.url, icon: fallback.icon };
+ }
+
+ const preset = SEARCH_ENGINE_PRESETS.find((preset) => preset.url === url);
+ return { name, url, icon: preset?.icon ?? "bx bx-search-alt" };
+}
diff --git a/apps/client/src/widgets/type_widgets/options/other.tsx b/apps/client/src/widgets/type_widgets/options/other.tsx
index 166876f260..0b217009c0 100644
--- a/apps/client/src/widgets/type_widgets/options/other.tsx
+++ b/apps/client/src/widgets/type_widgets/options/other.tsx
@@ -3,9 +3,9 @@ import "./other.css";
import { SANITIZER_DEFAULT_ALLOWED_TAGS } from "@triliumnext/commons";
import { useMemo, useState } from "preact/hooks";
-import { NO_SEARCH_ENGINE } from "../../../menus/note_context_menu";
import { t } from "../../../services/i18n";
import search from "../../../services/search";
+import { NO_SEARCH_ENGINE, SEARCH_ENGINE_PRESETS } from "../../../services/search_engine";
import server from "../../../services/server";
import toast from "../../../services/toast";
import Button from "../../react/Button";
@@ -140,25 +140,6 @@ function SearchEngineSettings() {
const CUSTOM_SEARCH_ENGINE = "custom";
-const SEARCH_ENGINE_PRESETS = [
- {
- 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. */
export function getSearchEngineChoice(url: string) {
if (url === NO_SEARCH_ENGINE) return NO_SEARCH_ENGINE;