Implémentation copie des messages

This commit is contained in:
2024-09-10 10:16:08 +02:00
parent c12f9c9639
commit fdc76685da
2 changed files with 44 additions and 3 deletions

36
vite/src/lib/clipboard.js Normal file
View File

@@ -0,0 +1,36 @@
// src/lib/clipboard.js
export async function copyToClipboard(text) {
if (navigator.clipboard) {
try {
await navigator.clipboard.writeText(text);
} catch (err) {
console.error('Failed to copy text:', err);
}
} else {
return fallbackCopyToClipboard(text);
}
}
function fallbackCopyToClipboard(text) {
return new Promise((resolve, reject) => {
const textArea = document.createElement("textarea");
textArea.value = text;
// Éviter le scrolling vers le bas
textArea.style.position = "fixed";
textArea.style.top = "-9999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
if (document.execCommand('copy')) {
resolve();
} else {
reject(new Error('Fallback: Copy command was unsuccessful'));
}
document.body.removeChild(textArea);
});
}