From dcf54cad8529526fd7f8d9d4b84b63ccb3fa9630 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Sun, 5 Apr 2026 08:40:09 +0200 Subject: feat: add JavaScript modules (theme toggle, matrix rain, progress tracking, copy-to-clipboard) Implement all 4 JavaScript modules: - theme-toggle.js: Theme switching with localStorage persistence - matrix-rain.js: Animated matrix-style rain effect on canvas - progress-bar.js: Reading progress tracking during scroll - copy-code.js: Copy-to-clipboard functionality for code blocks Co-Authored-By: Claude Haiku 4.5 --- assets/js/copy-code.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 assets/js/copy-code.js (limited to 'assets/js/copy-code.js') diff --git a/assets/js/copy-code.js b/assets/js/copy-code.js new file mode 100644 index 0000000..a18bf6c --- /dev/null +++ b/assets/js/copy-code.js @@ -0,0 +1,42 @@ +// copy-code.js +(function() { + // Add copy button to all code blocks + const codeBlocks = document.querySelectorAll('pre, .highlight'); + + codeBlocks.forEach(block => { + // Create copy button + const btn = document.createElement('button'); + btn.className = 'code-copy-btn'; + btn.textContent = 'copy'; + btn.type = 'button'; + btn.setAttribute('aria-label', 'Copy code'); + + // Get code text + const code = block.querySelector('code'); + const text = code ? code.textContent : block.textContent; + + // Copy on click + btn.addEventListener('click', async function() { + try { + await navigator.clipboard.writeText(text); + + // Show feedback + const originalText = btn.textContent; + btn.textContent = 'copied!'; + btn.classList.add('copied'); + + setTimeout(() => { + btn.textContent = originalText; + btn.classList.remove('copied'); + }, 2000); + } catch (err) { + console.error('Failed to copy:', err); + btn.textContent = 'error'; + } + }); + + // Add button to block + block.style.position = 'relative'; + block.appendChild(btn); + }); +})(); -- cgit v1.2.3