file-selector
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="0.1.3"></a> | ||
## [0.1.3](https://github.com/rolandjitsu/file-selector/compare/v0.1.2...v0.1.3) (2018-10-05) | ||
### Bug Fixes | ||
* return DataTransferItem items if evt type is not 'drop' ([5935b32](https://github.com/rolandjitsu/file-selector/commit/5935b32)) | ||
<a name="0.1.2"></a> | ||
@@ -7,0 +17,0 @@ ## [0.1.2](https://github.com/rolandjitsu/file-selector/compare/v0.1.1...v0.1.2) (2018-10-05) |
@@ -142,3 +142,3 @@ (function (global, factory) { | ||
if (dt.items && dt.items.length) { | ||
return [2 /*return*/, getDataTransferFiles(dt)]; | ||
return [2 /*return*/, getDataTransferFiles(dt, evt.type)]; | ||
} | ||
@@ -159,3 +159,3 @@ else if (dt.files && dt.files.length) { | ||
} | ||
function getDataTransferFiles(dt) { | ||
function getDataTransferFiles(dt, type) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -166,3 +166,5 @@ var items, files; | ||
case 0: | ||
items = Array.from(dt.items); | ||
items = Array.from(dt.items) | ||
.filter(function (item) { return item.kind === 'file'; }); | ||
if (!(type === 'drop')) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, Promise.all(items.map(function (item) { return toFilePromises(item); }))]; | ||
@@ -173,2 +175,3 @@ case 1: | ||
.filter(function (file) { return !FILES_TO_IGNORE.includes(file.name); })]; | ||
case 2: return [2 /*return*/, items]; | ||
} | ||
@@ -181,6 +184,3 @@ }); | ||
if (typeof item.webkitGetAsEntry !== 'function') { | ||
if (item.kind === 'file') { | ||
return fromDataTransferItem(item); | ||
} | ||
return []; | ||
return fromDataTransferItem(item); | ||
} | ||
@@ -187,0 +187,0 @@ var entry = item.webkitGetAsEntry(); |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.fileSelector={})}(this,function(e){"use strict";function i(o,u,a,f){return new(a||(a=Promise))(function(e,t){function n(e){try{i(f.next(e))}catch(e){t(e)}}function r(e){try{i(f.throw(e))}catch(e){t(e)}}function i(t){t.done?e(t.value):new a(function(e){e(t.value)}).then(n,r)}i((f=f.apply(o,u||[])).next())})}function u(n,r){var i,o,u,e,a={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return e={next:t(0),throw:t(1),return:t(2)},"function"==typeof Symbol&&(e[Symbol.iterator]=function(){return this}),e;function t(t){return function(e){return function(t){if(i)throw new TypeError("Generator is already executing.");for(;a;)try{if(i=1,o&&(u=2&t[0]?o.return:t[0]?o.throw||((u=o.return)&&u.call(o),0):o.next)&&!(u=u.call(o,t[1])).done)return u;switch(o=0,u&&(t=[2&t[0],u.value]),t[0]){case 0:case 1:u=t;break;case 4:return a.label++,{value:t[1],done:!1};case 5:a.label++,o=t[1],t=[0];continue;case 7:t=a.ops.pop(),a.trys.pop();continue;default:if(!(u=0<(u=a.trys).length&&u[u.length-1])&&(6===t[0]||2===t[0])){a=0;continue}if(3===t[0]&&(!u||t[1]>u[0]&&t[1]<u[3])){a.label=t[1];break}if(6===t[0]&&a.label<u[1]){a.label=u[1],u=t;break}if(u&&a.label<u[2]){a.label=u[2],a.ops.push(t);break}u[2]&&a.ops.pop(),a.trys.pop();continue}t=r.call(n,a)}catch(e){t=[6,e],o=0}finally{i=u=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}([t,e])}}}function n(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,i,o=n.call(e),u=[];try{for(;(void 0===t||0<t--)&&!(r=o.next()).done;)u.push(r.value)}catch(e){i={error:e}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return u}function r(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(n(arguments[t]));return e}var o=new Map([["avi","video/avi"],["gif","image/gif"],["ico","image/x-icon"],["jpeg","image/jpeg"],["jpg","image/jpeg"],["mkv","video/x-matroska"],["mov","video/quicktime"],["mp4","video/mp4"],["pdf","application/pdf"],["png","image/png"],["zip","application/zip"]]);function a(e,t){var n=function(e){var t=e.name,n=t&&-1!==t.lastIndexOf(".");if(t&&n&&!e.type){var r=t.split(".").pop(),i=o.get(r);if(i)return f(e,i)}return f(e)}(e);return Object.defineProperty(n,"path",{value:"string"==typeof t?t:e.name,writable:!1,configurable:!1}),n}function f(e,t){var n=e.slice();return new File([n],e.name,{lastModified:e.lastModified,type:t||e.type})}var c=[".DS_Store","Thumbs.db"];function l(e){return Array.from(e).map(function(e){return a(e)})}function s(n){return i(this,void 0,void 0,function(){return u(this,function(e){return n.isDirectory?[2,(t=n,o=t.createReader(),new Promise(function(r){var i=[];!function n(){o.readEntries(function(e){if(e.length){var t=Promise.all(e.map(s));i.push(t),n()}else r(Promise.all(i))},p)}()}))]:[2,function(r){return i(this,void 0,void 0,function(){return u(this,function(e){return[2,new Promise(function(n){r.file(function(e){var t=a(e,r.fullPath);n(t)})})]})})}(n)];var t,o})})}function p(){}e.fromEvent=function(n){return i(this,void 0,void 0,function(){var t;return u(this,function(e){if(n.dataTransfer){if((t=n.dataTransfer).items&&t.items.length)return[2,function(n){return i(this,void 0,void 0,function(){var t;return u(this,function(e){switch(e.label){case 0:return t=Array.from(n.items),[4,Promise.all(t.map(function(e){return function(e){if("function"!=typeof e.webkitGetAsEntry)return"file"===e.kind?(t=a(e.getAsFile()),Promise.resolve(t)):[];var t,n=e.webkitGetAsEntry();return n?s(n):[]}(e)}))];case 1:return[2,function n(e){return e.reduce(function(e,t){return r(e,Array.isArray(t)?n(t):[t])},[])}(e.sent()).filter(function(e){return!c.includes(e.name)})]}})})}(t)];if(t.files&&t.files.length)return[2,l(t.files)]}else if(n.target instanceof HTMLInputElement&&n.target.files&&n.target.files)return[2,l(n.target.files)];return[2,[]]})})},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.fileSelector={})}(this,function(e){"use strict";function i(o,u,a,f){return new(a||(a=Promise))(function(e,t){function n(e){try{i(f.next(e))}catch(e){t(e)}}function r(e){try{i(f.throw(e))}catch(e){t(e)}}function i(t){t.done?e(t.value):new a(function(e){e(t.value)}).then(n,r)}i((f=f.apply(o,u||[])).next())})}function u(n,r){var i,o,u,e,a={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return e={next:t(0),throw:t(1),return:t(2)},"function"==typeof Symbol&&(e[Symbol.iterator]=function(){return this}),e;function t(t){return function(e){return function(t){if(i)throw new TypeError("Generator is already executing.");for(;a;)try{if(i=1,o&&(u=2&t[0]?o.return:t[0]?o.throw||((u=o.return)&&u.call(o),0):o.next)&&!(u=u.call(o,t[1])).done)return u;switch(o=0,u&&(t=[2&t[0],u.value]),t[0]){case 0:case 1:u=t;break;case 4:return a.label++,{value:t[1],done:!1};case 5:a.label++,o=t[1],t=[0];continue;case 7:t=a.ops.pop(),a.trys.pop();continue;default:if(!(u=0<(u=a.trys).length&&u[u.length-1])&&(6===t[0]||2===t[0])){a=0;continue}if(3===t[0]&&(!u||t[1]>u[0]&&t[1]<u[3])){a.label=t[1];break}if(6===t[0]&&a.label<u[1]){a.label=u[1],u=t;break}if(u&&a.label<u[2]){a.label=u[2],a.ops.push(t);break}u[2]&&a.ops.pop(),a.trys.pop();continue}t=r.call(n,a)}catch(e){t=[6,e],o=0}finally{i=u=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}([t,e])}}}function n(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,i,o=n.call(e),u=[];try{for(;(void 0===t||0<t--)&&!(r=o.next()).done;)u.push(r.value)}catch(e){i={error:e}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return u}function o(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(n(arguments[t]));return e}var a=new Map([["avi","video/avi"],["gif","image/gif"],["ico","image/x-icon"],["jpeg","image/jpeg"],["jpg","image/jpeg"],["mkv","video/x-matroska"],["mov","video/quicktime"],["mp4","video/mp4"],["pdf","application/pdf"],["png","image/png"],["zip","application/zip"]]);function f(e,t){var n=function(e){var t=e.name,n=t&&-1!==t.lastIndexOf(".");if(t&&n&&!e.type){var r=t.split(".").pop(),i=a.get(r);if(i)return c(e,i)}return c(e)}(e);return Object.defineProperty(n,"path",{value:"string"==typeof t?t:e.name,writable:!1,configurable:!1}),n}function c(e,t){var n=e.slice();return new File([n],e.name,{lastModified:e.lastModified,type:t||e.type})}var l=[".DS_Store","Thumbs.db"];function r(e){return Array.from(e).map(function(e){return f(e)})}function s(n){return i(this,void 0,void 0,function(){return u(this,function(e){return n.isDirectory?[2,(t=n,o=t.createReader(),new Promise(function(r){var i=[];!function n(){o.readEntries(function(e){if(e.length){var t=Promise.all(e.map(s));i.push(t),n()}else r(Promise.all(i))},p)}()}))]:[2,function(r){return i(this,void 0,void 0,function(){return u(this,function(e){return[2,new Promise(function(n){r.file(function(e){var t=f(e,r.fullPath);n(t)})})]})})}(n)];var t,o})})}function p(){}e.fromEvent=function(n){return i(this,void 0,void 0,function(){var t;return u(this,function(e){if(n.dataTransfer){if((t=n.dataTransfer).items&&t.items.length)return[2,function(n,r){return i(this,void 0,void 0,function(){var t;return u(this,function(e){switch(e.label){case 0:return t=Array.from(n.items).filter(function(e){return"file"===e.kind}),"drop"!==r?[3,2]:[4,Promise.all(t.map(function(e){return function(e){if("function"!=typeof e.webkitGetAsEntry)return t=f(e.getAsFile()),Promise.resolve(t);var t,n=e.webkitGetAsEntry();return n?s(n):[]}(e)}))];case 1:return[2,function n(e){return e.reduce(function(e,t){return o(e,Array.isArray(t)?n(t):[t])},[])}(e.sent()).filter(function(e){return!l.includes(e.name)})];case 2:return[2,t]}})})}(t,n.type)];if(t.files&&t.files.length)return[2,r(t.files)]}else if(n.target instanceof HTMLInputElement&&n.target.files&&n.target.files)return[2,r(n.target.files)];return[2,[]]})})},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=file-selector.umd.min.js.map |
@@ -18,3 +18,3 @@ import * as tslib_1 from "tslib"; | ||
if (dt.items && dt.items.length) { | ||
return getDataTransferFiles(dt); | ||
return getDataTransferFiles(dt, evt.type); | ||
} | ||
@@ -34,8 +34,15 @@ else if (dt.files && dt.files.length) { | ||
} | ||
function getDataTransferFiles(dt) { | ||
function getDataTransferFiles(dt, type) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const items = Array.from(dt.items); | ||
const files = yield Promise.all(items.map(item => toFilePromises(item))); | ||
return flatten(files) | ||
.filter(file => !FILES_TO_IGNORE.includes(file.name)); | ||
const items = Array.from(dt.items) | ||
.filter(item => item.kind === 'file'); | ||
// According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents, | ||
// only dragstart, and dragend (drop) has access to the data (source node), | ||
// hence return the DataTransferItem for other event types | ||
if (type === 'drop') { | ||
const files = yield Promise.all(items.map(item => toFilePromises(item))); | ||
return flatten(files) | ||
.filter(file => !FILES_TO_IGNORE.includes(file.name)); | ||
} | ||
return items; | ||
}); | ||
@@ -46,6 +53,3 @@ } | ||
if (typeof item.webkitGetAsEntry !== 'function') { | ||
if (item.kind === 'file') { | ||
return fromDataTransferItem(item); | ||
} | ||
return []; | ||
return fromDataTransferItem(item); | ||
} | ||
@@ -52,0 +56,0 @@ const entry = item.webkitGetAsEntry(); |
@@ -20,3 +20,3 @@ import * as tslib_1 from "tslib"; | ||
if (dt.items && dt.items.length) { | ||
return [2 /*return*/, getDataTransferFiles(dt)]; | ||
return [2 /*return*/, getDataTransferFiles(dt, evt.type)]; | ||
} | ||
@@ -37,3 +37,3 @@ else if (dt.files && dt.files.length) { | ||
} | ||
function getDataTransferFiles(dt) { | ||
function getDataTransferFiles(dt, type) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
@@ -44,3 +44,5 @@ var items, files; | ||
case 0: | ||
items = Array.from(dt.items); | ||
items = Array.from(dt.items) | ||
.filter(function (item) { return item.kind === 'file'; }); | ||
if (!(type === 'drop')) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, Promise.all(items.map(function (item) { return toFilePromises(item); }))]; | ||
@@ -51,2 +53,3 @@ case 1: | ||
.filter(function (file) { return !FILES_TO_IGNORE.includes(file.name); })]; | ||
case 2: return [2 /*return*/, items]; | ||
} | ||
@@ -59,6 +62,3 @@ }); | ||
if (typeof item.webkitGetAsEntry !== 'function') { | ||
if (item.kind === 'file') { | ||
return fromDataTransferItem(item); | ||
} | ||
return []; | ||
return fromDataTransferItem(item); | ||
} | ||
@@ -65,0 +65,0 @@ var entry = item.webkitGetAsEntry(); |
@@ -8,2 +8,2 @@ import { FileWithPath } from './file'; | ||
*/ | ||
export declare function fromEvent(evt: Event): Promise<FileWithPath[]>; | ||
export declare function fromEvent(evt: Event): Promise<Array<FileWithPath | DataTransferItem>>; |
@@ -76,3 +76,3 @@ "use strict"; | ||
if (dt.items && dt.items.length) { | ||
return [2 /*return*/, getDataTransferFiles(dt)]; | ||
return [2 /*return*/, getDataTransferFiles(dt, evt.type)]; | ||
} | ||
@@ -94,3 +94,3 @@ else if (dt.files && dt.files.length) { | ||
} | ||
function getDataTransferFiles(dt) { | ||
function getDataTransferFiles(dt, type) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -101,3 +101,5 @@ var items, files; | ||
case 0: | ||
items = Array.from(dt.items); | ||
items = Array.from(dt.items) | ||
.filter(function (item) { return item.kind === 'file'; }); | ||
if (!(type === 'drop')) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, Promise.all(items.map(function (item) { return toFilePromises(item); }))]; | ||
@@ -108,2 +110,3 @@ case 1: | ||
.filter(function (file) { return !FILES_TO_IGNORE.includes(file.name); })]; | ||
case 2: return [2 /*return*/, items]; | ||
} | ||
@@ -116,6 +119,3 @@ }); | ||
if (typeof item.webkitGetAsEntry !== 'function') { | ||
if (item.kind === 'file') { | ||
return fromDataTransferItem(item); | ||
} | ||
return []; | ||
return fromDataTransferItem(item); | ||
} | ||
@@ -122,0 +122,0 @@ var entry = item.webkitGetAsEntry(); |
{ | ||
"name": "file-selector", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Convert DataTransfer object to a list of File objects", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -16,7 +16,7 @@ import {FileWithPath, toFileWithPath} from './file'; | ||
*/ | ||
export async function fromEvent(evt: Event): Promise<FileWithPath[]> { | ||
export async function fromEvent(evt: Event): Promise<Array<FileWithPath | DataTransferItem>> { | ||
if (isDragEvt(evt)) { | ||
const dt = evt.dataTransfer!; | ||
if (dt.items && dt.items.length) { | ||
return getDataTransferFiles(dt); | ||
return getDataTransferFiles(dt, evt.type); | ||
} else if (dt.files && dt.files.length) { | ||
@@ -36,7 +36,14 @@ return fromFileList(dt.files); | ||
async function getDataTransferFiles(dt: DataTransfer) { | ||
const items = Array.from(dt.items); | ||
const files = await Promise.all(items.map(item => toFilePromises(item))); | ||
return flatten<FileWithPath>(files) | ||
.filter(file => !FILES_TO_IGNORE.includes(file.name)); | ||
async function getDataTransferFiles(dt: DataTransfer, type: string) { | ||
const items = Array.from(dt.items) | ||
.filter(item => item.kind === 'file'); | ||
// According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents, | ||
// only dragstart, and dragend (drop) has access to the data (source node), | ||
// hence return the DataTransferItem for other event types | ||
if (type === 'drop') { | ||
const files = await Promise.all(items.map(item => toFilePromises(item))); | ||
return flatten<FileWithPath>(files) | ||
.filter(file => !FILES_TO_IGNORE.includes(file.name)); | ||
} | ||
return items; | ||
} | ||
@@ -47,6 +54,3 @@ | ||
if (typeof item.webkitGetAsEntry !== 'function') { | ||
if (item.kind === 'file') { | ||
return fromDataTransferItem(item); | ||
} | ||
return []; | ||
return fromDataTransferItem(item); | ||
} | ||
@@ -53,0 +57,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
110376
1010