[d565449] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | var deselectCurrent = require("toggle-selection");
|
---|
| 4 |
|
---|
| 5 | var clipboardToIE11Formatting = {
|
---|
| 6 | "text/plain": "Text",
|
---|
| 7 | "text/html": "Url",
|
---|
| 8 | "default": "Text"
|
---|
| 9 | }
|
---|
| 10 |
|
---|
| 11 | var defaultMessage = "Copy to clipboard: #{key}, Enter";
|
---|
| 12 |
|
---|
| 13 | function format(message) {
|
---|
| 14 | var copyKey = (/mac os x/i.test(navigator.userAgent) ? "⌘" : "Ctrl") + "+C";
|
---|
| 15 | return message.replace(/#{\s*key\s*}/g, copyKey);
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | function copy(text, options) {
|
---|
| 19 | var debug,
|
---|
| 20 | message,
|
---|
| 21 | reselectPrevious,
|
---|
| 22 | range,
|
---|
| 23 | selection,
|
---|
| 24 | mark,
|
---|
| 25 | success = false;
|
---|
| 26 | if (!options) {
|
---|
| 27 | options = {};
|
---|
| 28 | }
|
---|
| 29 | debug = options.debug || false;
|
---|
| 30 | try {
|
---|
| 31 | reselectPrevious = deselectCurrent();
|
---|
| 32 |
|
---|
| 33 | range = document.createRange();
|
---|
| 34 | selection = document.getSelection();
|
---|
| 35 |
|
---|
| 36 | mark = document.createElement("span");
|
---|
| 37 | mark.textContent = text;
|
---|
| 38 | // avoid screen readers from reading out loud the text
|
---|
| 39 | mark.ariaHidden = "true"
|
---|
| 40 | // reset user styles for span element
|
---|
| 41 | mark.style.all = "unset";
|
---|
| 42 | // prevents scrolling to the end of the page
|
---|
| 43 | mark.style.position = "fixed";
|
---|
| 44 | mark.style.top = 0;
|
---|
| 45 | mark.style.clip = "rect(0, 0, 0, 0)";
|
---|
| 46 | // used to preserve spaces and line breaks
|
---|
| 47 | mark.style.whiteSpace = "pre";
|
---|
| 48 | // do not inherit user-select (it may be `none`)
|
---|
| 49 | mark.style.webkitUserSelect = "text";
|
---|
| 50 | mark.style.MozUserSelect = "text";
|
---|
| 51 | mark.style.msUserSelect = "text";
|
---|
| 52 | mark.style.userSelect = "text";
|
---|
| 53 | mark.addEventListener("copy", function(e) {
|
---|
| 54 | e.stopPropagation();
|
---|
| 55 | if (options.format) {
|
---|
| 56 | e.preventDefault();
|
---|
| 57 | if (typeof e.clipboardData === "undefined") { // IE 11
|
---|
| 58 | debug && console.warn("unable to use e.clipboardData");
|
---|
| 59 | debug && console.warn("trying IE specific stuff");
|
---|
| 60 | window.clipboardData.clearData();
|
---|
| 61 | var format = clipboardToIE11Formatting[options.format] || clipboardToIE11Formatting["default"]
|
---|
| 62 | window.clipboardData.setData(format, text);
|
---|
| 63 | } else { // all other browsers
|
---|
| 64 | e.clipboardData.clearData();
|
---|
| 65 | e.clipboardData.setData(options.format, text);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | if (options.onCopy) {
|
---|
| 69 | e.preventDefault();
|
---|
| 70 | options.onCopy(e.clipboardData);
|
---|
| 71 | }
|
---|
| 72 | });
|
---|
| 73 |
|
---|
| 74 | document.body.appendChild(mark);
|
---|
| 75 |
|
---|
| 76 | range.selectNodeContents(mark);
|
---|
| 77 | selection.addRange(range);
|
---|
| 78 |
|
---|
| 79 | var successful = document.execCommand("copy");
|
---|
| 80 | if (!successful) {
|
---|
| 81 | throw new Error("copy command was unsuccessful");
|
---|
| 82 | }
|
---|
| 83 | success = true;
|
---|
| 84 | } catch (err) {
|
---|
| 85 | debug && console.error("unable to copy using execCommand: ", err);
|
---|
| 86 | debug && console.warn("trying IE specific stuff");
|
---|
| 87 | try {
|
---|
| 88 | window.clipboardData.setData(options.format || "text", text);
|
---|
| 89 | options.onCopy && options.onCopy(window.clipboardData);
|
---|
| 90 | success = true;
|
---|
| 91 | } catch (err) {
|
---|
| 92 | debug && console.error("unable to copy using clipboardData: ", err);
|
---|
| 93 | debug && console.error("falling back to prompt");
|
---|
| 94 | message = format("message" in options ? options.message : defaultMessage);
|
---|
| 95 | window.prompt(message, text);
|
---|
| 96 | }
|
---|
| 97 | } finally {
|
---|
| 98 | if (selection) {
|
---|
| 99 | if (typeof selection.removeRange == "function") {
|
---|
| 100 | selection.removeRange(range);
|
---|
| 101 | } else {
|
---|
| 102 | selection.removeAllRanges();
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | if (mark) {
|
---|
| 107 | document.body.removeChild(mark);
|
---|
| 108 | }
|
---|
| 109 | reselectPrevious();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | return success;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | module.exports = copy;
|
---|