From 70b1895245e9bf70039346548b6e6d5e7e470f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoi=CC=82t=20Rouleau?= Date: Sun, 8 Nov 2026 01:32:57 -0500 Subject: [PATCH] js: leave the clipboard to the page unless the canvas has the focus The paste listener on the window took every paste on the page and prevented the browser's own, so pasting into a text field next to the canvas did nothing. Copy, cut and paste now reach the app only while its canvas has the focus, as its keys do. They're still heard on the window, since Firefox sends them to the body, and every browser to the page's selection when there is one, rather than to the focused canvas. Copy and cut also read the event they're given, not the global `event`. Co-Authored-By: Claude Opus 5.5 --- js/gl.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/js/gl.js b/js/gl.js index df0116ac9..a19646e6f 100644 --- a/js/gl.js +++ b/js/gl.js @@ -1308,20 +1308,26 @@ var importObject = { window.onresize = function () { resize(canvas, wasm_exports.resize); }; + // Only while the canvas has the focus, so a text field on the page + // keeps its own. Browsers can send these to the body or to the + // page's selection rather than the focused canvas. window.addEventListener("copy", function (e) { - if (clipboard != null) { - event.clipboardData.setData('text/plain', clipboard); - event.preventDefault(); + if (document.activeElement == canvas && clipboard != null) { + e.clipboardData.setData('text/plain', clipboard); + e.preventDefault(); } }); window.addEventListener("cut", function (e) { - if (clipboard != null) { - event.clipboardData.setData('text/plain', clipboard); - event.preventDefault(); + if (document.activeElement == canvas && clipboard != null) { + e.clipboardData.setData('text/plain', clipboard); + e.preventDefault(); } }); window.addEventListener("paste", function (e) { + if (document.activeElement != canvas) { + return; + } e.stopPropagation(); e.preventDefault(); var clipboardData = e.clipboardData || window.clipboardData;