chore(repo): save local work

This commit is contained in:
slhx agent
2026-07-27 13:23:56 +02:00
parent 276f40ee59
commit 2dc7703936
5 changed files with 61 additions and 8 deletions
+47 -2
View File
@@ -343,6 +343,7 @@
await navigateUrl(pageRequestUrl(form, source), rootOf(form) || rootOf(el), mode);
return;
}
savePage(roots()[0]);
const response = await fetch(requestUrl(form, data, method), {
method,
body,
@@ -382,7 +383,32 @@
}
}
function snapshotPage(root) {
return root ? {
hemx: true,
pageHtml: root.innerHTML,
pageTitle: document.title,
scrollX: window.scrollX,
scrollY: window.scrollY,
} : null;
}
function savePage(root) {
const snapshot = snapshotPage(root);
if (snapshot) history.replaceState(snapshot, "", location.href);
}
function restorePage(state, root) {
if (!root || !state || typeof state.pageHtml !== "string") return false;
root.innerHTML = state.pageHtml;
if (state.pageTitle) document.title = state.pageTitle;
bindRoot(root);
requestAnimationFrame(() => scrollTo(state.scrollX, state.scrollY));
return true;
}
async function navigateUrl(href, root, mode = "replace") {
if (mode === "push") savePage(root);
const response = await fetch(href, {
headers: { "X-HEMX-Partial": "1", "Accept": "text/html" },
credentials: "same-origin",
@@ -443,6 +469,8 @@
return;
}
for (const op of batch.ops) applyOp(scope, op);
bindPolling(scope);
bindRevealed(scope);
}
function canApplyOp(scope, op) {
@@ -1101,6 +1129,19 @@
stopDescendantPolling(root);
}
function restoreRevealed(event) {
if (!event.persisted) return;
roots().forEach((root) => {
const observer = revealObservers.get(root);
if (observer) observer.disconnect();
revealObservers.delete(root);
forEachElement(root, (el) => {
if (attrEquals(el, "data-hemx-on", "revealed") || el.hasAttribute("data-hemx-revealed")) revealed.delete(el);
});
bindRevealed(root);
});
}
function start() {
roots().forEach((root) => {
root.setAttribute("data-hemx-request-timeout-ms", String(REQUEST_TIMEOUT_MS));
@@ -1147,9 +1188,13 @@
}
}
addEventListener("popstate", () => {
window.addEventListener("pageshow", restoreRevealed);
addEventListener("popstate", (event) => {
const root = roots()[0];
if (root) navigateUrl(location.href, root, "none").catch((error) => emit(root, "hemx:error", String(error)));
if (root && !restorePage(event.state, root)) {
if (root) navigateUrl(location.href, root, "none").catch((error) => emit(root, "hemx:error", String(error)));
}
});
window.hemx = Object.freeze({