Skip to content

Commit f74d95c

Browse files
committed
feat: Improve drag-and-drop file processing with error handling and user feedback
1 parent b8cf6df commit f74d95c

File tree

1 file changed

+63
-48
lines changed

1 file changed

+63
-48
lines changed

index.js

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,62 +2040,68 @@
20402040
return;
20412041
}
20422042

2043-
// Hide drag-drop zone and show editor
2044-
dragDropZone.style.display = 'none';
2045-
if (codeEditor) codeEditor.style.display = 'block';
2046-
20472043
// Show loader IMMEDIATELY
20482044
load(true, 'Processing dropped folder...');
20492045

2050-
// Process dropped items
2051-
const allFiles = [];
2052-
2053-
// Use webkitGetAsEntry for folder support
2054-
for (let i = 0; i < items.length; i++) {
2055-
const item = items[i].webkitGetAsEntry();
2056-
if (item) {
2057-
if (item.isDirectory) {
2058-
await traverseDirectory(item, '', allFiles);
2059-
} else if (item.isFile) {
2060-
const file = items[i].getAsFile();
2061-
if (file) {
2062-
// Add webkitRelativePath property
2063-
Object.defineProperty(file, 'webkitRelativePath', {
2064-
value: file.name,
2065-
writable: false
2066-
});
2067-
allFiles.push(file);
2046+
try {
2047+
// Process dropped items
2048+
const allFiles = [];
2049+
2050+
// Use webkitGetAsEntry for folder support
2051+
for (let i = 0; i < items.length; i++) {
2052+
const item = items[i].webkitGetAsEntry();
2053+
if (item) {
2054+
if (item.isDirectory) {
2055+
await traverseDirectory(item, '', allFiles);
2056+
} else if (item.isFile) {
2057+
const file = items[i].getAsFile();
2058+
if (file) {
2059+
// Add webkitRelativePath property
2060+
Object.defineProperty(file, 'webkitRelativePath', {
2061+
value: file.name,
2062+
writable: false
2063+
});
2064+
allFiles.push(file);
2065+
}
20682066
}
20692067
}
20702068
}
2071-
}
2072-
2073-
if (allFiles.length === 0) {
2074-
toast('No files found in dropped folder', 'warning');
2069+
2070+
console.log('Files collected:', allFiles.length);
2071+
2072+
if (allFiles.length === 0) {
2073+
toast('No files found in dropped folder', 'warning');
2074+
load(false);
2075+
return;
2076+
}
2077+
2078+
// Hide drag-drop zone and show editor
2079+
dragDropZone.style.display = 'none';
2080+
if (codeEditor) codeEditor.style.display = 'block';
2081+
2082+
// Create a FileList-like object
2083+
const fileList = {
2084+
length: allFiles.length,
2085+
item: i => allFiles[i],
2086+
[Symbol.iterator]: function* () {
2087+
for (let i = 0; i < allFiles.length; i++) {
2088+
yield allFiles[i];
2089+
}
2090+
}
2091+
};
2092+
2093+
// Add array access
2094+
allFiles.forEach((file, idx) => {
2095+
fileList[idx] = file;
2096+
});
2097+
2098+
// Process files
2099+
loadFiles(fileList);
2100+
} catch (err) {
2101+
console.error('Error processing drop:', err);
2102+
toast('Error processing dropped folder: ' + err.message, 'error');
20752103
load(false);
2076-
dragDropZone.style.display = 'flex';
2077-
if (codeEditor) codeEditor.style.display = 'none';
2078-
return;
20792104
}
2080-
2081-
// Create a FileList-like object
2082-
const fileList = {
2083-
length: allFiles.length,
2084-
item: i => allFiles[i],
2085-
[Symbol.iterator]: function* () {
2086-
for (let i = 0; i < allFiles.length; i++) {
2087-
yield allFiles[i];
2088-
}
2089-
}
2090-
};
2091-
2092-
// Add array access
2093-
allFiles.forEach((file, idx) => {
2094-
fileList[idx] = file;
2095-
});
2096-
2097-
// Process files
2098-
setTimeout(() => loadFiles(fileList), 0);
20992105
}, false);
21002106

21012107
// Recursive function to traverse directory structure
@@ -2158,6 +2164,15 @@
21582164
});
21592165
}
21602166

2167+
// IMPORTANT: Drag-drop doesn't work reliably with folders in all browsers
2168+
// Show a warning message
2169+
if (dragDropZone) {
2170+
const dragHint = dragDropZone.querySelector('.drag-drop-hint span:last-child');
2171+
if (dragHint) {
2172+
dragHint.textContent = 'Note: For best results, use "Select Directory" button';
2173+
}
2174+
}
2175+
21612176
// Optimize file input change handler
21622177
inp.onchange = e => {
21632178
if (!e.target.files.length) return;

0 commit comments

Comments
 (0)