From 43d0b22e6b2fb8c6b5d904d78e2b8fc238589a2d Mon Sep 17 00:00:00 2001 From: curben Date: Sun, 30 Sep 2018 13:21:59 +0930 Subject: [PATCH] Fix clipboardjs implemention Previous commit 4fb0adb24a7e8d4a9d3dd7ee0843885a6400946f didn't work Credit to https://www.jianshu.com/p/3e9d614c1e77 --- themes/typing/source/css/typing.css | 22 ++++++++++++++++++++++ themes/typing/source/js/typing.js | 29 ++++++++++++++--------------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/themes/typing/source/css/typing.css b/themes/typing/source/css/typing.css index fb1364d..be2e25d 100644 --- a/themes/typing/source/css/typing.css +++ b/themes/typing/source/css/typing.css @@ -983,3 +983,25 @@ pre .javascript .function,pre .keyword{ font-size:0.9em } } +.copy-button { + cursor: pointer; + border: 1px solid #d5d5d5; + border-radius: 3px; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + -webkit-appearance: none; + font-size: 0.8em; + font-weight: 700; + line-height: 1em; + font-family:Courier,Courier New,monospace; + -webkit-transition: opacity .3s ease-in-out; + -o-transition: opacity .3s ease-in-out; + transition: opacity .3s ease-in-out; + padding: 2px; + opacity: 0; +} +.highlight:hover .copy-button{ + opacity: 1; +} \ No newline at end of file diff --git a/themes/typing/source/js/typing.js b/themes/typing/source/js/typing.js index 430809e..b3ce8cf 100644 --- a/themes/typing/source/js/typing.js +++ b/themes/typing/source/js/typing.js @@ -22,24 +22,24 @@ $('.fancybox').fancybox() } + //Add "Copy" button to code snippet + var code = document.getElementsByClassName('code'); + + for (var i = 0; i < code.length; i++) { + var button = document.createElement('button'); + button.className = 'copy-button'; + button.textContent = 'Copy'; + + code[i].appendChild(button); + } $(document).ready(function() { - // Add "Copy" button to code snippet - var pre = document.getElementsByTagName('pre'); - - for (var i = 0; i < pre.length; i++) { - var button = document.createElement('button'); - button.className = 'copy-button'; - button.textContent = 'Copy'; - - pre[i].appendChild(button); - } - + // Add copy to clipboard button for code snippet var copyCode = new ClipboardJS('.copy-button', { - target: function(trigger) { - return trigger.previousElementSibling; - } + target: function(trigger) { + return trigger.previousElementSibling; + } }); copyCode.on('success', function(event) { @@ -74,4 +74,3 @@ }); }); })(jQuery) -