diff --git a/src/lib/disableNativeContextMenu.ts b/src/lib/disableNativeContextMenu.ts new file mode 100644 index 000000000..e684121b1 --- /dev/null +++ b/src/lib/disableNativeContextMenu.ts @@ -0,0 +1,17 @@ +export function disableNativeContextMenu(): void { + window.addEventListener("contextmenu", event => { + const selection = window.getSelection(); + if ( + selection && + !selection.isCollapsed && + selection.toString().length > 0 + ) { + return; + } + const target = event.target as HTMLElement | null; + if (target?.closest("input, textarea, [contenteditable]")) { + return; + } + event.preventDefault(); + }); +} diff --git a/src/main.ts b/src/main.ts index 274e9a19a..ef77b7bbc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,9 +3,12 @@ import { hotkeyKeyUX, hotkeyMacCompat, startKeyUX } from "keyux"; import { mount } from "svelte"; import App from "./App.svelte"; +import { disableNativeContextMenu } from "./lib/disableNativeContextMenu"; const app = mount(App, { target: document.body }); +disableNativeContextMenu(); + const mac = hotkeyMacCompat(); startKeyUX(window, [hotkeyKeyUX([mac])]);