feat: copy to clipboard (#18963)
* feat: copy to clipboard * fix: CI failure related issue * fix: CI failure issue * fix: copy to clipboard button bug * feat: copy pragmadots value to clipboard
This commit is contained in:
parent
5e2b9341f3
commit
d634587423
3 changed files with 118 additions and 2 deletions
|
|
@ -341,3 +341,59 @@ proc search*() {.exportc.} =
|
|||
|
||||
if timer != nil: clearTimeout(timer)
|
||||
timer = setTimeout(wrapper, 400)
|
||||
|
||||
proc copyToClipboard*() {.exportc.} =
|
||||
{.emit: """
|
||||
|
||||
function updatePreTags() {
|
||||
|
||||
const allPreTags = document.querySelectorAll("pre")
|
||||
|
||||
allPreTags.forEach((e) => {
|
||||
|
||||
const div = document.createElement("div")
|
||||
div.classList.add("copyToClipBoard")
|
||||
|
||||
const preTag = document.createElement("pre")
|
||||
preTag.innerHTML = e.innerHTML
|
||||
|
||||
const button = document.createElement("button")
|
||||
button.value = e.textContent.replace('...', '')
|
||||
button.classList.add("copyToClipBoardBtn")
|
||||
|
||||
div.appendChild(preTag)
|
||||
div.appendChild(button)
|
||||
|
||||
e.outerHTML = div.outerHTML
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
function copyTextToClipboard(e) {
|
||||
const clipBoardContent = e.target.value
|
||||
navigator.clipboard.writeText(clipBoardContent).then(function() {
|
||||
e.target.style.setProperty("--clipboard-image", "var(--clipboard-image-selected)")
|
||||
}, function(err) {
|
||||
console.error("Could not copy text: ", err);
|
||||
});
|
||||
}
|
||||
|
||||
window.addEventListener("click", (e) => {
|
||||
if (e.target.classList.contains("copyToClipBoardBtn")) {
|
||||
copyTextToClipboard(e)
|
||||
}
|
||||
})
|
||||
|
||||
window.addEventListener("mouseover", (e) => {
|
||||
if (e.target.nodeName === "PRE") {
|
||||
e.target.nextElementSibling.style.setProperty("--clipboard-image", "var(--clipboard-image-normal)")
|
||||
}
|
||||
})
|
||||
|
||||
window.addEventListener("DOMContentLoaded", updatePreTags)
|
||||
|
||||
"""
|
||||
.}
|
||||
|
||||
copyToClipboard()
|
||||
Loading…
Add table
Add a link
Reference in a new issue