DataTransfer.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. "use strict";
  2. /**
  3. * Copyright (c) 2013-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. * @typechecks
  9. */
  10. var PhotosMimeType = require("./PhotosMimeType");
  11. var createArrayFromMixed = require("./createArrayFromMixed");
  12. var emptyFunction = require("./emptyFunction");
  13. var CR_LF_REGEX = new RegExp("\r\n", 'g');
  14. var LF_ONLY = "\n";
  15. var RICH_TEXT_TYPES = {
  16. 'text/rtf': 1,
  17. 'text/html': 1
  18. };
  19. /**
  20. * If DataTransferItem is a file then return the Blob of data.
  21. *
  22. * @param {object} item
  23. * @return {?blob}
  24. */
  25. function getFileFromDataTransfer(item) {
  26. if (item.kind == 'file') {
  27. return item.getAsFile();
  28. }
  29. }
  30. var DataTransfer =
  31. /*#__PURE__*/
  32. function () {
  33. /**
  34. * @param {object} data
  35. */
  36. function DataTransfer(data) {
  37. this.data = data; // Types could be DOMStringList or array
  38. this.types = data.types ? createArrayFromMixed(data.types) : [];
  39. }
  40. /**
  41. * Is this likely to be a rich text data transfer?
  42. *
  43. * @return {boolean}
  44. */
  45. var _proto = DataTransfer.prototype;
  46. _proto.isRichText = function isRichText() {
  47. // If HTML is available, treat this data as rich text. This way, we avoid
  48. // using a pasted image if it is packaged with HTML -- this may occur with
  49. // pastes from MS Word, for example. However this is only rich text if
  50. // there's accompanying text.
  51. if (this.getHTML() && this.getText()) {
  52. return true;
  53. } // When an image is copied from a preview window, you end up with two
  54. // DataTransferItems one of which is a file's metadata as text. Skip those.
  55. if (this.isImage()) {
  56. return false;
  57. }
  58. return this.types.some(function (type) {
  59. return RICH_TEXT_TYPES[type];
  60. });
  61. };
  62. /**
  63. * Get raw text.
  64. *
  65. * @return {?string}
  66. */
  67. _proto.getText = function getText() {
  68. var text;
  69. if (this.data.getData) {
  70. if (!this.types.length) {
  71. text = this.data.getData('Text');
  72. } else if (this.types.indexOf('text/plain') != -1) {
  73. text = this.data.getData('text/plain');
  74. }
  75. }
  76. return text ? text.replace(CR_LF_REGEX, LF_ONLY) : null;
  77. };
  78. /**
  79. * Get HTML paste data
  80. *
  81. * @return {?string}
  82. */
  83. _proto.getHTML = function getHTML() {
  84. if (this.data.getData) {
  85. if (!this.types.length) {
  86. return this.data.getData('Text');
  87. } else if (this.types.indexOf('text/html') != -1) {
  88. return this.data.getData('text/html');
  89. }
  90. }
  91. };
  92. /**
  93. * Is this a link data transfer?
  94. *
  95. * @return {boolean}
  96. */
  97. _proto.isLink = function isLink() {
  98. return this.types.some(function (type) {
  99. return type.indexOf('Url') != -1 || type.indexOf('text/uri-list') != -1 || type.indexOf('text/x-moz-url');
  100. });
  101. };
  102. /**
  103. * Get a link url.
  104. *
  105. * @return {?string}
  106. */
  107. _proto.getLink = function getLink() {
  108. if (this.data.getData) {
  109. if (this.types.indexOf('text/x-moz-url') != -1) {
  110. var url = this.data.getData('text/x-moz-url').split('\n');
  111. return url[0];
  112. }
  113. return this.types.indexOf('text/uri-list') != -1 ? this.data.getData('text/uri-list') : this.data.getData('url');
  114. }
  115. return null;
  116. };
  117. /**
  118. * Is this an image data transfer?
  119. *
  120. * @return {boolean}
  121. */
  122. _proto.isImage = function isImage() {
  123. var isImage = this.types.some(function (type) {
  124. // Firefox will have a type of application/x-moz-file for images during
  125. // dragging
  126. return type.indexOf('application/x-moz-file') != -1;
  127. });
  128. if (isImage) {
  129. return true;
  130. }
  131. var items = this.getFiles();
  132. for (var i = 0; i < items.length; i++) {
  133. var type = items[i].type;
  134. if (!PhotosMimeType.isImage(type)) {
  135. return false;
  136. }
  137. }
  138. return true;
  139. };
  140. _proto.getCount = function getCount() {
  141. if (this.data.hasOwnProperty('items')) {
  142. return this.data.items.length;
  143. } else if (this.data.hasOwnProperty('mozItemCount')) {
  144. return this.data.mozItemCount;
  145. } else if (this.data.files) {
  146. return this.data.files.length;
  147. }
  148. return null;
  149. };
  150. /**
  151. * Get files.
  152. *
  153. * @return {array}
  154. */
  155. _proto.getFiles = function getFiles() {
  156. if (this.data.items) {
  157. // createArrayFromMixed doesn't properly handle DataTransferItemLists.
  158. return Array.prototype.slice.call(this.data.items).map(getFileFromDataTransfer).filter(emptyFunction.thatReturnsArgument);
  159. } else if (this.data.files) {
  160. return Array.prototype.slice.call(this.data.files);
  161. } else {
  162. return [];
  163. }
  164. };
  165. /**
  166. * Are there any files to fetch?
  167. *
  168. * @return {boolean}
  169. */
  170. _proto.hasFiles = function hasFiles() {
  171. return this.getFiles().length > 0;
  172. };
  173. return DataTransfer;
  174. }();
  175. module.exports = DataTransfer;