Skip to content

Commit 1bcdc8d

Browse files
nedtwiggclaude
andcommitted
Parallelize clipboard file-path and text reads in doPaste
Now that the standalone reads clipboard text natively, the file-first ordering no longer protects against the WKWebView paste prompt and can run alongside the text read. Image stays sequential since it allocates a temp file. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 47e6caf commit 1bcdc8d

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

lib/src/lib/clipboard.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ describe('doPaste three-tier fallthrough', () => {
3131
});
3232
});
3333

34-
it('uses file refs when present and never reads text or image', async () => {
34+
it('prefers file refs over text and skips the image read', async () => {
3535
mocks.readClipboardFilePaths.mockResolvedValue(['/tmp/a.png', '/tmp/b file.png']);
36-
mocks.readText.mockResolvedValue('should not be read');
36+
mocks.readText.mockResolvedValue('coexisting text payload');
3737
mocks.readClipboardImageAsFilePath.mockResolvedValue('/tmp/img.png');
3838

3939
await doPaste('t1');
4040

41-
expect(mocks.readText).not.toHaveBeenCalled();
4241
expect(mocks.readClipboardImageAsFilePath).not.toHaveBeenCalled();
4342
expect(mocks.writePty).toHaveBeenCalledTimes(1);
4443
expect(mocks.writePty).toHaveBeenCalledWith('t1', '/tmp/a.png /tmp/b\\ file.png ');

lib/src/lib/clipboard.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,24 @@ async function readTextFromClipboard(): Promise<string> {
8484

8585
/**
8686
* Read the clipboard and write its contents to the PTY, honoring the inside
87-
* program's bracketed-paste mode when enabled (spec §8.5). Falls through from
88-
* file references plain text → raw image (saved to a temp file) so a
89-
* Cmd+V of a Finder file or a screenshot both type a usable path.
87+
* program's bracketed-paste mode when enabled (spec §8.5). Prefers file
88+
* references over plain text (a Finder Cmd+V types the path, not "Document.pdf"
89+
* as a name string), with raw images saved to a temp file as a last resort.
9090
*
91-
* Files are checked before text so that a file-ref clipboard never reaches
92-
* `navigator.clipboard.readText()` — on macOS WKWebView that call can trigger
93-
* a native paste-permission popup when the clipboard came from another app.
91+
* File-path and text reads run in parallel since they're independent IPC
92+
* roundtrips; the image read is sequential because it allocates a temp file.
9493
*/
9594
export async function doPaste(terminalId: string): Promise<void> {
9695
const platform = getPlatform();
9796

98-
const paths = await platform.readClipboardFilePaths().catch(() => null);
97+
const [paths, text] = await Promise.all([
98+
platform.readClipboardFilePaths().catch(() => null),
99+
readTextFromClipboard(),
100+
]);
99101
if (paths && paths.length > 0) {
100102
pasteFilePaths(terminalId, paths);
101103
return;
102104
}
103-
104-
const text = await readTextFromClipboard();
105105
if (text) {
106106
writePasteToPty(terminalId, text);
107107
return;

0 commit comments

Comments
 (0)