note context menu: cache appContext import call

This commit is contained in:
contributor 2025-11-25 12:35:29 +02:00
parent b982ca2c5b
commit b1c77b508a

View File

@ -4,7 +4,7 @@ import zoomService from "../components/zoom.js";
import contextMenu, { type MenuItem } from "./context_menu.js"; import contextMenu, { type MenuItem } from "./context_menu.js";
import { t } from "../services/i18n.js"; import { t } from "../services/i18n.js";
import type { BrowserWindow } from "electron"; import type { BrowserWindow } from "electron";
import type { CommandNames } from "../components/app_context.js"; import type { CommandNames, AppContext } from "../components/app_context.js";
function setupContextMenu() { function setupContextMenu() {
const electron = utils.dynamicRequire("electron"); const electron = utils.dynamicRequire("electron");
@ -13,6 +13,8 @@ function setupContextMenu() {
// FIXME: Remove typecast once Electron is properly integrated. // FIXME: Remove typecast once Electron is properly integrated.
const { webContents } = remote.getCurrentWindow() as BrowserWindow; const { webContents } = remote.getCurrentWindow() as BrowserWindow;
let appContext: AppContext;
webContents.on("context-menu", (event, params) => { webContents.on("context-menu", (event, params) => {
const { editFlags } = params; const { editFlags } = params;
const hasText = params.selectionText.trim().length > 0; const hasText = params.selectionText.trim().length > 0;
@ -124,7 +126,10 @@ function setupContextMenu() {
title: t("electron_context_menu.search_in_trilium", { term: shortenedSelection }), title: t("electron_context_menu.search_in_trilium", { term: shortenedSelection }),
uiIcon: "bx bx-search", uiIcon: "bx bx-search",
handler: async () => { handler: async () => {
const appContext = (await import("../components/app_context.js")).default; if (!appContext) {
appContext = (await import("../components/app_context.js")).default;
}
await appContext.triggerCommand("searchNotes", { await appContext.triggerCommand("searchNotes", {
searchString: params.selectionText searchString: params.selectionText
}); });