-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-grid-generator.html
More file actions
590 lines (542 loc) · 32.8 KB
/
Copy pathimage-grid-generator.html
File metadata and controls
590 lines (542 loc) · 32.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
<!DOCTYPE html>
<html lang="en" data-theme="light">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image Grid Generator</title>
<link rel="preconnect" href="https://api.fontshare.com">
<link href="https://api.fontshare.com/v2/css?f[]=satoshi@400,500,700&f[]=cabinet-grotesk@500,700&display=swap" rel="stylesheet">
<style>
:root, [data-theme="light"] {
--text-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
--text-sm: clamp(0.875rem, 0.8rem + 0.35vw, 1rem);
--text-base: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
--text-lg: clamp(1.125rem, 1rem + 0.75vw, 1.5rem);
--text-xl: clamp(1.5rem, 1.2rem + 1.25vw, 2.25rem);
--space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem; --space-4: 1rem; --space-5: 1.25rem; --space-6: 1.5rem; --space-8: 2rem; --space-10: 2.5rem; --space-12: 3rem;
--color-bg: #f7f6f2; --color-surface: #fbfbf9; --color-surface-2: #f1efe9; --color-border: #d7d3cb; --color-divider: #dfdbd2;
--color-text: #25211a; --color-text-muted: #6f6a63; --color-text-faint: #9f998f; --color-text-inverse: #f7f6f2;
--color-primary: #2c74d8; --color-primary-hover: #1d5fb8; --color-primary-highlight: #dbe8fb;
--color-success: #2f7a49; --color-warning: #964219; --color-error: #a12c52;
--radius-sm: 0.5rem; --radius-md: 0.8rem; --radius-lg: 1.1rem; --radius-xl: 1.4rem; --radius-full: 9999px;
--shadow-sm: 0 1px 2px rgba(22,18,10,.05); --shadow-md: 0 12px 30px rgba(25,22,14,.08); --shadow-lg: 0 24px 60px rgba(25,22,14,.12);
--font-body: 'Satoshi', 'Inter', sans-serif; --font-display: 'Cabinet Grotesk', 'Satoshi', sans-serif;
--transition: 180ms cubic-bezier(0.16, 1, 0.3, 1);
}
[data-theme="dark"] {
--color-bg: #171614; --color-surface: #1d1b19; --color-surface-2: #262320; --color-border: #37332f; --color-divider: #2d2926;
--color-text: #ece8e1; --color-text-muted: #b4aea5; --color-text-faint: #827b72; --color-text-inverse: #171614;
--color-primary: #6ea6f3; --color-primary-hover: #8ab6f5; --color-primary-highlight: #26364d;
--color-success: #70b685; --color-warning: #c98658; --color-error: #de6b8e;
--shadow-sm: 0 1px 2px rgba(0,0,0,.2); --shadow-md: 0 12px 30px rgba(0,0,0,.32); --shadow-lg: 0 24px 60px rgba(0,0,0,.42);
}
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body { margin: 0; min-height: 100vh; font-family: var(--font-body); font-size: var(--text-base); color: var(--color-text); background: linear-gradient(180deg, var(--color-bg), var(--color-surface-2)); }
img, svg { display: block; max-width: 100%; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; }
:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 3px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.app-shell { width: min(1400px, calc(100% - 2rem)); margin: 0 auto; padding: var(--space-8) 0 var(--space-12); }
.skip-link { position: absolute; left: var(--space-4); top: -100px; background: var(--color-primary); color: var(--color-text-inverse); padding: var(--space-3) var(--space-4); border-radius: var(--radius-full); z-index: 100; }
.skip-link:focus { top: var(--space-4); }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); margin-bottom: var(--space-8); }
.brand { display: flex; align-items: center; gap: var(--space-3); }
.brand-mark { width: 2.75rem; height: 2.75rem; border-radius: 0.9rem; display: grid; place-items: center; background: linear-gradient(135deg, var(--color-primary-highlight), transparent 70%), var(--color-surface); border: 1px solid var(--color-border); box-shadow: var(--shadow-sm); }
.brand-copy h1 { margin: 0; font-family: var(--font-display); font-size: var(--text-xl); line-height: 1; }
.brand-copy p { margin: 0.35rem 0 0; color: var(--color-text-muted); font-size: var(--text-sm); }
.toolbar { display: flex; align-items: center; gap: var(--space-3); flex-wrap: wrap; }
.icon-button { min-width: 2.75rem; min-height: 2.75rem; border-radius: 0.9rem; border: 1px solid var(--color-border); background: var(--color-surface); display: inline-flex; align-items: center; justify-content: center; box-shadow: var(--shadow-sm); transition: transform var(--transition), border-color var(--transition), background var(--transition); }
.icon-button:hover { transform: translateY(-1px); border-color: var(--color-primary); }
.layout { display: grid; grid-template-columns: minmax(320px, 430px) minmax(0, 1fr); gap: var(--space-6); }
.panel { background: color-mix(in srgb, var(--color-surface) 92%, transparent); border: 1px solid var(--color-border); border-radius: var(--radius-xl); box-shadow: var(--shadow-md); backdrop-filter: blur(12px); }
.controls { padding: var(--space-6); }
.controls section + section { margin-top: var(--space-6); padding-top: var(--space-6); border-top: 1px solid var(--color-divider); }
.section-heading { display: flex; align-items: baseline; justify-content: space-between; gap: var(--space-3); margin-bottom: var(--space-4); }
.section-heading h2 { margin: 0; font-size: var(--text-lg); font-family: var(--font-display); }
.section-heading span { font-size: var(--text-xs); color: var(--color-text-muted); text-transform: uppercase; letter-spacing: .08em; }
.field { display: grid; gap: var(--space-2); margin-bottom: var(--space-4); }
.field:last-child { margin-bottom: 0; }
label { font-size: var(--text-sm); font-weight: 700; }
.hint { color: var(--color-text-muted); font-size: var(--text-xs); }
textarea, input[type="text"], input[type="number"], select {
width: 100%; border: 1px solid var(--color-border); border-radius: var(--radius-md); background: var(--color-bg); color: var(--color-text); padding: .9rem 1rem; transition: border-color var(--transition), box-shadow var(--transition), background var(--transition);
}
textarea { min-height: 10rem; resize: vertical; }
textarea:focus, input:focus, select:focus { border-color: var(--color-primary); box-shadow: 0 0 0 4px color-mix(in srgb, var(--color-primary) 16%, transparent); outline: none; }
input[type="file"] { width: 100%; font-size: var(--text-sm); }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); }
.toggle-row, .checkbox-row { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); padding: .9rem 1rem; background: var(--color-bg); border: 1px solid var(--color-border); border-radius: var(--radius-md); }
.toggle-row strong, .checkbox-row strong { font-size: var(--text-sm); }
.toggle-row span, .checkbox-row span { color: var(--color-text-muted); font-size: var(--text-xs); }
.checkbox-row { justify-content: flex-start; }
.checkbox-row input { width: 1.1rem; height: 1.1rem; margin: 0; accent-color: var(--color-primary); }
.button-row { display: flex; flex-wrap: wrap; gap: var(--space-3); }
.btn { min-height: 2.9rem; padding: 0 1.05rem; border-radius: var(--radius-full); border: 1px solid transparent; display: inline-flex; align-items: center; justify-content: center; gap: .55rem; font-size: var(--text-sm); font-weight: 700; transition: transform var(--transition), background var(--transition), border-color var(--transition), color var(--transition); }
.btn:hover { transform: translateY(-1px); }
.btn-primary { background: var(--color-primary); color: var(--color-text-inverse); }
.btn-primary:hover { background: var(--color-primary-hover); }
.btn-secondary { background: var(--color-surface); color: var(--color-text); border-color: var(--color-border); }
.btn-ghost { background: transparent; color: var(--color-text-muted); border-color: var(--color-border); }
.preview-panel { overflow: hidden; }
.preview-header { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); padding: var(--space-5) var(--space-6); border-bottom: 1px solid var(--color-divider); }
.preview-header h2 { margin: 0; font-size: var(--text-lg); font-family: var(--font-display); }
.preview-header p { margin: .2rem 0 0; color: var(--color-text-muted); font-size: var(--text-sm); }
.preview-meta { font-size: var(--text-sm); color: var(--color-text-muted); }
.preview-body { padding: var(--space-6); display: grid; gap: var(--space-6); }
.canvas-wrap { background: var(--color-bg); border: 1px solid var(--color-border); border-radius: var(--radius-lg); padding: var(--space-5); min-height: 26rem; }
.canvas { display: grid; gap: var(--grid-gap, 16px); grid-template-columns: repeat(var(--grid-columns, 4), minmax(0, 1fr)); align-items: start; }
.card { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: calc(var(--radius-lg) - .15rem); overflow: hidden; box-shadow: var(--shadow-sm); }
.card img { width: 100%; aspect-ratio: var(--preview-ratio, 1 / 1); object-fit: cover; background: var(--color-surface-2); }
.card figcaption { display: none; padding: .85rem .95rem; font-size: var(--text-xs); color: var(--color-text-muted); border-top: 1px solid var(--color-divider); word-break: break-word; }
.canvas.show-captions .card figcaption { display: block; }
.empty-state { min-height: 22rem; display: grid; place-items: center; text-align: center; border: 1px dashed var(--color-border); border-radius: var(--radius-lg); padding: var(--space-8); color: var(--color-text-muted); }
.empty-state svg { width: 3rem; height: 3rem; margin: 0 auto var(--space-4); color: var(--color-text-faint); }
.empty-state h3 { margin: 0 0 .4rem; font-size: var(--text-lg); color: var(--color-text); }
.empty-state p { margin: 0; max-width: 36ch; }
.output-grid { display: grid; grid-template-columns: 1fr; gap: var(--space-4); }
.code-block { background: #121212; color: #f5f5f5; border-radius: var(--radius-lg); padding: 1rem; border: 1px solid rgba(255,255,255,.08); min-height: 14rem; overflow: auto; white-space: pre-wrap; word-break: break-word; font-size: .9rem; line-height: 1.5; }
.output-head { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); margin-bottom: var(--space-2); }
.output-head h3 { margin: 0; font-size: var(--text-base); }
.status { padding: .8rem 1rem; border-radius: var(--radius-md); font-size: var(--text-sm); background: var(--color-primary-highlight); color: var(--color-text); border: 1px solid color-mix(in srgb, var(--color-primary) 25%, var(--color-border)); }
.status[data-tone="error"] { background: color-mix(in srgb, var(--color-error) 12%, var(--color-surface)); border-color: color-mix(in srgb, var(--color-error) 30%, var(--color-border)); }
.status[data-tone="success"] { background: color-mix(in srgb, var(--color-success) 12%, var(--color-surface)); border-color: color-mix(in srgb, var(--color-success) 30%, var(--color-border)); }
.footer-note { margin-top: var(--space-6); color: var(--color-text-muted); font-size: var(--text-xs); }
@media (max-width: 1024px) {
.layout { grid-template-columns: 1fr; }
.app-shell { width: min(100% - 1rem, 1400px); padding-top: var(--space-4); }
}
@media (max-width: 640px) {
.topbar, .preview-header, .output-head { flex-direction: column; align-items: flex-start; }
.grid-2 { grid-template-columns: 1fr; }
.controls, .preview-body { padding: var(--space-4); }
.canvas-wrap { padding: var(--space-4); }
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
</style>
</head>
<body>
<a class="skip-link" href="#main">Skip to content</a>
<div class="app-shell">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 24 24" width="26" height="26" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round">
<rect x="3.5" y="4" width="17" height="16" rx="3.2"></rect>
<path d="M8 14.4 10.7 11l2.5 2.9 2.6-3.4L20 16"></path>
<circle cx="8.1" cy="9.1" r="1.15" fill="currentColor" stroke="none"></circle>
</svg>
</div>
<div class="brand-copy">
<h1>Image Grid Generator</h1>
<p>Paste direct image URLs, import a file of URLs, preview the gallery, then copy Markdown or HTML.</p>
</div>
</div>
<div class="toolbar" aria-label="Utilities">
<button class="icon-button" type="button" id="sampleBtn" aria-label="Load sample URLs" title="Load sample URLs">
<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.8"><path d="M4 7.5h16M4 12h16M4 16.5h11"/><circle cx="17.5" cy="16.5" r="2.5"/></svg>
</button>
<button class="icon-button" type="button" id="clearBtn" aria-label="Clear all input" title="Clear all input">
<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.8"><path d="m5 5 14 14M19 5 5 19"/></svg>
</button>
<button class="icon-button" type="button" id="themeToggle" aria-label="Switch to dark mode" title="Toggle theme">
<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.8"><circle cx="12" cy="12" r="4.2"></circle><path d="M12 2v2.2M12 19.8V22M2 12h2.2M19.8 12H22M4.95 4.95l1.55 1.55M17.5 17.5l1.55 1.55M4.95 19.05l1.55-1.55M17.5 6.5l1.55-1.55"/></svg>
</button>
</div>
</header>
<main id="main" class="layout">
<aside class="panel controls" aria-label="Gallery options">
<section>
<div class="section-heading">
<h2>Image input</h2>
<span>URLs only</span>
</div>
<div class="field">
<label for="urlInput">Paste one image URL per line</label>
<textarea id="urlInput" placeholder="https://example.com/image-1.jpg https://example.com/image-2.png https://example.com/image-3.webp"></textarea>
<div class="hint">You can mix JPG, PNG, WebP, GIF, AVIF, and any other direct image URL.</div>
</div>
<div class="field">
<label for="fileInput">Or upload a text file with image URLs</label>
<input id="fileInput" type="file" accept=".txt,.md,.csv,.tsv,.json,text/plain,text/csv,application/json" />
<div class="hint">The parser grabs URLs line by line or from comma-separated content.</div>
</div>
<div class="button-row">
<button class="btn btn-primary" type="button" id="generateBtn">Generate grid</button>
<button class="btn btn-secondary" type="button" id="appendBtn">Append uploaded file</button>
</div>
</section>
<section>
<div class="section-heading">
<h2>Layout options</h2>
<span>Match your screenshot</span>
</div>
<div class="grid-2">
<div class="field">
<label for="columns">Columns</label>
<select id="columns">
<option value="1">1 Column</option>
<option value="2">2 Columns</option>
<option value="3">3 Columns</option>
<option value="4" selected>4 Columns</option>
<option value="5">5 Columns</option>
<option value="6">6 Columns</option>
</select>
</div>
<div class="field">
<label for="gap">Gap (px)</label>
<input id="gap" type="number" min="0" max="64" value="16" />
</div>
<div class="field">
<label for="imageWidth">Image width</label>
<input id="imageWidth" type="text" value="100%" placeholder="100%" />
</div>
<div class="field">
<label for="imageHeight">Image height</label>
<input id="imageHeight" type="text" value="auto" placeholder="auto" />
</div>
<div class="field">
<label for="objectFit">Object fit</label>
<select id="objectFit">
<option value="cover" selected>Cover</option>
<option value="contain">Contain</option>
<option value="fill">Fill</option>
<option value="scale-down">Scale down</option>
</select>
</div>
<div class="field">
<label for="aspectRatio">Aspect ratio</label>
<select id="aspectRatio">
<option value="auto">Original / auto</option>
<option value="1 / 1" selected>Square</option>
<option value="4 / 5">4:5 portrait</option>
<option value="3 / 4">3:4 portrait</option>
<option value="16 / 9">16:9 landscape</option>
<option value="3 / 2">3:2 landscape</option>
</select>
</div>
</div>
<div class="field">
<label for="htmlMode">HTML output preset</label>
<select id="htmlMode">
<option value="hotlink" selected>Hotlink for websites</option>
<option value="plain">Plain img tags</option>
<option value="figure">Figure + caption</option>
</select>
</div>
</section>
<section>
<div class="section-heading">
<h2>HTML options</h2>
<span>Visible in code output</span>
</div>
<div class="field">
<label for="wrapperClass">Wrapper class</label>
<input id="wrapperClass" type="text" value="image-grid" />
</div>
<div class="field">
<label for="imageClass">Image class</label>
<input id="imageClass" type="text" value="grid-image" />
</div>
<div class="checkbox-row"><input id="openLinks" type="checkbox" checked /><div><strong>Wrap images in links</strong><div class="hint">Makes each image clickable to its direct URL.</div></div></div>
<div class="checkbox-row"><input id="lazyLoad" type="checkbox" checked /><div><strong>Lazy loading</strong><div class="hint">Adds loading="lazy" and decoding="async".</div></div></div>
<div class="checkbox-row"><input id="includeAlt" type="checkbox" checked /><div><strong>Auto alt text</strong><div class="hint">Uses the filename from each URL.</div></div></div>
<div class="checkbox-row"><input id="showCaptions" type="checkbox" /><div><strong>Show captions in preview</strong><div class="hint">Captions use the file name for quick visual checking.</div></div></div>
</section>
<section>
<div class="section-heading">
<h2>Actions</h2>
<span>Copy ready</span>
</div>
<div id="status" class="status" role="status" aria-live="polite">Add URLs, choose your layout, and generate the gallery code.</div>
<div class="footer-note">This file runs entirely in the browser, so your image list stays local to the page.</div>
</section>
</aside>
<section class="panel preview-panel" aria-label="Preview and generated code">
<div class="preview-header">
<div>
<h2>Preview and export</h2>
<p>Preview the grid first, then copy either Markdown or HTML.</p>
</div>
<div class="preview-meta"><span id="imageCount">0 images</span></div>
</div>
<div class="preview-body">
<div class="canvas-wrap">
<div id="emptyState" class="empty-state">
<div>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7"><rect x="3.5" y="4" width="17" height="16" rx="3.2"></rect><path d="M7.8 15.4 10.5 12l2.35 2.7 2.8-3.55L20 16"></path><circle cx="8.2" cy="8.8" r="1.15" fill="currentColor" stroke="none"></circle></svg>
<h3>No gallery yet</h3>
<p>Paste a few direct image URLs or upload a text file full of them, then click Generate grid.</p>
</div>
</div>
<div id="previewGrid" class="canvas" hidden></div>
</div>
<div class="output-grid">
<div>
<div class="output-head">
<h3>Markdown output</h3>
<button class="btn btn-secondary" type="button" data-copy-target="markdownOutput">Copy Markdown</button>
</div>
<pre id="markdownOutput" class="code-block" aria-label="Markdown output"></pre>
</div>
<div>
<div class="output-head">
<h3>HTML output</h3>
<button class="btn btn-secondary" type="button" data-copy-target="htmlOutput">Copy HTML</button>
</div>
<pre id="htmlOutput" class="code-block" aria-label="HTML output"></pre>
</div>
</div>
</div>
</section>
</main>
</div>
<script>
const state = {
images: [],
theme: window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
};
const el = {
root: document.documentElement,
urlInput: document.getElementById('urlInput'),
fileInput: document.getElementById('fileInput'),
generateBtn: document.getElementById('generateBtn'),
appendBtn: document.getElementById('appendBtn'),
clearBtn: document.getElementById('clearBtn'),
sampleBtn: document.getElementById('sampleBtn'),
themeToggle: document.getElementById('themeToggle'),
status: document.getElementById('status'),
previewGrid: document.getElementById('previewGrid'),
emptyState: document.getElementById('emptyState'),
markdownOutput: document.getElementById('markdownOutput'),
htmlOutput: document.getElementById('htmlOutput'),
imageCount: document.getElementById('imageCount'),
columns: document.getElementById('columns'),
gap: document.getElementById('gap'),
imageWidth: document.getElementById('imageWidth'),
imageHeight: document.getElementById('imageHeight'),
objectFit: document.getElementById('objectFit'),
aspectRatio: document.getElementById('aspectRatio'),
htmlMode: document.getElementById('htmlMode'),
wrapperClass: document.getElementById('wrapperClass'),
imageClass: document.getElementById('imageClass'),
openLinks: document.getElementById('openLinks'),
lazyLoad: document.getElementById('lazyLoad'),
includeAlt: document.getElementById('includeAlt'),
showCaptions: document.getElementById('showCaptions')
};
function escapeHtml(value) {
return String(value)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function slugFromUrl(url, index) {
try {
const pathname = new URL(url).pathname.split('/').filter(Boolean).pop() || `image-${index + 1}`;
return decodeURIComponent(pathname).replace(/[-_]+/g, ' ').replace(/\.[a-z0-9]+$/i, '').trim() || `Image ${index + 1}`;
} catch {
return `Image ${index + 1}`;
}
}
function sanitizeUrls(raw) {
const chunks = raw.split(/[\n,\t\r]+/).map(item => item.trim()).filter(Boolean);
const unique = [];
const seen = new Set();
for (const item of chunks) {
if (!/^https?:\/\//i.test(item)) continue;
if (seen.has(item)) continue;
seen.add(item);
unique.push(item);
}
return unique;
}
async function readUploadedFile() {
const file = el.fileInput.files?.[0];
if (!file) return [];
const text = await file.text();
return sanitizeUrls(text);
}
function getSettings() {
return {
columns: Number(el.columns.value || 4),
gap: Math.max(0, Number(el.gap.value || 16)),
imageWidth: (el.imageWidth.value || '100%').trim(),
imageHeight: (el.imageHeight.value || 'auto').trim(),
objectFit: el.objectFit.value,
aspectRatio: el.aspectRatio.value,
htmlMode: el.htmlMode.value,
wrapperClass: (el.wrapperClass.value || 'image-grid').trim(),
imageClass: (el.imageClass.value || 'grid-image').trim(),
openLinks: el.openLinks.checked,
lazyLoad: el.lazyLoad.checked,
includeAlt: el.includeAlt.checked,
showCaptions: el.showCaptions.checked
};
}
function updateStatus(message, tone = 'default') {
el.status.textContent = message;
el.status.dataset.tone = tone;
}
function renderPreview(images, settings) {
el.previewGrid.style.setProperty('--grid-columns', settings.columns);
el.previewGrid.style.setProperty('--grid-gap', `${settings.gap}px`);
el.previewGrid.style.setProperty('--preview-ratio', settings.aspectRatio === 'auto' ? 'auto' : settings.aspectRatio);
el.previewGrid.classList.toggle('show-captions', settings.showCaptions);
el.previewGrid.innerHTML = '';
for (const [index, url] of images.entries()) {
const label = slugFromUrl(url, index);
const figure = document.createElement('figure');
figure.className = 'card';
const img = document.createElement('img');
img.src = url;
img.alt = label;
img.loading = 'lazy';
img.decoding = 'async';
img.style.width = settings.imageWidth;
img.style.height = settings.imageHeight;
img.style.objectFit = settings.objectFit;
if (settings.aspectRatio === 'auto') img.style.aspectRatio = 'auto';
figure.appendChild(img);
const caption = document.createElement('figcaption');
caption.textContent = label;
figure.appendChild(caption);
el.previewGrid.appendChild(figure);
}
const hasImages = images.length > 0;
el.emptyState.hidden = hasImages;
el.previewGrid.hidden = !hasImages;
el.imageCount.textContent = `${images.length} image${images.length === 1 ? '' : 's'}`;
}
function buildMarkdown(images) {
return images.map((url, index) => ``).join('\n');
}
function buildHtml(images, settings) {
const wrapperStyle = [
'display:grid',
`grid-template-columns:repeat(${settings.columns}, minmax(0, 1fr))`,
`gap:${settings.gap}px`
].join('; ');
const imgStyleParts = [`width:${settings.imageWidth}`];
if (settings.imageHeight) imgStyleParts.push(`height:${settings.imageHeight}`);
if (settings.objectFit) imgStyleParts.push(`object-fit:${settings.objectFit}`);
if (settings.aspectRatio !== 'auto') imgStyleParts.push(`aspect-ratio:${settings.aspectRatio}`);
const imgStyle = imgStyleParts.join('; ');
const lines = [`<div class="${escapeHtml(settings.wrapperClass)}" style="${escapeHtml(wrapperStyle)}">`];
for (const [index, url] of images.entries()) {
const alt = settings.includeAlt ? slugFromUrl(url, index) : '';
const attrs = [`src="${escapeHtml(url)}"`, `class="${escapeHtml(settings.imageClass)}"`, `style="${escapeHtml(imgStyle)}"`];
if (settings.includeAlt) attrs.push(`alt="${escapeHtml(alt)}"`);
if (settings.lazyLoad) attrs.push('loading="lazy"', 'decoding="async"');
const imgTag = `<img ${attrs.join(' ')}>`;
if (settings.htmlMode === 'plain') {
lines.push(` ${settings.openLinks ? `<a href="${escapeHtml(url)}" target="_blank" rel="noopener noreferrer">${imgTag}</a>` : imgTag}`);
continue;
}
if (settings.htmlMode === 'figure') {
const media = settings.openLinks ? `<a href="${escapeHtml(url)}" target="_blank" rel="noopener noreferrer">${imgTag}</a>` : imgTag;
lines.push(' <figure>');
lines.push(` ${media}`);
lines.push(` <figcaption>${escapeHtml(slugFromUrl(url, index))}</figcaption>`);
lines.push(' </figure>');
continue;
}
lines.push(` <a href="${escapeHtml(url)}" target="_blank" rel="noopener noreferrer">${imgTag}</a>`);
}
lines.push('</div>');
return lines.join('\n');
}
function syncOutputs() {
const settings = getSettings();
renderPreview(state.images, settings);
el.markdownOutput.textContent = state.images.length ? buildMarkdown(state.images) : 'Markdown output will appear here.';
el.htmlOutput.textContent = state.images.length ? buildHtml(state.images, settings) : 'HTML output will appear here.';
}
async function generateFromInputs(mode = 'replace') {
const textareaUrls = sanitizeUrls(el.urlInput.value);
const fileUrls = await readUploadedFile();
const merged = mode === 'append'
? [...state.images, ...textareaUrls, ...fileUrls]
: [...textareaUrls, ...fileUrls];
state.images = [...new Set(merged)];
if (!state.images.length) {
syncOutputs();
updateStatus('No valid image URLs were found. Make sure the links begin with http:// or https://.', 'error');
return;
}
syncOutputs();
updateStatus(`Generated a gallery with ${state.images.length} image${state.images.length === 1 ? '' : 's'}.`, 'success');
}
async function appendUploadedOnly() {
const fileUrls = await readUploadedFile();
if (!fileUrls.length) {
updateStatus('The uploaded file did not contain any valid direct image URLs.', 'error');
return;
}
state.images = [...new Set([...state.images, ...sanitizeUrls(el.urlInput.value), ...fileUrls])];
syncOutputs();
updateStatus(`Added ${fileUrls.length} URL${fileUrls.length === 1 ? '' : 's'} from the uploaded file.`, 'success');
}
async function copyFrom(targetId) {
const text = document.getElementById(targetId).textContent.trim();
if (!text || text.includes('will appear here')) {
updateStatus('There is nothing to copy yet. Generate the gallery first.', 'error');
return;
}
try {
await navigator.clipboard.writeText(text);
updateStatus(`${targetId === 'markdownOutput' ? 'Markdown' : 'HTML'} copied to the clipboard.`, 'success');
} catch {
updateStatus('Clipboard access failed in this browser view. You can still select the code manually.', 'error');
}
}
function loadSample() {
el.urlInput.value = [
'https://images.unsplash.com/photo-1500530855697-b586d89ba3ee?auto=format&fit=crop&w=900&q=80',
'https://images.unsplash.com/photo-1493246507139-91e8fad9978e?auto=format&fit=crop&w=900&q=80',
'https://images.unsplash.com/photo-1519681393784-d120267933ba?auto=format&fit=crop&w=900&q=80',
'https://images.unsplash.com/photo-1500534314209-a25ddb2bd429?auto=format&fit=crop&w=900&q=80'
].join('\n');
generateFromInputs('replace');
}
function clearAll() {
state.images = [];
el.urlInput.value = '';
el.fileInput.value = '';
syncOutputs();
updateStatus('Cleared the current URLs and outputs.', 'default');
}
function setTheme(theme) {
state.theme = theme;
el.root.setAttribute('data-theme', theme);
el.themeToggle.setAttribute('aria-label', `Switch to ${theme === 'dark' ? 'light' : 'dark'} mode`);
el.themeToggle.innerHTML = theme === 'dark'
? '<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.8"><path d="M20.5 14.2A8.5 8.5 0 1 1 9.8 3.5a6.9 6.9 0 0 0 10.7 10.7Z"></path></svg>'
: '<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.8"><circle cx="12" cy="12" r="4.2"></circle><path d="M12 2v2.2M12 19.8V22M2 12h2.2M19.8 12H22M4.95 4.95l1.55 1.55M17.5 17.5l1.55 1.55M4.95 19.05l1.55-1.55M17.5 6.5l1.55-1.55"></path></svg>';
}
el.generateBtn.addEventListener('click', () => generateFromInputs('replace'));
el.appendBtn.addEventListener('click', appendUploadedOnly);
el.clearBtn.addEventListener('click', clearAll);
el.sampleBtn.addEventListener('click', loadSample);
el.themeToggle.addEventListener('click', () => setTheme(state.theme === 'dark' ? 'light' : 'dark'));
document.querySelectorAll('[data-copy-target]').forEach(button => {
button.addEventListener('click', () => copyFrom(button.dataset.copyTarget));
});
['columns','gap','imageWidth','imageHeight','objectFit','aspectRatio','htmlMode','wrapperClass','imageClass','openLinks','lazyLoad','includeAlt','showCaptions'].forEach(id => {
document.getElementById(id).addEventListener('input', syncOutputs);
document.getElementById(id).addEventListener('change', syncOutputs);
});
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
setTheme(event.matches ? 'dark' : 'light');
});
setTheme(state.theme);
syncOutputs();
</script>
</body>
</html>