2024-01-15 21:10:43 +00:00
|
|
|
import Clipboard from "clipboard"
|
|
|
|
|
2024-08-28 07:31:33 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
2024-01-15 21:10:43 +00:00
|
|
|
let clipboard = new Clipboard(".clip")
|
|
|
|
|
|
|
|
clipboard.on("success", function (e) {
|
|
|
|
const trigger = e.trigger
|
|
|
|
|
|
|
|
if (trigger.hasAttribute("data-copy-feedback")) {
|
|
|
|
trigger.classList.add("gblog-post__codecopy--success", "gblog-post__codecopy--out")
|
|
|
|
trigger.querySelector(".gblog-icon.copy").classList.add("hidden")
|
|
|
|
trigger.querySelector(".gblog-icon.check").classList.remove("hidden")
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
trigger.classList.remove("gblog-post__codecopy--success", "gblog-post__codecopy--out")
|
|
|
|
trigger.querySelector(".gblog-icon.copy").classList.remove("hidden")
|
|
|
|
trigger.querySelector(".gblog-icon.check").classList.add("hidden")
|
|
|
|
}, 3000)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.clearSelection()
|
|
|
|
})
|
|
|
|
|
|
|
|
document.querySelectorAll(".highlight").forEach((highlightDiv) => createCopyButton(highlightDiv))
|
|
|
|
})
|
|
|
|
|
|
|
|
function createCopyButton(highlightDiv) {
|
2021-12-29 21:50:13 +00:00
|
|
|
const button = document.createElement("span")
|
2021-11-23 21:18:26 +00:00
|
|
|
|
2022-06-12 13:50:27 +00:00
|
|
|
let codeSelector = "pre > code"
|
2021-11-23 21:18:26 +00:00
|
|
|
if (highlightDiv.querySelector(".lntable")) {
|
2022-06-12 13:50:27 +00:00
|
|
|
codeSelector = ".lntable .lntd:last-child pre > code"
|
2021-11-23 21:18:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 13:50:27 +00:00
|
|
|
const codeContainer = highlightDiv.querySelector(codeSelector)
|
|
|
|
if (codeContainer !== null) {
|
|
|
|
const codeContent = codeContainer.innerText.trim()
|
2021-11-23 21:18:26 +00:00
|
|
|
|
2022-06-12 13:50:27 +00:00
|
|
|
button.classList.add("flex", "align-center", "justify-center", "clip", "gblog-post__codecopy")
|
|
|
|
button.type = "button"
|
|
|
|
button.innerHTML =
|
|
|
|
'<svg class="gblog-icon copy"><use xlink:href="#gblog_copy"></use></svg>' +
|
|
|
|
'<svg class="gblog-icon check hidden"><use xlink:href="#gblog_check"></use></svg>'
|
|
|
|
button.setAttribute("data-clipboard-text", codeContent)
|
|
|
|
button.setAttribute("data-copy-feedback", "Copied!")
|
|
|
|
button.setAttribute("role", "button")
|
|
|
|
button.setAttribute("aria-label", "Copy")
|
2021-11-23 21:18:26 +00:00
|
|
|
|
2022-06-12 13:50:27 +00:00
|
|
|
highlightDiv.classList.add("gblog-post__codecontainer")
|
|
|
|
highlightDiv.insertBefore(button, highlightDiv.firstChild)
|
|
|
|
}
|
2021-11-23 21:18:26 +00:00
|
|
|
}
|