DataTransfer.js 5.0 KB

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