DataTransfer.js.flow 4.4 KB

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