2021-12-29 21:50:13 +00:00
|
|
|
export function createCopyButton(highlightDiv) {
|
|
|
|
const button = document.createElement("span")
|
|
|
|
let selector = "pre > code"
|
2021-11-23 21:18:26 +00:00
|
|
|
|
|
|
|
if (highlightDiv.querySelector(".lntable")) {
|
2021-12-29 21:50:13 +00:00
|
|
|
selector = ".lntable .lntd:last-child pre > code"
|
2021-11-23 21:18:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 21:50:13 +00:00
|
|
|
const codeToCopy = highlightDiv.querySelector(selector).innerText.trim()
|
2021-11-23 21:18:26 +00:00
|
|
|
|
2021-12-29 21:50:13 +00:00
|
|
|
button.classList.add("flex", "align-center", "justify-center", "clip", "gblog-post__codecopy")
|
|
|
|
button.type = "button"
|
2021-11-23 21:18:26 +00:00
|
|
|
button.innerHTML =
|
|
|
|
'<svg class="icon copy"><use xlink:href="#gblog_copy"></use></svg>' +
|
2021-12-29 21:50:13 +00:00
|
|
|
'<svg class="icon check hidden"><use xlink:href="#gblog_check"></use></svg>'
|
|
|
|
button.setAttribute("data-clipboard-text", codeToCopy)
|
|
|
|
button.setAttribute("data-copy-feedback", "Copied!")
|
|
|
|
button.setAttribute("role", "button")
|
|
|
|
button.setAttribute("aria-label", "Copy")
|
2021-11-23 21:18:26 +00:00
|
|
|
|
2021-12-29 21:50:13 +00:00
|
|
|
highlightDiv.classList.add("gblog-post__codecontainer")
|
|
|
|
highlightDiv.insertBefore(button, highlightDiv.firstChild)
|
2021-11-23 21:18:26 +00:00
|
|
|
}
|