Use Clipboard.writeText

This commit is contained in:
Mimi 2020-11-24 21:27:23 +08:00
parent 29b86361b7
commit ab4fd95ab5
2 changed files with 24 additions and 16 deletions

View File

@ -849,7 +849,7 @@ canvas_ribbon:
#! ============================================================== #! ==============================================================
# It's recommended to use the same version as in `_vendors.yml` to avoid potential problems. # 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: vendors:
# The CDN provider of NexT internal scripts. # The CDN provider of NexT internal scripts.
# Available values: local | jsdelivr | unpkg | cdnjs # Available values: local | jsdelivr | unpkg | cdnjs

View File

@ -85,21 +85,29 @@ NexT.utils = {
button.addEventListener('click', () => { button.addEventListener('click', () => {
const lines = element.querySelector('.code') || element.querySelector('code'); const lines = element.querySelector('.code') || element.querySelector('code');
const code = lines.innerText; const code = lines.innerText;
const ta = document.createElement('textarea'); if (navigator.clipboard) {
ta.style.top = window.scrollY + 'px'; // Prevent page scrolling navigator.clipboard.writeText(code).then(() => {
ta.style.position = 'absolute'; button.querySelector('i').className = 'fa fa-check-circle fa-fw';
ta.style.opacity = '0'; }, () => {
ta.readOnly = true; button.querySelector('i').className = 'fa fa-times-circle fa-fw';
ta.value = code; });
document.body.append(ta); } else {
ta.select(); const ta = document.createElement('textarea');
ta.setSelectionRange(0, code.length); ta.style.top = window.scrollY + 'px'; // Prevent page scrolling
ta.readOnly = false; ta.style.position = 'absolute';
const result = document.execCommand('copy'); ta.style.opacity = '0';
button.querySelector('i').className = result ? 'fa fa-check-circle fa-fw' : 'fa fa-times-circle fa-fw'; ta.readOnly = true;
ta.blur(); // For iOS ta.value = code;
button.blur(); document.body.append(ta);
document.body.removeChild(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', () => { element.addEventListener('mouseleave', () => {
setTimeout(() => { setTimeout(() => {