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:
committed by
GitHub
parent
881e8bdf4f
commit
47cecfc356
@@ -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',
|
||||
};
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { useProjectsStore } from '@/stores/useProjectsStore';
|
||||
import { opencodeClient } from '@/lib/opencode/client';
|
||||
import { checkIsGitRepository } from '@/lib/gitApi';
|
||||
import { streamDebugEnabled } from '@/stores/utils/streamDebug';
|
||||
import { copyTextToClipboard as copyPlainTextToClipboard } from '@/lib/clipboard';
|
||||
|
||||
export interface DebugMessageInfo {
|
||||
messageId: string;
|
||||
@@ -373,13 +374,14 @@ export const debugUtils = {
|
||||
return JSON.stringify(report, null, 2);
|
||||
},
|
||||
|
||||
async copyTextToClipboard(text: string) {
|
||||
return copyPlainTextToClipboard(text);
|
||||
},
|
||||
|
||||
async copyDiagnosticsReport() {
|
||||
const report = await this.buildDiagnosticsReport();
|
||||
if (typeof navigator !== 'undefined' && navigator.clipboard) {
|
||||
await navigator.clipboard.writeText(report);
|
||||
return { ok: true, report } as const;
|
||||
}
|
||||
return { ok: false, report } as const;
|
||||
const result = await this.copyTextToClipboard(report);
|
||||
return { ...result, report } as const;
|
||||
},
|
||||
|
||||
checkLastMessage() {
|
||||
|
||||
Reference in New Issue
Block a user