Skip to content

Commit

Permalink
Only copy selectable content, closes #1496
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 21, 2022
1 parent 35903b8 commit b91cffc
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion assets/js/copy-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ function addCopyButtons () {
let timeout
button.addEventListener('click', () => {
timeout && clearTimeout(timeout)
navigator.clipboard.writeText(button.parentElement.querySelector('code').textContent)

let text =
Array.from(button.parentElement.querySelector('code').childNodes)
.filter(elem => !(elem.tagName === 'SPAN' && elem.classList.contains('unselectable')))
.map(elem => elem.textContent)
.join('')

navigator.clipboard.writeText(text)
button.classList.add('clicked')
timeout = setTimeout(() => button.classList.remove('clicked'), 3000)
})
Expand Down

0 comments on commit b91cffc

Please # to comment.