Sfoglia il codice sorgente

Use toArray instead of Object.keys

Object.keys added rubbish keys like “length” to the resulting file array
Artur Paikin 9 anni fa
parent
commit
29cca3daa5
3 ha cambiato i file con 21 aggiunte e 9 eliminazioni
  1. 8 0
      src/core/Utils.js
  2. 6 5
      src/plugins/DragDrop.js
  3. 7 4
      src/plugins/Formtag.js

+ 8 - 0
src/core/Utils.js

@@ -85,6 +85,13 @@ function every (array, predicateFn) {
   }, true)
   }, true)
 }
 }
 
 
+/**
+ * Converts list into array
+*/
+function toArray (list) {
+  return Array.prototype.slice.call(list || [], 0)
+}
+
 /**
 /**
  * Takes a fileName and turns it into fileID, by converting to lowercase,
  * Takes a fileName and turns it into fileID, by converting to lowercase,
  * removing extra characters and adding unix timestamp
  * removing extra characters and adding unix timestamp
@@ -122,6 +129,7 @@ export default {
   generateFileID,
   generateFileID,
   getFnName,
   getFnName,
   // addListenerMulti,
   // addListenerMulti,
+  toArray,
   every,
   every,
   flatten,
   flatten,
   groupBy,
   groupBy,

+ 6 - 5
src/plugins/DragDrop.js

@@ -1,4 +1,5 @@
 import Plugin from './Plugin'
 import Plugin from './Plugin'
+import Utils from '../core/Utils'
 import dragDrop from 'drag-drop'
 import dragDrop from 'drag-drop'
 import yo from 'yo-yo'
 import yo from 'yo-yo'
 
 
@@ -79,15 +80,15 @@ export default class DragDrop extends Plugin {
 
 
   handleInputChange (ev) {
   handleInputChange (ev) {
     this.core.log('All right, something selected through input...')
     this.core.log('All right, something selected through input...')
-    const files = ev.target.files
+    // const files = ev.target.files
 
 
-    const newFiles = Object.keys(files).map((file) => {
-      return files[file]
-    })
+    // const newFiles = Object.keys(files).map((file) => {
+    //   return files[file]
+    // })
 
 
     this.core.emitter.emit('file-add', {
     this.core.emitter.emit('file-add', {
       plugin: this,
       plugin: this,
-      acquiredFiles: newFiles
+      acquiredFiles: Utils.toArray(ev.target.files)
     })
     })
   }
   }
 
 

+ 7 - 4
src/plugins/Formtag.js

@@ -1,4 +1,5 @@
 import Plugin from './Plugin'
 import Plugin from './Plugin'
+import Utils from '../core/Utils'
 import yo from 'yo-yo'
 import yo from 'yo-yo'
 
 
 export default class Formtag extends Plugin {
 export default class Formtag extends Plugin {
@@ -22,13 +23,15 @@ export default class Formtag extends Plugin {
   handleInputChange (ev) {
   handleInputChange (ev) {
     this.core.log('All right, something selected through input...')
     this.core.log('All right, something selected through input...')
 
 
-    const files = Object.keys(ev.target.files).map((key) => {
-      return ev.target.files[key]
-    })
+    // this added rubbish keys like “length” to the resulting array
+    //
+    // const files = Object.keys(ev.target.files).map((key) => {
+    //   return ev.target.files[key]
+    // })
 
 
     this.core.emitter.emit('file-add', {
     this.core.emitter.emit('file-add', {
       plugin: this,
       plugin: this,
-      acquiredFiles: files
+      acquiredFiles: Utils.toArray(ev.target.files)
     })
     })
   }
   }