mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-06-26 00:29:18 +02:00
Rework contenus-edit: auto-save, OverType toolbar, dynamic sidebar links
- Auto-save: new autosave.js with 1.5s debounce, watches all forms with
data-autosave, POSTs to form action with Accept: application/json, shows
saving/saved/error status indicator
- All action handlers (page.php, apropos.php, form-help.php) now detect
JSON Accept header and return {success, csrf_token} or {error} responses
- OverType toolbar enabled (toolbar:true) on all three markdown editors
(page, about_page, form_help)
- Sidebar links: replaced fixed erg_site_url / source_code_url rows with
dynamic sidebar_links array of {label, url} objects. Add/remove via JS.
Fallback migration reads legacy keys if sidebar_links is empty.
- Updated AboutController and about.php template to render dynamic links
- Updated apropos.css: unified .apropos-toc-link replacing .apropos-toc-erg
and .apropos-toc-source
- New CSS: autosave-status states, sidebar-link-row layout
- Removed all Enregistrer + Annuler buttons — auto-save and h1 back-arrow
make them redundant
This commit is contained in:
76
app/public/assets/js/app/autosave.js
Normal file
76
app/public/assets/js/app/autosave.js
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Auto-save for admin content edit forms.
|
||||
*
|
||||
* Watches forms with [data-autosave] attribute. Debounces 1.5s after the
|
||||
* last change, POSTs to the form's action, and shows a small status bar.
|
||||
*
|
||||
* The status indicator lives in a sibling element with [data-autosave-status].
|
||||
* States: idle → saving… → saved ✓ / error !
|
||||
*/
|
||||
(() => {
|
||||
const forms = document.querySelectorAll('form[data-autosave]');
|
||||
if (!forms.length) return;
|
||||
|
||||
const DEBOUNCE_MS = 1500;
|
||||
|
||||
for (const form of forms) {
|
||||
const statusEl = document.querySelector(form.dataset.autosaveStatus || '[data-autosave-status]');
|
||||
let timer = null;
|
||||
let dirty = false;
|
||||
|
||||
const setStatus = (text, cls) => {
|
||||
if (!statusEl) return;
|
||||
statusEl.textContent = text;
|
||||
statusEl.className = 'autosave-status ' + (cls || '');
|
||||
};
|
||||
|
||||
const doSave = () => {
|
||||
if (!dirty) return;
|
||||
dirty = false;
|
||||
setStatus('Enregistrement…', 'autosave-status--saving');
|
||||
|
||||
const formData = new FormData(form);
|
||||
|
||||
fetch(form.action, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
headers: { 'Accept': 'application/json' },
|
||||
})
|
||||
.then((r) => {
|
||||
if (!r.ok) throw new Error('HTTP ' + r.status);
|
||||
setStatus('Enregistré ✓', 'autosave-status--saved');
|
||||
// Refresh CSRF token from response if provided
|
||||
r.json().then((data) => {
|
||||
if (data && data.csrf_token) {
|
||||
const csrfInput = form.querySelector('input[name="csrf_token"]');
|
||||
if (csrfInput) csrfInput.value = data.csrf_token;
|
||||
const meta = document.querySelector('meta[name="csrf-token"]');
|
||||
if (meta) meta.setAttribute('content', data.csrf_token);
|
||||
}
|
||||
}).catch(() => {});
|
||||
})
|
||||
.catch(() => {
|
||||
setStatus('Erreur !', 'autosave-status--error');
|
||||
dirty = true;
|
||||
});
|
||||
};
|
||||
|
||||
const schedule = () => {
|
||||
dirty = true;
|
||||
setStatus('', '');
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(doSave, DEBOUNCE_MS);
|
||||
};
|
||||
|
||||
// Watch all inputs inside the form
|
||||
form.addEventListener('input', schedule);
|
||||
form.addEventListener('change', schedule);
|
||||
|
||||
// Clear dirty on manual submit
|
||||
form.addEventListener('submit', () => {
|
||||
clearTimeout(timer);
|
||||
dirty = false;
|
||||
setStatus('', '');
|
||||
});
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user