123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- var PhotosMimeType = require('./PhotosMimeType');
- var createArrayFromMixed = require('./createArrayFromMixed');
- var emptyFunction = require('./emptyFunction');
- var CR_LF_REGEX = new RegExp('\u000D\u000A', 'g');
- var LF_ONLY = '\u000A';
- var RICH_TEXT_TYPES = {
- 'text/rtf': 1,
- 'text/html': 1
- };
- function getFileFromDataTransfer(item) {
- if (item.kind == 'file') {
- return item.getAsFile();
- }
- }
- class DataTransfer {
-
- constructor(data) {
- this.data = data;
-
- this.types = data.types ? createArrayFromMixed(data.types) : [];
- }
-
- isRichText() {
-
-
-
-
- if (this.getHTML() && this.getText()) {
- return true;
- }
-
-
- if (this.isImage()) {
- return false;
- }
- return this.types.some(type => RICH_TEXT_TYPES[type]);
- }
-
- getText() {
- var text;
- if (this.data.getData) {
- if (!this.types.length) {
- text = this.data.getData('Text');
- } else if (this.types.indexOf('text/plain') != -1) {
- text = this.data.getData('text/plain');
- }
- }
- return text ? text.replace(CR_LF_REGEX, LF_ONLY) : null;
- }
-
- getHTML() {
- if (this.data.getData) {
- if (!this.types.length) {
- return this.data.getData('Text');
- } else if (this.types.indexOf('text/html') != -1) {
- return this.data.getData('text/html');
- }
- }
- }
-
- isLink() {
- return this.types.some(type => {
- return type.indexOf('Url') != -1 || type.indexOf('text/uri-list') != -1 || type.indexOf('text/x-moz-url');
- });
- }
-
- getLink() {
- if (this.data.getData) {
- if (this.types.indexOf('text/x-moz-url') != -1) {
- let url = this.data.getData('text/x-moz-url').split('\n');
- return url[0];
- }
- return this.types.indexOf('text/uri-list') != -1 ? this.data.getData('text/uri-list') : this.data.getData('url');
- }
- return null;
- }
-
- isImage() {
- var isImage = this.types.some(type => {
-
-
- return type.indexOf('application/x-moz-file') != -1;
- });
- if (isImage) {
- return true;
- }
- var items = this.getFiles();
- for (var i = 0; i < items.length; i++) {
- var type = items[i].type;
- if (!PhotosMimeType.isImage(type)) {
- return false;
- }
- }
- return true;
- }
- getCount() {
- if (this.data.hasOwnProperty('items')) {
- return this.data.items.length;
- } else if (this.data.hasOwnProperty('mozItemCount')) {
- return this.data.mozItemCount;
- } else if (this.data.files) {
- return this.data.files.length;
- }
- return null;
- }
-
- getFiles() {
- if (this.data.items) {
-
- return Array.prototype.slice.call(this.data.items).map(getFileFromDataTransfer).filter(emptyFunction.thatReturnsArgument);
- } else if (this.data.files) {
- return Array.prototype.slice.call(this.data.files);
- } else {
- return [];
- }
- }
-
- hasFiles() {
- return this.getFiles().length > 0;
- }
- }
- module.exports = DataTransfer;
|