Skip to content

Commit

Permalink
copy working
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaut committed Nov 11, 2021
1 parent 8b3e2c4 commit 424f427
Showing 1 changed file with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,42 @@ const SharingLink = ({ nonce, bucketEncryptionKey, refreshNonces }: Props) => {

const debouncedSwitchCopied = debounce(() => setCopied(false), 3000)


const onCopyInfo = useCallback(() => {
navigator.clipboard.writeText(link)
.then(() => {
setCopied(true)
debouncedSwitchCopied()
})
.catch(console.error)
// navigator.clipboard.writeText(link)
// .then(() => {
// setCopied(true)
// debouncedSwitchCopied()
// })
// .catch(console.error)

//Create a textbox field where we can insert text to.
const copyFrom = document.createElement("textarea")

//Set the text content to be the text you wished to copy.
copyFrom.textContent = link

//Append the textbox field into the body as a child.
//"execCommand()" only works when there exists selected text, and the text is inside
//document.body (meaning the text is part of a valid rendered HTML element).
document.body.appendChild(copyFrom)

//Select all the text!
copyFrom.select()

//Execute command
document.execCommand("copy")

//(Optional) De-select the text using blur().
copyFrom.blur()

//Remove the textbox field from the document.body, so no other JavaScript nor
//other elements can get access to this.
document.body.removeChild(copyFrom)

setCopied(true)
debouncedSwitchCopied()

}, [debouncedSwitchCopied, link])

const onDeleteNonce = useCallback(() => {
Expand Down

0 comments on commit 424f427

Please sign in to comment.