From d7010dafd19be0b2662e555c1316e4c6b2fb164c Mon Sep 17 00:00:00 2001 From: Ethan Ruszanowski Date: Mon, 29 Jan 2024 18:38:53 -0500 Subject: [PATCH] Improve copy button script formatting --- assets/js/clipboard.js | 78 +++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/assets/js/clipboard.js b/assets/js/clipboard.js index 7febb0f..4284b37 100644 --- a/assets/js/clipboard.js +++ b/assets/js/clipboard.js @@ -1,47 +1,55 @@ -/* - * A nice little clipboard script provided by crykn. Thanks! +/* + * A nice little clipboard script provided by Crykn. Thanks! */ function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); + return new Promise((resolve) => setTimeout(resolve, ms)); } async function onClickEffect(btn, style) { - btn.removeClass("btn-light"); - btn.addClass(style); - await sleep(250); - btn.removeClass(style); - btn.addClass("btn-light"); + btn.removeClass("btn-light"); + btn.addClass(style); + await sleep(250); + btn.removeClass(style); + btn.addClass("btn-light"); } -$(document).ready(function() { - // Create butons - $(".page__content pre > code").each(function() { - $(this).parent().prepend( - $(document.createElement('button')).prop({ - type: 'button', - innerHTML: '', - }) - .attr('title', 'Copy to clipboard') - .addClass('btn') - .addClass('btn--primary') - .css('position', 'absolute') - .css('right', '1em') - // Click listener - .on('click', function() { - let codeElement = $(this).parent().children('code').first(); +$(document).ready(function () { + // Create button + $(".page__content pre > code").each(function () { + $(this) + .parent() + .prepend( + $(document.createElement("button")) + .prop({ + type: "button", + innerHTML: '', + }) + .attr("title", "Copy to clipboard") + .addClass("btn") + .addClass("btn--primary") + .css("position", "absolute") + .css("right", "1em") + // Click listener + .on("click", function () { + let codeElement = $(this).parent().children("code").first(); - if (!codeElement) { - throw new Error("Unexpected error! No corresponding code block was found for this button."); - } + if (!codeElement) { + throw new Error( + "Unexpected error! No corresponding code block was found for this button." + ); + } - // Blink effect - onClickEffect($(this), "btn--success") + // Blink effect + onClickEffect($(this), "btn--success"); - // Copy to clipoard function - navigator.clipboard.writeText($(codeElement).text()).then(() => true, () => true); - return true; - }) - ); - }); + // Copy to clipoard function + navigator.clipboard.writeText($(codeElement).text()).then( + () => true, + () => true + ); + return true; + }) + ); + }); });