Skip to content

Commit

Permalink
Update copy logic (#855)
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Crawfis <[email protected]>
  • Loading branch information
AaronCrawfis authored Oct 16, 2023
1 parent 38dd7f3 commit 280bad6
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions docs/static/js/copy-code-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ highlightClass.forEach(element => {
copyIcon.addEventListener('click', async () => {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(element);
const codeElements = element.querySelectorAll('code');
const codeElement = codeElements.length > 1 ? codeElements[1] : codeElements[0];
range.selectNodeContents(codeElement);
selection.removeAllRanges();
selection.addRange(range);
const text = selection.toString();

try {
await navigator.clipboard.writeText(selection.toString());
await navigator.clipboard.writeText(text);
console.log('Text copied to clipboard');
copyIcon.classList.replace('fa-copy', 'fa-check');
selection.removeAllRanges();
Expand Down

0 comments on commit 280bad6

Please sign in to comment.