diff --git a/assets/js/code-copy.js b/assets/js/code-copy.js index 5ea063d4..45df96a6 100644 --- a/assets/js/code-copy.js +++ b/assets/js/code-copy.js @@ -1,30 +1,63 @@ -document.querySelectorAll('.code-copy-btn').forEach(function (button) { - button.addEventListener('click', function (e) { - e.preventDefault(); - const targetId = button.getAttribute('data-clipboard-target'); - const target = document.querySelector(targetId); - let codeElement; - if (target.tagName === 'CODE') { - codeElement = target; - } else { - // Select the last code element in case line numbers are present - const codeElements = target.querySelectorAll('code'); - codeElement = codeElements[codeElements.length - 1]; - } - if (codeElement) { - // Replace double newlines with single newlines in the innerText - // as each line inside has trailing newline '\n' - const code = codeElement.innerText.replace(/\n\n/g, '\n'); - navigator.clipboard.writeText(code).then(function () { - button.classList.add('copied'); - setTimeout(function () { - button.classList.remove('copied'); - }, 500); - }).catch(function (err) { - console.error('Failed to copy text: ', err); - }); - } else { - console.error('Target element not found'); - } +// Copy button for code blocks + +document.addEventListener('DOMContentLoaded', function () { + const getCopyIcon = () => { + const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + svg.innerHTML = ` + + `; + svg.setAttribute('fill', 'none'); + svg.setAttribute('viewBox', '0 0 24 24'); + svg.setAttribute('stroke', 'currentColor'); + svg.setAttribute('stroke-width', '2'); + return svg; + } + + const getSuccessIcon = () => { + const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + svg.innerHTML = ` + + `; + svg.setAttribute('fill', 'none'); + svg.setAttribute('viewBox', '0 0 24 24'); + svg.setAttribute('stroke', 'currentColor'); + svg.setAttribute('stroke-width', '2'); + return svg; + } + + document.querySelectorAll('.code-copy-btn').forEach(function (button) { + // Add copy and success icons + button.querySelector('.copy-icon')?.appendChild(getCopyIcon()); + button.querySelector('.success-icon')?.appendChild(getSuccessIcon()); + + // Add click event listener for copy button + button.addEventListener('click', function (e) { + e.preventDefault(); + const targetId = button.getAttribute('data-clipboard-target'); + const target = document.querySelector(targetId); + let codeElement; + if (target.tagName === 'CODE') { + codeElement = target; + } else { + // Select the last code element in case line numbers are present + const codeElements = target.querySelectorAll('code'); + codeElement = codeElements[codeElements.length - 1]; + } + if (codeElement) { + // Replace double newlines with single newlines in the innerText + // as each line inside has trailing newline '\n' + const code = codeElement.innerText.replace(/\n\n/g, '\n'); + navigator.clipboard.writeText(code).then(function () { + button.classList.add('copied'); + setTimeout(function () { + button.classList.remove('copied'); + }, 500); + }).catch(function (err) { + console.error('Failed to copy text: ', err); + }); + } else { + console.error('Target element not found'); + } + }); }); }); diff --git a/exampleSite/hugo_stats.json b/exampleSite/hugo_stats.json index c98c5429..64083cab 100644 --- a/exampleSite/hugo_stats.json +++ b/exampleSite/hugo_stats.json @@ -154,6 +154,7 @@ "contrast-more:text-gray-800", "contrast-more:text-gray-900", "contrast-more:underline", + "copy-icon", "cursor-default", "cursor-pointer", "dark:before:bg-neutral-800", @@ -590,4 +591,4 @@ ], "ids": null } -} \ No newline at end of file +} diff --git a/layouts/_default/_markup/render-codeblock.html b/layouts/_default/_markup/render-codeblock.html index c3b7841a..b67ca6cf 100644 --- a/layouts/_default/_markup/render-codeblock.html +++ b/layouts/_default/_markup/render-codeblock.html @@ -15,9 +15,13 @@
{{ .Inner }}
{{- end -}}
-