|
1 | 1 | $(document).ready(function () { |
2 | | - const toggleButton = document.getElementById('toggleDarkMode'); |
3 | | - const darkThemeMedia = window.matchMedia('(prefers-color-scheme: dark)'); |
4 | 2 |
|
5 | | - function applyTheme(theme) { |
6 | | - if (theme === 'dark') { |
7 | | - document.body.classList.add('cDarkMode'); |
8 | | - $('.cColourMode').addClass('cDarkModeOn'); |
9 | | - } else { |
10 | | - document.body.classList.remove('cDarkMode'); |
11 | | - $('.cColourMode').removeClass('cDarkModeOn'); |
12 | | - } |
13 | | - } |
| 3 | + // Copy icon SVG |
| 4 | + const copyIconSVG = ` |
| 5 | + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> |
| 6 | + <path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/> |
| 7 | + </svg> |
| 8 | + `; |
14 | 9 |
|
15 | | - const savedTheme = localStorage.getItem('theme'); |
16 | | - if (savedTheme) { |
17 | | - applyTheme(savedTheme); |
18 | | - } else { |
19 | | - applyTheme(darkThemeMedia.matches ? 'dark' : 'light'); |
20 | | - } |
| 10 | + const checkIconSVG = ` |
| 11 | + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> |
| 12 | + <path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/> |
| 13 | + </svg> |
| 14 | + `; |
21 | 15 |
|
22 | | - // Toggle via button with ID |
23 | | - if (toggleButton) { |
24 | | - toggleButton.addEventListener('click', () => { |
25 | | - const isDark = document.body.classList.toggle('cDarkMode'); |
26 | | - $('.cColourMode').toggleClass('cDarkModeOn', isDark); |
27 | | - localStorage.setItem('theme', isDark ? 'dark' : 'light'); |
| 16 | + // Add copy buttons to code blocks (exclude inline code) |
| 17 | + function addCopyButtons() { |
| 18 | + $('.highlighter-rouge').each(function() { |
| 19 | + const $container = $(this); |
| 20 | + const $codeBlock = $container.find('.highlight'); |
| 21 | + |
| 22 | + // Only add copy button to fenced code blocks (with .highlight), not inline code |
| 23 | + if ($codeBlock.length > 0 && $container.find('.copy-btn').length === 0) { |
| 24 | + const $copyBtn = $(`<button class="copy-btn" title="Copy code">${copyIconSVG}</button>`); |
| 25 | + $container.append($copyBtn); |
| 26 | + |
| 27 | + $copyBtn.on('click', function(e) { |
| 28 | + e.preventDefault(); |
| 29 | + e.stopPropagation(); |
| 30 | + |
| 31 | + const codeText = $codeBlock.find('code').text().trim(); |
| 32 | + |
| 33 | + // Use modern clipboard API if available |
| 34 | + if (navigator.clipboard && window.isSecureContext) { |
| 35 | + navigator.clipboard.writeText(codeText).then(() => { |
| 36 | + showCopySuccess($copyBtn); |
| 37 | + }).catch(() => { |
| 38 | + fallbackCopyToClipboard(codeText, $copyBtn); |
| 39 | + }); |
| 40 | + } else { |
| 41 | + fallbackCopyToClipboard(codeText, $copyBtn); |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
28 | 45 | }); |
29 | 46 | } |
30 | 47 |
|
31 | | - // Toggle via .cColourMode click |
32 | | - $('.cColourMode').on('click', function () { |
33 | | - const isDark = $('body').toggleClass('cDarkMode').hasClass('cDarkMode'); |
34 | | - $(this).toggleClass('cDarkModeOn', isDark); |
35 | | - localStorage.setItem('theme', isDark ? 'dark' : 'light'); |
36 | | - }); |
| 48 | + function fallbackCopyToClipboard(text, $btn) { |
| 49 | + const textArea = document.createElement('textarea'); |
| 50 | + textArea.value = text; |
| 51 | + textArea.style.position = 'fixed'; |
| 52 | + textArea.style.left = '-999999px'; |
| 53 | + textArea.style.top = '-999999px'; |
| 54 | + document.body.appendChild(textArea); |
| 55 | + textArea.focus(); |
| 56 | + textArea.select(); |
| 57 | + |
| 58 | + try { |
| 59 | + document.execCommand('copy'); |
| 60 | + showCopySuccess($btn); |
| 61 | + } catch (err) { |
| 62 | + console.error('Failed to copy text: ', err); |
| 63 | + } |
| 64 | + |
| 65 | + textArea.remove(); |
| 66 | + } |
37 | 67 |
|
38 | | - // System theme listener |
39 | | - if (!savedTheme) { |
40 | | - darkThemeMedia.addEventListener('change', (e) => { |
41 | | - applyTheme(e.matches ? 'dark' : 'light'); |
42 | | - }); |
| 68 | + function showCopySuccess($btn) { |
| 69 | + $btn.addClass('copied').html(checkIconSVG); |
| 70 | + setTimeout(() => { |
| 71 | + $btn.removeClass('copied').html(copyIconSVG); |
| 72 | + }, 1000); |
43 | 73 | } |
| 74 | + |
| 75 | + // Initialize copy buttons |
| 76 | + addCopyButtons(); |
| 77 | + |
| 78 | + // Re-add copy buttons if content is dynamically loaded |
| 79 | + const observer = new MutationObserver(function(mutations) { |
| 80 | + mutations.forEach(function(mutation) { |
| 81 | + if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { |
| 82 | + setTimeout(addCopyButtons, 100); |
| 83 | + } |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + // Observe changes to the document body |
| 88 | + observer.observe(document.body, { |
| 89 | + childList: true, |
| 90 | + subtree: true |
| 91 | + }); |
44 | 92 | }); |
0 commit comments