-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
146 lines (122 loc) · 4.38 KB
/
Copy pathrenderer.js
File metadata and controls
146 lines (122 loc) · 4.38 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
let currentTree = null;
let expandedFolders = new Set();
async function selectFolder() {
const tree = await window.electronAPI.selectFolder();
if (tree) {
currentTree = tree;
expandedFolders.clear();
expandedFolders.add(tree.path);
renderTree();
document.getElementById('copy-btn').classList.remove('hidden');
document.getElementById('show-text-btn').classList.remove('hidden');
}
}
function renderTree() {
const container = document.getElementById('tree-container');
container.innerHTML = '';
if (!currentTree) return;
renderNode(currentTree, container, 0);
}
function renderNode(node, parent, level) {
const div = document.createElement('div');
div.className = 'tree-item';
div.style.paddingLeft = (level * 20) + 'px';
const isExpanded = expandedFolders.has(node.path);
const hasChildren = Object.keys(node.children || {}).length > 0;
if (node.isFolder && hasChildren) {
const chevron = document.createElement('span');
chevron.className = 'chevron' + (isExpanded ? ' expanded' : '');
chevron.textContent = '▶';
div.appendChild(chevron);
} else if (node.isFolder) {
div.innerHTML += '<span class="chevron" style="opacity:0">▶</span>';
} else {
div.innerHTML += '<span style="width:16px;display:inline-block"></span>';
}
const icon = node.isFolder ? '📁 ' : '📄 ';
const name = document.createElement('span');
name.className = node.isFolder ? 'folder' : 'file';
name.textContent = icon + node.name;
div.appendChild(name);
if (!node.isFolder && node.size) {
const size = document.createElement('span');
size.className = 'size';
size.textContent = formatSize(node.size);
div.appendChild(size);
}
if (node.isFolder) {
div.onclick = (e) => {
e.stopPropagation();
toggleFolder(node.path);
};
}
parent.appendChild(div);
if (node.isFolder && isExpanded && hasChildren) {
const children = Object.values(node.children).sort((a, b) => {
if (a.isFolder === b.isFolder) return a.name.localeCompare(b.name);
return a.isFolder ? -1 : 1;
});
children.forEach(child => renderNode(child, parent, level + 1));
}
}
function toggleFolder(path) {
if (expandedFolders.has(path)) {
expandedFolders.delete(path);
} else {
expandedFolders.add(path);
}
renderTree();
}
function formatSize(bytes) {
if (bytes === 0) return '';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `(${(bytes / Math.pow(k, i)).toFixed(1)} ${sizes[i]})`;
}
function generateTextTree(node, prefix = '', isLast = true, isRoot = false) {
let result = '';
if (!isRoot) {
const connector = isLast ? '└── ' : '├── ';
result += prefix + connector + node.name + (node.isFolder ? '' : ' ' + formatSize(node.size)) + '\n';
}
if (node.isFolder && node.children) {
const children = Object.values(node.children).sort((a, b) => {
if (a.isFolder === b.isFolder) return a.name.localeCompare(b.name);
return a.isFolder ? -1 : 1;
});
children.forEach((child, index) => {
const newPrefix = isRoot ? '' : prefix + (isLast ? ' ' : '│ ');
const childIsLast = index === children.length - 1;
result += generateTextTree(child, newPrefix, childIsLast, false);
});
}
return result;
}
function copyToClipboard() {
if (!currentTree) return;
const text = currentTree.name + '\n' + generateTextTree(currentTree, '', true, true);
navigator.clipboard.writeText(text).then(() => {
const btn = document.getElementById('copy-btn');
const originalText = btn.textContent;
btn.textContent = 'Copied to clipboard';
setTimeout(() => {
btn.textContent = originalText;
}, 2000);
});
}
function toggleTextView() {
const textarea = document.getElementById('text-output');
const treeContainer = document.getElementById('tree-container');
if (textarea.classList.contains('hidden')) {
const text = currentTree.name + '\n' + generateTextTree(currentTree, '', true, true);
textarea.value = text;
textarea.classList.remove('hidden');
treeContainer.classList.add('hidden');
document.getElementById('show-text-btn').textContent = 'Show tree';
} else {
textarea.classList.add('hidden');
treeContainer.classList.remove('hidden');
document.getElementById('show-text-btn').textContent = 'Show as text';
}
}