Skip to content

Commit

Permalink
Optimize copy-btn: always provide feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Jun 16, 2020
1 parent 44ba30e commit c7ea1ab
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 13 deletions.
2 changes: 0 additions & 2 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,6 @@ codeblock:
# Add copy button on codeblock
copy_button:
enable: false
# Show text copy result.
show_result: false
# Available values: default | flat | mac
style:

Expand Down
2 changes: 1 addition & 1 deletion scripts/helpers/next-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ hexo.extend.helper.register('next_config', function() {
version : next_version,
exturl : theme.exturl,
sidebar : theme.sidebar,
copycode : theme.codeblock.copy_button,
copycode : theme.codeblock.copy_button.enable,
back2top : theme.back2top,
bookmark : theme.bookmark,
fancybox : theme.fancybox,
Expand Down
12 changes: 2 additions & 10 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ NexT.utils = {
span.classList.add(`hljs-${name}`);
});
});
if (!CONFIG.copycode.enable) return;
if (!CONFIG.copycode) return;
element.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-clipboard fa-fw"></i></div>');
const button = element.querySelector('.copy-btn');
button.addEventListener('click', event => {
Expand All @@ -83,21 +83,13 @@ NexT.utils = {
ta.readOnly = true;
ta.value = code;
document.body.append(ta);
const selection = document.getSelection();
const selected = selection.rangeCount > 0 ? selection.getRangeAt(0) : false;
ta.select();
ta.setSelectionRange(0, code.length);
ta.readOnly = false;
const result = document.execCommand('copy');
if (CONFIG.copycode.show_result) {
target.querySelector('i').className = result ? 'fa fa-check-circle fa-fw' : 'fa fa-times-circle fa-fw';
}
target.querySelector('i').className = result ? 'fa fa-check-circle fa-fw' : 'fa fa-times-circle fa-fw';
ta.blur(); // For iOS
target.blur();
if (selected) {
selection.removeAllRanges();
selection.addRange(selected);
}
document.body.removeChild(ta);
});
element.addEventListener('mouseleave', () => {
Expand Down

0 comments on commit c7ea1ab

Please sign in to comment.