-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.html
More file actions
1119 lines (1010 loc) · 43.5 KB
/
Copy patheditor.html
File metadata and controls
1119 lines (1010 loc) · 43.5 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Chord Editor</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link
href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700&family=Roboto+Mono:wght@400;700&display=swap"
rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box
}
:root {
--font: 'Roboto Mono', monospace;
--serif: 'Playfair Display', serif;
--accent: #b5451b;
--bg: #e8e4dc;
--card: #fff;
--dark: #1a1a1a;
--muted: #888;
--border: #ddd;
--char-w: 8.4px;
}
body {
font-family: var(--font);
background: var(--bg);
min-height: 100vh;
padding: 24px 24px 140px;
color: var(--dark)
}
.container {
max-width: 680px;
margin: 0 auto
}
/* ── TOP NAV ── */
.topnav {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 28px
}
.topnav a {
font-size: .68em;
letter-spacing: .1em;
text-transform: uppercase;
color: var(--muted);
text-decoration: none
}
.topnav a:hover {
color: var(--dark)
}
h1 {
font-family: var(--serif);
font-size: 2em;
color: var(--dark);
line-height: 1.1
}
.subtitle {
font-size: .68em;
letter-spacing: .12em;
text-transform: uppercase;
color: var(--muted);
margin-top: 4px
}
/* ── PASTE SCREEN ── */
#paste-screen {
margin-top: 20px
}
.card {
background: var(--card);
border-radius: 4px;
box-shadow: 0 1px 6px rgba(0, 0, 0, .08);
padding: 22px
}
.card-title {
font-size: .68em;
letter-spacing: .12em;
text-transform: uppercase;
color: var(--muted);
margin-bottom: 14px
}
textarea {
width: 100%;
height: 200px;
font-family: var(--font);
font-size: 10px;
border: 1px solid var(--border);
border-radius: 3px;
padding: 10px;
resize: vertical;
outline: none;
background: #faf9f6;
color: #444;
margin-bottom: 10px;
}
textarea:focus {
border-color: var(--accent)
}
#paste-error {
font-size: 11px;
color: #c33;
margin-bottom: 8px;
display: none
}
.btn {
font-family: var(--font);
border: none;
border-radius: 3px;
cursor: pointer;
transition: background .15s;
}
.btn-primary {
background: var(--dark);
color: #fff;
padding: 10px 18px;
font-size: .75em;
letter-spacing: .08em;
text-transform: uppercase
}
.btn-primary:hover {
background: #333
}
.btn-secondary {
background: #f0ede6;
color: var(--muted);
padding: 8px 14px;
font-size: .72em;
letter-spacing: .08em;
text-transform: uppercase
}
.btn-secondary:hover {
background: #e8e4dc;
color: var(--dark)
}
.btn-github {
background: #24292f;
color: #fff;
padding: 10px 18px;
font-size: .75em;
letter-spacing: .08em;
text-transform: uppercase
}
.btn-github:hover {
background: #333
}
.btn-github:disabled {
background: var(--muted);
cursor: default
}
.divider {
display: flex;
align-items: center;
gap: 12px;
margin: 14px 0;
font-size: .68em;
color: var(--muted)
}
.divider::before,
.divider::after {
content: '';
flex: 1;
height: 1px;
background: var(--border)
}
#url-row {
display: flex;
gap: 8px
}
#url-input {
flex: 1;
font-family: var(--font);
font-size: .75em;
border: 1px solid var(--border);
border-radius: 3px;
padding: 8px 10px;
outline: none;
background: #faf9f6;
}
#url-input:focus {
border-color: var(--accent)
}
/* ── EDITOR SCREEN ── */
#editor-screen {
display: none;
margin-top: 20px
}
.editor-toolbar {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
gap: 8px;
flex-wrap: wrap;
}
.editor-toolbar .song-title {
font-family: var(--serif);
font-size: 1.3em
}
.editor-toolbar .hint {
font-size: .62em;
letter-spacing: .06em;
color: var(--muted);
margin-top: 2px
}
.toolbar-btns {
display: flex;
gap: 8px;
flex-shrink: 0
}
/* Meta editor */
#meta-editor {
width: 100%;
font-family: var(--font);
font-size: .72em;
border: 1px solid var(--border);
border-radius: 3px;
padding: 8px 10px;
resize: none;
outline: none;
background: #faf9f6;
color: #444;
margin-bottom: 8px;
line-height: 1.6;
overflow: hidden;
}
#meta-editor:focus {
border-color: var(--accent)
}
/* Song sheet */
#song-sheet {
background: var(--card);
border-radius: 4px;
box-shadow: 0 1px 6px rgba(0, 0, 0, .08);
padding: 16px 18px;
overflow-x: auto;
margin-bottom: 8px;
}
hr.section-divider {
border: none;
border-top: 1px dashed #ddd;
margin: 6px 0
}
.section-label {
font-size: .6em;
font-weight: 700;
letter-spacing: .14em;
text-transform: uppercase;
color: #bbb;
margin-bottom: 3px;
}
.chord-line {
position: relative;
height: 20px;
white-space: pre;
font-family: var(--font);
font-size: 14px;
margin-bottom: 1px;
}
.lyric-line {
font-family: var(--font);
font-size: 14px;
color: var(--dark);
white-space: pre;
line-height: 1.3;
margin-bottom: 3px;
}
.chords-only-line {
font-family: var(--font);
font-size: 14px;
color: #7a7a7a;
font-weight: 700;
white-space: pre;
line-height: 1.4;
}
.chord-token {
position: absolute;
top: 0;
font-weight: 700;
color: #7a7a7a;
border-radius: 3px;
padding: 0 2px;
cursor: grab;
user-select: none;
line-height: 1.3;
white-space: nowrap;
touch-action: none;
transition: color .1s, background .1s, outline .1s;
}
.chord-token.selected {
color: var(--accent);
background: #fff3ee;
outline: 2px solid var(--accent)
}
/* ── NUDGE BAR ── */
#nudge-bar {
display: none;
position: fixed;
bottom: 24px;
left: 50%;
transform: translateX(-50%);
background: var(--dark);
border-radius: 12px;
padding: 10px 14px;
align-items: center;
gap: 8px;
box-shadow: 0 6px 30px rgba(0, 0, 0, .4);
z-index: 100;
border: 1px solid #333;
}
#nudge-bar.visible {
display: flex
}
#nudge-label {
color: #e8a080;
font-family: var(--font);
font-size: 14px;
font-weight: 700;
min-width: 44px;
text-align: center;
padding: 0 4px
}
.nudge-sep {
width: 1px;
height: 28px;
background: #444;
margin: 0 2px
}
.nudge-btn {
background: #2a2a2a;
border: none;
border-radius: 6px;
color: #fff;
width: 40px;
height: 40px;
font-size: 18px;
cursor: pointer;
font-family: var(--font);
display: flex;
align-items: center;
justify-content: center;
-webkit-tap-highlight-color: transparent;
flex-shrink: 0;
}
#nudge-close {
background: #3a3a3a;
border: none;
border-radius: 6px;
color: #ccc;
width: 40px;
height: 40px;
font-size: 13px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
-webkit-tap-highlight-color: transparent;
}
/* ── MODAL ── */
.modal-overlay {
display: none;
position: fixed;
inset: 0;
background: rgba(0, 0, 0, .5);
align-items: center;
justify-content: center;
z-index: 200;
padding: 16px;
}
.modal-overlay.open {
display: flex
}
.modal-box {
background: var(--card);
border-radius: 4px;
padding: 24px;
width: 100%;
max-width: 380px;
font-family: var(--font);
box-shadow: 0 8px 40px rgba(0, 0, 0, .25);
}
.modal-title {
font-family: var(--serif);
font-size: 1.2em;
margin-bottom: 16px
}
.modal-field {
margin-bottom: 12px
}
.modal-field label {
display: block;
font-size: .65em;
letter-spacing: .1em;
text-transform: uppercase;
color: var(--muted);
margin-bottom: 4px
}
.modal-field input {
width: 100%;
font-family: var(--font);
font-size: .8em;
border: 1px solid var(--border);
border-radius: 3px;
padding: 8px 10px;
outline: none;
background: #faf9f6;
}
.modal-field input:focus {
border-color: var(--accent)
}
.modal-note {
font-size: .62em;
color: #aaa;
margin-bottom: 16px;
line-height: 1.7
}
.modal-btns {
display: flex;
gap: 8px
}
.modal-btns .btn {
flex: 1;
padding: 10px 0;
text-align: center
}
.modal-status {
margin-top: 12px;
padding: 10px 12px;
border-radius: 3px;
font-size: .72em;
line-height: 1.6
}
.modal-status.ok {
background: #e8f5e9;
color: #2e7d32
}
.modal-status.err {
background: #fdecea;
color: #c62828
}
.modal-status a {
color: inherit
}
</style>
</head>
<body>
<div class="container">
<!-- Top nav -->
<div class="topnav">
<a href="index.html">← All songs</a>
</div>
<h1>Chord Editor</h1>
<div class="subtitle">Paste ChordTxt · drag · nudge · save to GitHub</div>
<!-- ══ PASTE SCREEN ══ -->
<div id="paste-screen">
<div class="card">
<div class="card-title">Load chord sheet — ChordTxt or HTML</div>
<textarea id="html-input"
placeholder="title: Song Title artist: Artist Name key: G [Verse 1] G D Mama, take this badge off of me …or paste a sheet HTML generated by this tool"
spellcheck="false"></textarea>
<div id="paste-error">Could not parse input.</div>
<button class="btn btn-primary" id="load-btn">Load →</button>
</div>
<div class="divider">or fetch from URL</div>
<div class="card">
<div class="card-title">Load from URL (.txt or .html)</div>
<div id="url-row">
<input id="url-input" type="text"
placeholder="https://raw.githubusercontent.com/aubort/chords/main/ukulele/src/song.txt">
<button class="btn btn-secondary" id="url-btn">Fetch</button>
</div>
</div>
</div>
<!-- ══ EDITOR SCREEN ══ -->
<div id="editor-screen">
<div class="editor-toolbar">
<div>
<div class="song-title" id="editor-song-title">—</div>
<div class="hint">Drag chord left/right · tap to select · ‹ › to nudge · Esc to deselect</div>
</div>
<div class="toolbar-btns">
<button class="btn btn-secondary" id="dl-btn">↓ HTML</button>
<button class="btn btn-github" id="save-gh-btn">↑ GitHub</button>
<button class="btn btn-secondary" id="reset-btn">← Back</button>
</div>
</div>
<textarea id="meta-editor" spellcheck="false"></textarea>
<div id="song-sheet"></div>
</div>
</div><!-- /container -->
<!-- ══ NUDGE BAR ══ -->
<div id="nudge-bar">
<button class="nudge-btn" data-delta="-5">«</button>
<button class="nudge-btn" data-delta="-1">‹</button>
<div id="nudge-label">—</div>
<button class="nudge-btn" data-delta="1">›</button>
<button class="nudge-btn" data-delta="5">»</button>
<div class="nudge-sep"></div>
<button id="nudge-close">✕</button>
</div>
<!-- ══ GITHUB MODAL ══ -->
<div id="gh-modal" class="modal-overlay">
<div class="modal-box">
<div class="modal-title">Save to GitHub</div>
<div class="modal-field">
<label>Repo</label>
<input id="gh-repo" type="text" value="aubort/chords">
</div>
<div class="modal-field">
<label>HTML file path</label>
<input id="gh-path" type="text" placeholder="ukulele/song-title.html">
</div>
<div class="modal-field">
<label>Personal Access Token</label>
<input id="gh-token" type="password" placeholder="ghp_…">
</div>
<div class="modal-field" style="margin-bottom:6px">
<label
style="display:flex;align-items:center;gap:8px;cursor:pointer;text-transform:none;letter-spacing:0;font-size:.72em;color:#666">
<input type="checkbox" id="gh-remember"> Remember token on this device
</label>
</div>
<div class="modal-note">
Pushes <strong>two files</strong>: the rendered HTML and its ChordTxt source under <strong>src/</strong> · token
needs <strong>repo</strong> scope · stored only in your browser's localStorage
</div>
<div class="modal-btns">
<button class="btn btn-secondary" id="gh-cancel">Cancel</button>
<button class="btn btn-github" id="gh-push">Push</button>
</div>
<div id="gh-status" class="modal-status" style="display:none"></div>
</div>
</div>
<script>
const CHAR_W = 8.4;
const CHORD_RE = /^[A-G][#b]?(m|maj|min|dim|aug|sus|add)?[0-9]*(\/[A-G][#b]?)?$/;
const BAR_RE = /^(\||x\d+|%|N\.C\.)$/i;
/* ── CHORD LIBRARY (GCEA) ── */
const UKE_CHORDS = {
"A": [2, 1, 0, 0], "A#": [3, 2, 1, 1], "Bb": [3, 2, 1, 1], "B": [4, 3, 2, 2], "C": [0, 0, 0, 3],
"C#": [1, 1, 1, 4], "Db": [1, 1, 1, 4], "D": [2, 2, 2, 0], "D#": [0, 3, 3, 1], "Eb": [0, 3, 3, 1],
"E": [4, 4, 4, 2], "F": [2, 0, 1, 0], "F#": [3, 1, 2, 1], "Gb": [3, 1, 2, 1], "G": [0, 2, 3, 2],
"G#": [5, 3, 4, 3], "Ab": [5, 3, 4, 3],
"Am": [2, 0, 0, 0], "A#m": [3, 1, 1, 1], "Bbm": [3, 1, 1, 1], "Bm": [4, 2, 2, 2], "Cm": [0, 3, 3, 3],
"C#m": [1, 1, 0, 4], "Dbm": [1, 1, 0, 4], "Dm": [2, 2, 1, 0], "D#m": [3, 3, 2, 1], "Ebm": [3, 3, 2, 1],
"Em": [0, 4, 3, 2], "Fm": [1, 0, 1, 3], "F#m": [2, 1, 2, 0], "Gbm": [2, 1, 2, 0], "Gm": [0, 2, 3, 1],
"G#m": [1, 3, 4, 2], "Abm": [1, 3, 4, 2],
"A7": [0, 1, 0, 0], "A#7": [1, 2, 1, 1], "Bb7": [1, 2, 1, 1], "B7": [2, 3, 2, 2], "C7": [0, 0, 0, 1],
"C#7": [1, 1, 1, 2], "Db7": [1, 1, 1, 2], "D7": [2, 2, 2, 3], "D#7": [3, 3, 3, 4], "Eb7": [3, 3, 3, 4],
"E7": [1, 2, 0, 2], "F7": [2, 3, 1, 3], "F#7": [3, 4, 2, 4], "Gb7": [3, 4, 2, 4], "G7": [0, 2, 1, 2],
"G#7": [1, 3, 2, 3], "Ab7": [1, 3, 2, 3],
"Am7": [0, 0, 0, 0], "Bm7": [2, 2, 2, 2], "Cm7": [3, 3, 3, 3], "C#m7": [4, 4, 4, 4], "Dm7": [2, 2, 1, 3],
"Em7": [0, 2, 0, 2], "Fm7": [1, 3, 1, 3], "F#m7": [2, 4, 2, 4], "Gm7": [0, 2, 1, 1], "G#m7": [1, 3, 2, 2],
"Amaj7": [1, 1, 0, 0], "Bbmaj7": [3, 2, 1, 0], "Cmaj7": [0, 0, 0, 2], "Dmaj7": [2, 2, 2, 4],
"Emaj7": [1, 3, 0, 2], "Fmaj7": [2, 4, 1, 3], "Gmaj7": [0, 2, 2, 2],
"Asus4": [2, 2, 0, 0], "Csus4": [0, 0, 1, 3], "Dsus4": [0, 2, 3, 0], "Gsus4": [0, 2, 3, 3],
"Dsus2": [2, 2, 0, 0], "Gsus2": [0, 2, 3, 0],
"Cadd9": [0, 2, 0, 3], "Fadd9": [0, 0, 1, 0], "C6": [0, 0, 0, 0]
};
let song = null, selected = null;
/* ── HELPERS ── */
function esc(s) { return String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); }
function escAttr(s) { return esc(s).replace(/"/g, """); }
function slugify(t) { return String(t).trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, ""); }
function parseChords(str) {
const tokens = [], re = /(\S+)/g; let m;
while ((m = re.exec(str)) !== null) tokens.push({ name: m[1], col: m.index });
return tokens;
}
function buildChordStr(tokens, minLen) {
if (!tokens.length) return "";
const maxCol = Math.max(...tokens.map(t => t.col + t.name.length));
const arr = new Array(Math.max(maxCol, minLen || 0)).fill(" ");
tokens.forEach(t => { for (let i = 0; i < t.name.length; i++) arr[t.col + i] = t.name[i]; });
return arr.join("");
}
/* ── ChordTxt PARSER ── */
function isChordLine(line) {
const toks = line.trim().split(/\s+/).filter(Boolean);
if (!toks.length) return false;
let hasChord = false;
for (const t of toks) {
if (CHORD_RE.test(t)) { hasChord = true; continue; }
if (BAR_RE.test(t)) continue;
return false;
}
return hasChord;
}
function parseChordTxt(text) {
const lines = text.replace(/\r\n?/g, "\n").split("\n");
let i = 0;
const meta = {};
while (i < lines.length) {
const l = lines[i];
if (l.trim() === "" || /^\[/.test(l.trim())) break;
const m = l.match(/^([A-Za-z][A-Za-z0-9_-]*)\s*:\s*(.*)$/);
if (!m) break;
meta[m[1].toLowerCase()] = m[2].trim();
i++;
}
if (!meta.title || !meta.artist) return null;
const sections = [];
let cur = null;
while (i < lines.length) {
const raw = lines[i].replace(/\s+$/, "");
const trimmed = raw.trim();
if (trimmed === "") { i++; continue; }
const sm = trimmed.match(/^\[(.+)\]$/);
if (sm) { cur = { name: sm[1].trim(), lines: [] }; sections.push(cur); i++; continue; }
if (!cur) { cur = { name: "", lines: [] }; sections.push(cur); }
if (isChordLine(raw)) {
const next = lines[i + 1];
const nextTrim = next === undefined ? "" : next.replace(/\s+$/, "").trim();
const nextIsLyric = next !== undefined && nextTrim !== "" && !/^\[.+\]$/.test(nextTrim) && !isChordLine(next);
if (nextIsLyric) {
cur.lines.push({ type: "line", tokens: parseChords(raw), lyrics: next.replace(/\s+$/, "") });
i += 2;
} else {
cur.lines.push({ type: "chords-only", chords: raw, lyrics: "" });
i++;
}
} else {
cur.lines.push({ type: "line", tokens: [], lyrics: raw });
i++;
}
}
return sections.length ? { meta, sections } : null;
}
/* ── HTML PARSER ── */
function parseHtml(html) {
const doc = new DOMParser().parseFromString(html, "text/html");
const sectionEls = doc.querySelectorAll(".section");
if (!sectionEls.length) return null;
const meta = {};
meta.title = doc.querySelector("h1")?.textContent.trim() || "Chord Sheet";
const artistRaw = doc.querySelector(".artist")?.textContent.trim() || "";
meta.artist = artistRaw.split(" · arr. for ")[0].trim();
const sp = doc.querySelector(".streaming a.sp"); if (sp) meta.spotify = sp.getAttribute("href");
const yt = doc.querySelector(".streaming a.yt"); if (yt) meta.ytmusic = yt.getAttribute("href");
doc.querySelectorAll(".meta span").forEach(s => {
const km = s.textContent.match(/^\s*Key:\s*(.+)$/);
if (km) meta.key = km[1].trim();
});
const sections = [];
sectionEls.forEach(secEl => {
const name = secEl.querySelector(".section-label")?.textContent.trim() || "Section";
const lines = [];
secEl.querySelectorAll(".line").forEach(lineEl => {
const co = lineEl.querySelector(".chords-only");
if (co) { lines.push({ type: "chords-only", chords: co.textContent, lyrics: "" }); return; }
const ce = lineEl.querySelector(".chords");
if (ce) lines.push({
type: "line",
tokens: parseChords(ce.textContent),
lyrics: lineEl.querySelector(".lyrics")?.textContent || ""
});
});
if (lines.length) sections.push({ name, lines });
});
return sections.length ? { meta, sections } : null;
}
/* ── ChordTxt SERIALIZER ── */
function serializeChordTxt(s) {
const out = [];
out.push("title: " + s.meta.title);
out.push("artist: " + s.meta.artist);
if (s.meta.spotify) out.push("spotify: " + s.meta.spotify);
if (s.meta.ytmusic) out.push("ytmusic: " + s.meta.ytmusic);
if (s.meta.key) out.push("key: " + s.meta.key);
s.sections.forEach(sec => {
out.push("");
out.push("[" + (sec.name || "Section") + "]");
sec.lines.forEach(line => {
if (line.type === "chords-only") { out.push(line.chords.replace(/\s+$/, "")); return; }
const cs = buildChordStr(line.tokens).replace(/\s+$/, "");
if (cs) out.push(cs);
if (line.lyrics) out.push(line.lyrics.replace(/\s+$/, ""));
});
});
return out.join("\n") + "\n";
}
/* ── CHORD DIAGRAMS ── */
const STRING_X = [8, 20, 32, 44], STRING_NAMES = ["G", "C", "E", "A"];
function chordsUsed(s) {
const seen = [];
s.sections.forEach(sec => sec.lines.forEach(line => {
const toks = line.type === "chords-only" ? parseChords(line.chords) : line.tokens;
toks.forEach(t => { if (CHORD_RE.test(t.name) && !seen.includes(t.name)) seen.push(t.name); });
}));
return seen;
}
function diagramSvg(name) {
let frets = UKE_CHORDS[name];
if (!frets && name.includes("/")) frets = UKE_CHORDS[name.split("/")[0]];
let s = '<svg width="48" height="58" viewBox="0 0 52 62">';
s += '<line x1="8" y1="10" x2="44" y2="10" stroke="#333" stroke-width="3"/>';
STRING_X.forEach(x => { s += '<line x1="' + x + '" y1="10" x2="' + x + '" y2="58" stroke="#555" stroke-width="1"/>'; });
[22, 34, 46, 58].forEach(y => { s += '<line x1="8" y1="' + y + '" x2="44" y2="' + y + '" stroke="#ccc" stroke-width=".8"/>'; });
STRING_X.forEach((x, i) => { s += '<text x="' + x + '" y="7" text-anchor="middle" font-size="5" fill="#aaa">' + STRING_NAMES[i] + '</text>'; });
if (frets) {
STRING_X.forEach((x, i) => { if (frets[i] === 0) s += '<text x="' + x + '" y="8" text-anchor="middle" font-size="7" fill="#888">o</text>'; });
STRING_X.forEach((x, i) => { if (frets[i] > 0) s += '<circle cx="' + x + '" cy="' + (10 + 12 * frets[i]) + '" r="4.5" fill="#555"/>'; });
} else {
s += '<text x="26" y="38" text-anchor="middle" font-size="16" fill="#bbb">?</text>';
}
s += '</svg>';
return { svg: s, frets: frets ? frets.join(" ") : "?" };
}
/* ── EXPORT TEMPLATE ── */
/* Edit SHEET_TEMPLATE to change the generated HTML layout/style.
Placeholders: {{TITLE}} {{ARTIST}} {{SHEET}} {{DIAGRAMS}} {{STREAMING}} */
const SHEET_TEMPLATE = `<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>{{TITLE}} – Ukulele</title>
<style>@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700&family=Roboto+Mono:wght@400;600&display=swap');
*{margin:0;padding:0;box-sizing:border-box}body{background:#fff;font-family:'Roboto Mono',monospace}
.page{font-size:18px;width:210mm;min-height:297mm;background:#fff;margin:0 auto;padding:5mm;display:flex;flex-direction:column;position:relative}
.page-break{position:absolute;left:0;right:0;height:1px;background:#d0c8c0;pointer-events:none}
header{border-bottom:2px solid #1a1a1a;padding-bottom:3px;margin-bottom:5px;display:flex;justify-content:space-between;align-items:flex-start}
h1{font-family:'Playfair Display',serif;font-size:1.2em;line-height:1.1}
.artist{font-size:.67em;letter-spacing:.12em;text-transform:uppercase;color:#666;margin-top:1px}
.body{display:flex;gap:6mm}
.sheet{flex:4}
.diagrams{flex:1;border-left:1px solid #ddd;padding-left:4mm;display:flex;flex-direction:column;gap:5px}
.section{margin-bottom:4px}
.section-label{font-size:.60em;font-weight:700;letter-spacing:.14em;text-transform:uppercase;color:#aaa}
.chords{display:block;white-space:pre;font-family:'Roboto Mono',monospace;font-size:.78em;font-weight:300;color:#aaa;line-height:1.1}
.lyrics{display:block;white-space:pre;font-family:'Roboto Mono',monospace;font-size:.78em;font-weight:300;color:#1a1a1a;line-height:1.35}
.chords-only{display:block;white-space:pre;font-family:'Roboto Mono',monospace;font-size:.78em;font-weight:300;color:#aaa;line-height:1.4}
hr.d{border:none;border-top:1px dashed #e0e0e0;margin:3px 0}
.diagrams-title{font-size:.60em;font-weight:700;letter-spacing:.12em;text-transform:uppercase;color:#aaa;border-bottom:1px solid #ddd;padding-bottom:2px;margin-bottom:2px}
.chord-box{text-align:center}.chord-box .chord-name{font-size:.76em;font-weight:700}
.chord-box .frets{font-size:.54em;color:#aaa}
.streaming{display:flex;flex-direction:column;gap:4px;align-items:stretch}
.streaming a{display:flex;align-items:center;justify-content:center;gap:4px;font-size:.58em;color:#555;text-decoration:none;padding:3px 8px;border-radius:20px;border:1px solid #ddd}
.sp{color:#1db954!important;border-color:#1db954!important}
.yt{color:#ff0000!important;border-color:#ff0000!important}
.streaming-qr{display:none}
.qr-block{display:none;flex-direction:column;gap:5px;margin-top:auto;padding-top:4px}
.qr-block img{width:100%;max-width:28mm;height:auto;display:block;margin:0 auto}
.qr-block span{font-size:.5em;text-align:center;display:block;margin-top:1px}
footer{font-size:.55em;color:#bbb;text-align:center;border-top:1px solid #eee;padding-top:0px;margin-top:auto}
#font-ctrl{position:fixed;top:14px;left:50%;transform:translateX(-50%);background:#1a1a1a;border-radius:10px;display:flex;align-items:center;gap:6px;padding:6px 10px;z-index:999;box-shadow:0 4px 16px rgba(0,0,0,.3);font-family:'Roboto Mono',monospace;font-size:11px;color:#ccc}
#font-ctrl button{background:#333;border:none;border-radius:6px;color:#fff;font-family:'Roboto Mono',monospace;font-size:11px;padding:4px 8px;cursor:pointer}
#font-ctrl button.active{background:#b5451b}
#font-size-label{min-width:30px;text-align:center;color:#aaa}
@media print{
@page{margin:0}
html,body{margin:0;padding:0;background:white}
.page{box-shadow:none;margin:0;margin-bottom:10mm;width:100%;min-height:0;padding:5mm 10mm 24mm 10mm}
.section{break-inside:avoid}
#font-ctrl{display:none}
footer{position:fixed;bottom:5mm;left:0;right:0;padding:2mm 10mm;background:white;margin:0}
.streaming a{display:none}
.qr-block{display:flex;position:fixed;bottom:13mm;right:10mm;width:28mm}
.qr-block .sp-label{color:#1db954}
.qr-block .yt-label{color:#ff0000}
}</style></head>
<body><div class="page">
<header><div><h1>{{TITLE}}</h1>
<div class="artist">{{ARTIST}} · arr. for Ukulele</div>
</div>{{STREAMING}}</header>
<div class="body"><div class="sheet">{{SHEET}}</div>
<div class="diagrams">{{DIAGRAMS}}{{QR}}</div>
</div>
<footer id="page-footer"></footer>
</div>
<div id="font-ctrl"><span>Aa</span><span style="color:#555">|</span><button id="btn-default" class="active" onclick="setSize(18,this)">Default</button><button onclick="fitToA4(this)">Fit A4</button><span style="color:#555">|</span><button onclick="nudge(-0.5)">−</button><span id="font-size-label">18px</span><button onclick="nudge(+0.5)">+</button><span style="color:#555">|</span><a id="edit-lnk" href="#" style="color:#9a9;font-size:11px;text-decoration:none;padding:4px 6px;background:#333;border-radius:6px">Edit</a></div><script>const page=document.querySelector(".page"),sheet=document.querySelector(".sheet"),label=document.getElementById("font-size-label"),btns=document.querySelectorAll("#font-ctrl button");function setActive(b){btns.forEach(x=>x.classList.remove("active"));if(b)b.classList.add("active");}function updatePageBreaks(){page.querySelectorAll('.page-break').forEach(el=>el.remove());var ruler=document.createElement('div');ruler.style.cssText='position:fixed;height:297mm;width:0;visibility:hidden;';document.body.appendChild(ruler);var a4px=ruler.offsetHeight;document.body.removeChild(ruler);for(var y=a4px;y<page.scrollHeight;y+=a4px){var ln=document.createElement('div');ln.className='page-break';ln.style.top=y+'px';page.appendChild(ln);}}function setSize(px,b){sheet.style.fontSize=px+"px";label.textContent=px.toFixed(1).replace(".0","")+"px";setActive(b);updatePageBreaks();}function nudge(d){const cur=parseFloat(sheet.style.fontSize)||18;setSize(Math.max(7,cur+d),null);}function fitToA4(b){page.querySelectorAll('.page-break').forEach(el=>el.remove());var ruler=document.createElement('div');ruler.style.cssText='position:fixed;height:297mm;width:0;visibility:hidden;';document.body.appendChild(ruler);var a4px=ruler.offsetHeight;document.body.removeChild(ruler);var lo=7,hi=60,best=lo;for(var i=0;i<30;i++){var mid=Math.floor((lo+hi)/2);sheet.style.fontSize=mid+"px";if(page.scrollHeight<=a4px){best=mid;lo=mid+1;}else{hi=mid-1;}}setSize(best,b);}document.getElementById("page-footer").textContent=window.location.href;(function(){var loc=window.location.href.split("?")[0].split("#")[0];var m=loc.match(new RegExp('https?://([^.]+)\\.github\\.io/([^/]+)/(.+)'));var editUrl;if(m){var raw="https://raw.githubusercontent.com/"+m[1]+"/"+m[2]+"/main/"+m[3];var depth=m[3].split("/").length-1;var base=depth>0?"../".repeat(depth):"./";editUrl=base+"editor.html?url="+encodeURIComponent(raw);}else{editUrl="editor.html?url="+encodeURIComponent(loc);}document.getElementById("edit-lnk").href=editUrl;})();updatePageBreaks();<\/script>
</body></html>`;
function applyTemplate(tmpl, vars) {
return tmpl.replace(/\{\{(\w+)\}\}/g, (_, k) => k in vars ? vars[k] : '');
}
function exportHtml(s, template) {
if (template === undefined) template = SHEET_TEMPLATE;
const m = s.meta;
const sheetHtml = s.sections.map(sec => {
const linesHtml = sec.lines.map(line => {
if (line.type === "chords-only")
return '<div class="line"><span class="chords-only">' + esc(line.chords) + '</span></div>';
const cs = buildChordStr(line.tokens, line.lyrics.length);
return '<div class="line"><span class="chords">' + esc(cs) + '</span><span class="lyrics">' + esc(line.lyrics) + '</span></div>';
}).join("");
return '<div class="section"><div class="section-label">' + esc(sec.name || "Section") + '</div>' + linesHtml + '</div>';
}).join('<hr class="d">');
const diagramsHtml = chordsUsed(s).map(n => {
const d = diagramSvg(n);
return '<div class="chord-box"><div class="chord-name">' + esc(n) + '</div>' + d.svg + '<div class="frets">' + d.frets + '</div></div>';
}).join("");
const query = encodeURIComponent((m.artist ? m.artist + ' ' : '') + m.title);
const spUrl = m.spotify || 'https://open.spotify.com/search/' + query;
const ytUrl = m.ytmusic || 'https://music.youtube.com/search?q=' + query;
const qr = u => "https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=" + encodeURIComponent(u);
const streamingHtml =
'<div class="streaming">'
+ '<a href="' + escAttr(spUrl) + '" class="sp" target="_blank">▶ Spotify</a>'
+ '<a href="' + escAttr(ytUrl) + '" class="yt" target="_blank">▶ YouTube Music</a>'
+ '</div>';
const qrHtml =
'<div class="qr-block">'
+ '<div><img src="' + escAttr(qr(spUrl)) + '" alt="Spotify QR"><span class="sp-label">Spotify</span></div>'
+ '<div><img src="' + escAttr(qr(ytUrl)) + '" alt="YouTube Music QR"><span class="yt-label">YouTube Music</span></div>'
+ '</div>';
return applyTemplate(template, {
TITLE: esc(m.title),
ARTIST: esc(m.artist),
SHEET: sheetHtml,
DIAGRAMS: diagramsHtml,
STREAMING: streamingHtml,
QR: qrHtml,
});
}
/* ── BROWSER WIRING ── */
if (typeof document !== "undefined") {
/* ── RENDER ── */
function render() {
const sheet = document.getElementById("song-sheet");
sheet.innerHTML = "";
song.sections.forEach((sec, si) => {
if (si > 0) { const hr = document.createElement("hr"); hr.className = "section-divider"; sheet.appendChild(hr); }
const lbl = document.createElement("div"); lbl.className = "section-label"; lbl.textContent = sec.name; sheet.appendChild(lbl);
sec.lines.forEach((line, li) => {
if (line.type === "chords-only") {
const d = document.createElement("div"); d.className = "chords-only-line"; d.textContent = line.chords; sheet.appendChild(d); return;
}
if (line.tokens.length) {
const row = document.createElement("div"); row.className = "chord-line";
line.tokens.forEach((tok, ti) => {
const span = document.createElement("span");
span.className = "chord-token" + (selected?.secIdx === si && selected?.lineIdx === li && selected?.tokIdx === ti ? " selected" : "");
span.textContent = tok.name;
span.style.left = tok.col * CHAR_W + "px";
let drag = null;
span.addEventListener("pointerdown", e => { e.preventDefault(); span.setPointerCapture(e.pointerId); drag = { startX: e.clientX, startCol: tok.col, moved: false }; });
span.addEventListener("pointermove", e => { if (!drag) return; const d = Math.round((e.clientX - drag.startX) / CHAR_W); if (d) drag.moved = true; span.style.left = Math.max(0, drag.startCol + d) * CHAR_W + "px"; });
span.addEventListener("pointerup", e => {
if (!drag) return;
const d = Math.round((e.clientX - drag.startX) / CHAR_W), moved = drag.moved || Math.abs(d) > 0; drag = null;
if (moved) moveChordBy(si, li, ti, d);
else setSelected(selected?.secIdx === si && selected?.lineIdx === li && selected?.tokIdx === ti ? null : { secIdx: si, lineIdx: li, tokIdx: ti });
});
row.appendChild(span);
});
sheet.appendChild(row);
}
if (line.lyrics) { const lr = document.createElement("div"); lr.className = "lyric-line"; lr.textContent = line.lyrics; sheet.appendChild(lr); }
});
});
}
/* ── SELECTION ── */
function setSelected(sel) { selected = sel; render(); updateNudgeBar(); }
function updateNudgeBar() {
const bar = document.getElementById("nudge-bar"), lbl = document.getElementById("nudge-label");
const tok = selected ? song.sections[selected.secIdx]?.lines[selected.lineIdx]?.tokens[selected.tokIdx] : null;
if (tok) { lbl.textContent = tok.name; bar.classList.add("visible"); } else { bar.classList.remove("visible"); }
}
function moveChordBy(si, li, ti, d) {
const tok = song.sections[si]?.lines[li]?.tokens[ti]; if (!tok) return;
tok.col = Math.max(0, tok.col + d); setSelected(selected);
}
function moveSelected(d) { if (selected) moveChordBy(selected.secIdx, selected.lineIdx, selected.tokIdx, d); }
document.querySelectorAll(".nudge-btn").forEach(b => b.addEventListener("pointerdown", e => { e.preventDefault(); moveSelected(parseInt(b.dataset.delta)); }));
document.getElementById("nudge-close").addEventListener("click", () => setSelected(null));
document.addEventListener("keydown", e => {
if (!selected) return;
if (e.key === "ArrowLeft") { e.preventDefault(); moveSelected(-1); }
if (e.key === "ArrowRight") { e.preventDefault(); moveSelected(1); }
if (e.key === "Escape") setSelected(null);
});
/* ── META EDITOR ── */
function metaToText(m) {
const lines = [];
if (m.title) lines.push('title: ' + m.title);
if (m.artist) lines.push('artist: ' + m.artist);
lines.push('spotify: ' + (m.spotify || ''));
lines.push('ytmusic: ' + (m.ytmusic || ''));
if (m.key) lines.push('key: ' + m.key);
return lines.join('\n');
}
function textToMeta(text) {
const meta = {};
for (const line of text.split('\n')) {
const m = line.match(/^([A-Za-z][A-Za-z0-9_-]*)\s*:\s*(.*)$/);
if (m) meta[m[1].toLowerCase()] = m[2].trim();
}
return meta;
}
function fitMetaEditor() {
const ta = document.getElementById('meta-editor');
ta.style.height = 'auto';
ta.style.height = ta.scrollHeight + 'px';
}
document.getElementById('meta-editor').addEventListener('input', () => {
const updated = textToMeta(document.getElementById('meta-editor').value);
Object.assign(song.meta, updated);
if (updated.title) {
document.getElementById('editor-song-title').textContent = updated.title;
const sl = slugify(updated.title);
if (sl) document.getElementById('gh-path').value = 'ukulele/' + sl + '.html';
}