-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (60 loc) · 2.54 KB
/
Copy pathscript.js
File metadata and controls
77 lines (60 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
document.getElementById('folderInput').addEventListener('change', function(event) {
const files = event.target.files;
// Clear previous tree
const projectTree = document.getElementById('projectTree');
projectTree.innerHTML = '';
const folderStructure = {};
// Organize files into a tree structure
for (const file of files) {
const pathParts = file.webkitRelativePath.split('/');
let currentLevel = folderStructure;
pathParts.forEach((part, index) => {
if (!currentLevel[part]) {
currentLevel[part] = index === pathParts.length - 1 ? null : {};
}
currentLevel = currentLevel[part];
});
}
// Function to create tree nodes recursively
function createTreeNodes(obj) {
const ul = document.createElement('ul');
for (const key in obj) {
const li = document.createElement('li');
li.textContent = key;
if (obj[key] !== null) { // It's a folder
li.classList.add('folder');
li.appendChild(createTreeNodes(obj[key]));
} else { // It's a file
li.classList.add('file');
}
ul.appendChild(li);
}
return ul;
}
projectTree.appendChild(createTreeNodes(folderStructure));
document.getElementById('copyButton').style.display = 'inline-block'; // Show button
});
document.getElementById('copyButton').addEventListener('click', function() {
const projectTreeText = getTreeText(document.getElementById('projectTree'));
navigator.clipboard.writeText(projectTreeText).then(() => {
alert('Project structure copied to clipboard!');
}).catch(err => {
console.error('Could not copy text: ', err);
});
});
function getTreeText(ul, prefix = '', isLast = true) {
let text = '';
const children = Array.from(ul.children);
children.forEach((li, index) => {
const isLastChild = index === children.length - 1;
const connector = isLast ? ' ' : '│ ';
const linePrefix = prefix + (isLastChild ? ' ' : '│ ');
const itemName = li.firstChild ? li.firstChild.textContent : '';
text += prefix + (isLastChild ? '└─── ' : '├─── ') + itemName + '\n';
const nestedUl = li.querySelector('ul');
if (nestedUl) {
text += getTreeText(nestedUl, linePrefix, isLastChild);
}
});
return text;
}