|
| 1 | +document.addEventListener("click", function(e) { |
| 2 | + const btn = e.target.closest(".copy-btn"); |
| 3 | + |
| 4 | + if (!btn) { |
| 5 | + resetCopiedButtons(); |
| 6 | + return; |
| 7 | + } |
| 8 | + |
| 9 | + const targetId = btn.dataset.copyTarget; |
| 10 | + const target = document.getElementById(targetId); |
| 11 | + if (!target) return; |
| 12 | + |
| 13 | + if (!btn.dataset.originalLabel) { |
| 14 | + btn.dataset.originalLabel = btn.textContent.trim(); |
| 15 | + } |
| 16 | + |
| 17 | + const status = btn.parentElement.querySelector(".copy-status"); |
| 18 | + |
| 19 | + navigator.clipboard.writeText(target.textContent).then(() => { |
| 20 | + resetCopiedButtons(btn); |
| 21 | + btn.classList.remove("btn-primary"); |
| 22 | + btn.classList.add("btn-success", "copied"); |
| 23 | + btn.innerHTML = '<i class="fas fa-check" aria-hidden="true"></i> Copié!'; |
| 24 | + target.classList.add("copied"); |
| 25 | + if (status) status.textContent = "Invite copiée dans le presse-papiers."; |
| 26 | + }).catch(() => { |
| 27 | + if (status) status.textContent = "Impossible de copier l'invite. Veuillez la copier manuellement."; |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +function resetCopiedButtons(except) { |
| 32 | + document.querySelectorAll(".copy-btn.copied").forEach((btn) => { |
| 33 | + if (btn === except) return; |
| 34 | + |
| 35 | + btn.classList.remove("btn-success", "copied"); |
| 36 | + btn.classList.add("btn-primary"); |
| 37 | + btn.textContent = btn.dataset.originalLabel || "Copier l'invite"; |
| 38 | + |
| 39 | + const target = document.getElementById(btn.dataset.copyTarget); |
| 40 | + if (target) target.classList.remove("copied"); |
| 41 | + |
| 42 | + const status = btn.parentElement.querySelector(".copy-status"); |
| 43 | + if (status) status.textContent = ""; |
| 44 | + }); |
| 45 | +} |
0 commit comments