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(client): let Shift+right-click reach the browser's own menu
Elian Doran04492484b656
diff --git a/apps/client/src/menus/note_context_menu.spec.ts b/apps/client/src/menus/note_context_menu.spec.tsindex 4606c2f3d4..b182620ff5 100644--- a/apps/client/src/menus/note_context_menu.spec.ts+++ b/apps/client/src/menus/note_context_menu.spec.ts@@ -222,10 +222,10 @@ describe("setupContextMenu (browser)", () => {tabManager.activeContext = null;});- /** Right-clicks `element`, optionally with the event already claimed by another widget. */- function rightClick(element: Element, claimedByAnotherWidget = false) {- const event = new MouseEvent("contextmenu", { bubbles: true, cancelable: true });- if (claimedByAnotherWidget) {+ /** Right-clicks `element`, holding Shift or letting another widget claim the event first. */+ function rightClick(element: Element, { shiftKey = false, claimed = false } = {}) {+ const event = new MouseEvent("contextmenu", { bubbles: true, cancelable: true, shiftKey });+ if (claimed) {element.addEventListener("contextmenu", (e) => e.preventDefault(), { once: true });}element.dispatchEvent(event);@@ -252,12 +252,24 @@ describe("setupContextMenu (browser)", () => {document.body.appendChild(div);setSelection(div, "<b>text</b>", "text");- rightClick(div, true);+ rightClick(div, { claimed: true });await settle();expect(contextMenu.show).not.toHaveBeenCalled();});+ it("hands a Shift+right-click back to the browser, selection or not", async () => {+ const div = document.createElement("div");+ document.body.appendChild(div);+ setSelection(div, "<b>text</b>", "text");++ const event = rightClick(div, { shiftKey: true });+ await settle();++ expect(event.defaultPrevented).toBe(false);+ expect(contextMenu.show).not.toHaveBeenCalled();+ });+it("takes the menu over for a selection, and reads the link under the pointer", async () => {const link = document.createElement("a");link.href = "https://example.com/";diff --git a/apps/client/src/menus/note_context_menu.ts b/apps/client/src/menus/note_context_menu.tsindex 43c76c945e..289e57526c 100644--- a/apps/client/src/menus/note_context_menu.ts+++ b/apps/client/src/menus/note_context_menu.ts@@ -66,6 +66,14 @@ export function setupContextMenu() {return;}+ // Firefox suppresses the event outright on Shift+right-click, so its users already have a+ // way back to the browser's menu — to the extension entries registered against a selection+ // above all. Honoring the modifier gives Chrome and Safari, which have no bypass of their+ // own, the same one.+ if (event.shiftKey) {+ return;+ }+// `window.getSelection()` does not report a selection inside an `<input>` or `<textarea>`,// so those keep the browser's menu, which is the better one for a plain text field.const selectionText = window.getSelection()?.toString() ?? "";