mirror of
https://github.com/samber/awesome-prometheus-alerts.git
synced 2026-06-24 18:36:59 +08:00
Update site/src/components/CopyButton.astro
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
parent
e056652504
commit
96731e7ee2
1 changed files with 39 additions and 36 deletions
|
|
@ -43,45 +43,48 @@ const btnId = `copy-btn-${targetId}`;
|
|||
</button>
|
||||
)}
|
||||
|
||||
<script>
|
||||
document.querySelectorAll<HTMLButtonElement>('.copy-btn').forEach((btn) => {
|
||||
btn.addEventListener('click', async () => {
|
||||
const targetId = btn.dataset.copyTarget;
|
||||
if (!targetId) return;
|
||||
const target = document.getElementById(targetId);
|
||||
if (!target) return;
|
||||
<script define:vars={{ btnId }}>
|
||||
const btn = document.getElementById(btnId);
|
||||
if (!(btn instanceof HTMLButtonElement)) return;
|
||||
if (btn.dataset.copyBound === 'true') return;
|
||||
|
||||
const text = target.textContent ?? '';
|
||||
try {
|
||||
await navigator.clipboard.writeText(text.trim());
|
||||
} catch {
|
||||
// Fallback for older browsers
|
||||
const ta = document.createElement('textarea');
|
||||
ta.value = text.trim();
|
||||
ta.style.cssText = 'position:fixed;top:0;left:0;opacity:0;';
|
||||
document.body.appendChild(ta);
|
||||
ta.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(ta);
|
||||
}
|
||||
btn.dataset.copyBound = 'true';
|
||||
btn.addEventListener('click', async () => {
|
||||
const targetId = btn.dataset.copyTarget;
|
||||
if (!targetId) return;
|
||||
const target = document.getElementById(targetId);
|
||||
if (!target) return;
|
||||
|
||||
// Visual feedback
|
||||
const copyIcon = btn.querySelector('.copy-icon');
|
||||
const checkIcon = btn.querySelector('.check-icon');
|
||||
const copyLabel = btn.querySelector('.copy-label');
|
||||
const copiedLabel = btn.querySelector('.copied-label');
|
||||
const text = target.textContent ?? '';
|
||||
try {
|
||||
await navigator.clipboard.writeText(text.trim());
|
||||
} catch {
|
||||
// Fallback for older browsers
|
||||
const ta = document.createElement('textarea');
|
||||
ta.value = text.trim();
|
||||
ta.style.cssText = 'position:fixed;top:0;left:0;opacity:0;';
|
||||
document.body.appendChild(ta);
|
||||
ta.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(ta);
|
||||
}
|
||||
|
||||
copyIcon?.classList.add('hidden');
|
||||
checkIcon?.classList.remove('hidden');
|
||||
copyLabel?.classList.add('hidden');
|
||||
copiedLabel?.classList.remove('hidden');
|
||||
// Visual feedback
|
||||
const copyIcon = btn.querySelector('.copy-icon');
|
||||
const checkIcon = btn.querySelector('.check-icon');
|
||||
const copyLabel = btn.querySelector('.copy-label');
|
||||
const copiedLabel = btn.querySelector('.copied-label');
|
||||
|
||||
setTimeout(() => {
|
||||
copyIcon?.classList.remove('hidden');
|
||||
checkIcon?.classList.add('hidden');
|
||||
copyLabel?.classList.remove('hidden');
|
||||
copiedLabel?.classList.add('hidden');
|
||||
}, 2000);
|
||||
});
|
||||
copyIcon?.classList.add('hidden');
|
||||
checkIcon?.classList.remove('hidden');
|
||||
copyLabel?.classList.add('hidden');
|
||||
copiedLabel?.classList.remove('hidden');
|
||||
|
||||
setTimeout(() => {
|
||||
copyIcon?.classList.remove('hidden');
|
||||
checkIcon?.classList.add('hidden');
|
||||
copyLabel?.classList.remove('hidden');
|
||||
copiedLabel?.classList.add('hidden');
|
||||
}, 2000);
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Reference in a new issue