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
|
# Add copy button on codeblock
|
||||||
copy_button:
|
copy_button:
|
||||||
enable: false
|
enable: false
|
||||||
# Show text copy result.
|
|
||||||
show_result: false
|
|
||||||
# Available values: default | flat | mac
|
# Available values: default | flat | mac
|
||||||
style:
|
style:
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,7 @@ hexo.extend.helper.register('next_config', function() {
|
|||||||
version : next_version,
|
version : next_version,
|
||||||
exturl : theme.exturl,
|
exturl : theme.exturl,
|
||||||
sidebar : theme.sidebar,
|
sidebar : theme.sidebar,
|
||||||
copycode : theme.codeblock.copy_button,
|
copycode : theme.codeblock.copy_button.enable,
|
||||||
back2top : theme.back2top,
|
back2top : theme.back2top,
|
||||||
bookmark : theme.bookmark,
|
bookmark : theme.bookmark,
|
||||||
fancybox : theme.fancybox,
|
fancybox : theme.fancybox,
|
||||||
|
|||||||
@ -70,7 +70,7 @@ NexT.utils = {
|
|||||||
span.classList.add(`hljs-${name}`);
|
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>');
|
element.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-clipboard fa-fw"></i></div>');
|
||||||
const button = element.querySelector('.copy-btn');
|
const button = element.querySelector('.copy-btn');
|
||||||
button.addEventListener('click', event => {
|
button.addEventListener('click', event => {
|
||||||
@ -83,21 +83,13 @@ NexT.utils = {
|
|||||||
ta.readOnly = true;
|
ta.readOnly = true;
|
||||||
ta.value = code;
|
ta.value = code;
|
||||||
document.body.append(ta);
|
document.body.append(ta);
|
||||||
const selection = document.getSelection();
|
|
||||||
const selected = selection.rangeCount > 0 ? selection.getRangeAt(0) : false;
|
|
||||||
ta.select();
|
ta.select();
|
||||||
ta.setSelectionRange(0, code.length);
|
ta.setSelectionRange(0, code.length);
|
||||||
ta.readOnly = false;
|
ta.readOnly = false;
|
||||||
const result = document.execCommand('copy');
|
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
|
ta.blur(); // For iOS
|
||||||
target.blur();
|
target.blur();
|
||||||
if (selected) {
|
|
||||||
selection.removeAllRanges();
|
|
||||||
selection.addRange(selected);
|
|
||||||
}
|
|
||||||
document.body.removeChild(ta);
|
document.body.removeChild(ta);
|
||||||
});
|
});
|
||||||
element.addEventListener('mouseleave', () => {
|
element.addEventListener('mouseleave', () => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user