-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1252 lines (1124 loc) · 44.8 KB
/
index.html
File metadata and controls
1252 lines (1124 loc) · 44.8 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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
VisiData-Web
A browser-based reimplementation inspired by VisiData.
Copyright (C) 2026 Cody C. Baker
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
VisiData-Web is an independent implementation of VisiData-like
concepts in JavaScript. No source code from VisiData was used.
VisiData (https://www.visidata.org/) is created by Saul Pwanson
and is also licensed under GPLv3.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>VisiData-Web: A Browser-based VisiData Clone</title>
<style>
:root {
--bg-primary: #1a1a2e;
--bg-secondary: #16213e;
--bg-tertiary: #0f3460;
--bg-row-even: #1a1a2e;
--bg-row-odd: #1e1e36;
--bg-selected: #2a2a5e;
--bg-cursor: #3a3a7e;
--text-primary: #e0e0e0;
--text-secondary: #a0a0c0;
--text-header: #00d4ff;
--text-key: #ffd700;
--text-cursor: #ffffff;
--border-color: #2a2a4a;
--accent-blue: #00d4ff;
--accent-green: #00ff88;
--accent-red: #ff4466;
--accent-yellow: #ffd700;
--accent-purple: #aa88ff;
--scrollbar-bg: #1a1a2e;
--scrollbar-thumb: #3a3a6e;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
background: var(--bg-primary);
color: var(--text-primary);
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
user-select: none;
}
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--scrollbar-bg); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }
#top-bar {
display: flex;
align-items: center;
background: var(--bg-secondary);
border-bottom: 1px solid var(--border-color);
padding: 4px 8px;
gap: 8px;
flex-shrink: 0;
min-height: 36px;
}
#top-bar .logo {
color: var(--accent-blue);
font-weight: bold;
font-size: 14px;
white-space: nowrap;
}
#top-bar label {
color: var(--text-secondary);
font-size: 12px;
white-space: nowrap;
}
#url-input {
flex: 1;
background: var(--bg-primary);
border: 1px solid var(--border-color);
color: var(--text-primary);
padding: 4px 8px;
font-family: inherit;
font-size: 12px;
border-radius: 3px;
min-width: 100px;
}
#url-input:focus { border-color: var(--accent-blue); outline: none; }
.btn {
background: var(--bg-tertiary);
color: var(--accent-blue);
border: 1px solid var(--border-color);
padding: 4px 12px;
font-family: inherit;
font-size: 12px;
cursor: pointer;
border-radius: 3px;
white-space: nowrap;
}
.btn:hover { background: var(--accent-blue); color: var(--bg-primary); }
#sheet-area {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
position: relative;
}
#col-header-row {
display: flex;
background: var(--bg-secondary);
border-bottom: 2px solid var(--accent-blue);
flex-shrink: 0;
overflow: hidden;
min-height: 28px;
}
.col-header {
padding: 4px 8px;
font-size: 12px;
font-weight: bold;
color: var(--text-header);
border-right: 1px solid var(--border-color);
cursor: pointer;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
min-width: 40px;
flex-shrink: 0;
position: relative;
display: flex;
align-items: center;
gap: 4px;
}
.col-header.key-col { color: var(--text-key); }
.col-header.cursor-col { background: var(--bg-tertiary); }
.col-header .sort-indicator { font-size: 10px; }
.col-header .type-indicator {
font-size: 9px;
color: var(--text-secondary);
margin-left: auto;
}
.col-header .resize-handle {
position: absolute;
right: 0; top: 0; bottom: 0;
width: 5px;
cursor: col-resize;
}
.col-header:hover { background: var(--bg-tertiary); }
#row-number-col {
min-width: 50px; max-width: 50px;
color: var(--text-secondary);
text-align: right;
font-size: 11px;
border-right: 2px solid var(--border-color);
}
#data-viewport {
flex: 1;
overflow: auto;
position: relative;
}
#data-table {
width: max-content;
min-width: 100%;
}
.data-row {
display: flex;
border-bottom: 1px solid var(--border-color);
min-height: 24px;
}
.data-row:nth-child(even) { background: var(--bg-row-even); }
.data-row:nth-child(odd) { background: var(--bg-row-odd); }
.data-row.selected { background: var(--bg-selected); }
.data-row.selected .row-marker::before { content: '●'; color: var(--accent-green); }
.data-row.cursor-row { background: var(--bg-cursor); }
.data-row.cursor-row.selected { background: #3a3a8e; }
.row-num {
min-width: 50px; max-width: 50px;
padding: 2px 6px 2px 2px;
text-align: right;
color: var(--text-secondary);
font-size: 11px;
border-right: 2px solid var(--border-color);
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: flex-end;
gap: 2px;
}
.row-marker { font-size: 10px; width: 10px; text-align: center; }
.data-cell {
padding: 2px 8px;
font-size: 12px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
border-right: 1px solid var(--border-color);
min-width: 40px;
flex-shrink: 0;
display: flex;
align-items: center;
}
.data-cell.cursor-cell {
background: var(--accent-blue);
color: var(--bg-primary);
font-weight: bold;
}
.data-cell.cursor-col { background: rgba(0, 212, 255, 0.08); }
.data-cell.type-int, .data-cell.type-float { text-align: right; justify-content: flex-end; }
.data-cell.type-date { color: var(--accent-purple); }
.data-cell.editing {
padding: 0;
background: var(--bg-primary);
}
.data-cell.editing input {
width: 100%;
height: 100%;
background: var(--bg-primary);
color: var(--accent-green);
border: 2px solid var(--accent-blue);
font-family: inherit;
font-size: 12px;
padding: 2px 6px;
outline: none;
}
#status-bar {
display: flex;
align-items: center;
background: var(--bg-secondary);
border-top: 1px solid var(--border-color);
padding: 2px 8px;
font-size: 12px;
flex-shrink: 0;
gap: 16px;
min-height: 28px;
}
.status-item { color: var(--text-secondary); white-space: nowrap; }
.status-item .label { color: var(--text-secondary); }
.status-item .value { color: var(--accent-blue); margin-left: 4px; }
#status-message {
flex: 1;
color: var(--accent-green);
text-align: right;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
#status-message.error { color: var(--accent-red); }
#command-bar {
display: none;
background: var(--bg-secondary);
border-top: 1px solid var(--accent-blue);
padding: 4px 8px;
flex-shrink: 0;
}
#command-bar.active { display: flex; align-items: center; gap: 8px; }
#command-bar .prompt {
color: var(--accent-yellow);
font-size: 13px;
font-weight: bold;
}
#command-input {
flex: 1;
background: var(--bg-primary);
border: 1px solid var(--accent-blue);
color: var(--text-primary);
padding: 4px 8px;
font-family: inherit;
font-size: 13px;
outline: none;
}
#help-overlay {
display: none;
position: fixed;
inset: 0;
background: rgba(0,0,0,0.7);
z-index: 1000;
justify-content: center;
align-items: center;
}
#help-overlay.active { display: flex; }
#help-content {
background: var(--bg-secondary);
border: 2px solid var(--accent-blue);
border-radius: 8px;
max-width: 700px;
width: 90%;
max-height: 85vh;
overflow-y: auto;
padding: 20px;
}
#help-content h2 {
color: var(--accent-blue);
margin-bottom: 16px;
font-size: 18px;
text-align: center;
}
#help-content h3 {
color: var(--accent-yellow);
margin: 14px 0 8px 0;
font-size: 14px;
border-bottom: 1px solid var(--border-color);
padding-bottom: 4px;
}
.help-row {
display: flex;
padding: 3px 0;
font-size: 12px;
}
.help-key {
min-width: 140px;
color: var(--accent-green);
font-weight: bold;
}
.help-desc { color: var(--text-primary); }
#context-menu {
display: none;
position: fixed;
background: var(--bg-secondary);
border: 1px solid var(--accent-blue);
border-radius: 4px;
padding: 4px 0;
z-index: 500;
min-width: 200px;
box-shadow: 0 4px 12px rgba(0,0,0,0.5);
}
#context-menu.active { display: block; }
.ctx-item {
padding: 6px 16px;
font-size: 12px;
cursor: pointer;
display: flex;
justify-content: space-between;
color: var(--text-primary);
}
.ctx-item:hover { background: var(--bg-tertiary); }
.ctx-item .shortcut { color: var(--text-secondary); margin-left: 24px; }
#loading-overlay {
display: none;
position: fixed;
inset: 0;
background: rgba(0,0,0,0.5);
z-index: 2000;
justify-content: center;
align-items: center;
font-size: 18px;
color: var(--accent-blue);
}
#loading-overlay.active { display: flex; }
</style>
</head>
<body>
<div id="top-bar">
<span class="logo">WebVisiData</span>
<label>URL:</label>
<input type="text" id="url-input" placeholder="Paste a raw TSV URL (e.g., GitHub raw URL)…" />
<button class="btn" id="btn-load" title="Load TSV from URL">LOAD</button>
<button class="btn" id="btn-sample" title="Load sample data">📊 Sample Data</button>
</div>
<div id="sheet-area">
<div id="col-header-row">
<div class="col-header" id="row-number-col">#</div>
</div>
<div id="data-viewport">
<div id="data-table"></div>
</div>
</div>
<div id="status-bar">
<span class="status-item"><span class="label">Sheet:</span><span class="value" id="st-sheet">—</span></span>
<span class="status-item"><span class="label">Rows:</span><span class="value" id="st-rows">0</span></span>
<span class="status-item"><span class="label">Cols:</span><span class="value" id="st-cols">0</span></span>
<span class="status-item"><span class="label">Cursor:</span><span class="value" id="st-cursor">0,0</span></span>
<span class="status-item"><span class="label">Selected:</span><span class="value" id="st-selected">0</span></span>
<span class="status-item"><span class="label">Cell:</span><span class="value" id="st-cell"></span></span>
<span class="status-item"><span class="label">Type:</span><span class="value" id="st-type">~</span></span>
<span class="status-item" style="color:var(--accent-yellow)">? = help</span>
<span id="status-message">Ready. Press ? for help, or load a TSV from a GitHub raw URL.</span>
</div>
<div id="command-bar">
<span class="prompt" id="command-prompt">Input</span>
<input type="text" id="command-input" />
</div>
<div id="help-overlay">
<div id="help-content">
<h2>⌨️ WebVisiData — Keyboard Shortcuts</h2>
<h3>Navigation</h3>
<div class="help-row"><span class="help-key">↑ / k</span><span class="help-desc">Move up one row</span></div>
<div class="help-row"><span class="help-key">↓ / j</span><span class="help-desc">Move down one row</span></div>
<div class="help-row"><span class="help-key">← / h</span><span class="help-desc">Move left one column</span></div>
<div class="help-row"><span class="help-key">→ / l</span><span class="help-desc">Move right one column</span></div>
<div class="help-row"><span class="help-key">Home / gg</span><span class="help-desc">Go to first row</span></div>
<div class="help-row"><span class="help-key">End / G</span><span class="help-desc">Go to last row</span></div>
<div class="help-row"><span class="help-key">PageUp</span><span class="help-desc">Page up</span></div>
<div class="help-row"><span class="help-key">PageDown</span><span class="help-desc">Page down</span></div>
<div class="help-row"><span class="help-key">gh</span><span class="help-desc">Go to first column</span></div>
<div class="help-row"><span class="help-key">gl</span><span class="help-desc">Go to last column</span></div>
<h3>Row Selection</h3>
<div class="help-row"><span class="help-key">s</span><span class="help-desc">Select current row</span></div>
<div class="help-row"><span class="help-key">u</span><span class="help-desc">Unselect current row</span></div>
<div class="help-row"><span class="help-key">t</span><span class="help-desc">Toggle current row selection</span></div>
<div class="help-row"><span class="help-key">gs</span><span class="help-desc">Select all rows</span></div>
<div class="help-row"><span class="help-key">gu</span><span class="help-desc">Unselect all rows</span></div>
<div class="help-row"><span class="help-key">gt</span><span class="help-desc">Toggle all rows</span></div>
<div class="help-row"><span class="help-key">| + regex</span><span class="help-desc">Select rows matching regex (cur col)</span></div>
<div class="help-row"><span class="help-key">\ + regex</span><span class="help-desc">Unselect rows matching regex</span></div>
<div class="help-row"><span class="help-key">,</span><span class="help-desc">Select rows matching current cell</span></div>
<h3>Sorting & Filtering</h3>
<div class="help-row"><span class="help-key">[</span><span class="help-desc">Sort ascending by current column</span></div>
<div class="help-row"><span class="help-key">]</span><span class="help-desc">Sort descending by current column</span></div>
<div class="help-row"><span class="help-key">"</span><span class="help-desc">Open duplicate sheet with selected rows</span></div>
<div class="help-row"><span class="help-key">/ + regex</span><span class="help-desc">Search forward in current column</span></div>
<div class="help-row"><span class="help-key">n</span><span class="help-desc">Go to next search match</span></div>
<div class="help-row"><span class="help-key">N</span><span class="help-desc">Go to previous search match</span></div>
<h3>Columns</h3>
<div class="help-row"><span class="help-key">- (minus)</span><span class="help-desc">Hide current column</span></div>
<div class="help-row"><span class="help-key">_ (underscore)</span><span class="help-desc">Auto-resize current column</span></div>
<div class="help-row"><span class="help-key">g_</span><span class="help-desc">Auto-resize all columns</span></div>
<div class="help-row"><span class="help-key">^</span><span class="help-desc">Rename current column</span></div>
<div class="help-row"><span class="help-key">!</span><span class="help-desc">Toggle column as key column</span></div>
<div class="help-row"><span class="help-key">H</span><span class="help-desc">Move column left</span></div>
<div class="help-row"><span class="help-key">L</span><span class="help-desc">Move column right</span></div>
<div class="help-row"><span class="help-key">~</span><span class="help-desc">Set column type: text</span></div>
<div class="help-row"><span class="help-key">#</span><span class="help-desc">Set column type: integer</span></div>
<div class="help-row"><span class="help-key">%</span><span class="help-desc">Set column type: float</span></div>
<div class="help-row"><span class="help-key">@</span><span class="help-desc">Set column type: date</span></div>
<h3>Editing</h3>
<div class="help-row"><span class="help-key">e</span><span class="help-desc">Edit current cell</span></div>
<div class="help-row"><span class="help-key">a</span><span class="help-desc">Add new row</span></div>
<div class="help-row"><span class="help-key">d</span><span class="help-desc">Delete current row</span></div>
<div class="help-row"><span class="help-key">gd</span><span class="help-desc">Delete all selected rows</span></div>
<div class="help-row"><span class="help-key">J</span><span class="help-desc">Move current row down</span></div>
<div class="help-row"><span class="help-key">K</span><span class="help-desc">Move current row up</span></div>
<h3>Sheets & Analysis</h3>
<div class="help-row"><span class="help-key">F</span><span class="help-desc">Frequency table for current column</span></div>
<div class="help-row"><span class="help-key">I</span><span class="help-desc">Describe sheet (column stats)</span></div>
<div class="help-row"><span class="help-key">C</span><span class="help-desc">Columns sheet</span></div>
<div class="help-row"><span class="help-key">S</span><span class="help-desc">Sheets sheet (list all sheets)</span></div>
<div class="help-row"><span class="help-key">q</span><span class="help-desc">Close current sheet</span></div>
<div class="help-row"><span class="help-key">Ctrl+S</span><span class="help-desc">Save/download as TSV</span></div>
<div class="help-row"><span class="help-key">= + expr</span><span class="help-desc">New column from Python expression</span></div>
<h3>Other</h3>
<div class="help-row"><span class="help-key">?</span><span class="help-desc">Toggle this help screen</span></div>
<div class="help-row"><span class="help-key">Ctrl+G</span><span class="help-desc">Show sheet info</span></div>
<div class="help-row"><span class="help-key">Ctrl+R</span><span class="help-desc">Reload data from URL</span></div>
<p style="text-align:center;color:var(--text-secondary);margin-top:16px;font-size:12px;">Press ? or Escape to close</p>
</div>
</div>
<div id="loading-overlay"><span>Loading…</span></div>
<div id="context-menu">
<div class="ctx-item" data-action="sort-asc">Sort Ascending<span class="shortcut">[</span></div>
<div class="ctx-item" data-action="sort-desc">Sort Descending<span class="shortcut">]</span></div>
<div class="ctx-item" data-action="freq">Frequency Table<span class="shortcut">F</span></div>
<div class="ctx-item" data-action="describe">Describe Sheet<span class="shortcut">I</span></div>
<div class="ctx-item" data-action="key-col">Toggle Key Column<span class="shortcut">!</span></div>
<div class="ctx-item" data-action="rename-col">Rename Column<span class="shortcut">^</span></div>
<div class="ctx-item" data-action="hide-col">Hide Column<span class="shortcut">-</span></div>
<div class="ctx-item" data-action="resize-col">Auto-resize Column<span class="shortcut">_</span></div>
<div class="ctx-item" data-action="resize-all">Auto-resize All<span class="shortcut">g_</span></div>
</div>
<script>
(function() {
"use strict";
// ── State ──
const S = {
sheets: [],
si: -1,
gPending: false,
lastSearch: '',
lastSearchCol: -1,
commandMode: null,
commandResolve: null,
editingCell: false,
url: ''
};
function curSheet() { return S.sheets[S.si]; }
function makeSheet(name, cols, rows) {
return {
name,
columns: cols.map(c => ({
name: c,
width: Math.max(80, Math.min(200, c.length * 9 + 20)),
type: '~',
key: false,
hidden: false
})),
rows: rows.map(r => ({ data: r, selected: false })),
curRow: 0,
curCol: 0,
scrollTop: 0
};
}
// ── Sample Data ──
const SAMPLE_TSV = `Name\tAge\tCity\tSalary\tDepartment\tStart_Date\tRating
Alice Johnson\t32\tNew York\t85000\tEngineering\t2019-03-15\t4.5
Bob Smith\t45\tSan Francisco\t120000\tManagement\t2015-07-01\t4.8
Carol Williams\t28\tChicago\t72000\tMarketing\t2021-01-10\t3.9
David Brown\t38\tAustin\t95000\tEngineering\t2018-06-22\t4.2
Eva Martinez\t51\tSeattle\t135000\tManagement\t2012-11-30\t4.9
Frank Lee\t26\tDenver\t65000\tDesign\t2022-04-18\t3.7
Grace Kim\t33\tBoston\t88000\tEngineering\t2020-08-05\t4.1
Henry Wilson\t42\tPortland\t98000\tMarketing\t2016-09-14\t4.4
Iris Chen\t29\tNew York\t78000\tDesign\t2021-07-25\t4.0
Jack Thompson\t36\tSan Francisco\t110000\tEngineering\t2017-02-28\t4.6
Karen Davis\t48\tChicago\t125000\tManagement\t2013-05-12\t4.7
Leo Garcia\t31\tAustin\t82000\tMarketing\t2020-03-01\t3.8
Mia Robinson\t27\tSeattle\t70000\tDesign\t2022-09-15\t3.6
Nathan Clark\t40\tDenver\t102000\tEngineering\t2016-01-20\t4.3
Olivia Wright\t34\tBoston\t91000\tMarketing\t2019-11-08\t4.1`;
// ── DOM refs ──
const $ = id => document.getElementById(id);
const colHeaderRow = $('col-header-row');
const dataTable = $('data-table');
const dataViewport = $('data-viewport');
const statusMsg = $('status-message');
const urlInput = $('url-input');
const helpOverlay = $('help-overlay');
const commandBar = $('command-bar');
const commandInput = $('command-input');
const commandPrompt = $('command-prompt');
const contextMenu = $('context-menu');
const loadingOverlay = $('loading-overlay');
// ── Parsing ──
function parseTSV(text, name) {
const lines = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n').split('\n').filter(l => l.trim());
if (lines.length === 0) return null;
const headers = lines[0].split('\t');
const rows = lines.slice(1).map(l => {
const cells = l.split('\t');
while (cells.length < headers.length) cells.push('');
return cells;
});
return makeSheet(name || 'sheet', headers, rows);
}
// ── Load TSV ──
function loadTSVFromURL(url) {
S.url = url;
urlInput.value = url;
showLoading(true);
setStatus('Loading…');
fetch(url)
.then(r => { if (!r.ok) throw new Error(`HTTP ${r.status}`); return r.text(); })
.then(text => {
const name = url.split('/').pop().replace(/\.\w+$/, '') || 'sheet';
const sheet = parseTSV(text, name);
if (!sheet) throw new Error('Empty or invalid TSV');
pushSheet(sheet);
setStatus(`Loaded ${sheet.rows.length} rows × ${sheet.columns.length} cols from URL.`);
})
.catch(e => { setStatus('Error: ' + e.message, true); })
.finally(() => showLoading(false));
}
function loadSampleData() {
const sheet = parseTSV(SAMPLE_TSV, 'sample');
pushSheet(sheet);
setStatus(`Loaded sample data: ${sheet.rows.length} rows × ${sheet.columns.length} cols.`);
}
function pushSheet(sheet) {
S.sheets.push(sheet);
S.si = S.sheets.length - 1;
render();
}
function popSheet() {
if (S.sheets.length > 1) {
S.sheets.splice(S.si, 1);
S.si = Math.min(S.si, S.sheets.length - 1);
render();
setStatus('Closed sheet. Returned to ' + curSheet().name);
} else {
setStatus('Only one sheet open.', true);
}
}
// ── Rendering ──
function render() {
const sh = curSheet();
if (!sh) { dataTable.innerHTML = ''; updateStatus(); return; }
renderHeaders(sh);
renderRows(sh);
updateStatus();
scrollCursorIntoView(sh);
}
function renderHeaders(sh) {
colHeaderRow.innerHTML = '<div class="col-header" id="row-number-col">#</div>';
sh.columns.forEach((col, ci) => {
if (col.hidden) return;
const div = document.createElement('div');
div.className = 'col-header' + (col.key ? ' key-col' : '') + (ci === sh.curCol ? ' cursor-col' : '');
div.style.width = col.width + 'px';
div.style.minWidth = col.width + 'px';
div.style.maxWidth = col.width + 'px';
let html = '';
if (col.key) html += '🔑 ';
html += escHtml(col.name);
if (col.sortDir) html += ` <span class="sort-indicator">${col.sortDir === 'asc' ? '▲' : '▼'}</span>`;
html += ` <span class="type-indicator">${col.type}</span>`;
html += '<div class="resize-handle"></div>';
div.innerHTML = html;
div.dataset.col = ci;
// Resize handle
const handle = div.querySelector('.resize-handle');
handle.addEventListener('mousedown', e => startResize(e, ci));
// Context menu on right-click
div.addEventListener('contextmenu', e => {
e.preventDefault();
sh.curCol = ci;
showContextMenu(e.clientX, e.clientY);
render();
});
// Click to move cursor
div.addEventListener('click', () => { sh.curCol = ci; render(); });
colHeaderRow.appendChild(div);
});
}
function renderRows(sh) {
dataTable.innerHTML = '';
const visibleCols = sh.columns.map((c, i) => ({ ...c, idx: i })).filter(c => !c.hidden);
sh.rows.forEach((row, ri) => {
const div = document.createElement('div');
div.className = 'data-row' + (row.selected ? ' selected' : '') + (ri === sh.curRow ? ' cursor-row' : '');
// Row number
let rn = `<div class="row-num"><span class="row-marker"></span>${ri + 1}</div>`;
div.innerHTML = rn;
visibleCols.forEach(vc => {
const cell = document.createElement('div');
const isCursor = (ri === sh.curRow && vc.idx === sh.curCol);
cell.className = 'data-cell'
+ (isCursor ? ' cursor-cell' : '')
+ (vc.idx === sh.curCol && !isCursor ? ' cursor-col' : '')
+ (vc.type === '#' ? ' type-int' : '')
+ (vc.type === '%' ? ' type-float' : '')
+ (vc.type === '@' ? ' type-date' : '');
cell.style.width = vc.width + 'px';
cell.style.minWidth = vc.width + 'px';
cell.style.maxWidth = vc.width + 'px';
cell.textContent = row.data[vc.idx] ?? '';
cell.dataset.row = ri;
cell.dataset.col = vc.idx;
cell.addEventListener('click', () => {
sh.curRow = ri;
sh.curCol = vc.idx;
render();
});
cell.addEventListener('dblclick', () => {
sh.curRow = ri;
sh.curCol = vc.idx;
startEdit();
});
div.appendChild(cell);
});
dataTable.appendChild(div);
});
}
function scrollCursorIntoView(sh) {
const row = dataTable.children[sh.curRow];
if (row) row.scrollIntoView({ block: 'nearest' });
}
function updateStatus() {
const sh = curSheet();
if (!sh) return;
$('st-sheet').textContent = sh.name;
$('st-rows').textContent = sh.rows.length;
$('st-cols').textContent = sh.columns.filter(c => !c.hidden).length;
$('st-cursor').textContent = `${sh.curRow},${sh.curCol}`;
$('st-selected').textContent = sh.rows.filter(r => r.selected).length;
const val = sh.rows[sh.curRow]?.data[sh.curCol] ?? '';
$('st-cell').textContent = val.length > 30 ? val.slice(0, 30) + '…' : val;
$('st-type').textContent = sh.columns[sh.curCol]?.type || '~';
}
function setStatus(msg, isError) {
statusMsg.textContent = msg;
statusMsg.className = isError ? 'error' : '';
}
function showLoading(on) {
loadingOverlay.className = on ? 'active' : '';
}
function escHtml(s) {
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
// ── Column resize ──
let resizeState = null;
function startResize(e, ci) {
e.preventDefault();
e.stopPropagation();
resizeState = { ci, startX: e.clientX, startW: curSheet().columns[ci].width };
document.addEventListener('mousemove', onResizeMove);
document.addEventListener('mouseup', onResizeEnd);
}
function onResizeMove(e) {
if (!resizeState) return;
const diff = e.clientX - resizeState.startX;
curSheet().columns[resizeState.ci].width = Math.max(40, resizeState.startW + diff);
render();
}
function onResizeEnd() {
resizeState = null;
document.removeEventListener('mousemove', onResizeMove);
document.removeEventListener('mouseup', onResizeEnd);
}
// ── Context menu ──
function showContextMenu(x, y) {
contextMenu.style.left = x + 'px';
contextMenu.style.top = y + 'px';
contextMenu.className = 'active';
}
function hideContextMenu() { contextMenu.className = ''; }
document.addEventListener('click', hideContextMenu);
contextMenu.querySelectorAll('.ctx-item').forEach(item => {
item.addEventListener('click', e => {
e.stopPropagation();
const action = item.dataset.action;
hideContextMenu();
const sh = curSheet();
if (!sh) return;
switch (action) {
case 'sort-asc': sortColumn(sh, sh.curCol, 'asc'); break;
case 'sort-desc': sortColumn(sh, sh.curCol, 'desc'); break;
case 'freq': frequencyTable(sh); break;
case 'describe': describeSheet(sh); break;
case 'key-col': sh.columns[sh.curCol].key = !sh.columns[sh.curCol].key; render(); break;
case 'rename-col': openCommand('Rename column to:', val => { sh.columns[sh.curCol].name = val; render(); }); break;
case 'hide-col': sh.columns[sh.curCol].hidden = true; if (sh.curCol > 0) sh.curCol--; render(); break;
case 'resize-col': autoResize(sh, sh.curCol); break;
case 'resize-all': sh.columns.forEach((_, i) => autoResize(sh, i)); break;
}
});
});
// ── Command input ──
function openCommand(prompt, callback) {
S.commandMode = true;
S.commandResolve = callback;
commandPrompt.textContent = prompt;
commandBar.className = 'active';
commandInput.value = '';
commandInput.focus();
}
function closeCommand(submit) {
if (!S.commandMode) return;
const val = commandInput.value;
S.commandMode = false;
commandBar.className = '';
if (submit && S.commandResolve) S.commandResolve(val);
S.commandResolve = null;
document.body.focus();
}
commandInput.addEventListener('keydown', e => {
if (e.key === 'Enter') { e.preventDefault(); closeCommand(true); }
else if (e.key === 'Escape') { e.preventDefault(); closeCommand(false); }
e.stopPropagation();
});
// ── Cell editing ──
function startEdit() {
const sh = curSheet();
if (!sh || S.editingCell) return;
S.editingCell = true;
const ri = sh.curRow, ci = sh.curCol;
const cell = dataTable.querySelector(`.data-cell[data-row="${ri}"][data-col="${ci}"]`);
if (!cell) return;
const val = sh.rows[ri].data[ci] ?? '';
cell.classList.add('editing');
cell.innerHTML = `<input type="text" value="${escHtml(val)}" />`;
const inp = cell.querySelector('input');
inp.focus();
inp.select();
inp.addEventListener('keydown', e => {
if (e.key === 'Enter') { sh.rows[ri].data[ci] = inp.value; S.editingCell = false; render(); }
else if (e.key === 'Escape') { S.editingCell = false; render(); }
e.stopPropagation();
});
inp.addEventListener('blur', () => {
if (S.editingCell) { sh.rows[ri].data[ci] = inp.value; S.editingCell = false; render(); }
});
}
// ── Sort ──
function sortColumn(sh, ci, dir) {
sh.columns.forEach(c => c.sortDir = null);
sh.columns[ci].sortDir = dir;
const type = sh.columns[ci].type;
sh.rows.sort((a, b) => {
let va = a.data[ci] ?? '', vb = b.data[ci] ?? '';
if (type === '#') { va = parseInt(va) || 0; vb = parseInt(vb) || 0; }
else if (type === '%') { va = parseFloat(va) || 0; vb = parseFloat(vb) || 0; }
if (va < vb) return dir === 'asc' ? -1 : 1;
if (va > vb) return dir === 'asc' ? 1 : -1;
return 0;
});
sh.curRow = 0;
render();
setStatus(`Sorted by ${sh.columns[ci].name} (${dir}).`);
}
// ── Auto-resize ──
function autoResize(sh, ci) {
const header = sh.columns[ci].name.length * 9 + 30;
let maxData = 0;
sh.rows.forEach(r => {
maxData = Math.max(maxData, (r.data[ci] ?? '').length * 8 + 16);
});
sh.columns[ci].width = Math.max(60, Math.min(400, Math.max(header, maxData)));
render();
}
// ── Frequency table ──
function frequencyTable(sh) {
const ci = sh.curCol;
const colName = sh.columns[ci].name;
const counts = {};
sh.rows.forEach(r => {
const val = r.data[ci] ?? '';
counts[val] = (counts[val] || 0) + 1;
});
const entries = Object.entries(counts).sort((a, b) => b[1] - a[1]);
const cols = [colName, 'count', 'percent', 'histogram'];
const total = sh.rows.length;
const maxCount = entries.length ? entries[0][1] : 1;
const rows = entries.map(([val, cnt]) => {
const pct = (cnt / total * 100).toFixed(1) + '%';
const bar = '█'.repeat(Math.round(cnt / maxCount * 20));
return [val, String(cnt), pct, bar];
});
const freqSheet = makeSheet(`${colName}_freq`, cols, rows);
pushSheet(freqSheet);
setStatus(`Frequency table for "${colName}": ${entries.length} unique values.`);
}
// ── Describe sheet ──
function describeSheet(sh) {
const cols = ['column', 'type', 'nulls', 'unique', 'min', 'max', 'mean', 'mode'];
const rows = sh.columns.filter(c => !c.hidden).map(col => {
const ci = sh.columns.indexOf(col);
const vals = sh.rows.map(r => r.data[ci] ?? '');
const nonEmpty = vals.filter(v => v !== '');
const nulls = vals.length - nonEmpty.length;
const unique = new Set(nonEmpty).size;
const nums = nonEmpty.map(Number).filter(n => !isNaN(n));
const min = nums.length ? String(Math.min(...nums)) : nonEmpty.sort()[0] || '';
const max = nums.length ? String(Math.max(...nums)) : nonEmpty.sort()[nonEmpty.length - 1] || '';
const mean = nums.length ? (nums.reduce((a, b) => a + b, 0) / nums.length).toFixed(2) : '';
const freq = {};
nonEmpty.forEach(v => freq[v] = (freq[v] || 0) + 1);
const mode = Object.entries(freq).sort((a, b) => b[1] - a[1])[0]?.[0] ?? '';
return [col.name, col.type, String(nulls), String(unique), min, max, mean, mode];
});
const dSheet = makeSheet(sh.name + '_describe', cols, rows);
pushSheet(dSheet);
setStatus('Describe sheet created.');
}
// ── Columns sheet ──
function columnsSheet(sh) {
const cols = ['name', 'width', 'type', 'key', 'hidden'];
const rows = sh.columns.map(c => [c.name, String(c.width), c.type, c.key ? 'yes' : '', c.hidden ? 'yes' : '']);
pushSheet(makeSheet(sh.name + '_columns', cols, rows));
setStatus('Columns sheet created.');
}
// ── Sheets sheet ──
function sheetsSheet() {
const cols = ['index', 'name', 'rows', 'cols'];
const rows = S.sheets.map((s, i) => [String(i), s.name, String(s.rows.length), String(s.columns.length)]);
pushSheet(makeSheet('sheets', cols, rows));
setStatus('Sheets sheet created.');
}
// ── Save/download ──
function downloadTSV() {
const sh = curSheet();
if (!sh) return;
const visibleCols = sh.columns.map((c, i) => ({ ...c, idx: i })).filter(c => !c.hidden);
let tsv = visibleCols.map(c => c.name).join('\t') + '\n';
sh.rows.forEach(r => {
tsv += visibleCols.map(c => r.data[c.idx] ?? '').join('\t') + '\n';
});
const blob = new Blob([tsv], { type: 'text/tab-separated-values' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = (sh.name || 'sheet') + '.tsv';
a.click();
URL.revokeObjectURL(a.href);
setStatus('Downloaded ' + a.download);
}
// ── Expression column ──
function addExprColumn(expr) {
const sh = curSheet();
if (!sh) return;
sh.columns.push({ name: '=' + expr, width: 120, type: '~', key: false, hidden: false });
const ci = sh.columns.length - 1;
sh.rows.forEach(row => {
try {
const scope = {};
sh.columns.forEach((c, i) => { if (i < ci) scope[c.name] = isNaN(row.data[i]) ? row.data[i] : Number(row.data[i]); });
const fn = new Function(...Object.keys(scope), `return (${expr});`);
row.data[ci] = String(fn(...Object.values(scope)));
} catch (e) {
row.data[ci] = '!ERR';
}