2022-01-18 17:56:02 +01:00
|
|
|
/*
|
|
|
|
This file has been taken from following blogpost with some modifications:
|
|
|
|
https://koki-nakamura22.github.io/blog/2019/10/03/hugo-adding-copy-button/
|
|
|
|
Many thanks to Koki Nakamura!
|
|
|
|
*/
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", function(event) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
if(!document.queryCommandSupported('copy')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-09 10:58:28 +02:00
|
|
|
let svgCopyCode = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-clipboard"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M9 5h-2a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-12a2 2 0 0 0 -2 -2h-2" /><path d="M9 3m0 2a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-2a2 2 0 0 1 -2 -2z" /></svg>';
|
|
|
|
let svgSuccessCode = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-clipboard-check"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M9 5h-2a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-12a2 2 0 0 0 -2 -2h-2" /><path d="M9 3m0 2a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-2a2 2 0 0 1 -2 -2z" /><path d="M9 14l2 2l4 -4" /></svg>';
|
|
|
|
let svgFailCode = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-clipboard-x"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M9 5h-2a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-12a2 2 0 0 0 -2 -2h-2" /><path d="M9 3m0 2a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-2a2 2 0 0 1 -2 -2z" /><path d="M10 12l4 4m0 -4l-4 4" /></svg>';
|
2022-01-18 17:56:02 +01:00
|
|
|
|
|
|
|
function changeIcon(el, innerHtml) {
|
|
|
|
el.innerHTML = innerHtml;
|
|
|
|
setTimeout(() => {
|
|
|
|
el.innerHTML = svgCopyCode;
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectText(node) {
|
|
|
|
let selection = window.getSelection();
|
|
|
|
let range = document.createRange();
|
|
|
|
if (node.childElementCount === 2) {
|
|
|
|
// Skip the title.
|
|
|
|
range.selectNodeContents(node.children[1]);
|
|
|
|
} else {
|
|
|
|
range.selectNodeContents(node);
|
|
|
|
}
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
return selection;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addCopyButton(containerEl) {
|
|
|
|
let copyBtn = document.createElement("button");
|
|
|
|
copyBtn.className = "highlight-copy-btn";
|
|
|
|
copyBtn.innerHTML = svgCopyCode;
|
|
|
|
|
|
|
|
let codeEl = containerEl.firstElementChild;
|
|
|
|
copyBtn.addEventListener('click', () => {
|
|
|
|
try {
|
|
|
|
let selection = selectText(codeEl);
|
|
|
|
document.execCommand('copy');
|
|
|
|
selection.removeAllRanges();
|
|
|
|
|
|
|
|
changeIcon(copyBtn, svgSuccessCode)
|
|
|
|
} catch(e) {
|
|
|
|
console && console.log(e);
|
|
|
|
changeIcon(copyBtn, svgFailCode)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
containerEl.appendChild(copyBtn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add copy button to code blocks
|
|
|
|
let highlightBlocks = document.getElementsByClassName('highlight');
|
|
|
|
Array.prototype.forEach.call(highlightBlocks, addCopyButton);
|
2024-04-09 14:03:09 +02:00
|
|
|
}, false);
|