Correct copy code block text

This commit is contained in:
Thomas Dehaeze 2020-11-26 19:57:07 +01:00
parent dd2a51ff03
commit a7c77da875

View File

@ -34,15 +34,24 @@ docReady(function() {
boxCopy.textContent = "Copy" boxCopy.textContent = "Copy"
boxCopy.onclick = function() { boxCopy.onclick = function() {
// Get text inside (remove 4 first char corresponding to "Copy") // Create a clone the node to not affect the original one
let srcText = this.parentNode.textContent.slice(4) let nodeClone = this.parentNode.cloneNode(true)
// Remove Line Numbers and Copy Button
let linenumNodes = nodeClone.querySelectorAll('.linenr,.src-copy')
for (var i = linenumNodes.length - 1 ; i >= 0 ; --i) {
nodeClone.removeChild(linenumNodes[i])
}
// Copy the filetered content to the clipboard
let srcText = nodeClone.textContent
navigator.clipboard.writeText(srcText).then(function() { navigator.clipboard.writeText(srcText).then(function() {
boxCopy.textContent = "Copied" boxCopy.textContent = "Copied"
boxCopy.classList.add("src-copied") boxCopy.classList.add("src-copied")
setTimeout(function() { setTimeout(function() {
boxCopy.textContent = "Copy" boxCopy.textContent = "Copy"
boxCopy.classList.remove("src-copied") boxCopy.classList.remove("src-copied")
},1000); }, 2000);
}); });
} }