feat: Enter to save, Escape to discard, auto-save on unit/group selection

- selectUnit/selectGroup now async; call autoSaveCurrentForm before switching
- Enter in any form field (except textarea) triggers save
- Escape restores form to snapshot (discards unsaved changes)
- discardUnitForm/discardGroupForm helpers for snapshot restore
This commit is contained in:
Falkan
2026-03-18 19:42:56 -04:00
parent 554b5ba079
commit 1204ede140
2 changed files with 65 additions and 20 deletions

View File

@@ -2,68 +2,69 @@
"units": [
{
"id": "pound",
"label": "Pound",
"labelPlural": "Pounds",
"label": "pound",
"labelPlural": "pounds",
"symbol": "lb",
"group": "dickloads",
"toBase": 0.038
},
{
"id": "dickload",
"label": "Dickload",
"labelPlural": "Dickloads",
"label": "dickload",
"labelPlural": "dickloads",
"symbol": "dl",
"group": "dickloads",
"toBase": 1
},
{
"id": "barrel",
"label": "Barrel",
"labelPlural": "Barrels",
"label": "barrel",
"labelPlural": "barrels",
"symbol": "bbl",
"group": "dickloads",
"toBase": 19
},
{
"id": "hogshead",
"label": "Hogshead",
"labelPlural": "Hogsheads",
"label": "hogshead",
"labelPlural": "hogsheads",
"symbol": "hhd",
"group": "dickloads",
"toBase": 38
},
{
"id": "short-ton",
"label": "Short ton",
"labelPlural": "Short tons",
"label": "short ton",
"labelPlural": "short tons",
"symbol": "t",
"group": "dickloads",
"toBase": 76
},
{
"id": "fuckton",
"label": "Fuck ton",
"labelPlural": "Fucktons",
"label": "fuck ton",
"labelPlural": "fuck tons",
"symbol": "fcktn",
"group": "dickloads",
"toBase": 152
},
{
"id": "long-ton",
"label": "Long ton",
"labelPlural": "Long tons",
"label": "long ton",
"labelPlural": "long tons",
"symbol": "long tn",
"group": "dickloads",
"toBase": 85.1199999999
"toBase": 85.1199999999,
"hidden": false
},
{
"id": "metric-fuckton",
"label": "Metric fuckton",
"labelPlural": "Metric fucktons",
"label": "metric fuckton",
"labelPlural": "metric fucktons",
"symbol": "mfcktn",
"group": "dickloads",
"toBase": 170.2399999998,
"description": "Trivia: although the name is \"metric fuckton,\" it is actually equal to two long tons!",
"description": "Trivia: although the name is \"metric fuckton,\" it is actually equal to two {long-ton:plural}!",
"hidden": false
},
{

View File

@@ -214,7 +214,9 @@
});
// ── Selection ────────────────────────────────────────────────────────────────
function selectUnit(unit: Unit) {
async function selectUnit(unit: Unit) {
const ok = await autoSaveCurrentForm();
if (!ok) return;
selectedItem = { type: 'unit', id: unit.id };
unitForm = { ...unit };
unitFormSnapshot = { ...unit };
@@ -226,7 +228,9 @@
formSuccess = null;
}
function selectGroup(group: Group) {
async function selectGroup(group: Group) {
const ok = await autoSaveCurrentForm();
if (!ok) return;
selectedItem = { type: 'group', id: group.id };
groupForm = { ...group };
groupFormSnapshot = { ...group };
@@ -446,6 +450,40 @@
await loadData();
}
// ── Keyboard shortcuts for forms ─────────────────────────────────────────────
function discardUnitForm() {
unitForm = { ...unitFormSnapshot };
unitFieldError = null;
formError = null;
formSuccess = null;
}
function discardGroupForm() {
groupForm = { ...groupFormSnapshot };
groupFieldError = null;
formError = null;
formSuccess = null;
}
function onUnitFormKeydown(e: KeyboardEvent) {
// Don't intercept Enter inside textareas
if (e.key === 'Enter' && (e.target as HTMLElement).tagName !== 'TEXTAREA') {
e.preventDefault();
saveUnit();
} else if (e.key === 'Escape') {
discardUnitForm();
}
}
function onGroupFormKeydown(e: KeyboardEvent) {
if (e.key === 'Enter' && (e.target as HTMLElement).tagName !== 'TEXTAREA') {
e.preventDefault();
saveGroup();
} else if (e.key === 'Escape') {
discardGroupForm();
}
}
// ── Unit save/delete ─────────────────────────────────────────────────────────
async function saveUnit() {
formError = null;
@@ -817,6 +855,8 @@
{/if}
{#if selectedItem?.type === 'unit' || unitFormNew}
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div onkeydown={onUnitFormKeydown}>
<h3>{unitFormNew ? 'New Unit' : 'Edit Unit'}</h3>
<label>
ID {#if !unitFormNew}<small class="muted">(readonly)</small>{/if}
@@ -871,8 +911,11 @@
<button class="outline secondary" onclick={deleteUnit}>Delete</button>
{/if}
</div>
</div><!-- /onkeydown unit form -->
{:else if selectedItem?.type === 'group' || groupFormNew}
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div onkeydown={onGroupFormKeydown}>
<h3>{groupFormNew ? 'New Group' : 'Edit Group'}</h3>
<label>
ID {#if !groupFormNew}<small class="muted">(readonly)</small>{/if}
@@ -926,6 +969,7 @@
>Delete</button>
{/if}
</div>
</div><!-- /onkeydown group form -->
{:else}
<p class="select-hint">Select a unit or group to edit, or add a new one.</p>