@@ -2,6 +2,7 @@ import {FileWithPath, toFileWithPath} from './file';
22
33
44const FILES_TO_IGNORE = [
5+ // Thumbnail cache files for macOS and Windows
56 '.DS_Store' , // macOs
67 'Thumbs.db' // Windows
78] ;
@@ -26,7 +27,7 @@ function isDragEvt(value: any): value is DragEvent {
2627function getInputFiles ( evt : Event ) {
2728 const files = isInput ( evt . target )
2829 ? evt . target . files
29- ? Array . from ( evt . target . files )
30+ ? fromList < File > ( evt . target . files )
3031 : [ ]
3132 : [ ] ;
3233 return files . map ( file => toFileWithPath ( file ) ) ;
@@ -37,17 +38,42 @@ function isInput(value: EventTarget | null): value is HTMLInputElement {
3738}
3839
3940async 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 ) ;
4974 }
50- return items ;
75+
76+ return files as any ;
5177}
5278
5379// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem
0 commit comments