fix(desktop): pasting an image with Cmd+V works in the Mac app

Cmd+V with a copied picture in a session's terminal did nothing in the Mac
app, while the same server worked in a browser.

Codeman's Cmd+V handler (_handleImagePaste, src/web/public/image-input.js)
focuses a hidden contenteditable trap and calls
document.execCommand('paste') before the key's own paste lands there.
Chromium refuses that call at once. From a WKWebView, WebKit answers it with
its DOM paste access menu ("Paste", opened at the last mouse-down) and holds
the page on a synchronous request until the menu is picked or the clipboard
changes. The key's own paste, the one that carries the image, was stuck
behind it.

Measured on macOS 27.2 (WebKit 22625) with a throwaway WKWebView probe:
- a menu Paste hands the page the image as a File for PNG data, a
  TIFF+PNG screenshot and a file copied in Finder, so WebKit itself is fine;
- Cmd+V through the same trap hung until the next clipboard change;
- with execCommand('paste') answered false, six Cmd+V presses in a row
  (image data, screenshot, Finder file, text, two repeats) each delivered
  exactly one paste event to the trap.

The fix lives in the app's HOST_SHIM, which every server page already gets:
it wraps Document.prototype.execCommand so 'paste' returns false the way
Chromium answers it, and every other command reaches WebKit unchanged. The
web page keeps its execCommand call: src/web/CLAUDE.md keeps it for mobile
engines that need it, and doing it in the app also covers servers on older
Codeman versions.

test/desktop-host-shim-paste.test.ts runs the shim's real text, cut out of
assets.rs, in a vm with a stub Document. shim_keeps_its_contract pins the
override too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-11-14 19:23:59 +01:00
1 parent e99b42c387
commit ab9c574ca4
3 files changed
+97

No files matched your search

+24
View File
@@ -59,6 +59,15 @@ pub fn respond(request: Request<Vec<u8>>) -> Response<Cow<'static, [u8]>> {
/// server restarted, and nothing in the page asks for the login again.
/// - defines `__codemanDesktopRefused(id)` (not enumerable) for
/// [`refused_script`].
/// - answers `document.execCommand('paste')` with false, the way Chromium
/// does, without asking WebKit. Codeman's ⌘V handler calls it on its hidden
/// paste trap (`_handleImagePaste` in src/web/public/image-input.js). From a
/// WKWebView, WebKit answers that call by opening its "Paste" access menu at
/// the last click and holding the page until it is picked or the clipboard
/// changes, so the key's own paste, the one that carries a copied image, got
/// stuck behind it and a pasted picture never arrived. With this answer each
/// ⌘V delivers exactly one paste event to the trap, measured on macOS 27.2
/// for image data, a screenshot, a file copied in Finder, and text.
pub const HOST_SHIM: &str = r#"(function () {
'use strict';
if (!/^https?:$/.test(location.protocol) || window.CodemanHost) return;
@@ -113,6 +122,19 @@ pub const HOST_SHIM: &str = r#"(function () {
}
});
// Cmd+V: Codeman's paste trap calls document.execCommand('paste'). WebKit
// answers that from a WKWebView with its "Paste" access menu and holds the
// page until someone picks it, and the key's own paste (the one carrying a
// copied image) is stuck behind it. Refuse it as Chromium does; every other
// command reaches WebKit unchanged.
var nativeExec = typeof Document === 'function' ? Document.prototype.execCommand : null;
if (typeof nativeExec === 'function') {
Document.prototype.execCommand = function execCommand(command) {
if (typeof command === 'string' && command.toLowerCase() === 'paste') return false;
return nativeExec.apply(this, arguments);
};
}
var markDownload = function (ev) {
if (ev.button > 1) return;
var a = link(ev);
@@ -278,6 +300,8 @@ mod tests {
"'auxclick'",
"MAX_URL = 4096",
"typeof id === 'string' && id !== ''",
"Document.prototype.execCommand = function execCommand(command)",
"command.toLowerCase() === 'paste') return false",
] {
assert!(HOST_SHIM.contains(needle), "{needle}");
}