-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #500 from zsnmwy/code_copy
fix/dashboard: code copy
- Loading branch information
Showing
5 changed files
with
112 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export const copyToClipboard = (textToCopy: string) => { | ||
// navigator clipboard api needs a secure context (https) | ||
if (navigator.clipboard && window.isSecureContext) { | ||
// navigator clipboard api method' | ||
return navigator.clipboard.writeText(textToCopy); | ||
} | ||
// text area method | ||
const textArea = document.createElement('textarea'); | ||
textArea.value = textToCopy; | ||
// make the textarea out of viewport | ||
textArea.style.position = 'fixed'; | ||
textArea.style.left = '-999999px'; | ||
textArea.style.top = '-999999px'; | ||
document.body.appendChild(textArea); | ||
textArea.focus(); | ||
textArea.select(); | ||
return new Promise<void>((res, rej) => { | ||
// here the magic happens | ||
// eslint-disable-next-line no-unused-expressions | ||
document.execCommand('copy') ? res() : rej(); | ||
textArea.remove(); | ||
}); | ||
}; |