Skip to content

Commit

Permalink
refactor: simplify clipboard logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 18, 2024
1 parent 459d60b commit ce5f832
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/js/components/CopyToClipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export default class extends React.PureComponent {
}
}

copyToClipboardFallback = textToCopy => {
toClipboard = async textToCopy => {
if (navigator.clipboard) return navigator.clipboard.writeText(textToCopy)
const textArea = document.createElement('textarea')
textArea.value = textToCopy
textArea.style.visibility = 'hidden'
document.body.appendChild(textArea)
textArea.select()
document.execCommand('copy')
Expand All @@ -37,17 +39,7 @@ export default class extends React.PureComponent {
handleCopy = () => {
const { clickCallback, src, namespace } = this.props

const textToCopy = JSON.stringify(this.clipboardValue(src), null, ' ')

if (navigator.clipboard) {
navigator.clipboard.writeText(textToCopy).catch(() => {
// Fallback for non-secure contexts (i.e. http)
this.copyToClipboardFallback(textToCopy)
})
} else {
// Fallback for old browsers and test environments
this.copyToClipboardFallback(textToCopy)
}
this.toClipboard(JSON.stringify(this.clipboardValue(src), null, ' '))

this.copiedTimer = setTimeout(() => {
this.setState({
Expand Down Expand Up @@ -95,7 +87,7 @@ export default class extends React.PureComponent {
}

render () {
const { src, theme, hidden, rowHovered } = this.props
const { theme, hidden, rowHovered } = this.props
const style = Theme(theme, 'copy-to-clipboard').style
let display = 'inline'

Expand Down

0 comments on commit ce5f832

Please sign in to comment.