/*
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;
}
let svgCopyCode = '';
let svgSuccessCode = '';
let svgFailCode = '';
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);
}, false);