-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
517 lines (452 loc) · 15.7 KB
/
app.js
File metadata and controls
517 lines (452 loc) · 15.7 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// register service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./service-worker.js', { scope: './' })
.then(reg => console.log('Service worker registered', reg))
.catch(err => console.log('Service worker registration failed:', err));
}
// constants
const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50MB
// state
let jsonData = null;
let currentView = 'tree';
let searchQuery = '';
let currentFileName = '';
let isDarkMode = localStorage.getItem('theme') !== 'light';
// dom elements
const dropZone = document.getElementById('dropZone');
const viewerContainer = document.getElementById('viewerContainer');
const fileInfo = document.getElementById('fileInfo');
const fileName = document.getElementById('fileName');
const fileSize = document.getElementById('fileSize');
const validation = document.getElementById('validation');
const fileInput = document.getElementById('fileInput');
const openFileBtn = document.getElementById('openFile');
const toggleViewBtn = document.getElementById('toggleView');
const collapseAllBtn = document.getElementById('collapseAll');
const expandAllBtn = document.getElementById('expandAll');
const toggleThemeBtn = document.getElementById('toggleTheme');
const searchInput = document.getElementById('searchInput');
const clearSearchBtn = document.getElementById('clearSearch');
const treeView = document.getElementById('treeView');
const rawView = document.getElementById('rawView');
const rawContent = document.getElementById('rawContent');
const statsContainer = document.getElementById('statsContainer');
const toast = document.getElementById('toast');
// apply saved theme
document.body.classList.toggle('light-theme', !isDarkMode);
// theme handling
toggleThemeBtn.addEventListener('click', () => {
isDarkMode = !isDarkMode;
document.body.classList.toggle('light-theme', !isDarkMode);
localStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
});
// file handling api
if ('launchQueue' in window) {
window.launchQueue.setConsumer(async (launchParams) => {
if (!launchParams.files.length) return;
const fileHandle = launchParams.files[0];
const file = await fileHandle.getFile();
await loadJSONFile(file);
});
}
// manual file selection
openFileBtn.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', async (e) => {
const file = e.target.files[0];
if (file) await loadJSONFile(file);
});
// drag and drop
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('drag-over');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('drag-over');
});
dropZone.addEventListener('drop', async (e) => {
e.preventDefault();
dropZone.classList.remove('drag-over');
const file = e.dataTransfer.files[0];
if (file) await loadJSONFile(file);
});
// load json file
async function loadJSONFile(file) {
if (file.size > MAX_FILE_SIZE) {
showToast(`File too large (${formatFileSize(file.size)}). Max size is ${formatFileSize(MAX_FILE_SIZE)}.`, true);
return;
}
try {
currentFileName = file.name;
const text = await file.text();
try {
jsonData = JSON.parse(text);
dropZone.style.display = 'none';
viewerContainer.style.display = 'block';
fileInfo.style.display = 'flex';
toggleViewBtn.style.display = 'inline-block';
collapseAllBtn.style.display = 'inline-block';
expandAllBtn.style.display = 'inline-block';
fileName.textContent = file.name;
fileSize.textContent = formatFileSize(file.size);
validation.textContent = '✓ Valid JSON';
validation.className = 'validation valid';
updateStats();
renderTree();
document.title = `${file.name} - JSON Viewer`;
} catch (err) {
validation.textContent = `✗ Invalid JSON: ${err.message}`;
validation.className = 'validation invalid';
fileInfo.style.display = 'flex';
showToast(`Invalid JSON: ${err.message}`, true);
}
} catch (err) {
showToast(`Error loading file: ${err.message}`, true);
}
}
// update statistics
function updateStats() {
const stats = getJSONStats(jsonData);
statsContainer.textContent = `${stats.keys} keys, ${stats.depth} max depth, ${stats.arrays} arrays, ${stats.objects} objects`;
}
function getJSONStats(obj, depth = 0) {
let keys = 0;
let maxDepth = depth;
let arrays = 0;
let objects = 0;
if (Array.isArray(obj)) {
arrays++;
obj.forEach(item => {
const stats = getJSONStats(item, depth + 1);
keys += stats.keys;
maxDepth = Math.max(maxDepth, stats.depth);
arrays += stats.arrays;
objects += stats.objects;
});
} else if (typeof obj === 'object' && obj !== null) {
objects++;
Object.keys(obj).forEach(key => {
keys++;
const stats = getJSONStats(obj[key], depth + 1);
keys += stats.keys;
maxDepth = Math.max(maxDepth, stats.depth);
arrays += stats.arrays;
objects += stats.objects;
});
}
return { keys, depth: maxDepth, arrays, objects };
}
// render tree view
function renderTree() {
treeView.innerHTML = '';
const tree = buildTreeNode(jsonData, 'root', []);
treeView.appendChild(tree);
}
function buildTreeNode(data, key, path) {
const container = document.createElement('div');
container.className = 'tree-node';
const currentPath = [...path, key];
const pathString = buildPathString(currentPath);
const matchesSearch = !searchQuery ||
key.toLowerCase().includes(searchQuery.toLowerCase()) ||
(typeof data === 'string' && data.toLowerCase().includes(searchQuery.toLowerCase()));
if (searchQuery && !matchesSearch && !hasMatchingChildren(data, searchQuery)) {
return container;
}
if (Array.isArray(data)) {
buildCollapsibleNode(container, data, key, currentPath, pathString, matchesSearch, `Array[${data.length}]`, (children) => {
data.forEach((item, i) => {
children.appendChild(buildTreeNode(item, `[${i}]`, currentPath));
});
});
} else if (typeof data === 'object' && data !== null) {
buildCollapsibleNode(container, data, key, currentPath, pathString, matchesSearch, `Object{${Object.keys(data).length}}`, (children) => {
Object.entries(data).forEach(([k, v]) => {
children.appendChild(buildTreeNode(v, k, currentPath));
});
});
} else {
buildLeafNode(container, data, key, pathString, matchesSearch);
}
return container;
}
function buildCollapsibleNode(container, data, key, currentPath, pathString, matchesSearch, typeLabel, populateChildren) {
const header = document.createElement('div');
header.className = 'tree-header' + (matchesSearch && searchQuery ? ' highlight' : '');
header.setAttribute('role', 'treeitem');
header.setAttribute('aria-expanded', 'true');
header.setAttribute('tabindex', '0');
header.dataset.path = pathString;
const toggle = document.createElement('span');
toggle.className = 'toggle';
toggle.textContent = '▼';
toggle.setAttribute('aria-hidden', 'true');
const keySpan = document.createElement('span');
keySpan.className = 'key';
keySpan.textContent = key;
const typeSpan = document.createElement('span');
typeSpan.className = 'type';
typeSpan.textContent = typeLabel;
const copyBtn = document.createElement('button');
copyBtn.className = 'copy-path';
copyBtn.textContent = '📋';
copyBtn.setAttribute('aria-label', `Copy path: ${pathString}`);
header.appendChild(toggle);
header.appendChild(keySpan);
header.appendChild(typeSpan);
header.appendChild(copyBtn);
container.appendChild(header);
const children = document.createElement('div');
children.className = 'tree-children';
children.setAttribute('role', 'group');
populateChildren(children);
container.appendChild(children);
}
function buildLeafNode(container, data, key, pathString, matchesSearch) {
const header = document.createElement('div');
header.className = 'tree-header tree-leaf' + (matchesSearch && searchQuery ? ' highlight' : '');
header.setAttribute('role', 'treeitem');
header.setAttribute('tabindex', '0');
header.dataset.path = pathString;
const keySpan = document.createElement('span');
keySpan.className = 'key';
keySpan.textContent = key;
const separator = document.createElement('span');
separator.textContent = ': ';
const valueSpan = document.createElement('span');
valueSpan.className = `value value-${typeof data}`;
if (data === null) {
valueSpan.className = 'value value-null';
valueSpan.textContent = 'null';
} else if (typeof data === 'string') {
valueSpan.textContent = `"${data}"`;
} else {
valueSpan.textContent = String(data);
}
const copyBtn = document.createElement('button');
copyBtn.className = 'copy-path';
copyBtn.textContent = '📋';
copyBtn.setAttribute('aria-label', `Copy path: ${pathString}`);
header.appendChild(keySpan);
header.appendChild(separator);
header.appendChild(valueSpan);
header.appendChild(copyBtn);
container.appendChild(header);
}
// event delegation for tree clicks
treeView.addEventListener('click', (e) => {
const copyBtn = e.target.closest('.copy-path');
if (copyBtn) {
e.stopPropagation();
const header = copyBtn.closest('.tree-header');
if (header) copyPath(header.dataset.path);
return;
}
const header = e.target.closest('.tree-header:not(.tree-leaf)');
if (header) {
toggleNode(header);
}
});
// keyboard navigation for tree
treeView.addEventListener('keydown', (e) => {
const header = e.target.closest('.tree-header');
if (!header) return;
switch (e.key) {
case 'Enter':
case ' ':
e.preventDefault();
if (header.classList.contains('tree-leaf')) return;
toggleNode(header);
break;
case 'ArrowDown':
e.preventDefault();
focusAdjacentTreeItem(header, 1);
break;
case 'ArrowUp':
e.preventDefault();
focusAdjacentTreeItem(header, -1);
break;
case 'ArrowRight':
e.preventDefault();
if (header.classList.contains('tree-leaf')) return;
if (header.getAttribute('aria-expanded') === 'false') {
toggleNode(header);
} else {
const children = header.nextElementSibling;
if (children) {
const firstChild = children.querySelector('.tree-header');
if (firstChild) firstChild.focus();
}
}
break;
case 'ArrowLeft':
e.preventDefault();
if (!header.classList.contains('tree-leaf') && header.getAttribute('aria-expanded') === 'true') {
toggleNode(header);
} else {
const parentGroup = header.closest('.tree-children');
if (parentGroup) {
const parentHeader = parentGroup.previousElementSibling;
if (parentHeader && parentHeader.classList.contains('tree-header')) {
parentHeader.focus();
}
}
}
break;
}
});
function toggleNode(header) {
const children = header.nextElementSibling;
if (!children || !children.classList.contains('tree-children')) return;
const toggle = header.querySelector('.toggle');
children.classList.toggle('collapsed');
const isCollapsed = children.classList.contains('collapsed');
toggle.textContent = isCollapsed ? '▶' : '▼';
header.setAttribute('aria-expanded', String(!isCollapsed));
}
function focusAdjacentTreeItem(current, direction) {
const allHeaders = [...treeView.querySelectorAll('.tree-header')];
const visibleHeaders = allHeaders.filter(h => {
let el = h.closest('.tree-children');
while (el) {
if (el.classList.contains('collapsed')) return false;
el = el.parentElement?.closest('.tree-children');
}
return true;
});
const idx = visibleHeaders.indexOf(current);
const next = visibleHeaders[idx + direction];
if (next) next.focus();
}
function hasMatchingChildren(data, query) {
query = query.toLowerCase();
if (Array.isArray(data)) {
return data.some(item => {
if (typeof item === 'string' && item.toLowerCase().includes(query)) return true;
if (typeof item === 'object') return hasMatchingChildren(item, query);
return false;
});
} else if (typeof data === 'object' && data !== null) {
return Object.entries(data).some(([k, v]) => {
if (k.toLowerCase().includes(query)) return true;
if (typeof v === 'string' && v.toLowerCase().includes(query)) return true;
if (typeof v === 'object') return hasMatchingChildren(v, query);
return false;
});
}
return false;
}
function buildPathString(path) {
return path.slice(1).map((p, i) => {
if (p.startsWith('[')) return p;
return i === 0 ? p : `.${p}`;
}).join('');
}
// copy path to clipboard
function copyPath(path) {
if (!path) return;
try {
navigator.clipboard.writeText(path).then(() => {
showToast('Path copied to clipboard!');
}).catch(() => {
showToast('Failed to copy path', true);
});
} catch {
showToast('Clipboard not available', true);
}
}
// toast notifications
let toastTimeout;
function showToast(message, isError = false) {
clearTimeout(toastTimeout);
toast.textContent = message;
toast.className = 'toast' + (isError ? ' toast-error' : '');
// force reflow so re-adding 'show' triggers transition even if already visible
toast.offsetHeight;
toast.classList.add('show');
toastTimeout = setTimeout(() => {
toast.classList.remove('show');
}, 3000);
}
// toggle view
toggleViewBtn.addEventListener('click', () => {
if (currentView === 'tree') {
currentView = 'raw';
treeView.style.display = 'none';
rawView.style.display = 'block';
rawContent.textContent = JSON.stringify(jsonData, null, 2);
toggleViewBtn.textContent = 'Tree View';
collapseAllBtn.style.display = 'none';
expandAllBtn.style.display = 'none';
} else {
currentView = 'tree';
treeView.style.display = 'block';
rawView.style.display = 'none';
toggleViewBtn.textContent = 'Raw View';
collapseAllBtn.style.display = 'inline-block';
expandAllBtn.style.display = 'inline-block';
}
});
// collapse/expand all
collapseAllBtn.addEventListener('click', () => {
treeView.querySelectorAll('.tree-children').forEach(el => {
el.classList.add('collapsed');
});
treeView.querySelectorAll('.toggle').forEach(el => {
el.textContent = '▶';
});
treeView.querySelectorAll('.tree-header[aria-expanded]').forEach(el => {
el.setAttribute('aria-expanded', 'false');
});
});
expandAllBtn.addEventListener('click', () => {
treeView.querySelectorAll('.tree-children').forEach(el => {
el.classList.remove('collapsed');
});
treeView.querySelectorAll('.toggle').forEach(el => {
el.textContent = '▼';
});
treeView.querySelectorAll('.tree-header[aria-expanded]').forEach(el => {
el.setAttribute('aria-expanded', 'true');
});
});
// search with debounce
let searchTimeout;
searchInput.addEventListener('input', () => {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => {
searchQuery = searchInput.value;
if (jsonData && currentView === 'tree') {
renderTree();
}
}, 250);
});
clearSearchBtn.addEventListener('click', () => {
searchInput.value = '';
searchQuery = '';
clearTimeout(searchTimeout);
if (jsonData && currentView === 'tree') {
renderTree();
}
});
// keyboard shortcuts
document.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'o') {
e.preventDefault();
fileInput.click();
}
if ((e.ctrlKey || e.metaKey) && e.key === 'f') {
e.preventDefault();
searchInput.focus();
}
if ((e.ctrlKey || e.metaKey) && e.key === 'd') {
e.preventDefault();
toggleThemeBtn.click();
}
});
// utility functions
function formatFileSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
}
console.log('JSON Viewer initialized');