Created
December 29, 2017 09:23
-
-
Save lele85/c428cdf81dedc6b0a380c820ac36004c to your computer and use it in GitHub Desktop.
Clipboard
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
import BrowserWindow from "modules/common/lib/BrowserWindow"; | |
const Clipboard = { | |
copyToClipboard: (text) => { | |
const browseWin = BrowserWindow.get(); | |
const browserDoc = browseWin.document; | |
// IE specific | |
if (browseWin.clipboardData && browseWin.clipboardData.setData) { | |
browseWin.clipboardData.setData("Text", text); | |
return true; | |
} | |
// all other modern | |
const scrollY = browseWin.scrollY; | |
let target = browserDoc.createElement("textarea"); | |
target.style.position = "absolute"; | |
target.style.left = "-9999px"; | |
target.style.top = 0; | |
target.textContent = text; | |
browserDoc.body.appendChild(target); | |
target.focus(); | |
target.setSelectionRange(0, target.value.length); | |
browseWin.scrollTo(0, scrollY); | |
// copy the selection of fall back to prompt | |
try { | |
browserDoc.execCommand("copy"); | |
target.remove(); | |
return true; | |
} catch(e) { | |
target.remove(); | |
return false; | |
} | |
} | |
} | |
export default Clipboard; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment