index.js 3.3 KB

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