From 280bad69d48f1b04f89ef44924bd368a7eb9c2b5 Mon Sep 17 00:00:00 2001 From: Aaron Crawfis Date: Mon, 16 Oct 2023 14:57:43 -0700 Subject: [PATCH] Update copy logic (#855) Signed-off-by: Aaron Crawfis --- docs/static/js/copy-code-button.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/static/js/copy-code-button.js b/docs/static/js/copy-code-button.js index ca5d0e26e..eb42c3d80 100644 --- a/docs/static/js/copy-code-button.js +++ b/docs/static/js/copy-code-button.js @@ -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();