From a5d0e988f87ceeba499429b58ea21a144f408b44 Mon Sep 17 00:00:00 2001 From: Falkan Date: Tue, 17 Mar 2026 20:00:53 -0400 Subject: [PATCH] feat: auto-ID generation from label with kebab conversion --- src/routes/admin/+page.svelte | 54 ++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/src/routes/admin/+page.svelte b/src/routes/admin/+page.svelte index d3ba651..45f86f1 100644 --- a/src/routes/admin/+page.svelte +++ b/src/routes/admin/+page.svelte @@ -27,6 +27,46 @@ let groupFormSnapshot = $state>({}); let groupFieldError = $state(null); // which field has error + // ── Auto-ID state ───────────────────────────────────────────────────────────── + let unitIdManuallyEdited = $state(false); + let groupIdManuallyEdited = $state(false); + + function toKebab(s: string): string { + return s.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, ''); + } + + function onUnitLabelInput() { + if (!unitIdManuallyEdited) { + unitForm.id = toKebab(unitForm.label ?? ''); + } + } + + function onUnitIdInput() { + const val = unitForm.id ?? ''; + if (val === '') { + unitIdManuallyEdited = false; + unitForm.id = toKebab(unitForm.label ?? ''); + } else { + unitIdManuallyEdited = true; + } + } + + function onGroupLabelInput() { + if (!groupIdManuallyEdited) { + groupForm.id = toKebab(groupForm.label ?? ''); + } + } + + function onGroupIdInput() { + const val = groupForm.id ?? ''; + if (val === '') { + groupIdManuallyEdited = false; + groupForm.id = toKebab(groupForm.label ?? ''); + } else { + groupIdManuallyEdited = true; + } + } + // ── Drag-to-reorder state ───────────────────────────────────────────────────── let dragGroupId = $state(null); // group being dragged let dragOverGroupId = $state(null); // group being hovered over @@ -311,6 +351,7 @@ groupFormNew = false; unitFieldError = null; groupFieldError = null; + unitIdManuallyEdited = false; formError = null; formSuccess = null; } @@ -325,6 +366,7 @@ unitFormNew = false; unitFieldError = null; groupFieldError = null; + groupIdManuallyEdited = false; formError = null; formSuccess = null; } @@ -731,12 +773,14 @@