Éditeur : brouillon, bouton Rétablir et catalogue mis à jour en direct
- Chaque modification est écrite dans src/config.draft.json ; config.json ne change qu'au clic sur Enregistrer. L'aperçu affiche le brouillon tant qu'il existe. - Nouveau bouton "Rétablir l'original" qui abandonne le brouillon. - Le catalogue YunoHost est filtré côté éditeur selon l'état courant : une app retirée avec la croix y réapparaît immédiatement. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a35b732fa6
commit
d42141eb9f
6 files changed
+162
-32
No files matched your search
+82
-15
@@ -1,7 +1,7 @@
|
||||
let state = null;
|
||||
let dirty = false;
|
||||
let dragSource = null; // { appId }, { groupId } or { catalogApp } currently being dragged
|
||||
let catalog = []; // apps YunoHost pas encore dans le portail (voir /__catalog)
|
||||
let catalog = []; // toutes les apps installees sur YunoHost (voir /__catalog)
|
||||
let catalogError = null;
|
||||
|
||||
const PALETTE = [
|
||||
@@ -32,9 +32,53 @@ function setStatus(text, kind) {
|
||||
el.className = "status" + (kind ? " " + kind : "");
|
||||
}
|
||||
|
||||
// Les modifications sont ecrites dans un brouillon (config.draft.json) que
|
||||
// l'apercu utilise ; config.json ne change qu'au clic sur Enregistrer.
|
||||
let draftTimer = null;
|
||||
let draftWrite = Promise.resolve();
|
||||
|
||||
function setDirty(value) {
|
||||
dirty = value;
|
||||
document.getElementById("revert-btn").disabled = !value;
|
||||
if (value) setStatus("Brouillon non enregistré");
|
||||
}
|
||||
|
||||
function writeDraft() {
|
||||
draftTimer = null;
|
||||
const body = JSON.stringify(state, null, 2);
|
||||
draftWrite = draftWrite
|
||||
.then(() => fetch("/__draft", { method: "POST", headers: { "Content-Type": "application/json" }, body }))
|
||||
.then((res) => { if (!res.ok) throw new Error(res.statusText); })
|
||||
.catch((err) => {
|
||||
setStatus("Erreur d'écriture du brouillon", "error");
|
||||
console.error(err);
|
||||
});
|
||||
return draftWrite;
|
||||
}
|
||||
|
||||
function cancelPendingDraft() {
|
||||
if (draftTimer) clearTimeout(draftTimer);
|
||||
draftTimer = null;
|
||||
}
|
||||
|
||||
function markDirty() {
|
||||
dirty = true;
|
||||
setStatus("Modifications non enregistrées");
|
||||
setDirty(true);
|
||||
cancelPendingDraft();
|
||||
draftTimer = setTimeout(writeDraft, 300);
|
||||
}
|
||||
|
||||
function normalizeUrl(url) {
|
||||
return (url || "").replace(/^https?:\/\//, "").replace(/\/+$/, "").toLowerCase();
|
||||
}
|
||||
|
||||
// Apps du catalogue YunoHost pas encore presentes dans le portail. Calcule
|
||||
// a chaque rendu depuis l'etat courant : une app supprimee avec la croix
|
||||
// y reapparait aussitot, une app ajoutee en disparait.
|
||||
function availableCatalog() {
|
||||
const used = new Set();
|
||||
const all = [...(state.pinned || []), ...state.ungrouped, ...state.groups.flatMap((g) => g.apps)];
|
||||
all.forEach((a) => used.add(normalizeUrl(a.url)));
|
||||
return catalog.filter((a) => !used.has(normalizeUrl(a.url)));
|
||||
}
|
||||
|
||||
function findAppLocation(appId) {
|
||||
@@ -108,7 +152,7 @@ function renderTile(app) {
|
||||
removeBtn.textContent = "✕";
|
||||
removeBtn.title = "Supprimer cette application";
|
||||
removeBtn.addEventListener("click", () => {
|
||||
if (!confirm(`Supprimer "${app.name}" définitivement ?`)) return;
|
||||
if (!confirm(`Retirer "${app.name}" de la page ?`)) return;
|
||||
const loc = findAppLocation(app.id);
|
||||
if (loc) loc.list.splice(loc.index, 1);
|
||||
markDirty();
|
||||
@@ -166,7 +210,6 @@ function attachGridDropzone(gridEl, targetListGetter) {
|
||||
if (dragSource.catalogApp) {
|
||||
const app = { ...dragSource.catalogApp };
|
||||
targetList.splice(Math.min(dropIndex, targetList.length), 0, app);
|
||||
catalog = catalog.filter((a) => a.id !== app.id);
|
||||
markDirty();
|
||||
render();
|
||||
return;
|
||||
@@ -185,7 +228,6 @@ function attachGridDropzone(gridEl, targetListGetter) {
|
||||
|
||||
function addCatalogAppTo(app, targetList) {
|
||||
targetList.push({ ...app });
|
||||
catalog = catalog.filter((a) => a.id !== app.id);
|
||||
markDirty();
|
||||
render();
|
||||
}
|
||||
@@ -424,9 +466,10 @@ function render() {
|
||||
const layout = document.createElement("div");
|
||||
layout.className = "editor-layout";
|
||||
|
||||
const half = Math.ceil(catalog.length / 2);
|
||||
const leftCatalog = renderCatalogColumn("Catalogue YunoHost", catalog.slice(0, half));
|
||||
const rightCatalog = renderCatalogColumn("Catalogue YunoHost", catalog.slice(half));
|
||||
const available = availableCatalog();
|
||||
const half = Math.ceil(available.length / 2);
|
||||
const leftCatalog = renderCatalogColumn("Catalogue YunoHost", available.slice(0, half));
|
||||
const rightCatalog = renderCatalogColumn("Catalogue YunoHost", available.slice(half));
|
||||
|
||||
const center = document.createElement("div");
|
||||
center.className = "editor-center";
|
||||
@@ -517,11 +560,33 @@ async function loadCatalog() {
|
||||
async function load() {
|
||||
const res = await fetch("config.json", { cache: "no-store" });
|
||||
state = await res.json();
|
||||
setDirty(res.headers.get("X-Homepage-Draft") === "1");
|
||||
await loadCatalog();
|
||||
render();
|
||||
}
|
||||
|
||||
async function revert() {
|
||||
if (!confirm("Abandonner toutes les modifications non enregistrées et revenir à la dernière version enregistrée ?")) return;
|
||||
cancelPendingDraft();
|
||||
try {
|
||||
await draftWrite;
|
||||
const res = await fetch("/__discard", { method: "POST" });
|
||||
if (!res.ok) throw new Error(await res.text());
|
||||
const orig = await fetch("/__original", { cache: "no-store" });
|
||||
state = await orig.json();
|
||||
setDirty(false);
|
||||
render();
|
||||
setStatus("Original rétabli ✓", "saved");
|
||||
setTimeout(() => setStatus(""), 2000);
|
||||
} catch (err) {
|
||||
setStatus("Erreur lors du rétablissement", "error");
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
cancelPendingDraft();
|
||||
await draftWrite;
|
||||
try {
|
||||
const res = await fetch("/__save", {
|
||||
method: "POST",
|
||||
@@ -529,7 +594,7 @@ async function save() {
|
||||
body: JSON.stringify(state, null, 2),
|
||||
});
|
||||
if (!res.ok) throw new Error(await res.text());
|
||||
dirty = false;
|
||||
setDirty(false);
|
||||
setStatus("Enregistré ✓", "saved");
|
||||
setTimeout(() => setStatus(""), 2000);
|
||||
} catch (err) {
|
||||
@@ -553,12 +618,14 @@ async function exportToOutput() {
|
||||
|
||||
document.getElementById("save-btn").addEventListener("click", save);
|
||||
document.getElementById("export-btn").addEventListener("click", exportToOutput);
|
||||
document.getElementById("revert-btn").addEventListener("click", revert);
|
||||
|
||||
window.addEventListener("beforeunload", (e) => {
|
||||
if (dirty) {
|
||||
e.preventDefault();
|
||||
e.returnValue = "";
|
||||
}
|
||||
// Le brouillon est sur disque : fermer ou recharger l'editeur ne perd rien,
|
||||
// il suffit d'envoyer la derniere modification si elle est encore en attente.
|
||||
window.addEventListener("pagehide", () => {
|
||||
if (!draftTimer) return;
|
||||
cancelPendingDraft();
|
||||
navigator.sendBeacon("/__draft", new Blob([JSON.stringify(state)], { type: "application/json" }));
|
||||
});
|
||||
|
||||
load();
|
||||
Reference in new issue
Block a user