fix(app): Never show the webview's native context menu (#11)

Right-clicks the app doesn't handle itself, such as on code-lens or inlay
widgets inside the editor or in text fields, fell through to WebKit's own
menu, whose Reload item reloads the whole app. Block it everywhere; the
app's own menus already call preventDefault before this guard runs.

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu
This commit is contained in:
l0ng-ai
2026-10-23 20:02:58 +08:00
committed by GitHub
parent acf6931017
commit 19aee5f262
+5 -10
View File
@@ -1,9 +1,9 @@
/**
* Makes the webview behave like a desktop app: no browser context menu
* outside text fields/editors, no reload/print/save-page/history shortcuts,
* no navigation on file drop. Copy/paste/undo stay untouched.
* Makes the webview behave like a desktop app: never the browser context menu
* (its Reload/AutoFill items would act on the webview), no reload/print/
* save-page/history shortcuts, no navigation on file drop. Copy/paste/undo
* shortcuts stay untouched.
*/
const EDITABLE = "input, textarea, [contenteditable=''], [contenteditable='true'], .cm-editor, .xterm";
const BLOCKED = new Set([
"Meta+P",
@@ -35,12 +35,7 @@ function keyId(e: KeyboardEvent) {
export function installBrowserGuards(opts: { blockReload?: boolean } = {}): () => void {
const blockReload = opts.blockReload ?? import.meta.env.PROD;
const onContextMenu = (e: MouseEvent) => {
if (e.defaultPrevented) return;
const target = e.target as Element | null;
if (target?.closest?.(EDITABLE)) return;
e.preventDefault();
};
const onContextMenu = (e: MouseEvent) => e.preventDefault();
const onKeyDown = (e: KeyboardEvent) => {
const id = keyId(e);
if (BLOCKED.has(id) || (blockReload && RELOAD.has(id))) e.preventDefault();