Do not consider edit mode as a history entry

This commit is contained in:
2026-07-20 17:31:59 +02:00
parent 34f750beff
commit 28d22c040f
6 changed files with 93 additions and 2 deletions
+40 -1
View File
@@ -140,7 +140,46 @@
function syncContent() {
hidden.value = view.state.doc.toString();
}
form.addEventListener('submit', syncContent);
// Save POSTs via fetch and then rewrites the *editor's* history entry with
// the resulting page, so the edit session and its result share a single
// entry (see history-nav.js). A plain form submit would push a second one
// and leave the editor sitting in history behind the saved page.
//
// The server answers 204 + X-Target instead of a 303 because the target may
// carry a #section anchor the client cannot compute, and fetch drops the
// fragment from a followed redirect.
function postSave() {
syncContent();
var body = new URLSearchParams(new FormData(form)).toString();
fetch(form.action, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-Save-Mode': 'replace',
},
body: body,
}).then(function (res) {
if (!res.ok) {
return res.text().then(function (msg) {
alert(msg || ('Save failed (' + res.status + ')'));
});
}
var target = res.headers.get('X-Target') || form.action;
// replaceState + reload rather than location.replace: if target
// differs from the current URL only by fragment the browser would
// skip the re-fetch and show pre-save content.
window.history.replaceState(null, '', target);
window.location.reload();
}).catch(function () {
alert('Network error — the page was not saved');
});
}
form.addEventListener('submit', function (e) {
e.preventDefault();
postSave();
});
// --- Actions ---