Browse Source

Fix paste files

master
morethanwords 4 years ago
parent
commit
a50e593cfd
  1. 25
      src/helpers/dom.ts

25
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<any[]> { export async function getFilesFromEvent(e: ClipboardEvent | DragEvent, onlyTypes = false): Promise<any[]> {
const files: any[] = []; const files: any[] = [];
const scanFiles = async(item: any) => { const scanFiles = async(entry: any, item: DataTransferItem) => {
if(item.isDirectory) { if(entry.isDirectory) {
const directoryReader = item.createReader(); const directoryReader = entry.createReader();
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
directoryReader.readEntries(async(entries: any) => { directoryReader.readEntries(async(entries: any) => {
for(const entry of entries) { for(const entry of entries) {
await scanFiles(entry); await scanFiles(entry, item);
} }
resolve(); resolve();
}); });
}); });
} else if(item) { } else if(entry) {
if(onlyTypes) { if(onlyTypes) {
files.push(item.type); files.push(entry.type);
} else { } else {
const file = item instanceof File ? const itemFile = item.getAsFile(); // * Safari can't handle entry.file with pasting
item : const file = entry instanceof File ?
entry :
( (
item instanceof DataTransferItem ? entry instanceof DataTransferItem ?
item.getAsFile() : entry.getAsFile() :
await new Promise((resolve, reject) => item.file(resolve, reject)) await new Promise((resolve, reject) => entry.file(resolve, (err: any) => resolve(itemFile)))
); );
/* if(!onlyTypes) { /* if(!onlyTypes) {
@ -657,7 +658,7 @@ export async function getFilesFromEvent(e: ClipboardEvent | DragEvent, onlyTypes
const item: DataTransferItem = items[i]; const item: DataTransferItem = items[i];
if(item.kind === 'file') { if(item.kind === 'file') {
const entry = (onlyTypes ? item : item.webkitGetAsEntry()) || item.getAsFile(); const entry = (onlyTypes ? item : item.webkitGetAsEntry()) || item.getAsFile();
promises.push(scanFiles(entry)); promises.push(scanFiles(entry, item));
} }
} }

Loading…
Cancel
Save