/* global ClipboardJS */ "use strict"; (function () { const CONTAINER_CLASS_NAME = "code-block-with-actions"; const ACTIONS_CONTAINER_CLASS_NAME = "code-block-actions"; const COPY_BUTTON_CLASS_NAME = "code-block-copy-button"; const COPY_BUTTON_COPIED_CLASS_NAME = `${COPY_BUTTON_CLASS_NAME}--copied`; const COPY_BUTTON_ICON_CLASS_NAME = `${COPY_BUTTON_CLASS_NAME}__icon`; const COPY_BUTTON_COPY_ICON_CLASS_NAME = `${COPY_BUTTON_ICON_CLASS_NAME}--copy`; const COPY_BUTTON_COPIED_ICON_CLASS_NAME = `${COPY_BUTTON_ICON_CLASS_NAME}--copied`; const ARIA_LABEL = "Copy code to clipboard"; const ARIA_LABEL_COPIED = "Copied"; function init(codeBlock) { const container = codeBlock.parentNode; container.classList.add(CONTAINER_CLASS_NAME); const actionsContainer = Object.assign(document.createElement("div"), { className: ACTIONS_CONTAINER_CLASS_NAME, }); const copyButton = Object.assign(document.createElement("button"), { className: COPY_BUTTON_CLASS_NAME, type: "button", innerHTML: `` + ``, }); copyButton.setAttribute("aria-label", ARIA_LABEL); new ClipboardJS(copyButton, { target: () => codeBlock }).on( "success", (event) => { event.clearSelection(); copyButton.classList.add(COPY_BUTTON_COPIED_CLASS_NAME); copyButton.setAttribute("aria-label", ARIA_LABEL_COPIED); copyButton.disabled = true; setTimeout(() => { copyButton.classList.remove(COPY_BUTTON_COPIED_CLASS_NAME); copyButton.setAttribute("aria-label", ARIA_LABEL); copyButton.disabled = false; }, 2000); } ); actionsContainer.appendChild(copyButton); container.appendChild(actionsContainer); } window.addEventListener("load", () => { for (const codeBlock of document.querySelectorAll("pre > code.hljs")) { init(codeBlock); } }); })();