From f324b9f286a8a17f46ac90b2afded39fb32d5acc Mon Sep 17 00:00:00 2001 From: Octavian Rotari Date: Thu, 9 Jul 2026 12:39:26 +0100 Subject: [PATCH] fix: make yank-to-system-clipboard work when the document isn't focused `navigator.clipboard.writeText` rejects with "Document is not focused" during focus changes, so with `set clipboard=unnamed` a yank silently never reaches the system clipboard and the setting appears to stop working (intermittently). The clipboard->yank read path already guards against this exact error, but the yank->clipboard write path did not, and an unhandled rejection also dropped the state update. Prefer Electron's synchronous, focus-independent clipboard on the desktop app, falling back to the async API where `require` is unavailable (e.g. mobile). On any failure, leave `lastYankBuffer` unchanged so a later keyup/click/focusin event retries the write. Refs #276 --- main.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/main.ts b/main.ts index 7911085..55f76b2 100644 --- a/main.ts +++ b/main.ts @@ -595,9 +595,23 @@ export default class VimrcPlugin extends Plugin { // yank -> clipboard const buf = currentYankBuffer[0] if (buf !== this.lastYankBuffer[0]) { - await win.navigator.clipboard.writeText(buf); - this.lastYankBuffer = currentYankBuffer; - this.lastSystemClipboard = await win.navigator.clipboard.readText(); + try { + // `navigator.clipboard.writeText` rejects with "Document is not focused" + // during focus changes (the same failure the read path below guards + // against), silently dropping the yank. On the desktop app, Electron's + // clipboard is synchronous and focus-independent, so prefer it and fall + // back to the async API where `require` is unavailable (e.g. mobile). + const electronClipboard = (win as any).require?.('electron')?.clipboard; + if (electronClipboard) { + electronClipboard.writeText(buf); + } else { + await win.navigator.clipboard.writeText(buf); + } + this.lastYankBuffer = currentYankBuffer; + this.lastSystemClipboard = buf; + } catch (e) { + // Leave lastYankBuffer unchanged so a later keyup/click/focusin retries. + } return }