refactor: unify clipboard copy flow across desktop/web/vscode runtimes (#458)

* fix: prevent copying incomplete diagnostics report

* refactoring: clipboard writes to unified cross-runtime fallback helper
This commit is contained in:
Bohdan Triapitsyn
2026-02-20 14:50:26 +02:00
committed by GitHub
parent 881e8bdf4f
commit 47cecfc356
22 changed files with 232 additions and 209 deletions
+39
View File
@@ -0,0 +1,39 @@
export type ClipboardCopyResult =
| { ok: true; method: 'clipboard' | 'execCommand' }
| { ok: false; error: string };
export async function copyTextToClipboard(text: string): Promise<ClipboardCopyResult> {
let clipboardError: string | null = null;
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
try {
await navigator.clipboard.writeText(text);
return { ok: true, method: 'clipboard' };
} catch (error) {
clipboardError = error instanceof Error ? error.message : String(error);
}
}
if (typeof document !== 'undefined' && document.body) {
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.setAttribute('readonly', '');
textarea.style.position = 'fixed';
textarea.style.top = '-1000px';
textarea.style.left = '-1000px';
document.body.appendChild(textarea);
textarea.select();
textarea.setSelectionRange(0, textarea.value.length);
const copied = document.execCommand('copy');
document.body.removeChild(textarea);
if (copied) {
return { ok: true, method: 'execCommand' };
}
}
return {
ok: false,
error: clipboardError ?? 'Clipboard access denied in current context',
};
}