AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
feat(desktop): add the standard editing context menu (#476)
Dariusz Koryto892f472ee681
diff --git a/apps/desktop/src/main/desktop-menu.mjs b/apps/desktop/src/main/desktop-menu.mjsindex a828a136..f1936f18 100644--- a/apps/desktop/src/main/desktop-menu.mjs+++ b/apps/desktop/src/main/desktop-menu.mjs@@ -1,8 +1,10 @@const menuCopy = {en: {about: "About EdgeEver",+ addToDictionary: "Add to Dictionary",close: "Close Window",copy: "Copy",+ copyLinkAddress: "Copy Link Address",cut: "Cut",edit: "Edit",file: "File",@@ -13,6 +15,7 @@ const menuCopy = {newMemo: "New Note",newNotebook: "New Notebook",paste: "Paste",+ pasteAsPlainText: "Paste as Plain Text",quit: "Quit EdgeEver",resetZoom: "Reset Zoom",restartToUpdate: "Restart to update",@@ -32,8 +35,10 @@ const menuCopy = {},zh: {about: "关于 EdgeEver",+ addToDictionary: "添加到词典",close: "关闭窗口",copy: "复制",+ copyLinkAddress: "复制链接地址",cut: "剪切",edit: "编辑",file: "文件",@@ -44,6 +49,7 @@ const menuCopy = {newMemo: "新建笔记",newNotebook: "新建笔记本",paste: "粘贴",+ pasteAsPlainText: "粘贴为纯文本",quit: "退出 EdgeEver",resetZoom: "重置缩放",restartToUpdate: "重启以更新",@@ -61,10 +67,11 @@ const menuCopy = {zoomIn: "放大",zoomOut: "缩小",},+};-export const desktopMenuCopy = (locale) => (- typeof locale === "string" && locale.toLowerCase().startsWith("zh")- ? menuCopy.zh- : menuCopy.en-);+export const desktopMenuCopy = (locale) => {+ const normalized = typeof locale === "string" ? locale.toLowerCase() : "";+ if (normalized.startsWith("zh")) return menuCopy.zh;+ return menuCopy.en;+};diff --git a/apps/desktop/src/main/edit-context-menu.mjs b/apps/desktop/src/main/edit-context-menu.mjsnew file mode 100644index 00000000..6454dab6--- /dev/null+++ b/apps/desktop/src/main/edit-context-menu.mjs@@ -0,0 +1,76 @@+// Native editing context menu for the desktop renderer.+//+// Electron does not show any context menu by default, so right-clicking a text+// field in the desktop app did nothing while the same page in a browser offers+// Cut / Copy / Paste / Paste as plain text. This module only restores that+// standard browser parity; it intentionally adds no app-specific commands.+//+// Chromium emits `webContents` "context-menu" only when the page did not call+// `preventDefault()` on the DOM `contextmenu` event, so the web app's own+// context menus (images, memo cards, ...) keep working without interference.++const MAX_SPELLING_SUGGESTIONS = 5;++const hasText = (value) => typeof value === "string" && value.trim().length > 0;++/**+ * Builds an Electron menu template from `context-menu` params.+ *+ * @param {object} params Electron `context-menu` params.+ * @param {object} copy Localized labels (see desktop-menu.mjs).+ * @param {object} actions Side effects: replaceMisspelling(word), addToDictionary(word), copyLink(url).+ * @returns {Array<object>} Menu template; empty when there is nothing to show.+ */+export const buildEditContextMenuTemplate = (params = {}, copy = {}, actions = {}) => {+ const editFlags = params.editFlags ?? {};+ const isEditable = params.isEditable === true;+ const hasSelection = hasText(params.selectionText);+ const sections = [];++ if (isEditable && hasText(params.misspelledWord)) {+ const suggestions = Array.isArray(params.dictionarySuggestions)+ ? params.dictionarySuggestions.filter(hasText).slice(0, MAX_SPELLING_SUGGESTIONS)+ : [];+ const misspelledWord = params.misspelledWord;+ sections.push([+ ...suggestions.map((suggestion) => ({+ label: suggestion,+ click: () => actions.replaceMisspelling?.(suggestion),+ })),+ {+ label: copy.addToDictionary,+ click: () => actions.addToDictionary?.(misspelledWord),+ },+ ]);+ }++ if (hasText(params.linkURL)) {+ const linkURL = params.linkURL;+ sections.push([+ {+ label: copy.copyLinkAddress,+ click: () => actions.copyLink?.(linkURL),+ },+ ]);+ }++ if (isEditable) {+ sections.push([+ { label: copy.cut, role: "cut", enabled: editFlags.canCut !== false },+ { label: copy.copy, role: "copy", enabled: editFlags.canCopy !== false },+ { label: copy.paste, role: "paste", enabled: editFlags.canPaste !== false },+ { label: copy.pasteAsPlainText, role: "pasteAndMatchStyle", enabled: editFlags.canPaste !== false },+ ]);+ sections.push([+ { label: copy.selectAll, role: "selectAll", enabled: editFlags.canSelectAll !== false },+ ]);+ } else if (hasSelection) {+ sections.push([+ { label: copy.copy, role: "copy", enabled: editFlags.canCopy !== false },+ ]);+ }++ return sections.flatMap((section, index) => (+ index === 0 ? section : [{ type: "separator" }, ...section]+ ));+};diff --git a/apps/desktop/src/main/edit-context-menu.test.mjs b/apps/desktop/src/main/edit-context-menu.test.mjsnew file mode 100644index 00000000..194d05b2--- /dev/null+++ b/apps/desktop/src/main/edit-context-menu.test.mjs@@ -0,0 +1,107 @@+import { describe, expect, test } from "bun:test";+import { buildEditContextMenuTemplate } from "./edit-context-menu.mjs";+import { desktopMenuCopy } from "./desktop-menu.mjs";++const copy = desktopMenuCopy("en-US");+const allowAll = { canCut: true, canCopy: true, canPaste: true, canSelectAll: true };+const shape = (template) => template.map((item) => item.type === "separator" ? "-" : item.role ?? item.label);++describe("desktop edit context menu", () => {+ test("editable field gets standard editing items with paste-as-plain-text", () => {+ const template = buildEditContextMenuTemplate({ isEditable: true, selectionText: "", editFlags: allowAll }, copy);+ expect(shape(template)).toEqual(["cut", "copy", "paste", "pasteAndMatchStyle", "-", "selectAll"]);+ expect(template.map((item) => item.label).filter(Boolean)).toEqual([+ "Cut", "Copy", "Paste", "Paste as Plain Text", "Select All",+ ]);+ });++ test("editFlags drive enabled state", () => {+ const template = buildEditContextMenuTemplate({+ isEditable: true,+ selectionText: "",+ editFlags: { canCut: false, canCopy: false, canPaste: true, canSelectAll: true },+ }, copy);+ const byRole = Object.fromEntries(template.filter((item) => item.role).map((item) => [item.role, item.enabled]));+ expect(byRole).toEqual({ cut: false, copy: false, paste: true, pasteAndMatchStyle: true, selectAll: true });++ const noPaste = buildEditContextMenuTemplate({ isEditable: true, editFlags: { ...allowAll, canPaste: false } }, copy);+ expect(noPaste.find((item) => item.role === "paste").enabled).toBe(false);+ expect(noPaste.find((item) => item.role === "pasteAndMatchStyle").enabled).toBe(false);+ });++ test("non-editable selection offers only Copy", () => {+ const template = buildEditContextMenuTemplate({ isEditable: false, selectionText: "hello", editFlags: allowAll }, copy);+ expect(shape(template)).toEqual(["copy"]);+ });++ test("link offers Copy link address and writes the URL", () => {+ const copied = [];+ const template = buildEditContextMenuTemplate(+ { isEditable: false, selectionText: "", linkURL: "https://example.com/a", editFlags: allowAll },+ copy,+ { copyLink: (url) => copied.push(url) },+ );+ expect(shape(template)).toEqual(["Copy Link Address"]);+ template[0].click();+ expect(copied).toEqual(["https://example.com/a"]);++ const withSelection = buildEditContextMenuTemplate(+ { isEditable: false, selectionText: "link text", linkURL: "https://example.com/a", editFlags: allowAll },+ copy,+ );+ expect(shape(withSelection)).toEqual(["Copy Link Address", "-", "copy"]);+ });++ test("misspelled word shows up to five suggestions and Add to dictionary", () => {+ const replaced = [];+ const added = [];+ const template = buildEditContextMenuTemplate({+ isEditable: true,+ selectionText: "",+ misspelledWord: "helo",+ dictionarySuggestions: ["hello", "help", "held", "hero", "halo", "helot"],+ editFlags: allowAll,+ }, copy, {+ replaceMisspelling: (word) => replaced.push(word),+ addToDictionary: (word) => added.push(word),+ });+ expect(shape(template)).toEqual([+ "hello", "help", "held", "hero", "halo", "Add to Dictionary", "-",+ "cut", "copy", "paste", "pasteAndMatchStyle", "-", "selectAll",+ ]);+ template[0].click();+ template[5].click();+ expect(replaced).toEqual(["hello"]);+ expect(added).toEqual(["helo"]);+ });++ test("misspelled word without suggestions still offers Add to dictionary", () => {+ const template = buildEditContextMenuTemplate({+ isEditable: true, misspelledWord: "qwzx", dictionarySuggestions: [], editFlags: allowAll,+ }, copy);+ expect(shape(template).slice(0, 2)).toEqual(["Add to Dictionary", "-"]);+ });++ test("returns an empty template when there is nothing to show", () => {+ expect(buildEditContextMenuTemplate({ isEditable: false, selectionText: "", linkURL: "", editFlags: allowAll }, copy)).toEqual([]);+ expect(buildEditContextMenuTemplate({ isEditable: false, selectionText: " " }, copy)).toEqual([]);+ expect(buildEditContextMenuTemplate(undefined, copy)).toEqual([]);+ });++ test("never adds app-specific formatting commands", () => {+ const template = buildEditContextMenuTemplate({ isEditable: true, selectionText: "x", editFlags: allowAll }, copy);+ const roles = template.filter((item) => item.role).map((item) => item.role);+ expect(roles.every((role) => ["cut", "copy", "paste", "pasteAndMatchStyle", "selectAll"].includes(role))).toBe(true);+ expect(template.every((item) => item.type === "separator" || item.role)).toBe(true);+ });++ test("context menu labels are localized for Chinese", () => {+ expect(desktopMenuCopy("zh-CN").pasteAsPlainText).toBe("粘贴为纯文本");+ expect(desktopMenuCopy("en").pasteAsPlainText).toBe("Paste as Plain Text");+ });++ test("every locale defines the same menu keys", () => {+ const keys = Object.keys(desktopMenuCopy("en")).sort();+ expect(Object.keys(desktopMenuCopy("zh")).sort()).toEqual(keys);+ });+});diff --git a/apps/desktop/src/main/index.mjs b/apps/desktop/src/main/index.mjsindex 99c35d2a..7b9d7e9b 100644--- a/apps/desktop/src/main/index.mjs+++ b/apps/desktop/src/main/index.mjs@@ -41,6 +41,7 @@ import { createRendererStartupGuard } from "./renderer-startup-guard.mjs";import { waitForChildProcessSpawn } from "./child-process-start.mjs";import { ScheduledTaskScheduler } from "./scheduled-task-scheduler.mjs";import { desktopMenuCopy } from "./desktop-menu.mjs";+import { buildEditContextMenuTemplate } from "./edit-context-menu.mjs";import {fetchTrustedWindowsUpdate,verifyDownloadedWindowsUpdate,@@ -1315,6 +1316,22 @@ const scheduleSidecarRestart = () => {}, delayMs);};+// Restores the standard browser editing menu (Cut/Copy/Paste/Paste as plain+// text/Select all, spelling suggestions, Copy link address). Pages that call+// preventDefault() on the DOM contextmenu event never reach this handler.+const attachEditContextMenu = (contents) => {+ contents.on("context-menu", (_event, params) => {+ const template = buildEditContextMenuTemplate(params, desktopMenuCopy(app.getLocale()), {+ replaceMisspelling: (word) => contents.replaceMisspelling(word),+ addToDictionary: (word) => contents.session.addWordToSpellCheckerDictionary(word),+ copyLink: (url) => clipboard.writeText(url),+ });+ if (!template.length) return;+ const window = BrowserWindow.fromWebContents(contents) ?? undefined;+ Menu.buildFromTemplate(template).popup(window ? { window } : {});+ });+};+const createWindow = async () => {const state = await readWindowState();mainWindow = new BrowserWindow({@@ -1343,6 +1360,8 @@ const createWindow = async () => {mainWindow.hide();}});+ attachEditContextMenu(mainWindow.webContents);+ mainWindow.webContents.on("did-create-window", (childWindow) => attachEditContextMenu(childWindow.webContents));mainWindow.on("hide", syncRendererHibernate);mainWindow.on("show", syncRendererHibernate);mainWindow.on("minimize", syncRendererHibernate);