-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerate-FolderTreemapHTML.ps1
More file actions
454 lines (411 loc) · 13.1 KB
/
Generate-FolderTreemapHTML.ps1
File metadata and controls
454 lines (411 loc) · 13.1 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
param(
[string]$Path = ".",
[string]$OutputHtml = "nested_treemap.html"
)
function Get-FolderSizeJson {
param (
[string]$BasePath,
[int]$DepthLimit = 8
)
$stack = New-Object System.Collections.Stack
$visited = @{ }
$root = [PSCustomObject]@{
name = Split-Path $BasePath -Leaf
fullPath = (Resolve-Path $BasePath).Path
type = "folder"
children = @()
size = 0
depth = 0
}
$stack.Push($root)
while ($stack.Count -gt 0) {
$node = $stack.Pop()
if (-not $node.fullPath) { continue }
if ($visited.ContainsKey($node.fullPath)) { continue }
$visited[$node.fullPath] = $true
if ($node.depth -ge $DepthLimit) {
continue
}
$children = @()
$files = Get-ChildItem -Path $node.fullPath -File -ErrorAction SilentlyContinue
$totalSize = 0
foreach ($file in $files) {
if ($file.Length -ge 1MB) {
$children += [PSCustomObject]@{
name = $file.Name
size = $file.Length
type = "file"
fullPath = $file.FullName
}
$totalSize += $file.Length
} else {
$totalSize += $file.Length
}
}
$subfolders = Get-ChildItem -Path $node.fullPath -Directory -ErrorAction SilentlyContinue
foreach ($subfolder in $subfolders) {
$childNode = [PSCustomObject]@{
name = $subfolder.Name
fullPath = $subfolder.FullName
type = "folder"
children = @()
size = 0
depth = $node.depth + 1
}
$children += $childNode
$stack.Push($childNode)
}
$node.children = $children
$node.size = $totalSize + ($children | Measure-Object -Property size -Sum).Sum
}
function FilterSmallNodes([PSCustomObject]$node) {
if ($node.type -eq "file") {
return ($node.size -ge 1MB)
}
if ($node.children) {
$node.children = $node.children | Where-Object { FilterSmallNodes $_ }
$node.size = ($node.children | Measure-Object -Property size -Sum).Sum
}
return ($node.size -ge 1MB)
}
if (-not (FilterSmallNodes $root)) {
return $null
}
function ConvertTo-JsonCustom($obj) {
return $obj | ConvertTo-Json -Depth 100 -Compress
}
return ConvertTo-JsonCustom $root
}
$json = Get-FolderSizeJson -BasePath $Path
if (-not $json) {
Write-Warning "指定フォルダに1MB以上のフォルダ/ファイルがありません。"
exit
}
$utf8Bytes = [System.Text.Encoding]::UTF8.GetBytes($json)
$base64 = [Convert]::ToBase64String($utf8Bytes)
$html = @"
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Nested Folder Treemap</title>
<style>
body { margin: 0; font-family: sans-serif; }
h2 { margin: 10px; }
#breadcrumb {
margin: 10px;
font-size: 14px;
user-select: none;
}
#breadcrumb span {
cursor: pointer;
color: #337ab7;
margin-right: 5px;
}
#breadcrumb span:hover {
text-decoration: underline;
}
#chart {
position: relative;
width: 100vw;
height: 80vh;
overflow: auto;
padding: 10px;
box-sizing: border-box;
}
.node {
position: absolute;
border: 2px solid white;
box-sizing: border-box;
border-radius: 4px;
color: white;
font-size: 12px;
overflow: hidden;
transition: transform 0.2s;
cursor: default;
display: flex;
flex-direction: column;
justify-content: flex-start;
padding: 4px;
}
.node.folder {
background-color: #f39c12;
}
.node.file {
background-color: #2980b9;
}
.node.enlarge:hover {
transform: scale(1.05);
z-index: 10;
cursor: pointer;
}
.label {
font-weight: bold;
font-size: 11px;
color: white;
text-shadow: 0 0 3px rgba(0,0,0,0.7);
word-break: break-word;
z-index: 1000;
position: relative;
user-select: none;
}
.label.folder-label {
position: absolute;
top: 4px;
left: 4px;
background: rgba(0,0,0,0.3);
padding: 2px 4px;
border-radius: 3px;
user-select: none;
}
#backButton {
margin: 10px 5px 10px 10px;
padding: 6px 12px;
font-size: 14px;
user-select: none;
}
#folderPath {
vertical-align: middle;
margin-left: 10px;
font-size: 13px;
/* 横幅をウィンドウ幅からボタン分を引いた幅に調整 */
width: calc(100vw - 150px);
max-width: 80vw;
user-select: all;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: 6px 8px;
box-sizing: border-box;
}
.node.file {
cursor: default !important;
}
/* コピー完了ツールチップ */
#copyTooltip {
position: fixed;
background: #444;
color: white;
padding: 5px 10px;
border-radius: 4px;
font-size: 14px;
pointer-events: none;
opacity: 0;
transition: opacity 0.3s ease;
z-index: 10000;
user-select: none;
}
.link-icon {
position: absolute;
top: 2px;
right: 2px;
width: 16px;
height: 16px;
background: rgba(255, 255, 255, 0.8);
border-radius: 3px;
text-align: center;
font-size: 12px;
color: #333;
text-decoration: none;
z-index: 2000;
opacity: 0;
pointer-events: none;
transition: opacity 0.2s ease;
}
/* ノードにホバーしたときだけチェーンマークを表示 */
.node:hover .link-icon {
opacity: 1;
pointer-events: auto;
}
</style>
</head>
<body>
<h2>Nested Folder Treemap for '$Path'</h2>
<div id='breadcrumb'></div>
<button id='backButton'>1つ上の階層へ</button>
<input type='text' id='folderPath' readonly />
<div id='chart'></div>
<div id='copyTooltip'>コピーしました</div>
<script>
const base64 = '$base64';
function base64ToUtf8(str) {
const bytes = Uint8Array.from(atob(str), c => c.charCodeAt(0));
return new TextDecoder('utf-8').decode(bytes);
}
const jsonStr = base64ToUtf8(base64);
const data = JSON.parse(jsonStr);
function assignParents(node, parent = null) {
node._parent = parent;
if (node.children) {
if (!Array.isArray(node.children)) {
node.children = [node.children];
}
node.children.forEach(child => assignParents(child, node));
}
}
assignParents(data);
let currentNode = data;
let backButton = document.getElementById('backButton');
let folderPathInput = document.getElementById('folderPath');
let copyTooltip = document.getElementById('copyTooltip');
let tooltipTimeout = null;
let tooltipFadeInterval = null;
function updateBreadcrumb() {
const breadcrumb = document.getElementById('breadcrumb');
breadcrumb.innerHTML = '';
let path = [];
let temp = currentNode;
while (temp) {
path.unshift(temp);
temp = temp._parent;
}
path.forEach((node, i) => {
const span = document.createElement('span');
span.textContent = node.name || 'Root';
span.addEventListener('click', () => {
currentNode = node;
createTreemap(document.getElementById('chart'), currentNode, currentNode.size);
updateBreadcrumb();
updateBackButton();
updateFolderPath();
});
breadcrumb.appendChild(span);
if (i < path.length - 1) breadcrumb.appendChild(document.createTextNode(' > '));
});
}
function updateBackButton() {
backButton.disabled = !currentNode._parent;
}
function updateFolderPath() {
folderPathInput.value = currentNode.fullPath || '';
}
function createTreemap(container, node, totalSize, scale = 1, level = 0) {
container.innerHTML = '';
const sorted = node.children?.slice().sort((a, b) => b.size - a.size) || [];
const containerRect = container.getBoundingClientRect();
const maxW = containerRect.width;
const maxH = containerRect.height;
const area = maxW * maxH * 0.90;
const ratio = area / totalSize;
let x = 0, y = 0, rowH = 0;
sorted.forEach(child => {
const w = Math.max(80, Math.sqrt((child.size || 1) * ratio * scale));
const h = w * 0.75;
if (x + w > maxW) {
x = 0;
y += rowH + 10;
rowH = 0;
}
const div = document.createElement('div');
div.className = 'node ' + (child.type === 'file' ? 'file' : 'folder');
div.style.width = w + 'px';
div.style.height = h + 'px';
div.style.left = x + 'px';
div.style.top = (y + 20) + 'px';
// サブフォルダのみenlargeを付与(見た目向上)
if (child.type === 'folder' && child.children && child.children.length > 0) {
div.classList.add('enlarge');
div.style.cursor = 'pointer';
} else if (child.type === 'folder') {
div.style.cursor = 'default';
}
const label = document.createElement('div');
label.className = 'label';
if (child.type === 'folder') {
label.classList.add('folder-label');
}
label.textContent = child.name + ' (' + (child.size / 1024 / 1024).toFixed(1) + ' MB)';
div.appendChild(label);
container.appendChild(div);
// file:/// リンクを右上に追加(↗マーク)
if (child.type === 'folder' || child.type === 'file') {
const fileUrl = 'file:///' + encodeURIComponent((child.fullPath || '').replace(/\\/g, '/')).replace(/%2F/g, '/');
const link = document.createElement('a');
link.href = fileUrl;
link.target = '_blank';
link.className = 'link-icon';
link.title = 'このフォルダ/ファイルを開く';
link.textContent = '↗';
link.addEventListener('click', e => e.stopPropagation()); // 親のクリックイベントを無効化
div.appendChild(link);
}
// 2階層下までのサブツリーマップをネスト表示
if (child.type === 'folder' && child.children?.length && level < 2) {
const subContainer = document.createElement('div');
subContainer.style.position = 'relative';
subContainer.style.width = '100%';
subContainer.style.height = '100%';
subContainer.style.padding = '4px';
div.appendChild(subContainer);
setTimeout(() => createTreemap(subContainer, child, child.size, 0.85, level + 1), 0);
}
div.addEventListener('click', e => {
e.stopPropagation();
if (child.type === 'folder' && child.children?.length) {
currentNode = child;
createTreemap(document.getElementById('chart'), currentNode, currentNode.size);
updateBreadcrumb();
updateBackButton();
updateFolderPath();
}
});
x += w + 10;
if (h > rowH) rowH = h;
});
}
backButton.addEventListener('click', () => {
if (currentNode._parent) {
currentNode = currentNode._parent;
createTreemap(document.getElementById('chart'), currentNode, currentNode.size);
updateBreadcrumb();
updateBackButton();
updateFolderPath();
}
});
folderPathInput.addEventListener('click', async (e) => {
folderPathInput.select();
try {
await navigator.clipboard.writeText(folderPathInput.value);
// コピー完了後も選択状態を維持
folderPathInput.select();
showCopyTooltip(e.pageX, e.pageY);
} catch {
alert('クリップボードへのコピーに失敗しました。');
}
});
// コピー完了ツールチップの管理
function showCopyTooltip(x, y) {
if (tooltipTimeout) clearTimeout(tooltipTimeout);
if (tooltipFadeInterval) clearInterval(tooltipFadeInterval);
copyTooltip.style.opacity = 1;
copyTooltip.style.left = (x + 10) + 'px'; // マウスカーソルの右に10pxずらす
copyTooltip.style.top = (y + 10) + 'px'; // マウスカーソルの下に10pxずらす
copyTooltip.style.pointerEvents = 'none';
let elapsed = 0;
tooltipFadeInterval = setInterval(() => {
elapsed += 100;
let opacity = 1 - (elapsed / 3000);
if (opacity <= 0) {
opacity = 0;
clearInterval(tooltipFadeInterval);
copyTooltip.style.opacity = 0;
} else {
copyTooltip.style.opacity = opacity;
}
}, 100);
tooltipTimeout = setTimeout(() => {
clearInterval(tooltipFadeInterval);
copyTooltip.style.opacity = 0;
}, 3000);
}
updateBreadcrumb();
updateBackButton();
updateFolderPath();
createTreemap(document.getElementById('chart'), currentNode, currentNode.size);
</script>
</body>
</html>
"@
Set-Content -Path $OutputHtml -Value $html -Encoding utf8
Write-Host "Treemap HTML file saved as '$OutputHtml'"
Start-Process $OutputHtml