Skip to content

Commit

Permalink
Use Clipboard.writeText
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Nov 24, 2020
1 parent 29b8636 commit ab4fd95
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion _config.yml
Expand Up @@ -849,7 +849,7 @@ canvas_ribbon:
#! ==============================================================

# It's recommended to use the same version as in `_vendors.yml` to avoid potential problems.
# Remember to use the https protocol of CDN links when you enable https on your site.
# Remember to use the HTTPS protocol of CDN links when you enable HTTPS on your site.
vendors:
# The CDN provider of NexT internal scripts.
# Available values: local | jsdelivr | unpkg | cdnjs
Expand Down
38 changes: 23 additions & 15 deletions source/js/utils.js
Expand Up @@ -85,21 +85,29 @@ NexT.utils = {
button.addEventListener('click', () => {
const lines = element.querySelector('.code') || element.querySelector('code');
const code = lines.innerText;
const ta = document.createElement('textarea');
ta.style.top = window.scrollY + 'px'; // Prevent page scrolling
ta.style.position = 'absolute';
ta.style.opacity = '0';
ta.readOnly = true;
ta.value = code;
document.body.append(ta);
ta.select();
ta.setSelectionRange(0, code.length);
ta.readOnly = false;
const result = document.execCommand('copy');
button.querySelector('i').className = result ? 'fa fa-check-circle fa-fw' : 'fa fa-times-circle fa-fw';
ta.blur(); // For iOS
button.blur();
document.body.removeChild(ta);
if (navigator.clipboard) {
navigator.clipboard.writeText(code).then(() => {
button.querySelector('i').className = 'fa fa-check-circle fa-fw';
}, () => {
button.querySelector('i').className = 'fa fa-times-circle fa-fw';
});
} else {
const ta = document.createElement('textarea');
ta.style.top = window.scrollY + 'px'; // Prevent page scrolling
ta.style.position = 'absolute';
ta.style.opacity = '0';
ta.readOnly = true;
ta.value = code;
document.body.append(ta);
ta.select();
ta.setSelectionRange(0, code.length);
ta.readOnly = false;
const result = document.execCommand('copy');
button.querySelector('i').className = result ? 'fa fa-check-circle fa-fw' : 'fa fa-times-circle fa-fw';
ta.blur(); // For iOS
button.blur();
document.body.removeChild(ta);
}
});
element.addEventListener('mouseleave', () => {
setTimeout(() => {
Expand Down

0 comments on commit ab4fd95

Please sign in to comment.