Spaces:
Running
Running
Update js/utils.js
Browse files- js/utils.js +26 -3
js/utils.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
// Display directory structure
|
| 2 |
function displayDirectoryStructure(tree) {
|
|
|
|
|
|
|
|
|
|
| 3 |
const container = document.getElementById('directoryStructure');
|
| 4 |
container.innerHTML = '';
|
| 5 |
const rootUl = document.createElement('ul');
|
|
@@ -11,13 +14,18 @@ function displayDirectoryStructure(tree) {
|
|
| 11 |
|
| 12 |
// Sort the tree before processing
|
| 13 |
tree.sort(sortContents);
|
|
|
|
| 14 |
|
| 15 |
// Build directory structure
|
| 16 |
tree.forEach(item => {
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
|
|
|
| 19 |
item.path = item.path.startsWith('/') ? item.path : '/' + item.path;
|
| 20 |
const pathParts = item.path.split('/');
|
|
|
|
| 21 |
let currentLevel = directoryStructure;
|
| 22 |
|
| 23 |
pathParts.forEach((part, index) => {
|
|
@@ -29,17 +37,23 @@ function displayDirectoryStructure(tree) {
|
|
| 29 |
});
|
| 30 |
});
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
function createTreeNode(name, item, parentUl) {
|
|
|
|
| 33 |
const li = document.createElement('li');
|
| 34 |
const checkbox = document.createElement('input');
|
| 35 |
checkbox.type = 'checkbox';
|
| 36 |
checkbox.className = 'mr-2';
|
| 37 |
|
| 38 |
-
if (typeof item === 'object' && (!item.type || typeof item.type !== 'string')) {
|
| 39 |
// Directory node
|
|
|
|
| 40 |
createDirectoryNode(li, checkbox, name, item, parentUl);
|
| 41 |
} else {
|
| 42 |
// File node
|
|
|
|
| 43 |
createFileNode(li, checkbox, name, item);
|
| 44 |
}
|
| 45 |
|
|
@@ -63,7 +77,16 @@ function displayDirectoryStructure(tree) {
|
|
| 63 |
ul.className = 'ml-6 mt-2';
|
| 64 |
li.appendChild(ul);
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
createTreeNode(childName, childItem, ul);
|
| 68 |
}
|
| 69 |
|
|
|
|
| 1 |
// Display directory structure
|
| 2 |
function displayDirectoryStructure(tree) {
|
| 3 |
+
console.log("--- displayDirectoryStructure called ---");
|
| 4 |
+
console.log("Received tree:", JSON.parse(JSON.stringify(tree)));
|
| 5 |
+
|
| 6 |
const container = document.getElementById('directoryStructure');
|
| 7 |
container.innerHTML = '';
|
| 8 |
const rootUl = document.createElement('ul');
|
|
|
|
| 14 |
|
| 15 |
// Sort the tree before processing
|
| 16 |
tree.sort(sortContents);
|
| 17 |
+
console.log("Sorted tree:", JSON.parse(JSON.stringify(tree)));
|
| 18 |
|
| 19 |
// Build directory structure
|
| 20 |
tree.forEach(item => {
|
| 21 |
+
// This was the problematic line. HF API only returns files.
|
| 22 |
+
// We build the directory structure from file paths.
|
| 23 |
+
// if (item.type !== 'blob') return;
|
| 24 |
|
| 25 |
+
console.log("Processing item:", item);
|
| 26 |
item.path = item.path.startsWith('/') ? item.path : '/' + item.path;
|
| 27 |
const pathParts = item.path.split('/');
|
| 28 |
+
console.log("Path parts:", pathParts);
|
| 29 |
let currentLevel = directoryStructure;
|
| 30 |
|
| 31 |
pathParts.forEach((part, index) => {
|
|
|
|
| 37 |
});
|
| 38 |
});
|
| 39 |
|
| 40 |
+
console.log("Built directory structure object:", directoryStructure);
|
| 41 |
+
|
| 42 |
+
|
| 43 |
function createTreeNode(name, item, parentUl) {
|
| 44 |
+
console.log(`Creating tree node for: ${name}`, "Item:", item);
|
| 45 |
const li = document.createElement('li');
|
| 46 |
const checkbox = document.createElement('input');
|
| 47 |
checkbox.type = 'checkbox';
|
| 48 |
checkbox.className = 'mr-2';
|
| 49 |
|
| 50 |
+
if (typeof item === 'object' && item !== null && (!item.type || typeof item.type !== 'string')) {
|
| 51 |
// Directory node
|
| 52 |
+
console.log(` -> Identified as DIRECTORY`);
|
| 53 |
createDirectoryNode(li, checkbox, name, item, parentUl);
|
| 54 |
} else {
|
| 55 |
// File node
|
| 56 |
+
console.log(` -> Identified as FILE`);
|
| 57 |
createFileNode(li, checkbox, name, item);
|
| 58 |
}
|
| 59 |
|
|
|
|
| 77 |
ul.className = 'ml-6 mt-2';
|
| 78 |
li.appendChild(ul);
|
| 79 |
|
| 80 |
+
// Sort children: directories first, then files, then alphabetically
|
| 81 |
+
const sortedEntries = Object.entries(item).sort(([nameA, itemA], [nameB, itemB]) => {
|
| 82 |
+
const isDirA = typeof itemA === 'object' && itemA !== null && !itemA.type;
|
| 83 |
+
const isDirB = typeof itemB === 'object' && itemB !== null && !itemB.type;
|
| 84 |
+
if (isDirA && !isDirB) return -1;
|
| 85 |
+
if (!isDirA && isDirB) return 1;
|
| 86 |
+
return nameA.localeCompare(nameB);
|
| 87 |
+
});
|
| 88 |
+
|
| 89 |
+
for (const [childName, childItem] of sortedEntries) {
|
| 90 |
createTreeNode(childName, childItem, ul);
|
| 91 |
}
|
| 92 |
|