Skip to content

Commit

Permalink
fix: remove trailling newline when copy text (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobDu authored May 7, 2024
1 parent 4246fce commit aaae763
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/utils/copy.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
document.addEventListener('copy', (e) => {
const selectedText = window.getSelection()?.toString() ?? ''
const cleanedText = selectedText?.replace(/\n+$/, '\n')
e.clipboardData?.setData('text/plain', cleanedText)

e.preventDefault()
})

export function copyToClip(text: string) {
return new Promise((resolve, reject) => {
try {
const input: HTMLTextAreaElement = document.createElement('textarea')
input.setAttribute('readonly', 'readonly')
input.value = text
document.body.appendChild(input)
input.select()
if (document.execCommand('copy'))
document.execCommand('copy')
document.body.removeChild(input)
resolve(text)
}
catch (error) {
reject(error)
}
})
return navigator.clipboard.writeText(text)
}

0 comments on commit aaae763

Please sign in to comment.