Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
1872 lines (1718 loc) · 85.2 KB
/
Copy pathapp.js
File metadata and controls
1872 lines (1718 loc) · 85.2 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
/* ==========================================================================
BADGEWORKS - APPLICATION LOGIC
========================================================================== */
// Application State
const BADGE_STYLES = ['cozy', 'compact', 'cozy-minimal', 'compact-minimal'];
// Measurements from the upstream Devin's Badges design template.
const BADGE_LAYOUTS = {
cozy: { height: 56, logo: 40, left: 12, gap: 8, right: 12, border: 2.1, radius: 8, subtitleY: 24.5, titleY: 43.5, subtitleSize: 16 },
compact: { height: 40, logo: 28, left: 8, gap: 6, right: 8, border: 2.143, radius: 8, subtitleY: 26.5, titleY: 26.5, subtitleSize: 17, inline: true },
'cozy-minimal': { height: 56, logo: 40, left: 8, border: 2.1, radius: 8.4, minimal: true },
'compact-minimal': { height: 40, logo: 28, left: 6, border: 2.1, radius: 8, minimal: true }
};
const BADGE_FONT = 'Badgeworks Inter';
const BADGE_FONT_FACE = `@font-face{font-family:"${BADGE_FONT}";src:url(${BADGE_FONT_URL}) format("woff2");font-weight:500 800;}`;
const BADGE_FONT_STYLE = `<style>${BADGE_FONT_FACE}</style>`;
const BADGE_TITLE_SIZE = 17;
const BADGE_TITLE_WEIGHT = 800;
const BADGE_SUBTITLE_WEIGHT = 500;
const DEFAULT_LOGO_SIZE = 40;
const COMPACT_LABEL_GAP = 4;
const MAX_BADGE_WIDTH = 420;
const GRADIENT_CONTROLS = {
title: { label: 'Title', toggle: 'text-grad-toggle', solid: 'text-color' },
subtitle: { label: 'Subtitle', toggle: 'subtitle-grad-toggle', solid: 'subtitle-color' },
logo: { label: 'Logo', toggle: 'logo-grad-toggle', solid: 'logo-color', parent: 'custom-logo-color-toggle' },
outline: { label: 'Outline', toggle: 'logo-stroke-grad-toggle', parent: 'logo-stroke-toggle' },
border: { label: 'Border' }
};
function defaultTextGradient(color = '#61dafb') {
return { angle: 180, stops: [{ color, offset: 0 }, { color: '#ffffff', offset: 100 }] };
}
const state = {
officialBadge: '',
customizedBadge: '',
customizedBaseline: null,
style: 'cozy', // 'cozy' | 'compact' | 'cozy-minimal' | 'compact-minimal'
iconMode: 'fontawesome', // 'preset' | 'fontawesome' | 'thesvg' | 'upload' | 'raw'
logoPosition: 'left', // 'left' | 'right' | 'none'
uploadedDataUrl: '', // base64 DataURL for PNG/JPG/SVG
rawSvgDataUrl: '', // data URL built from pasted raw SVG code
uploadFilename: '',
bgStops: ['#0d6ffb', '#0157ff'], // background gradient stops (2–7)
textGradients: { title: defaultTextGradient(), subtitle: defaultTextGradient('#e8e8e8'),
logo: defaultTextGradient(), outline: { angle: 180, stops: [{ color: '#ffffff', offset: 0 }, { color: '#61dafb', offset: 100 }] }, border: defaultTextGradient() },
bgGradPreset: 'custom', // name of the currently-applied gradient preset ('custom' = manual)
codeTab: 'md' // 'md' | 'html' | 'svg'
};
// Icons with no equivalent in the official badge collection.
const EXTRA_PRESET_ICONS = {
vscode: {
path: "M23.15 2.587L18.21.21a1.494 1.494 0 0 0-1.705.29l-9.46 8.63-4.12-3.128a.999.999 0 0 0-1.276.057L.327 7.261A1 1 0 0 0 .326 8.74L3.899 12 .326 15.26a1 1 0 0 0 .001 1.479L1.65 17.94a.999.999 0 0 0 1.276.057l4.12-3.128 9.46 8.63a1.492 1.492 0 0 0 1.704.29l4.942-2.377A1.5 1.5 0 0 0 24 20.06V3.939a1.5 1.5 0 0 0-.85-1.352zm-5.146 14.861L10.826 12l7.178-5.448v10.896z",
color: "#007ACC"
},
docker: {
path: "M6.94221099,14.9002344 C6.9980621,14.9002344 7.05128211,14.9107588 7.10043586,14.9297745 C7.04721586,14.9606302 7.01109801,15.018335 7.01109801,15.0842919 C7.01109801,15.1828984 7.09098782,15.2626686 7.18959432,15.2626686 C7.25710599,15.2626686 7.31570779,15.2251754 7.34608506,15.1698027 C7.36743286,15.2214082 7.37939241,15.2780367 7.37939241,15.3374756 C7.37939241,15.578939 7.18361455,15.774657 6.94221099,15.774657 C6.70080744,15.774657 6.50496978,15.578939 6.50496978,15.3374756 C6.50496978,15.0959525 6.70080744,14.9002344 6.94221099,14.9002344 L6.94221099,14.9002344 Z M6.94221099,16.0853662 C6.52978585,16.0853662 6.19420083,15.7499008 6.19420083,15.3374756 C6.19420083,14.9250505 6.52978585,14.5895253 6.94221099,14.5895253 C7.35457634,14.5895253 7.69010156,14.9250505 7.69010156,15.3374756 C7.69010156,15.7499008 7.35457634,16.0853662 6.94221099,16.0853662 L6.94221099,16.0853662 Z M20.3859431,11.1838037 C18.2619865,16.8117894 13.4653093,19.318631 7.81023526,19.318631 C5.13823222,19.318631 3.00656172,18.3995992 1.64323262,16.8672219 L1.65327865,16.8605843 C2.04609012,16.880497 2.39758135,16.8872541 2.75439457,16.8872541 C3.08065114,16.8872541 3.39979178,16.8838457 3.6953721,16.8672219 C3.72108514,16.8657867 3.75325633,16.8621989 3.77878997,16.8605843 C3.77902916,16.8605245 3.86998155,16.8546046 3.82549202,16.853887 C4.57667146,16.8075437 5.15892224,16.7031368 5.70188589,16.5482008 C5.70200548,16.548141 5.70212508,16.548141 5.70224467,16.5480812 C5.80091098,16.5198567 5.89658739,16.4901372 5.98825735,16.4583846 C6.09081051,16.4228049 6.14510687,16.3108635 6.109587,16.2083104 C6.07406714,16.1056974 5.96218553,16.0512815 5.85957258,16.0869807 C5.16992503,16.3259326 4.26010213,16.4574876 3.14505333,16.4821841 L3.14475434,16.4821841 C2.57739321,16.4947416 1.95717085,16.4797922 1.28450587,16.4365584 L1.28444607,16.4365584 C1.14529669,16.2507668 1.01649231,16.0576798 0.89869073,15.8577161 L0.71248051,15.5172277 C0.149903198,14.4112083 -0.0964037696,13.1191582 0.0343141305,11.7160038 L16.3965356,11.7160038 C17.7407294,11.7160038 19.0534696,11.2143604 19.6764427,10.6609919 C18.5601381,9.75332174 18.670764,7.59731356 19.3822377,6.774616 C19.9997093,7.270758 20.9954018,8.31584342 20.824141,9.64622396 C21.6011531,9.255625 22.9506091,9.06259783 24,9.66816973 C23.3411483,10.9541803 21.8929064,11.3383809 20.3859431,11.1838037 L20.3859431,11.1838037 Z M2.25508329,11.3188869 L4.46771995,11.3188869 L4.46771995,9.1061306 L2.25508329,9.1061306 L2.25508329,11.3188869 Z M4.80808879,11.3188869 L7.02096464,11.3188869 L7.02096464,9.1061306 L4.80808879,9.1061306 L4.80808879,11.3188869 Z M4.80808879,8.76576176 L7.02096464,8.76576176 L7.02096464,6.5530653 L4.80808879,6.5530653 L4.80808879,8.76576176 Z M7.36127369,11.3188869 L9.57402994,11.3188869 L9.57402994,9.1061306 L7.36127369,9.1061306 L7.36127369,11.3188869 Z M7.36127369,8.76576176 L9.57402994,8.76576176 L9.57402994,6.5530653 L7.36127369,6.5530653 L7.36127369,8.76576176 Z M9.91433899,11.3188869 L12.1270952,11.3188869 L12.1270952,9.1061306 L9.91433899,9.1061306 L9.91433899,11.3188869 Z M9.91433899,8.76576176 L12.1270952,8.76576176 L12.1270952,6.5530653 L9.91433899,6.5530653 L9.91433899,8.76576176 Z M9.91433899,6.21275626 L12.1270952,6.21275626 L12.1270952,4 L9.91433899,4 L9.91433899,6.21275626 Z M12.4674043,11.3188869 L14.6801605,11.3188869 L14.6801605,9.1061306 L12.4674043,9.1061306 L12.4674043,11.3188869 Z",
color: "#0DB7ED"
},
rust: {
isCustomSvg: true,
svg: `<g transform="translate(32, 32) scale(0.6)"><path transform="translate(0.5, 0.5)" stroke="#ffffff" stroke-width="1" stroke-linejoin="round" d="M -9,-15 H 4 C 12,-15 12,-7 4,-7 H -9 Z M -40,22 H 0 V 11 H -9 V 3 H 1 C 12,3 6,22 15,22 H 40 V 3 H 34 V 5 C 34,13 25,12 24,7 C 23,2 19,-2 18,-2 C 33,-10 24,-26 12,-26 H -35 V -15 H -25 V 11 H -40 Z"/><g mask="url(#rust-holes)"><circle r="43" fill="none" stroke="#ffffff" stroke-width="9"/><g id="cogs"><polygon id="rust-cog" stroke="#ffffff" stroke-width="3" stroke-linejoin="round" points="46,3 51,0 46,-3"/><use xlink:href="#rust-cog" transform="rotate(11.25)"/><use xlink:href="#rust-cog" transform="rotate(22.50)"/><use xlink:href="#rust-cog" transform="rotate(33.75)"/><use xlink:href="#rust-cog" transform="rotate(45.00)"/><use xlink:href="#rust-cog" transform="rotate(56.25)"/><use xlink:href="#rust-cog" transform="rotate(67.50)"/><use xlink:href="#rust-cog" transform="rotate(78.75)"/><use xlink:href="#rust-cog" transform="rotate(90.00)"/><use xlink:href="#rust-cog" transform="rotate(101.25)"/><use xlink:href="#rust-cog" transform="rotate(112.50)"/><use xlink:href="#rust-cog" transform="rotate(123.75)"/><use xlink:href="#rust-cog" transform="rotate(135.00)"/><use xlink:href="#rust-cog" transform="rotate(146.25)"/><use xlink:href="#rust-cog" transform="rotate(157.50)"/><use xlink:href="#rust-cog" transform="rotate(168.75)"/><use xlink:href="#rust-cog" transform="rotate(180.00)"/><use xlink:href="#rust-cog" transform="rotate(191.25)"/><use xlink:href="#rust-cog" transform="rotate(202.50)"/><use xlink:href="#rust-cog" transform="rotate(213.75)"/><use xlink:href="#rust-cog" transform="rotate(225.00)"/><use xlink:href="#rust-cog" transform="rotate(236.25)"/><use xlink:href="#rust-cog" transform="rotate(247.50)"/><use xlink:href="#rust-cog" transform="rotate(258.75)"/><use xlink:href="#rust-cog" transform="rotate(270.00)"/><use xlink:href="#rust-cog" transform="rotate(281.25)"/><use xlink:href="#rust-cog" transform="rotate(292.50)"/><use xlink:href="#rust-cog" transform="rotate(303.75)"/><use xlink:href="#rust-cog" transform="rotate(315.00)"/><use xlink:href="#rust-cog" transform="rotate(326.25)"/><use xlink:href="#rust-cog" transform="rotate(337.50)"/><use xlink:href="#rust-cog" transform="rotate(348.75)"/></g><g id="mounts"><polygon id="rust-mount" stroke="#ffffff" stroke-width="6" stroke-linejoin="round" points="-7,-42 0,-35 7,-42"/><use xlink:href="#rust-mount" transform="rotate(72)"/><use xlink:href="#rust-mount" transform="rotate(144)"/><use xlink:href="#rust-mount" transform="rotate(216)"/><use xlink:href="#rust-mount" transform="rotate(288)"/></g></g><mask id="rust-holes"><rect x="-60" y="-60" width="120" height="120" fill="#ffffff"/><circle id="rust-hole" cy="-40" r="3"/><use xlink:href="#rust-hole" transform="rotate(72)"/><use xlink:href="#rust-hole" transform="rotate(144)"/><use xlink:href="#rust-hole" transform="rotate(216)"/><use xlink:href="#rust-hole" transform="rotate(288)"/></mask></g>`,
color: '#ffffff'
},
star: {
path: "M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z",
color: "#FFD700"
},
terminal: {
path: "M4 17l6-5-6-5v10zm8 0h8v-2h-8v2zM2 3h20a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z",
color: "#4AF626"
}
};
// Initialize App
document.addEventListener('DOMContentLoaded', () => {
setupOfficialPresets();
setupWorkspace();
const surfaces = document.querySelectorAll('.surface-option');
for (const button of surfaces) {
button.addEventListener('click', () => {
document.getElementById('badge-stage').dataset.surface = button.dataset.surface;
for (const option of surfaces) option.setAttribute('aria-pressed', String(option === button));
});
}
setupPresetIcons();
setIconMode(state.iconMode, false);
setLogoPosition(state.logoPosition, false);
setStyle(state.style, false);
// Build the background gradient stop editor
renderBgStopEditor();
setupTextGradientEditors();
// Wire up drag & drop on the upload dropzone
const dropzone = document.getElementById('image-dropzone');
if (dropzone) {
['dragover', 'dragenter'].forEach((evtName) => dropzone.addEventListener(evtName, (e) => {
e.preventDefault();
dropzone.classList.add('dragging');
}));
['dragleave', 'dragend'].forEach((evtName) => dropzone.addEventListener(evtName, (e) => {
e.preventDefault();
dropzone.classList.remove('dragging');
}));
dropzone.addEventListener('drop', (e) => {
e.preventDefault();
dropzone.classList.remove('dragging');
const file = e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files[0];
if (file) processUploadedFile(file);
});
}
// Prevent the browser from navigating away when a file is dropped elsewhere on the page
document.addEventListener('dragover', (e) => e.preventDefault());
document.addEventListener('drop', (e) => e.preventDefault());
new ResizeObserver(fitPreviewSvg).observe(document.getElementById('badge-stage'));
const header = document.querySelector('.app-header');
new ResizeObserver(() => {
document.documentElement.style.setProperty('--header-height', `${header.offsetHeight}px`);
}).observe(header);
setCodeTab(state.codeTab);
new FontFace(BADGE_FONT, `url(${BADGE_FONT_URL})`, { weight: '500 800' })
.load().then(font => document.fonts.add(font)).catch(() => {})
.finally(() => {
textMeasurementCache.clear();
renderBadge();
});
});
// Set Active Style Variant
function setStyle(styleName, render = true) {
state.style = styleName;
document.querySelectorAll('.style-option').forEach(card => {
const selected = card.dataset.style === styleName;
card.classList.toggle('active', selected);
card.setAttribute('aria-pressed', selected);
});
if (render) renderBadge();
}
// Icon Input Mode Switcher
function setIconMode(mode, render = true) {
state.iconMode = mode;
for (const option of ICON_MODES) {
const selected = option === mode;
const button = document.getElementById(`seg-${option}`);
button.classList.toggle('active', selected);
button.setAttribute('aria-pressed', selected);
document.getElementById(`icon-mode-${option}`).hidden = !selected;
}
// Update FontAwesome preview
if (mode === 'fontawesome') {
updateFontAwesomePreview();
ensureFaRegistry();
}
// Update theSVG preview (and lazily load the icon registry for autocomplete)
if (mode === 'thesvg') {
refreshTheSvgVariantOptions();
updateTheSvgPreview();
ensureTheSvgRegistry();
}
if (render) renderBadge();
}
// Logo Position Switcher (left / right / none)
function setLogoPosition(pos, render = true) {
state.logoPosition = pos;
for (const option of LOGO_POSITIONS) {
const button = document.getElementById(`seg-pos-${option}`);
button.classList.toggle('active', option === pos);
button.setAttribute('aria-pressed', option === pos);
}
if (render) renderBadge();
}
// Known FA pack tokens for dropdown matching & class parsing (Free packs only:
// Font Awesome Free ships solid, regular, and brands — light/thin/duotone/sharp
// and the pixel/mosaic/vellum/slab families are Pro and 404 on the free CDN)
const FA_PACK_TOKENS = [
'fa-brands', 'fa-solid', 'fa-regular'
];
function parseFontAwesomeInput(raw) {
const pack = document.getElementById('fa-pack-select');
const input = document.getElementById('fa-icon-name');
const text = (raw ?? input.value).trim().toLowerCase();
const classes = /<i\b[^>]*\bclass\s*=\s*["']([^"']+)/i.exec(text)?.[1] || text;
const tokens = classes.split(/\s+/);
const aliases = { fas: 'fa-solid', far: 'fa-regular', fab: 'fa-brands', 'fa-brand': 'fa-brands' };
const explicitPack = tokens.map(token => aliases[token] || token).find(token => FA_PACK_TOKENS.includes(token));
const ignored = /^fa-(?:solid|brands?|regular|thin|light|duotone|sharp(?:-.*)?|fw|spin.*|pulse|beat.*|bounce|flip.*|[0-9]+x|xs|sm|lg|xl|2xs|2xl)$/;
const iconClass = tokens.find(token => /^fa-[a-z0-9-]+$/.test(token) && !ignored.test(token));
const name = text === '?' ? 'question'
: iconClass?.slice(3) || (tokens.length === 1 && /^[a-z0-9-]+$/.test(text) ? text : '');
if (name === 'question') pack.value = 'fa-solid';
else if (explicitPack) pack.value = explicitPack;
if (name && input.value !== name) input.value = name;
return name ? `${pack.value} fa-${name}` : '';
}
function updateFontAwesomePreview() {
const value = parseFontAwesomeInput();
const art = document.querySelector('#fa-preview .icon-source-art');
setIconPreview(art, extractFontAwesomeSvg(value).then(icon => icon
? svgDataUrl(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="${escapeHtml(icon.viewBox)}" fill="#f6f6fa"><path d="${escapeHtml(icon.pathData)}"/></svg>`)
: ''));
}
function onFaSlugInput() {
refreshFaPackOption();
updateFontAwesomePreview();
renderBadge();
}
// Lazily-loaded FontAwesome metadata: icon name -> { label, freeStyles }.
// Source is the free metadata file pinned to 6.7.2 (the same icons the
// `@fortawesome/fontawesome-free@6` SVG CDN serves), loaded once on first
// FontAwesome use to power name autocomplete. Failures are silent: the name
// input still works via direct CDN fetch.
let faRegistry = null;
let faRegistryPromise = null;
const FA_METADATA_URL = 'https://cdn.jsdelivr.net/gh/FortAwesome/Font-Awesome@6.7.2/metadata/icons.json';
function ensureFaRegistry() {
if (faRegistry) return Promise.resolve(faRegistry);
if (faRegistryPromise) return faRegistryPromise;
faRegistryPromise = fetch(FA_METADATA_URL)
.then(r => r.ok ? r.json() : null)
.then((data) => {
if (!data || typeof data !== 'object') { faRegistry = false; return null; }
const map = new Map();
for (const [name, meta] of Object.entries(data)) {
if (!meta || typeof meta !== 'object') continue;
const freeStyles = Array.isArray(meta.free)
? meta.free.filter(s => ['solid', 'regular', 'brands'].includes(s))
: [];
if (freeStyles.length === 0) continue;
map.set(name, { label: meta.label || name, freeStyles });
}
if (map.size === 0) { faRegistry = false; return null; }
faRegistry = map;
populateFaDatalist(map);
const selection = document.getElementById('fa-pack-select').value;
refreshFaPackOption();
if (!state.officialBadge && state.iconMode === 'fontawesome' && selection !== document.getElementById('fa-pack-select').value) {
updateFontAwesomePreview();
renderBadge();
}
return map;
})
.catch(() => { faRegistry = false; return null; });
return faRegistryPromise;
}
// Fill the icon-name autocomplete list (built once — ~1.9k entries)
function populateFaDatalist(map) {
const list = document.getElementById('fa-icon-list');
if (!list || list.dataset.populated) return;
const frag = document.createDocumentFragment();
for (const [name, entry] of map) {
const opt = document.createElement('option');
opt.value = name;
if (entry.label && entry.label.toLowerCase() !== name.toLowerCase()) opt.label = entry.label;
frag.appendChild(opt);
}
list.appendChild(frag);
list.dataset.populated = '1';
}
// When the typed name exactly matches a known icon but the selected pack
// doesn't ship it (e.g. `github` only exists in brands), switch the pack to
// the icon's first free style instead of rendering a dangling `?`.
function refreshFaPackOption() {
if (!faRegistry || faRegistry === false) return;
const nameInput = document.getElementById('fa-icon-name');
const packSelect = document.getElementById('fa-pack-select');
if (!nameInput || !packSelect) return;
const raw = nameInput.value;
// Only for bare names — pasted classes/HTML carry their own pack
if (!raw || raw.includes(' ') || raw.includes('<')) return;
const clean = raw.replace(/^fa-/, '').trim().toLowerCase();
if (!clean) return;
const entry = faRegistry.get(clean);
if (!entry) return;
const packFor = (style) => style === 'brands' ? 'fa-brands' : style === 'regular' ? 'fa-regular' : 'fa-solid';
const available = entry.freeStyles.map(packFor);
if (!available.includes(packSelect.value)) {
packSelect.value = available[0];
if (nameInput.value !== clean) nameInput.value = clean;
}
}
// The default mark also lives in the header, so it works offline.
const faIconCache = new Map();
function extractFontAwesomeSvg(iconClass) {
const match = /^fa-(solid|regular|brands) fa-([a-z0-9-]+)$/.exec(iconClass);
if (!match) return Promise.resolve(null);
const [, folder, name] = match;
if (folder === 'solid' && name === 'b') {
return Promise.resolve({
viewBox: '0 0 320 512',
pathData: document.querySelector('.brand-logo path').getAttribute('d')
});
}
if (folder === 'solid' && name === 'question') {
// Font Awesome Free 6.7.2 by Fonticons, Inc. (CC BY 4.0).
return Promise.resolve({
viewBox: '0 0 320 512',
pathData: 'M80 160c0-35.3 28.7-64 64-64l32 0c35.3 0 64 28.7 64 64l0 3.6c0 21.8-11.1 42.1-29.4 53.8l-42.2 27.1c-25.2 16.2-40.4 44.1-40.4 74l0 1.4c0 17.7 14.3 32 32 32s32-14.3 32-32l0-1.4c0-8.2 4.2-15.8 11-20.2l42.2-27.1c36.6-23.6 58.8-64.1 58.8-107.7l0-3.6c0-70.7-57.3-128-128-128l-32 0C73.3 32 16 89.3 16 160c0 17.7 14.3 32 32 32s32-14.3 32-32zm80 320a40 40 0 1 0 0-80 40 40 0 1 0 0 80z'
});
}
const url = `https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.7.2/svgs/${folder}/${name}.svg`;
return loadRemoteIcon(faIconCache, url, markup => {
const svg = parseSvg(markup);
const pathData = svg ? Array.from(svg.querySelectorAll('path'), path => path.getAttribute('d')).filter(Boolean).join(' ') : '';
return pathData ? { viewBox: svg.getAttribute('viewBox') || '0 0 512 512', pathData } : null;
});
}
function loadRemoteIcon(cache, url, parse) {
if (!cache.has(url)) {
if (cache.size >= 160) cache.delete(cache.keys().next().value);
const request = fetch(url, { signal: AbortSignal.timeout(12000) })
.then(response => {
if (response.status === 404) return '';
if (!response.ok) throw new Error('Icon service unavailable');
return response.text();
})
.then(markup => markup ? parse(markup) : null)
.catch(() => {
cache.delete(url);
return null;
});
cache.set(url, request);
}
return cache.get(url);
}
// Listen for FontAwesome input changes to update preview
document.addEventListener('DOMContentLoaded', () => {
const faNameInput = document.getElementById('fa-icon-name');
const faPackSelect = document.getElementById('fa-pack-select');
if (faNameInput) faNameInput.addEventListener('input', onFaSlugInput);
if (faPackSelect) faPackSelect.addEventListener('change', () => {
updateFontAwesomePreview();
renderBadge();
});
});
// --- theSVG brand icons (https://thesvg.org) --------------------------------
// Static, keyless CDN: raw files live at /icons/{slug}/{variant}.svg and the
// searchable manifest at /api/registry.json (same online-first pattern as the
// FontAwesome CDN integration above).
const THESVG_VARIANTS = ['default', 'color', 'mono', 'light', 'dark', 'wordmark'];
const theSvgCache = new Map();
let theSvgRegistry;
let theSvgRegistryPromise;
function normalizeTheSvgSlug(raw) {
return String(raw ?? '').trim().toLowerCase()
.replace(/[\s_]+/g, '-')
.replace(/[^a-z0-9\-.+~]/g, '');
}
function parseTheSvgInput() {
return {
slug: normalizeTheSvgSlug(document.getElementById('thesvg-slug').value),
variant: document.getElementById('thesvg-variant-select').value || 'default'
};
}
function extractSelectedTheSvg() {
const { slug, variant } = parseTheSvgInput();
if (!slug || slug === '.' || slug === '..' || !/^[a-z][a-z0-9-]*$/i.test(variant)) {
return Promise.resolve(null);
}
const filename = variant.replace(/[A-Z]/g, letter => '-' + letter.toLowerCase());
const url = `https://thesvg.org/icons/${encodeURIComponent(slug)}/${filename}.svg`;
return loadRemoteIcon(theSvgCache, url, markup => {
const svg = parseSvg(markup);
if (!svg) return null;
if (!svg.namespaceURI) svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
if (!svg.hasAttribute('viewBox')) {
const width = parseFloat(svg.getAttribute('width'));
const height = parseFloat(svg.getAttribute('height'));
svg.setAttribute('viewBox', width > 0 && height > 0 ? `0 0 ${width} ${height}` : '0 0 24 24');
}
return svgDataUrl(new XMLSerializer().serializeToString(svg));
});
}
// Lazily fetch the registry manifest once (on first theSVG use) to power slug
// autocomplete + per-icon variant options. Failures are silent: the slug input
// still works via direct CDN fetch.
function ensureTheSvgRegistry() {
if (theSvgRegistry) return Promise.resolve(theSvgRegistry);
if (theSvgRegistryPromise) return theSvgRegistryPromise;
theSvgRegistryPromise = fetch('https://thesvg.org/api/registry.json')
.then(r => r.ok ? r.json() : null)
.then((data) => {
if (!data || !Array.isArray(data.icons)) { theSvgRegistry = false; return null; }
const map = new Map();
for (const icon of data.icons) {
if (icon && icon.slug) map.set(icon.slug, icon);
}
theSvgRegistry = map;
populateTheSvgDatalist(map);
if (!state.officialBadge && state.iconMode === 'thesvg') {
refreshTheSvgVariantOptions();
updateTheSvgPreview();
renderBadge();
}
return map;
})
.catch(() => { theSvgRegistry = false; return null; });
return theSvgRegistryPromise;
}
// Fill the slug autocomplete list (built once — ~6.5k entries)
function populateTheSvgDatalist(map) {
const list = document.getElementById('thesvg-slug-list');
if (!list || list.dataset.populated) return;
const frag = document.createDocumentFragment();
for (const [slug, icon] of map) {
const opt = document.createElement('option');
opt.value = slug;
if (icon.title && icon.title !== slug) opt.label = icon.title;
frag.appendChild(opt);
}
list.appendChild(frag);
list.dataset.populated = '1';
}
function refreshTheSvgVariantOptions(preferred) {
const { slug, variant } = parseTheSvgInput();
const entry = theSvgRegistry && theSvgRegistry.get(slug);
const variants = entry?.variants?.length ? entry.variants : [...new Set([...THESVG_VARIANTS, preferred || variant])];
const select = document.getElementById('thesvg-variant-select');
if (select.options.length !== variants.length || variants.some((name, index) => select.options[index].value !== name)) {
select.replaceChildren(...variants.map(name => new Option(name, name)));
}
const selected = preferred || variant;
select.value = variants.includes(selected) ? selected : variants[0];
}
function setIconPreview(art, request) {
const loading = document.createTextNode('Loading…');
art.replaceChildren(loading);
request.then(source => {
if (art.firstChild !== loading) return;
if (!source) {
art.textContent = 'Icon unavailable';
return;
}
const icon = new Image(48, 48);
icon.alt = '';
icon.src = source;
art.replaceChildren(icon);
});
}
function updateTheSvgPreview() {
setIconPreview(document.querySelector('#thesvg-preview .icon-source-art'), extractSelectedTheSvg());
}
document.addEventListener('DOMContentLoaded', () => {
const update = () => {
updateTheSvgPreview();
renderBadge();
};
document.getElementById('thesvg-slug').addEventListener('input', () => {
refreshTheSvgVariantOptions();
update();
});
document.getElementById('thesvg-variant-select').addEventListener('change', update);
});
// Handle File Upload (PNG, JPG, SVG)
function handleFileChange(e) {
const file = e.target.files[0];
if (!file) return;
processUploadedFile(file);
// Reset the input so picking the same file again still fires `change`
e.target.value = '';
}
let uploadRevision = 0;
const imageDataPattern = /^data:image\/(?:png|jpeg|webp|gif|svg\+xml)(?:;[^,]*)?,/i;
async function processUploadedFile(file) {
const revision = ++uploadRevision;
const extension = file.name.toLowerCase().split('.').pop();
const types = { svg: 'image/svg+xml', png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', webp: 'image/webp' };
const type = Object.values(types).includes(file.type) ? file.type : types[extension];
if (!type || file.size > 8 * 1024 * 1024) {
showToast('Choose a PNG, JPG, WebP, GIF, or SVG under 8 MB.', 'error');
return;
}
try {
const dataUrl = type === 'image/svg+xml'
? normalizedSvgSource(await file.text())
: await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(file.slice(0, file.size, type));
});
if (!dataUrl) throw new Error('Invalid SVG');
const image = new Image();
image.src = dataUrl;
await image.decode();
if (revision !== uploadRevision) return;
state.uploadedDataUrl = dataUrl;
state.uploadFilename = file.name;
syncUploadPreview();
renderBadge();
} catch {
if (revision === uploadRevision) showToast('This image could not be opened.', 'error');
}
}
function syncUploadPreview() {
const thumbnail = document.getElementById('upload-thumb');
document.getElementById('upload-preview-bar').hidden = !state.uploadedDataUrl;
document.getElementById('upload-filename').textContent = state.uploadFilename || 'Uploaded logo';
thumbnail.replaceChildren();
if (state.uploadedDataUrl) {
const image = new Image(28, 28);
image.className = 'upload-thumb';
image.alt = '';
image.src = state.uploadedDataUrl;
thumbnail.append(image);
}
}
function clearUpload() {
uploadRevision++;
state.uploadedDataUrl = '';
state.uploadFilename = '';
syncUploadPreview();
renderBadge();
}
function parseSvg(markup) {
const { documentElement: svg } = new DOMParser().parseFromString(markup, 'image/svg+xml');
return svg.localName === 'svg' && (!svg.namespaceURI || svg.namespaceURI === 'http://www.w3.org/2000/svg')
? svg : null;
}
function normalizedSvgSource(markup) {
const svg = parseSvg(markup);
if (!svg) return '';
if (!svg.namespaceURI) svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
return svgDataUrl(new XMLSerializer().serializeToString(svg));
}
function updateRawSvg() {
const input = document.getElementById('raw-svg-code');
const markup = input.value.trim();
state.rawSvgDataUrl = markup ? normalizedSvgSource(markup) : '';
const invalid = Boolean(markup && !state.rawSvgDataUrl);
input.setAttribute('aria-invalid', String(invalid));
document.getElementById('raw-svg-status').textContent = invalid ? 'Enter a complete SVG element.' : '';
}
function handleRawSvgChange() {
updateRawSvg();
renderBadge();
}
// Text measurement cache to prevent flickering
const textMeasurementCache = new Map();
const measureContext = document.createElement('canvas').getContext('2d');
// Accurately measure text width using Canvas API
function measureText(text, fontSpec) {
if (!text) return 0;
// Check cache first
const cacheKey = `${text}|${fontSpec}`;
if (textMeasurementCache.has(cacheKey)) {
return textMeasurementCache.get(cacheKey);
}
if (!measureContext) return Math.ceil(String(text).length * 8);
measureContext.font = fontSpec;
const width = measureContext.measureText(text).width;
// Cache the measurement (cap size so long sessions can't grow it without bound)
if (textMeasurementCache.size > 1000) textMeasurementCache.clear();
textMeasurementCache.set(cacheKey, width);
return width;
}
// Incremented on every render; lets async FontAwesome swaps detect (and skip) stale renders
let renderToken = 0;
// Native-size markup of the last committed preview (display scaling is applied
// to the live DOM node only, so exports and the SVG tab stay native-sized)
let cachedNativeSvg = '';
// Keep display scaling out of the downloaded SVG.
function fitPreviewSvg() {
const stage = document.getElementById('badge-stage');
const art = stage.firstElementChild;
if (!art?.hasAttribute('width')) return;
const width = Number(art.getAttribute('width'));
const height = Number(art.getAttribute('height'));
const available = Math.max(0, stage.clientWidth - 32);
art.style.width = `${Math.min(available, width * 1.65, 190 * width / height)}px`;
}
// Background gradient presets (2–7 stops each)
const BG_GRADIENT_PRESETS = {
rainbow: ['#ff0000', '#ff7f00', '#ffff00', '#00ff00', '#007fff', '#0000ff', '#8b00ff'],
'rainbow-pastel': ['#ffb3ba', '#ffdfba', '#ffffba', '#baffc9', '#bae1ff', '#e8baff', '#ffb3e6'],
ocean: ['#0f2027', '#203a43', '#2c5364'],
sunset: ['#ff512f', '#f09819', '#dd2476', '#6a11cb'],
fire: ['#ff0000', '#ff4d00', '#ff9900', '#ffcc00', '#ffe066'],
forest: ['#134e5e', '#56ab2f', '#71b280'],
synthwave: ['#8e2de2', '#4a00e0', '#ff00cc', '#00e5ff'],
grayscale: ['#2b2b2b', '#4a4a4a', '#6f6f6f', '#999999', '#c2c2c2'],
candy: ['#fdfbfb', '#f6d365', '#fda085', '#fbc2eb']
};
const BG_GRADIENT_MAX_STOPS = 7;
// Rebuild the background gradient stop editor rows from state.bgStops
function renderBgStopEditor() {
const list = document.getElementById('bg-stop-list');
if (!list) return;
const sel = document.getElementById('bg-grad-preset');
if (sel) sel.value = state.bgGradPreset || 'custom';
list.innerHTML = '';
state.bgStops.forEach((hex, i) => {
const row = document.createElement('div');
row.className = 'bg-stop-row';
const colorInput = document.createElement('input');
colorInput.type = 'color';
colorInput.className = 'bg-stop-color';
colorInput.value = hex;
colorInput.setAttribute('aria-label', `Background gradient stop ${i + 1}`);
const hexLabel = document.createElement('span');
hexLabel.className = 'bg-stop-hex';
hexLabel.innerText = hex;
const removeBtn = document.createElement('button');
removeBtn.type = 'button';
removeBtn.className = 'bg-stop-remove';
removeBtn.setAttribute('aria-label', `Remove gradient stop ${i + 1}`);
removeBtn.textContent = '×';
removeBtn.disabled = state.bgStops.length <= 2;
colorInput.addEventListener('input', () => {
const val = colorInput.value;
state.bgStops[i] = val;
hexLabel.innerText = val;
setBgGradPresetSelect('custom');
renderBadge();
});
removeBtn.addEventListener('click', () => {
if (state.bgStops.length <= 2) return;
state.bgStops.splice(i, 1);
setBgGradPresetSelect('custom');
renderBgStopEditor();
renderBadge();
});
row.appendChild(colorInput);
row.appendChild(hexLabel);
row.appendChild(removeBtn);
list.appendChild(row);
});
const addBtn = document.getElementById('bg-add-stop');
if (addBtn) addBtn.disabled = state.bgStops.length >= BG_GRADIENT_MAX_STOPS;
}
// Sync the preset dropdown + state with a given preset name
function setBgGradPresetSelect(name) {
state.bgGradPreset = name;
const sel = document.getElementById('bg-grad-preset');
if (sel) sel.value = name;
}
// Preset dropdown change: apply the preset's stops (or leave stops untouched for 'custom')
function onBgGradPresetSelect() {
const sel = document.getElementById('bg-grad-preset');
const name = sel ? sel.value : 'custom';
if (name !== 'custom' && BG_GRADIENT_PRESETS[name]) {
state.bgStops = BG_GRADIENT_PRESETS[name].slice();
renderBgStopEditor();
}
setBgGradPresetSelect(name);
renderBadge();
}
// Add another gradient stop (max 7)
function addBgStop() {
if (state.bgStops.length >= BG_GRADIENT_MAX_STOPS) return;
state.bgStops.push('#ffffff');
setBgGradPresetSelect('custom');
renderBgStopEditor();
renderBadge();
}
function sortedGradientStops(gradient) {
return [...gradient.stops].sort((first, second) => first.offset - second.offset);
}
function selectedTextGradient(part) {
return document.getElementById(GRADIENT_CONTROLS[part].toggle).checked ? state.textGradients[part] : null;
}
function setupGradientPresets(part) {
const select = document.getElementById(`${part}-gradient-preset`);
if (!select) return;
const background = document.getElementById('bg-grad-preset');
select.replaceChildren(...Array.from(background.options, option => option.cloneNode(true)));
// Custom reflects edited stops. Each named preset replaces only the stops.
select.options[0].disabled = true;
select.addEventListener('change', () => {
const colors = BG_GRADIENT_PRESETS[select.value];
if (!colors) return;
state.textGradients[part].stops = colors.map((color, index) => ({
color, offset: svgNumber(100 * index / (colors.length - 1))
}));
renderTextGradientStops(part);
renderBadge();
});
}
function syncGradientPreset(part, gradient) {
const select = document.getElementById(`${part}-gradient-preset`);
if (!select) return;
const stops = sortedGradientStops(gradient);
const match = Object.entries(BG_GRADIENT_PRESETS).find(([, colors]) =>
colors.length === stops.length && colors.every((color, index) =>
color === stops[index].color.toLowerCase() &&
Math.abs(stops[index].offset - 100 * index / (colors.length - 1)) < 0.001
)
);
select.value = match?.[0] || 'custom';
}
function setupGradientDial(part, input) {
const dial = document.getElementById(`${part}-gradient-dial`);
const setAngle = angle => {
const gradient = state.textGradients[part];
if (angle === gradient.angle) return;
gradient.angle = angle;
input.value = angle;
renderBadge();
};
const pointToAngle = event => {
const { left, top, width, height } = dial.getBoundingClientRect();
const dx = event.clientX - left - width / 2;
const dy = event.clientY - top - height / 2;
if (Math.hypot(dx, dy) < 3) return;
const degrees = Math.atan2(dx, -dy) * 180 / Math.PI;
setAngle((Math.round(degrees) + 360) % 360);
};
dial.addEventListener('pointerdown', event => {
if (!event.isPrimary || event.button !== 0) return;
dial.focus({ preventScroll: true });
dial.setPointerCapture(event.pointerId);
pointToAngle(event);
});
dial.addEventListener('pointermove', event => {
if (dial.hasPointerCapture(event.pointerId)) pointToAngle(event);
});
dial.addEventListener('pointerup', event => {
if (dial.hasPointerCapture(event.pointerId)) pointToAngle(event);
});
dial.addEventListener('keydown', event => {
const step = event.shiftKey ? 15 : 1;
const angle = state.textGradients[part].angle;
const values = {
ArrowUp: angle + step, ArrowRight: angle + step,
ArrowDown: angle - step, ArrowLeft: angle - step,
Home: 0, End: 360
};
if (!Object.hasOwn(values, event.key)) return;
event.preventDefault();
setAngle(Math.min(360, Math.max(0, values[event.key])));
});
}
function setupTextGradientEditors() {
for (const part of Object.keys(GRADIENT_CONTROLS)) {
setupGradientPresets(part);
const angleInput = document.getElementById(`${part}-gradient-angle`);
setupGradientDial(part, angleInput);
angleInput.addEventListener('input', event => {
const angle = event.target.valueAsNumber;
if (!Number.isFinite(angle)) return;
state.textGradients[part].angle = Math.min(360, Math.max(0, angle));
renderBadge();
});
angleInput.addEventListener('change', event => {
event.target.value = state.textGradients[part].angle;
});
document.getElementById(`${part}-gradient-add`).addEventListener('click', () => {
const gradient = state.textGradients[part];
const sorted = sortedGradientStops(gradient);
// Insert into the largest gap, including the ends of the track.
const points = [{ ...sorted[0], offset: 0 }, ...sorted, { ...sorted.at(-1), offset: 100 }];
let start = points[0];
let gap = 0;
for (let index = 1; index < points.length; index++) {
const previous = points[index - 1];
const distance = points[index].offset - previous.offset;
if (distance > gap) {
start = previous;
gap = distance;
}
}
gradient.stops.push({ color: start.color, offset: Math.round((start.offset + gap / 2) * 100) / 100 });
renderTextGradientStops(part);
document.querySelector(`#${part}-gradient-stops .text-gradient-stop:last-child input`).focus();
renderBadge();
});
renderTextGradientStops(part);
}
}
function renderTextGradientStops(part) {
const { label } = GRADIENT_CONTROLS[part];
const gradient = state.textGradients[part];
const list = document.getElementById(`${part}-gradient-stops`);
document.getElementById(`${part}-gradient-angle`).value = gradient.angle;
list.replaceChildren();
gradient.stops.forEach((stop, index) => {
const row = document.createElement('div');
row.className = 'text-gradient-stop';
const color = document.createElement('input');
color.type = 'color';
color.className = 'bg-stop-color';
color.value = stop.color;
color.setAttribute('aria-label', `${label} gradient stop ${index + 1} color`);
const hex = document.createElement('span');
hex.className = 'bg-stop-hex';
hex.textContent = stop.color.toUpperCase();
const offset = document.createElement('input');
offset.type = 'number';
offset.className = 'input-text';
offset.min = '0';
offset.max = '100';
offset.step = 'any';
offset.value = stop.offset;
offset.setAttribute('aria-label', `${label} gradient stop ${index + 1} position in percent`);
const percent = document.createElement('span');
percent.textContent = '%';
percent.setAttribute('aria-hidden', 'true');
const remove = document.createElement('button');
remove.type = 'button';
remove.className = 'bg-stop-remove';
remove.textContent = '×';
remove.disabled = gradient.stops.length <= 2;
remove.setAttribute('aria-label', `Remove ${part} gradient stop ${index + 1}`);
color.addEventListener('input', () => {
stop.color = color.value;
hex.textContent = stop.color.toUpperCase();
renderBadge();
});
offset.addEventListener('input', () => {
if (!Number.isFinite(offset.valueAsNumber)) return;
stop.offset = Math.min(100, Math.max(0, offset.valueAsNumber));
renderBadge();
});
offset.addEventListener('change', () => { offset.value = stop.offset; });
remove.addEventListener('click', () => {
gradient.stops.splice(index, 1);
renderTextGradientStops(part);
document.getElementById(`${part}-gradient-add`).focus();
renderBadge();
});
row.append(color, hex, offset, percent, remove);
list.append(row);
});
}
function updateTextGradientControls() {
const style = BADGE_LAYOUTS[state.style];
for (const [part, controls] of Object.entries(GRADIENT_CONTROLS)) {
const toggle = document.getElementById(controls.toggle);
const enabled = toggle ? toggle.checked : document.getElementById('border-mode').value === 'gradient';
const available = controls.parent
? state.logoPosition !== 'none' && document.getElementById(controls.parent).checked
&& (part !== 'outline' || !document.getElementById('show-disk-toggle').checked)
: part === 'border' || !style.minimal;
if (toggle) toggle.disabled = !available;
if (controls.solid) document.getElementById(controls.solid).disabled = !available || enabled;
document.getElementById(`${part}-gradient-editor`).hidden = !available || !enabled;
const gradient = state.textGradients[part];
const dial = document.getElementById(`${part}-gradient-dial`);
dial.style.setProperty('--dial-angle', `${gradient.angle}deg`);
dial.setAttribute('aria-valuenow', gradient.angle);
dial.setAttribute('aria-valuetext', `${gradient.angle} degrees`);
if (available && enabled) syncGradientPreset(part, gradient);
const stops = sortedGradientStops(gradient).map(stop => `${stop.color} ${stop.offset}%`);
document.getElementById(`${part}-gradient-preview`).style.backgroundImage =
`linear-gradient(${gradient.angle}deg, ${stops.join(', ')})`;
}
}
function buildTextGradient(id, gradient, { x, y, width, height }) {
const radians = gradient.angle * Math.PI / 180;
const dx = Math.sin(radians);
const dy = -Math.cos(radians);
const halfSpan = Math.max(0.001, (Math.abs(dx) * width + Math.abs(dy) * height) / 2);
const cx = x + width / 2;
const cy = y + height / 2;
const coordinates = `x1="${svgNumber(cx - dx * halfSpan)}" y1="${svgNumber(cy - dy * halfSpan)}" x2="${svgNumber(cx + dx * halfSpan)}" y2="${svgNumber(cy + dy * halfSpan)}"`;
const stops = sortedGradientStops(gradient).map(({ color, offset }) => `<stop offset="${offset}%" stop-color="${color}"/>`).join('');
return `<linearGradient id="${id}" gradientUnits="userSpaceOnUse" ${coordinates}>${stops}</linearGradient>`;
}
function restoreTextGradients(config) {
for (const part of Object.keys(GRADIENT_CONTROLS)) {
const gradient = config.textGradients?.[part];
if (gradient && Number.isFinite(gradient.angle) && Array.isArray(gradient.stops) && gradient.stops.length >= 2
&& gradient.stops.every(stop => stop && isHexColor(stop.color) && Number.isFinite(stop.offset))) {
state.textGradients[part] = {
angle: Math.min(360, Math.max(0, gradient.angle)),
stops: gradient.stops.map(stop => ({ color: stop.color, offset: Math.min(100, Math.max(0, stop.offset)) }))
};
} else if (part === 'title' && (isHexColor(config.textGradTop) || isHexColor(config.textGradBot))) {
const legacy = defaultTextGradient(isHexColor(config.textGradTop) ? config.textGradTop : undefined);
if (isHexColor(config.textGradBot)) legacy.stops[1].color = config.textGradBot;
state.textGradients.title = legacy;
}
if (part === 'outline' && !gradient && (isHexColor(config.logoStrokeGradTop) || isHexColor(config.logoStrokeGradBot))) {
state.textGradients.outline = { angle: 180, stops: [
{ color: isHexColor(config.logoStrokeGradTop) ? config.logoStrokeGradTop : '#ffffff', offset: 0 },
{ color: isHexColor(config.logoStrokeGradBot) ? config.logoStrokeGradBot : '#61dafb', offset: 100 }
] };
}
renderTextGradientStops(part);
}
}
// Live-sync the hex text next to every color picker (uppercased).
function updateColorHexes() {
document.querySelectorAll('.color-val').forEach(span => {
const wrap = span.closest ? span.closest('.color-picker-wrap') : null;
const input = wrap && wrap.querySelector('input[type="color"]');
if (input) span.textContent = input.value.toUpperCase();
});
}
// Disable controls that only matter when their parent toggle is on, so the UI reads clearly.
function syncDependentControls() {
const dep = (enabled, ids) => {
ids.forEach(id => {
const el = document.getElementById(id);
if (!el) return;
el.disabled = !enabled;
el.classList.toggle('fx-dim', !enabled);
});
};
updateTextGradientControls();
const borderMode = document.getElementById('border-mode').value;
dep(borderMode === 'solid', ['border-color']);
dep(['solid', 'gradient'].includes(borderMode), ['border-width', 'border-opacity']);
const hasText = !BADGE_LAYOUTS[state.style].minimal;
const hasLogo = state.logoPosition !== 'none';
const diskOn = hasLogo && document.getElementById('show-disk-toggle').checked;
dep(hasText, ['top-text', 'bottom-text', 'text-alignment', 'text-stroke-toggle', 'text-shadow-toggle']);
dep(hasLogo, ['show-disk-toggle', 'custom-logo-color-toggle', 'logo-shadow-toggle', 'icon-scale-slider']);
dep(hasLogo && !diskOn, ['logo-stroke-toggle']);
dep(hasLogo && hasText, ['logo-gap']);
dep(hasText && document.getElementById('text-stroke-toggle').checked, ['text-stroke-color', 'text-stroke-width']);
dep(hasText && document.getElementById('text-shadow-toggle').checked, ['text-shadow-color', 'text-shadow-blur']);
dep(diskOn, ['disk-color', 'disk-size-slider']);
const logoStrokeOn = hasLogo && !diskOn && document.getElementById('logo-stroke-toggle').checked;
const logoStrokeGradOn = document.getElementById('logo-stroke-grad-toggle')?.checked || false;
// Solid outline color is unused while the gradient outline is on
dep(logoStrokeOn && !logoStrokeGradOn, ['logo-stroke-color']);
dep(logoStrokeOn, ['logo-stroke-width']);
dep(hasLogo && document.getElementById('logo-shadow-toggle').checked, ['logo-shadow-color', 'logo-shadow-blur']);
}
// Disk-shaped convolution kernel for round (Photoshop-style) outline dilation.
// feMorphology dilates with a SQUARE kernel, which makes diagonals ~1.4x
// thicker than axes at small widths and turns circles into rounded squares at