-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from btholt/copy-code-functionality
feature: copy code
- Loading branch information
Showing
3 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
function createDOMElements() { | ||
const container = document.createElement("div"); | ||
container.innerHTML = | ||
"<div class='tooltip-copy'><input type='submit' value='Copy' /></div>"; | ||
container.className = "div-copy"; | ||
return container; | ||
} | ||
|
||
function attachCopyCodeFunctionality(div) { | ||
const elementsToClean = []; | ||
document | ||
.querySelectorAll("pre") | ||
.forEach(function createButtonAndAttachHandlers(pre) { | ||
let timeout = null; | ||
const copy = div.cloneNode(true); | ||
pre.appendChild(copy); | ||
elementsToClean.push(pre); | ||
|
||
copy.onclick = function copyTextToClipboard() { | ||
navigator.clipboard.writeText(pre.textContent); | ||
copy.classList.add("clicked"); | ||
clearTimeout(timeout); | ||
timeout = setTimeout(function hidePopup() { | ||
copy.classList.remove("clicked"); | ||
}, 1500); | ||
}; | ||
}); | ||
|
||
return elementsToClean; | ||
} | ||
|
||
export default function createCopyCodeFunctionality() { | ||
const container = createDOMElements(); | ||
return attachCopyCodeFunctionality(container); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters