Skip to content

Commit

Permalink
fix: fix execption while creating the code copy button (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Jun 12, 2022
1 parent 7195431 commit 3ea8562
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/js/copycode.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
export function createCopyButton(highlightDiv) {
const button = document.createElement("span")
let selector = "pre > code"

let codeSelector = "pre > code"
if (highlightDiv.querySelector(".lntable")) {
selector = ".lntable .lntd:last-child pre > code"
codeSelector = ".lntable .lntd:last-child pre > code"
}

const codeToCopy = highlightDiv.querySelector(selector).innerText.trim()
const codeContainer = highlightDiv.querySelector(codeSelector)
if (codeContainer !== null) {
const codeContent = codeContainer.innerText.trim()

button.classList.add("flex", "align-center", "justify-center", "clip", "gdoc-post__codecopy")
button.type = "button"
button.innerHTML =
'<svg class="gdoc-icon copy"><use xlink:href="#gdoc_copy"></use></svg>' +
'<svg class="gdoc-icon check hidden"><use xlink:href="#gdoc_check"></use></svg>'
button.setAttribute("data-clipboard-text", codeToCopy)
button.setAttribute("data-copy-feedback", "Copied!")
button.setAttribute("role", "button")
button.setAttribute("aria-label", "Copy")
button.classList.add("flex", "align-center", "justify-center", "clip", "gdoc-post__codecopy")
button.type = "button"
button.innerHTML =
'<svg class="gdoc-icon copy"><use xlink:href="#gdoc_copy"></use></svg>' +
'<svg class="gdoc-icon check hidden"><use xlink:href="#gdoc_check"></use></svg>'
button.setAttribute("data-clipboard-text", codeContent)
button.setAttribute("data-copy-feedback", "Copied!")
button.setAttribute("role", "button")
button.setAttribute("aria-label", "Copy")

highlightDiv.classList.add("gdoc-post__codecontainer")
highlightDiv.insertBefore(button, highlightDiv.firstChild)
highlightDiv.classList.add("gdoc-post__codecontainer")
highlightDiv.insertBefore(button, highlightDiv.firstChild)
}
}

0 comments on commit 3ea8562

Please sign in to comment.