Browse Source
* authentication none introduced * Added Copy to Clipboard on double click of text This is to mimic the behaviour of select to copy on double click feature of Unix terminals * Logger added instead of console.log * CopyToClipboard seperated, yarn lint done, consistent return ignored to return any * Consistent return type introducedpull/162/head
SouraDutta
5 years ago
committed by
Cian Butler
4 changed files with 55 additions and 6 deletions
@ -0,0 +1,26 @@ |
|||||
|
// NOTE text selection on double click or select
|
||||
|
const copyToClipboard = (text: string) : boolean => { |
||||
|
if (window.clipboardData && window.clipboardData.setData) { |
||||
|
window.clipboardData.setData("Text", text); |
||||
|
return true; |
||||
|
} if (document.queryCommandSupported && document.queryCommandSupported("copy")) { |
||||
|
const textarea = document.createElement("textarea"); |
||||
|
textarea.textContent = text; |
||||
|
textarea.style.position = "fixed"; |
||||
|
document.body.appendChild(textarea); |
||||
|
textarea.select(); |
||||
|
try { |
||||
|
document.execCommand("copy"); |
||||
|
return true; |
||||
|
} catch (ex) { |
||||
|
console.warn("Copy to clipboard failed.", ex); |
||||
|
return false; |
||||
|
} finally { |
||||
|
document.body.removeChild(textarea); |
||||
|
} |
||||
|
} |
||||
|
console.warn("Copy to clipboard failed."); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
export default copyToClipboard; |
Loading…
Reference in new issue