From a50e593cfdd264e9c6c33e929e1e302b3c0e23f2 Mon Sep 17 00:00:00 2001 From: morethanwords Date: Thu, 31 Dec 2020 18:12:31 +0400 Subject: [PATCH] Fix paste files --- src/helpers/dom.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/helpers/dom.ts b/src/helpers/dom.ts index 5352f40e..0b9fc232 100644 --- a/src/helpers/dom.ts +++ b/src/helpers/dom.ts @@ -609,28 +609,29 @@ export const getElementByPoint = (container: HTMLElement, verticalSide: 'top' | export async function getFilesFromEvent(e: ClipboardEvent | DragEvent, onlyTypes = false): Promise { const files: any[] = []; - const scanFiles = async(item: any) => { - if(item.isDirectory) { - const directoryReader = item.createReader(); + const scanFiles = async(entry: any, item: DataTransferItem) => { + if(entry.isDirectory) { + const directoryReader = entry.createReader(); await new Promise((resolve, reject) => { directoryReader.readEntries(async(entries: any) => { for(const entry of entries) { - await scanFiles(entry); + await scanFiles(entry, item); } resolve(); }); }); - } else if(item) { + } else if(entry) { if(onlyTypes) { - files.push(item.type); + files.push(entry.type); } else { - const file = item instanceof File ? - item : + const itemFile = item.getAsFile(); // * Safari can't handle entry.file with pasting + const file = entry instanceof File ? + entry : ( - item instanceof DataTransferItem ? - item.getAsFile() : - await new Promise((resolve, reject) => item.file(resolve, reject)) + entry instanceof DataTransferItem ? + entry.getAsFile() : + await new Promise((resolve, reject) => entry.file(resolve, (err: any) => resolve(itemFile))) ); /* if(!onlyTypes) { @@ -657,7 +658,7 @@ export async function getFilesFromEvent(e: ClipboardEvent | DragEvent, onlyTypes const item: DataTransferItem = items[i]; if(item.kind === 'file') { const entry = (onlyTypes ? item : item.webkitGetAsEntry()) || item.getAsFile(); - promises.push(scanFiles(entry)); + promises.push(scanFiles(entry, item)); } }