fix: fix execption while creating the code copy button (#439)

This commit is contained in:
Robert Kaussow 2022-06-12 15:41:20 +02:00 committed by GitHub
parent 7195431d8c
commit 3ea85629a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,19 +1,21 @@
export function createCopyButton(highlightDiv) { export function createCopyButton(highlightDiv) {
const button = document.createElement("span") const button = document.createElement("span")
let selector = "pre > code"
let codeSelector = "pre > code"
if (highlightDiv.querySelector(".lntable")) { 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.classList.add("flex", "align-center", "justify-center", "clip", "gdoc-post__codecopy")
button.type = "button" button.type = "button"
button.innerHTML = button.innerHTML =
'<svg class="gdoc-icon copy"><use xlink:href="#gdoc_copy"></use></svg>' + '<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>' '<svg class="gdoc-icon check hidden"><use xlink:href="#gdoc_check"></use></svg>'
button.setAttribute("data-clipboard-text", codeToCopy) button.setAttribute("data-clipboard-text", codeContent)
button.setAttribute("data-copy-feedback", "Copied!") button.setAttribute("data-copy-feedback", "Copied!")
button.setAttribute("role", "button") button.setAttribute("role", "button")
button.setAttribute("aria-label", "Copy") button.setAttribute("aria-label", "Copy")
@ -21,3 +23,4 @@ export function createCopyButton(highlightDiv) {
highlightDiv.classList.add("gdoc-post__codecontainer") highlightDiv.classList.add("gdoc-post__codecontainer")
highlightDiv.insertBefore(button, highlightDiv.firstChild) highlightDiv.insertBefore(button, highlightDiv.firstChild)
} }
}