mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-12-05 08:15:41 -08:00
Let Ctrl+V reach the agent on macOS, and leave Cmd+V to the webview's own paste
AI Assisted
This commit is contained in:
1 parent
b629f5ef04
commit
91479b21a1
3 files changed
+36
-3
No files matched your search
@@ -774,7 +774,10 @@ export function shortcutFromMessage(data) {
|
|||||||
* What a key press does in the terminal's clipboard handling: "copy" (Ctrl+Shift+C, Cmd+C, and
|
* What a key press does in the terminal's clipboard handling: "copy" (Ctrl+Shift+C, Cmd+C, and
|
||||||
* Ctrl+C while text is selected, the Windows Terminal rule: with nothing selected Ctrl+C is the
|
* Ctrl+C while text is selected, the Windows Terminal rule: with nothing selected Ctrl+C is the
|
||||||
* interrupt and returns null), "paste" (Ctrl+V, Ctrl+Shift+V, Cmd+V), or null. With `mac`, a plain
|
* interrupt and returns null), "paste" (Ctrl+V, Ctrl+Shift+V, Cmd+V), or null. With `mac`, a plain
|
||||||
* Ctrl+C is always the interrupt, as in every macOS terminal: copy there is Cmd+C.
|
* Ctrl+C is always the interrupt, as in every macOS terminal: copy there is Cmd+C. Ctrl+V belongs to
|
||||||
|
* the agent there too (Claude Code pastes an image with it), and Cmd+V returns null as well: it is
|
||||||
|
* left to the webview's own paste event, which xterm takes as one paste, because reading the
|
||||||
|
* clipboard from script makes WebKit ask with a Paste menu before the text arrives.
|
||||||
*/
|
*/
|
||||||
export function clipboardAction(event, hasSelection, { mac = false } = {}) {
|
export function clipboardAction(event, hasSelection, { mac = false } = {}) {
|
||||||
if (!event || event.altKey) return null;
|
if (!event || event.altKey) return null;
|
||||||
@@ -784,7 +787,7 @@ export function clipboardAction(event, hasSelection, { mac = false } = {}) {
|
|||||||
if (event.ctrlKey && !event.metaKey) return event.shiftKey || (hasSelection && !mac) ? 'copy' : null;
|
if (event.ctrlKey && !event.metaKey) return event.shiftKey || (hasSelection && !mac) ? 'copy' : null;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (key === 'v' && (event.ctrlKey || event.metaKey)) return 'paste';
|
if (key === 'v' && !mac && (event.ctrlKey || event.metaKey)) return 'paste';
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ export async function mountTerminal(container, { onInput, onResize, onFontSize,
|
|||||||
// Ctrl+C copies while text is selected and is the interrupt otherwise (the Windows Terminal
|
// Ctrl+C copies while text is selected and is the interrupt otherwise (the Windows Terminal
|
||||||
// rule; on a Mac it is always the interrupt); Ctrl+Shift+C and Cmd+C always copy.
|
// rule; on a Mac it is always the interrupt); Ctrl+Shift+C and Cmd+C always copy.
|
||||||
// preventDefault on the paste keys: the browser's own paste event would otherwise reach the
|
// preventDefault on the paste keys: the browser's own paste event would otherwise reach the
|
||||||
// terminal a second time.
|
// terminal a second time. On a Mac no key is a paste key here: Cmd+V is that own paste event.
|
||||||
const action = clipboardAction(event, term.hasSelection(), { mac: IS_MAC });
|
const action = clipboardAction(event, term.hasSelection(), { mac: IS_MAC });
|
||||||
if (action === 'copy') {
|
if (action === 'copy') {
|
||||||
copySelection();
|
copySelection();
|
||||||
|
|||||||
@@ -1383,6 +1383,36 @@ test("clipboardAction: Ctrl+C copies only with a selection (else it is the inter
|
|||||||
assert.equal(clipboardAction(null, true), null);
|
assert.equal(clipboardAction(null, true), null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("clipboardAction on a Mac: Ctrl+V goes to the agent, Cmd+V is left to the webview's own paste", () => {
|
||||||
|
const ev = (key, mods = {}) => ({ key, ctrlKey: false, metaKey: false, shiftKey: false, altKey: false, ...mods });
|
||||||
|
assert.equal(clipboardAction(ev("v", { ctrlKey: true }), false, { mac: true }), null, "xterm sends \\x16 to the agent (Claude Code's image paste)");
|
||||||
|
assert.equal(clipboardAction(ev("V", { ctrlKey: true, shiftKey: true }), false, { mac: true }), null);
|
||||||
|
assert.equal(clipboardAction(ev("v", { metaKey: true }), false, { mac: true }), null, "a script clipboard read makes WebKit ask with a Paste menu first");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("clipboardAction off a Mac: every key decides exactly as before the Mac rule", () => {
|
||||||
|
// The rule as it was before Ctrl+V was left to the agent on a Mac.
|
||||||
|
const before = (event, hasSelection) => {
|
||||||
|
if (event.altKey) return null;
|
||||||
|
const key = event.key.toLowerCase();
|
||||||
|
if (key === "c") {
|
||||||
|
if (event.metaKey && !event.ctrlKey) return "copy";
|
||||||
|
if (event.ctrlKey && !event.metaKey) return event.shiftKey || hasSelection ? "copy" : null;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (key === "v" && (event.ctrlKey || event.metaKey)) return "paste";
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
for (const key of ["c", "C", "v", "V", "x"]) {
|
||||||
|
for (let bits = 0; bits < 16; bits++) {
|
||||||
|
const event = { key, ctrlKey: Boolean(bits & 1), metaKey: Boolean(bits & 2), shiftKey: Boolean(bits & 4), altKey: Boolean(bits & 8) };
|
||||||
|
for (const sel of [true, false]) {
|
||||||
|
assert.equal(clipboardAction(event, sel), before(event, sel), `${key} ${JSON.stringify(event)} selection ${sel}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
test("the terminal pastes once and in brackets, copies on select only by preference, and has a Copy / Paste / Clear menu", () => {
|
test("the terminal pastes once and in brackets, copies on select only by preference, and has a Copy / Paste / Clear menu", () => {
|
||||||
const term = fs.readFileSync(path.join(HERE, "../plan2code-app/ui/terminal.js"), "utf8");
|
const term = fs.readFileSync(path.join(HERE, "../plan2code-app/ui/terminal.js"), "utf8");
|
||||||
assert.match(term, /term\.paste\(text\)/, "bracketed paste, not onInput(text)");
|
assert.match(term, /term\.paste\(text\)/, "bracketed paste, not onInput(text)");
|
||||||
|
|||||||
Reference in new issue
Block a user