From c7ea1abe64de55e2b5039650ece20c0b2b2f733b Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Wed, 17 Jun 2020 01:07:52 +0800 Subject: [PATCH] Optimize copy-btn: always provide feedback --- _config.yml | 2 -- scripts/helpers/next-config.js | 2 +- source/js/utils.js | 12 ++---------- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/_config.yml b/_config.yml index ebbfb79..dfe5d19 100644 --- a/_config.yml +++ b/_config.yml @@ -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: diff --git a/scripts/helpers/next-config.js b/scripts/helpers/next-config.js index ffb1f82..c5bbc0e 100644 --- a/scripts/helpers/next-config.js +++ b/scripts/helpers/next-config.js @@ -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, diff --git a/source/js/utils.js b/source/js/utils.js index e7582df..60b406d 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -70,7 +70,7 @@ NexT.utils = { span.classList.add(`hljs-${name}`); }); }); - if (!CONFIG.copycode.enable) return; + if (!CONFIG.copycode) return; element.insertAdjacentHTML('beforeend', '
'); const button = element.querySelector('.copy-btn'); button.addEventListener('click', event => { @@ -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', () => {