@@ -2,6 +2,7 @@ import {FileWithPath, toFileWithPath} from './file';
2
2
3
3
4
4
const FILES_TO_IGNORE = [
5
+ // Thumbnail cache files for macOS and Windows
5
6
'.DS_Store' , // macOs
6
7
'Thumbs.db' // Windows
7
8
] ;
@@ -26,7 +27,7 @@ function isDragEvt(value: any): value is DragEvent {
26
27
function getInputFiles ( evt : Event ) {
27
28
const files = isInput ( evt . target )
28
29
? evt . target . files
29
- ? Array . from ( evt . target . files )
30
+ ? fromList < File > ( evt . target . files )
30
31
: [ ]
31
32
: [ ] ;
32
33
return files . map ( file => toFileWithPath ( file ) ) ;
@@ -37,17 +38,42 @@ function isInput(value: EventTarget | null): value is HTMLInputElement {
37
38
}
38
39
39
40
async function getDataTransferFiles ( dt : DataTransfer , type : string ) {
40
- const items = Array . from ( dt . items )
41
- . filter ( item => item . kind === 'file' ) ;
42
- // According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents,
43
- // only 'dragstart' and 'drop' has access to the data (source node),
44
- // hence return the DataTransferItem for other event types
45
- if ( type === 'drop' ) {
46
- const files = await Promise . all ( items . map ( item => toFilePromises ( item ) ) ) ;
47
- return flatten < FileWithPath > ( files )
48
- . filter ( file => ! FILES_TO_IGNORE . includes ( file . name ) ) ;
41
+ // IE11 does not support dataTransfer.items
42
+ // See https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/items#Browser_compatibility
43
+ if ( dt . items ) {
44
+ const items = fromList < DataTransferItem > ( dt . items )
45
+ . filter ( item => item . kind === 'file' ) ;
46
+ // According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents,
47
+ // only 'dragstart' and 'drop' has access to the data (source node)
48
+ if ( type !== 'drop' ) {
49
+ return items ;
50
+ }
51
+ const files = await Promise . all ( items . map ( toFilePromises ) ) ;
52
+ return noIgnoredFiles ( flatten < FileWithPath > ( files ) ) ;
53
+ }
54
+
55
+ return noIgnoredFiles ( fromList < File > ( dt . files )
56
+ . map ( file => toFileWithPath ( file ) ) ) ;
57
+ }
58
+
59
+ function noIgnoredFiles ( files : FileWithPath [ ] ) {
60
+ return files . filter ( file => FILES_TO_IGNORE . indexOf ( file . name ) === - 1 ) ;
61
+ }
62
+
63
+ // IE11 does not support Array.from()
64
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Browser_compatibility
65
+ // https://developer.mozilla.org/en-US/docs/Web/API/FileList
66
+ // https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItemList
67
+ function fromList < T > ( items : DataTransferItemList | FileList ) : T [ ] {
68
+ const files = [ ] ;
69
+
70
+ // tslint:disable: prefer-for-of
71
+ for ( let i = 0 ; i < items . length ; i ++ ) {
72
+ const file = items [ i ] ;
73
+ files . push ( file ) ;
49
74
}
50
- return items ;
75
+
76
+ return files as any ;
51
77
}
52
78
53
79
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem
0 commit comments