diff --git a/assets/js/click-to-copy.js b/assets/js/click-to-copy.js index ebca6b6e..c9963255 100644 --- a/assets/js/click-to-copy.js +++ b/assets/js/click-to-copy.js @@ -1,5 +1,9 @@ let codeListings = document.querySelectorAll('.highlight > pre'); +const copyCode = (codeSample) => { + navigator.clipboard.writeText(codeSample.textContent.trim() + '\n'); +}; + for (let index = 0; index < codeListings.length; index++) { const parentWrapper = codeListings[index].parentElement const plausibleClass = parentWrapper.dataset.plausible @@ -23,14 +27,18 @@ for (let index = 0; index < codeListings.length; index++) { 'btn', 'btn-dark', 'btn-sm', - 'td-click-to-copy', - plausibleClass ? `plausible-event-name=${plausibleClass}` : "" + 'td-click-to-copy' ); + + if (plausibleClass) { + copyButton.classList.add(`plausible-event-name=${plausibleClass}`); + } + const tooltip = new bootstrap.Tooltip(copyButton); copyButton.onclick = () => { copyCode(codeSample); - copyButton.setAttribute('data-bs-original-title', 'Copied'); + copyButton.setAttribute('data-bs-original-title', 'Copied!'); tooltip.show(); }; @@ -44,7 +52,3 @@ for (let index = 0; index < codeListings.length; index++) { buttonDiv.append(copyButton); codeListings[index].insertBefore(buttonDiv, codeSample); } - -const copyCode = (codeSample) => { - navigator.clipboard.writeText(codeSample.textContent.trim() + '\n'); -}; \ No newline at end of file