Skip to content

Commit 827affc

Browse files
Fix copy feature in codeTrans (opea-project#2206)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6647c85 commit 827affc

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

CodeTrans/ui/svelte/src/routes/+page.svelte

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,37 @@
8383
let deleteFlag: boolean = false;
8484
let inputClick: boolean = true;
8585
86-
function handelCopy() {
87-
navigator.clipboard.writeText(output);
88-
copyText = "copied!";
86+
async function handelCopy() {
87+
try {
88+
if (navigator.clipboard && navigator.clipboard.writeText) {
89+
await navigator.clipboard.writeText(output);
90+
copyText = "copied!";
91+
} else {
92+
const textArea = document.createElement('textarea');
93+
textArea.value = output;
94+
textArea.style.position = 'fixed';
95+
textArea.style.left = '-999999px';
96+
textArea.style.top = '-999999px';
97+
document.body.appendChild(textArea);
98+
textArea.focus();
99+
textArea.select();
100+
101+
if (document.execCommand('copy')) {
102+
copyText = "copied!";
103+
} else {
104+
copyText = "copy failed";
105+
}
106+
107+
document.body.removeChild(textArea);
108+
}
109+
} catch (err) {
110+
console.error('Copy failed:', err);
111+
copyText = "copy failed";
112+
}
113+
89114
setTimeout(() => {
90115
copyText = "copy";
91-
}, 1000);
116+
}, 2000);
92117
}
93118
94119
function handelInputClick() {

0 commit comments

Comments
 (0)