DataTransfer.js.flow 4.4 KB

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