Spaces:
Runtime error
Runtime error
Damián Pumar
commited on
fix: 🐛 Fix copy button (#1672)
Browse files
src/lib/components/CopyToClipBoardBtn.svelte
CHANGED
|
@@ -10,10 +10,30 @@
|
|
| 10 |
let isSuccess = false;
|
| 11 |
let timeout: ReturnType<typeof setTimeout>;
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
const handleClick = async () => {
|
| 14 |
-
// writeText() can be unavailable or fail in some cases (iframe, etc) so we try/catch
|
| 15 |
try {
|
| 16 |
-
await
|
| 17 |
|
| 18 |
isSuccess = true;
|
| 19 |
if (timeout) {
|
|
|
|
| 10 |
let isSuccess = false;
|
| 11 |
let timeout: ReturnType<typeof setTimeout>;
|
| 12 |
|
| 13 |
+
const unsecuredCopy = (text: string) => {
|
| 14 |
+
//Old or insecure browsers
|
| 15 |
+
|
| 16 |
+
const textArea = document.createElement("textarea");
|
| 17 |
+
textArea.value = text;
|
| 18 |
+
document.body.appendChild(textArea);
|
| 19 |
+
textArea.focus();
|
| 20 |
+
textArea.select();
|
| 21 |
+
document.execCommand("copy");
|
| 22 |
+
document.body.removeChild(textArea);
|
| 23 |
+
|
| 24 |
+
return Promise.resolve();
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
const copy = async (text: string) => {
|
| 28 |
+
if (window.isSecureContext && navigator.clipboard) {
|
| 29 |
+
return navigator.clipboard.writeText(text);
|
| 30 |
+
}
|
| 31 |
+
return unsecuredCopy(text);
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
const handleClick = async () => {
|
|
|
|
| 35 |
try {
|
| 36 |
+
await copy(value);
|
| 37 |
|
| 38 |
isSuccess = true;
|
| 39 |
if (timeout) {
|