mirror of
https://github.com/next-theme/hexo-theme-next.git
synced 2026-01-18 18:33:42 +00:00
Optimize copy-btn: always provide feedback
This commit is contained in:
parent
44ba30e367
commit
c7ea1abe64
@ -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:
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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 => {
|
||||
@ -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', () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user