Skip to content

Instantly share code, notes, and snippets.

@lele85
Created December 29, 2017 09:23
Show Gist options
  • Save lele85/c428cdf81dedc6b0a380c820ac36004c to your computer and use it in GitHub Desktop.
Save lele85/c428cdf81dedc6b0a380c820ac36004c to your computer and use it in GitHub Desktop.
Clipboard
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