From 4fb0adb24a7e8d4a9d3dd7ee0843885a6400946f Mon Sep 17 00:00:00 2001 From: curben Date: Sat, 29 Sep 2018 18:37:34 +0930 Subject: [PATCH] Add 'Copy' button to code snippet https://webdesign.tutsplus.com/tutorials/copy-to-clipboard-made-easy-with-clipboardjs--cms-25086 https://davidwalsh.name/clipboard --- themes/typing/source/js/typing.js | 65 +++++++++++++++---------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/themes/typing/source/js/typing.js b/themes/typing/source/js/typing.js index 6997de5..430809e 100644 --- a/themes/typing/source/js/typing.js +++ b/themes/typing/source/js/typing.js @@ -24,48 +24,46 @@ $(document).ready(function() { - var QRBox = $('#QRBox'); - var MainBox = $('.MainBox'); - var BTCQR = $('#BTCQR'); - var AliPayQR = $('#AliPayQR'); - var WeChatQR = $('#WeChatQR'); - var currentQR; - function showQR(QR) { - $('#DonateText,#donateBox,#github').addClass('blur'); - currentQR = QR; - QRBox.fadeIn(300,function(argument) { - QR.addClass('showQR'); - }); - } + // Add "Copy" button to code snippet + var pre = document.getElementsByTagName('pre'); - $('#donateBox>li').click(function(event) { - var thisID = $(this).attr('id'); - if (thisID === 'BTC') { - showQR(BTCQR); - new Clipboard('#BTCBn'); - } else if (thisID === 'AliPay') { - showQR(AliPayQR); - } else if (thisID === 'WeChat') { - showQR(WeChatQR); - } - }); + for (var i = 0; i < pre.length; i++) { + var button = document.createElement('button'); + button.className = 'copy-button'; + button.textContent = 'Copy'; - MainBox.click(function(event) { - if (currentQR) currentQR.removeClass('showQR').addClass('hideQR'); - setTimeout (function(a) { - QRBox.fadeOut(300,function(argument) { - MainBox.removeClass('hideQR'); - }); - $('#DonateText,#donateBox,#github').removeClass('blur'); - },600); + pre[i].appendChild(button); + } + + var copyCode = new ClipboardJS('.copy-button', { + target: function(trigger) { + return trigger.previousElementSibling; + } }); + copyCode.on('success', function(event) { + event.clearSelection(); + event.trigger.textContent = 'Copied'; + window.setTimeout(function() { + event.trigger.textContent = 'Copy'; + }, 2000); + }); + + copyCode.on('error', function(event) { + event.trigger.textContent = 'Press "Ctrl + C" to copy'; + window.setTimeout(function() { + event.trigger.textContent = 'Copy'; + }, 2000); + }); + + // Navigation menu $('#menu').click(function (event) { var nav = $('#main-nav'); nav.toggle('fast'); }); + // Show navigation button for smaller screen $(window).resize(function () { var viewportWidth = $(window).width(); if (viewportWidth > 468) { @@ -75,4 +73,5 @@ } }); }); -})(jQuery) \ No newline at end of file +})(jQuery) +