import './github-clipboard-copy-element.js';
http://localhost:4000/assets/js/github_clipboard_copy_element
String.prototype.format = function () {
var args = arguments;
return this.replace(/{([0-9]+)}/g, function (match, index) {
return typeof args[index] == 'undefined' ? match : args[index];
});
};
function fromHTML(html, trim = true) {
// Process the HTML string.
html = trim ? html.trim() : html;
if (!html) return null;
// Then set up a new template element.
const template = document.createElement('template');
template.innerHTML = html;
const result = template.content.children;
// Then return either an HTMLElement or HTMLCollection,
// based on whether the input HTML had one or more roots.
if (result.length === 1) return result[0];
return result;
}
function makeCopyButtons() {
const clipboard = `
`
const blocks = document.querySelectorAll('div.highlighter-rouge>div.highlight');
blocks.forEach((block) => {
// Fix quotes
const sanitized = block.textContent.replaceAll('"', '"')
// Put this blocks content into the clipboard string
const block_clipboard = clipboard.format(sanitized);
// Make an html element from the block clipboard string
const block_clipboard_node = fromHTML(block_clipboard);
// Add it in to the end of the html code block
block.appendChild(block_clipboard_node);
});
}
makeCopyButtons()
const CLIPBOARD_COPY_TIMER_DURATION = 2000;
function showSVG(svg) {
svg.classList.remove('d-none');
}
function hideSVG(svg) {
svg.classList.add('d-none');
}
function activateTooltip(button) {
button.classList.add("tooltipped");
button.classList.add("tooltipped-w");
button.setAttribute("aria-label", "Copied!");
}
function deactivateTooltip(button) {
button.classList.remove("tooltipped");
button.classList.remove("tooltipped-w");
button.setAttribute("aria-label", "Copy");
}
// Toggle a copy button.
function showCopy(button) {
const [copyIcon, checkIcon] = button.querySelectorAll('.octicon');
if (!copyIcon || !checkIcon)
return;
showSVG(copyIcon);
hideSVG(checkIcon);
}
// Toggle a copy button.
function showCheck(button) {
const [copyIcon, checkIcon] = button.querySelectorAll('.octicon');
if (!copyIcon || !checkIcon)
return;
hideSVG(copyIcon);
showSVG(checkIcon);
}
const clipboardCopyElementTimers = new WeakMap();
document.addEventListener('clipboard-copy', ({ target }) => {
if (!(target instanceof HTMLElement))
return;
const currentTimeout = clipboardCopyElementTimers.get(target);
if (currentTimeout) {
clearTimeout(currentTimeout);
clipboardCopyElementTimers.delete(target);
}
else {
showCheck(target);
activateTooltip(target);
}
clipboardCopyElementTimers.set(target, setTimeout(() => {
showCopy(target);
deactivateTooltip(target);
clipboardCopyElementTimers.delete(target);
}, CLIPBOARD_COPY_TIMER_DURATION));
});