-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathcommit-message-generator.html
More file actions
591 lines (515 loc) · 18.7 KB
/
Copy pathcommit-message-generator.html
File metadata and controls
591 lines (515 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
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
<!--
Commit Message Generator
Single-file client-side tool to build Conventional Commits-formatted commit
messages with live preview and validation.
Built for One File Tools.
-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Commit Message Generator - One File Tools</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet" />
<style>
:root {
--bg: #030712;
--grid-line: #111827;
--panel: rgba(17, 24, 39, 0.7);
--panel-border: #1f2937;
--accent: #fb923c;
--accent-hover: #f97316;
--accent-glow: rgba(251, 146, 60, 0.15);
--text: #f9fafb;
--text-muted: #9ca3af;
--text-dim: #4b5563;
--radius-sm: 12px;
--radius-md: 20px;
--font-sans: "Inter", system-ui, sans-serif;
--font-mono: "JetBrains Mono", monospace;
--font-display: "Space Grotesk", sans-serif;
--color-weak: #ef4444;
--color-fair: #f59e0b;
--color-strong: #10b981;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: var(--font-sans);
background-color: var(--bg);
background-image: linear-gradient(var(--grid-line) 1px, transparent 1px), linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
background-size: 32px 32px;
color: var(--text);
padding: 48px 20px 80px;
min-height: 100vh;
display: flex;
justify-content: center;
}
.app {
width: 100%;
max-width: 860px;
}
.hero {
text-align: center;
margin-bottom: 28px;
}
.eyebrow {
font-family: var(--font-mono);
font-size: 12px;
letter-spacing: 0.2em;
color: var(--accent);
text-transform: uppercase;
margin-bottom: 12px;
font-weight: 500;
}
.wordmark {
font-family: var(--font-display);
font-weight: 700;
font-size: clamp(26px, 5vw, 38px);
letter-spacing: -0.02em;
margin-bottom: 12px;
}
.tagline {
color: var(--text-muted);
font-size: 15px;
line-height: 1.5;
max-width: 580px;
margin: 0 auto;
}
.studio {
background: var(--panel);
backdrop-filter: blur(20px);
border: 1px solid var(--panel-border);
border-radius: var(--radius-md);
padding: 24px;
box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.5);
margin-bottom: 24px;
}
.section-label {
font-family: var(--font-mono);
font-size: 12px;
font-weight: 600;
color: var(--text-muted);
text-transform: uppercase;
letter-spacing: 0.1em;
display: block;
margin-bottom: 14px;
}
.field {
margin-bottom: 16px;
}
.field label {
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-dim);
text-transform: uppercase;
letter-spacing: 0.05em;
display: block;
margin-bottom: 6px;
}
.field input,
.field select,
.field textarea {
width: 100%;
background: rgba(17, 24, 39, 0.8);
border: 1px solid var(--panel-border);
border-radius: 8px;
color: var(--text);
font-family: var(--font-mono);
font-size: 14px;
padding: 10px 12px;
}
.field textarea {
resize: vertical;
min-height: 70px;
line-height: 1.5;
font-family: var(--font-sans);
}
.field input:focus,
.field select:focus,
.field textarea:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-glow);
}
.type-help {
font-size: 12px;
color: var(--text-dim);
margin-top: 6px;
}
.type-row {
display: grid;
grid-template-columns: 140px 1fr;
gap: 10px;
}
.subject-row {
display: flex;
gap: 10px;
}
.subject-row .field {
flex: 1;
margin-bottom: 0;
}
.char-counter {
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-dim);
margin-top: 6px;
text-align: right;
}
.char-counter.over {
color: var(--color-fair);
}
.checkbox-row {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 14px;
}
.checkbox-row input {
width: auto;
accent-color: var(--accent);
}
.checkbox-row label {
font-size: 13px;
color: var(--text-muted);
margin-bottom: 0;
text-transform: none;
letter-spacing: normal;
font-family: var(--font-sans);
}
.field-error {
font-size: 12px;
color: var(--color-weak);
margin-top: 6px;
min-height: 14px;
}
.examples-row {
display: flex;
gap: 6px;
flex-wrap: wrap;
margin-top: 8px;
}
.example-chip {
background: rgba(31, 41, 55, 0.5);
border: 1px solid var(--panel-border);
color: var(--text-muted);
border-radius: 6px;
padding: 5px 10px;
font-family: var(--font-mono);
font-size: 11px;
cursor: pointer;
}
.example-chip:hover {
background: rgba(55, 65, 81, 0.7);
color: var(--text);
}
.preview-block {
font-family: var(--font-mono);
font-size: 14px;
line-height: 1.7;
background: rgba(3, 7, 18, 0.5);
border: 1px solid var(--panel-border);
border-radius: var(--radius-sm);
padding: 16px;
white-space: pre-wrap;
word-break: break-word;
min-height: 40px;
color: var(--text);
}
.preview-block:empty::before {
content: "Your commit message will appear here.";
color: var(--text-dim);
}
.actions {
display: flex;
gap: 10px;
margin-top: 14px;
flex-wrap: wrap;
}
.btn {
background: var(--accent);
color: #030712;
border: none;
border-radius: 8px;
padding: 11px 18px;
font-family: var(--font-sans);
font-size: 14px;
font-weight: 700;
cursor: pointer;
}
.btn:hover {
background: var(--accent-hover);
}
.btn:focus-visible,
.btn-ghost:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
.btn-ghost {
background: rgba(31, 41, 55, 0.6);
border: 1px solid var(--panel-border);
color: var(--text);
border-radius: 8px;
padding: 11px 16px;
font-family: var(--font-sans);
font-size: 13px;
font-weight: 600;
cursor: pointer;
}
.btn-ghost:hover {
background: rgba(55, 65, 81, 0.8);
}
.footer-note {
text-align: center;
color: var(--text-dim);
font-size: 13px;
margin-top: 8px;
}
.footer-note a {
color: var(--text-muted);
}
.toast {
position: fixed;
bottom: 24px;
left: 50%;
transform: translateX(-50%) translateY(100px);
background: var(--text);
color: var(--bg);
padding: 12px 22px;
border-radius: 99px;
font-family: var(--font-sans);
font-weight: 600;
font-size: 14px;
opacity: 0;
transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
z-index: 100;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}
.toast.show {
transform: translateX(-50%) translateY(0);
opacity: 1;
}
@media (max-width: 640px) {
.studio {
padding: 18px;
}
.type-row {
grid-template-columns: 1fr;
}
.subject-row {
flex-direction: column;
}
}
</style>
</head>
<body>
<div class="app">
<header class="hero">
<div class="eyebrow">Developer Utilities</div>
<h1 class="wordmark">Commit Message Generator</h1>
<p class="tagline">Build a properly formatted Conventional Commits message — type, scope, description, body, and breaking changes — with live preview and validation.</p>
</header>
<main class="studio">
<span class="section-label">Commit details</span>
<div class="field type-row">
<div>
<label for="typeSelect">Type</label>
<select id="typeSelect"></select>
</div>
<div class="field" style="margin-bottom: 0">
<label for="scopeInput">Scope (optional)</label>
<input type="text" id="scopeInput" placeholder="e.g. auth, api, ui" spellcheck="false" />
</div>
</div>
<div class="type-help" id="typeHelp"></div>
<div class="field" style="margin-top: 16px">
<label for="subjectInput">Short description</label>
<div class="subject-row">
<input type="text" id="subjectInput" placeholder="add login with Google OAuth" spellcheck="false" />
</div>
<div class="char-counter" id="subjectCounter">0 / 50</div>
<div class="examples-row" id="examplesRow"></div>
</div>
<div class="field">
<label for="bodyInput">Body (optional)</label>
<textarea id="bodyInput" placeholder="Explain what changed and why, in more detail…"></textarea>
</div>
<div class="field">
<label for="footerInput">Footer (optional)</label>
<input type="text" id="footerInput" placeholder="e.g. Closes #123" spellcheck="false" />
</div>
<div class="checkbox-row">
<input type="checkbox" id="breakingToggle" />
<label for="breakingToggle">This is a breaking change</label>
</div>
<div class="field" id="breakingFieldWrap" style="display: none">
<label for="breakingInput">BREAKING CHANGE description</label>
<textarea id="breakingInput" placeholder="Describe what breaks and how to migrate…"></textarea>
</div>
<div class="field-error" id="validationError"></div>
</main>
<main class="studio">
<span class="section-label">Preview</span>
<div class="preview-block" id="previewBlock"></div>
<div class="actions">
<button class="btn" id="btnCopy" type="button">Copy commit message</button>
<button class="btn-ghost" id="btnReset" type="button">Reset</button>
</div>
</main>
<p class="footer-note">Part of <a href="https://github.com/praveenscience/One-File-Tools">One File Tools</a>. Follows the <a href="https://www.conventionalcommits.org/">Conventional Commits</a> specification. Runs completely offline.</p>
</div>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
<script>
/* ==========================================================
Commit Message Generator
Vanilla JS, no dependencies.
========================================================== */
const typeSelect = document.getElementById("typeSelect");
const typeHelp = document.getElementById("typeHelp");
const scopeInput = document.getElementById("scopeInput");
const subjectInput = document.getElementById("subjectInput");
const subjectCounter = document.getElementById("subjectCounter");
const examplesRow = document.getElementById("examplesRow");
const bodyInput = document.getElementById("bodyInput");
const footerInput = document.getElementById("footerInput");
const breakingToggle = document.getElementById("breakingToggle");
const breakingFieldWrap = document.getElementById("breakingFieldWrap");
const breakingInput = document.getElementById("breakingInput");
const validationError = document.getElementById("validationError");
const previewBlock = document.getElementById("previewBlock");
const btnCopy = document.getElementById("btnCopy");
const btnReset = document.getElementById("btnReset");
const toast = document.getElementById("toast");
const SUBJECT_RECOMMENDED_MAX = 50;
// Conventional Commits standard types, with a brief plain-English
// explanation and one worked example each for beginners.
const COMMIT_TYPES = [
{ type: "feat", help: "A new feature for the user.", example: "add dark mode toggle to settings" },
{ type: "fix", help: "A bug fix.", example: "prevent crash when input is empty" },
{ type: "docs", help: "Documentation-only changes.", example: "update README with setup instructions" },
{ type: "style", help: "Formatting changes with no code meaning change (whitespace, semicolons, etc.).", example: "run prettier across the codebase" },
{ type: "refactor", help: "A code change that neither fixes a bug nor adds a feature.", example: "extract validation logic into a helper" },
{ type: "perf", help: "A code change that improves performance.", example: "memoize expensive calculation" },
{ type: "test", help: "Adding or correcting tests.", example: "add unit tests for date parser" },
{ type: "build", help: "Changes to the build system or external dependencies.", example: "bump webpack to v5" },
{ type: "ci", help: "Changes to CI configuration and scripts.", example: "add lint step to GitHub Actions" },
{ type: "chore", help: "Other changes that don't modify src or test files.", example: "update .gitignore" },
{ type: "revert", help: "Reverts a previous commit.", example: "revert 'add experimental cache layer'" }
];
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
setTimeout(() => toast.classList.remove("show"), 2000);
}
/** Populates the type dropdown from COMMIT_TYPES. */
function populateTypes() {
COMMIT_TYPES.forEach((t) => {
const opt = document.createElement("option");
opt.value = t.type;
opt.textContent = t.type;
typeSelect.appendChild(opt);
});
}
function getSelectedType() {
return COMMIT_TYPES.find((t) => t.type === typeSelect.value) || COMMIT_TYPES[0];
}
/** Updates the type explanation line and example chip below it. */
function renderTypeHelp() {
const info = getSelectedType();
typeHelp.textContent = info.help;
examplesRow.innerHTML = "";
const chip = document.createElement("button");
chip.type = "button";
chip.className = "example-chip";
chip.textContent = "Try example: " + info.example;
chip.addEventListener("click", () => {
subjectInput.value = info.example;
render();
});
examplesRow.appendChild(chip);
}
/** Validates a raw scope string — lowercase letters, digits, hyphens only. */
function isValidScope(scope) {
return scope === "" || /^[a-z0-9-]+$/i.test(scope);
}
/** Builds the full commit message string from current field values. */
function buildMessage() {
const type = typeSelect.value;
const scope = scopeInput.value.trim();
const subject = subjectInput.value.trim();
const body = bodyInput.value.trim();
const footer = footerInput.value.trim();
const isBreaking = breakingToggle.checked;
const breakingDesc = breakingInput.value.trim();
const header = type + (scope ? "(" + scope + ")" : "") + (isBreaking ? "!" : "") + ": " + subject;
const parts = [header];
if (body) parts.push(body);
const footerLines = [];
if (isBreaking && breakingDesc) footerLines.push("BREAKING CHANGE: " + breakingDesc);
if (footer) footerLines.push(footer);
if (footerLines.length) parts.push(footerLines.join("\n"));
return parts.join("\n\n");
}
/** Validates the form and returns an error message, or empty string if valid. */
function validate() {
const subject = subjectInput.value.trim();
const scope = scopeInput.value.trim();
if (subject === "") return "A short description is required.";
if (/[.]$/.test(subject)) return "Conventional Commits recommends no trailing period on the subject.";
if (!isValidScope(scope)) return "Scope should only contain letters, numbers, and hyphens (e.g. auth, api-v2).";
if (breakingToggle.checked && breakingInput.value.trim() === "") return "Describe the breaking change, or uncheck the breaking-change box.";
return "";
}
/** Recomputes and renders the preview, character counter, and validation state. */
function render() {
renderTypeHelp();
const len = subjectInput.value.length;
subjectCounter.textContent = len + " / " + SUBJECT_RECOMMENDED_MAX;
subjectCounter.classList.toggle("over", len > SUBJECT_RECOMMENDED_MAX);
breakingFieldWrap.style.display = breakingToggle.checked ? "block" : "none";
const error = validate();
validationError.textContent = error;
previewBlock.textContent = error ? "" : buildMessage();
}
[typeSelect, scopeInput, subjectInput, bodyInput, footerInput, breakingInput].forEach((el) => {
el.addEventListener("input", render);
});
typeSelect.addEventListener("change", render);
breakingToggle.addEventListener("change", render);
btnCopy.addEventListener("click", () => {
const text = previewBlock.textContent;
if (!text) {
showToast("Fix the errors above first");
return;
}
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard
.writeText(text)
.then(() => showToast("Commit message copied!"))
.catch(() => showToast("Copy failed — select the text manually"));
} else {
showToast("Clipboard unavailable");
}
});
btnReset.addEventListener("click", () => {
typeSelect.value = "feat";
scopeInput.value = "";
subjectInput.value = "";
bodyInput.value = "";
footerInput.value = "";
breakingToggle.checked = false;
breakingInput.value = "";
render();
subjectInput.focus();
});
populateTypes();
render();
</script>
</body>
</html>