mirror of
https://github.com/gcushen/hugo-academic.git
synced 2025-07-26 19:15:16 +02:00
feat: add legacy fallback to clipboard copy
This commit is contained in:
parent
fac5d24b4c
commit
c83227df43
1 changed files with 16 additions and 0 deletions
|
@ -316,10 +316,26 @@ document.querySelectorAll('pre > code').forEach((codeblock) => {
|
|||
}
|
||||
|
||||
copyBtn.addEventListener('click', () => {
|
||||
console.debug('Code block copy click. Is secure context for Clipboard API? ' + window.isSecureContext);
|
||||
if ('clipboard' in navigator) {
|
||||
// Note: Clipboard API requires HTTPS or localhost
|
||||
navigator.clipboard.writeText(codeblock.textContent);
|
||||
copiedNotification();
|
||||
return;
|
||||
} else {
|
||||
console.debug('Falling back to legacy clipboard copy');
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(codeblock);
|
||||
const selection = window.getSelection();
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
try {
|
||||
document.execCommand('copy');
|
||||
copiedNotification();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
selection.removeRange(range);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue