Implémentation copie des messages
This commit is contained in:
36
vite/src/lib/clipboard.js
Normal file
36
vite/src/lib/clipboard.js
Normal 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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user