# Autosave System — Architecture & HTMX Migration Assessment ## Overview The admin panel has a custom JavaScript autosave system (`autosave.js`) that auto-submits forms after a 1.5s debounce. It is used on pages where content is edited with the OverType Markdown editor (static pages, form help blocks) and on structured-data forms (contacts, sidebar links). Three server-side action handlers implement the same CSRF-rotation + JSON response contract that `autosave.js` consumes. ## Architecture ``` ┌──────────────────────────────────┐ │ autosave.js │ │ ───────── │ │ Watches
│ │ Listens: input / change (bubble)│ │ Debounce: 1500ms │ │ POST via fetch() │ │ Accept: application/json │ │ On 2xx: shows "Enregistré ✓" │ │ On err: shows "Erreur !", retry │ │ Updates CSRF token from response│ └──────────┬───────────────────────┘ │ POST (JSON) ▼ ┌──────────────────────────────────┐ │ Backend handler (page.php, │ │ apropos.php, form-help.php) │ │ ────────────────────────────────│ │ 1. Check CSRF token │ │ 2. Validate payload │ │ 3. Save to DB │ │ 4. Regenerate CSRF token │ │ 5. Return JSON: │ │ {success:true, csrf_token:X} │ └──────────────────────────────────┘ ``` ## Forms Using `data-autosave` ### 1. Static Pages (about, licenses, charte) | Attribute | Value | |---|---| | **Template** | `app/templates/admin/contenus-edit.php` (branches `about_page` and `page`) | | **Form action** | `/admin/actions/page.php` | | **Editor** | OverType (custom `contenteditable`-based Markdown WYSIWYG) | | **Fields** | `csrf_token` (hidden), `slug` (hidden), `content` (hidden, synced by OverType `onChange`) | | **How events reach autosave** | User types in OverType's `contenteditable` div → native `input` events bubble up to `` → autosave.js detects them | **Key interaction**: OverType's `onChange(value) { hiddenInput.value = value }` sets the hidden `#content` input's value programmatically. This does NOT fire native DOM events. The autosave trigger comes from `input` events on the contenteditable editor div, not from the hidden input. ### 2. About Contacts (apropos groups) | Attribute | Value | |---|---| | **Template** | `app/templates/admin/apropos-groups-form.php` | | **Form action** | `/admin/actions/apropos.php` | | **Editor** | Native text/email/url inputs | | **Fields** | `csrf_token`, `apropos_key`, `groups[][role]`, `groups[][entries][][text/email/url]` | Has custom JS for adding/removing contact groups and entries (inline `