Skip to content

Commit

Permalink
Adjust copy-button style (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang authored Nov 15, 2019
1 parent 3846991 commit f4e757d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 26 deletions.
3 changes: 0 additions & 3 deletions languages/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ post:
sticky: Sticky
views: Views
related_posts: Related Posts
copy_button: Copy
copy_success: Copied
copy_failure: Copy failed
copyright:
author: Post author
link: Post link
Expand Down
5 changes: 0 additions & 5 deletions layout/_partials/head/head.swig
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@
localsearch: {{ theme.local_search | json }},
path: '{{ config.search.path }}',
motion: {{ theme.motion | json }},
translation: {
copy_button: '{{ __("post.copy_button") }}',
copy_success: '{{ __("post.copy_success") }}',
copy_failure: '{{ __("post.copy_failure") }}'
},
sidebarPadding: 40
};
</script>
6 changes: 3 additions & 3 deletions source/css/_common/scaffolding/highlight/copy-code.styl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.highlight-wrap {
.highlight-container {
position: relative;
}

.highlight-wrap:hover .copy-btn, .highlight-wrap .copy-btn:focus {
.highlight-container:hover .copy-btn, .highlight-container .copy-btn:focus {
opacity: 1;
}

Expand Down Expand Up @@ -44,7 +44,7 @@
}

if (hexo-config('codeblock.copy_button.style') == 'mac') {
.highlight-wrap {
.highlight-container {
background: #21252b;
border-radius: 5px;
box-shadow: 0 10px 30px 0 rgba(0, 0, 0, .4);
Expand Down
11 changes: 10 additions & 1 deletion source/css/_common/scaffolding/highlight/highlight.styl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pre {
pre {
border: 0;
margin: 0;
padding: 10px 0;
padding: 0;
}

table {
Expand All @@ -59,6 +59,15 @@ pre {
width: auto;
}

tr {
&:first-child pre {
padding-top: 10px;
}
&:last-child pre {
padding-bottom: 10px;
}
}

td {
border: 0;
padding: 0;
Expand Down
19 changes: 5 additions & 14 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,18 @@ NexT.utils = {
*/
registerCopyCode: function() {
document.querySelectorAll('figure.highlight').forEach(element => {
const initButton = button => {
if (CONFIG.copycode.style === 'mac') {
button.innerHTML = '<i class="fa fa-clipboard"></i>';
} else {
button.innerText = CONFIG.translation.copy_button;
}
};
const box = document.createElement('div');
box.classList.add('highlight-wrap');
element.wrap(box);
element.parentNode.insertAdjacentHTML('beforeend', '<div class="copy-btn"></div>');
box.classList.add('highlight-container');
box.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-clipboard"></i></div>');
var button = element.parentNode.querySelector('.copy-btn');
button.addEventListener('click', event => {
var target = event.currentTarget;
var code = [...target.parentNode.querySelectorAll('.code .line')].map(line => {
return line.innerText;
}).join('\n');
var ta = document.createElement('textarea');
var yPosition = window.scrollY;
ta.style.top = yPosition + 'px'; // Prevent page scrolling
ta.style.top = window.scrollY + 'px'; // Prevent page scrolling
ta.style.position = 'absolute';
ta.style.opacity = '0';
ta.readOnly = true;
Expand All @@ -92,7 +84,7 @@ NexT.utils = {
ta.readOnly = false;
var result = document.execCommand('copy');
if (CONFIG.copycode.show_result) {
target.innerText = result ? CONFIG.translation.copy_success : CONFIG.translation.copy_failure;
target.querySelector('i').className = result ? 'fa fa-check' : 'fa fa-times';
}
ta.blur(); // For iOS
target.blur();
Expand All @@ -104,10 +96,9 @@ NexT.utils = {
});
button.addEventListener('mouseleave', event => {
setTimeout(() => {
initButton(event.target);
event.target.querySelector('i').className = 'fa fa-clipboard';
}, 300);
});
initButton(button);
});
},

Expand Down

0 comments on commit f4e757d

Please sign in to comment.