fix done btn on edit

This commit is contained in:
2026-06-09 21:06:26 -04:00
parent 6c30fd8b63
commit 6f646c2505
2 changed files with 105 additions and 17 deletions

View File

@@ -226,14 +226,17 @@
return; return;
} }
isSaving = true;
hideConflict(); hideConflict();
setStatus("Saving"); setStatus("Saving");
if (!window.MDHubSync) { if (!window.MDHubSync) {
isSaving = false;
setStatus("Queued"); setStatus("Queued");
return; return;
} }
try {
var result = await window.MDHubSync.saveDocument(path, value, baseHash); var result = await window.MDHubSync.saveDocument(path, value, baseHash);
if (result.status === "saved") { if (result.status === "saved") {
lastSavedValue = value; lastSavedValue = value;
@@ -245,15 +248,23 @@
} }
} }
setStatus("Saved"); setStatus("Saved");
isSaving = false;
return; return;
} }
if (result.status === "conflict") { if (result.status === "conflict") {
showConflict(result.conflict, value); showConflict(result.conflict, value);
isSaving = false;
return; return;
} }
setStatus("Queued"); setStatus("Queued");
} catch (err) {
setStatus("Queued");
throw err;
} finally {
isSaving = false;
}
} }
async function applyResolvedConflict() { async function applyResolvedConflict() {
@@ -571,6 +582,83 @@
restorePendingConflict(); restorePendingConflict();
var doneLink = document.querySelector("[data-done-link]");
var isSaving = false;
if (doneLink) {
doneLink.addEventListener("click", function (event) {
var currentValue = getEditorValue();
var href = doneLink.getAttribute("href");
// If there's an unresolved conflict, warn the user
if (currentConflict) {
event.preventDefault();
setStatus("Conflict");
if (conflictNotice) {
conflictNotice.scrollIntoView({ behavior: "smooth", block: "center" });
}
return;
}
// If nothing changed, allow normal navigation
if (currentValue === lastSavedValue) {
return;
}
// Unsaved changes — cancel any pending timer and save immediately
event.preventDefault();
if (saveTimer) {
clearTimeout(saveTimer);
saveTimer = null;
}
if (isSaving) {
// Already saving, just wait and then navigate
waitForSaveThenNavigate(href);
return;
}
isSaving = true;
setStatus("Saving");
saveNow()
.then(function () {
isSaving = false;
window.location.href = href;
})
.catch(function () {
isSaving = false;
setStatus("Queued");
// If save fails, ask user if they want to leave anyway
if (window.confirm("Could not save. Leave without saving?")) {
window.location.href = href;
}
});
});
}
function waitForSaveThenNavigate(href) {
var checkInterval = setInterval(function () {
if (!isSaving) {
clearInterval(checkInterval);
window.location.href = href;
}
}, 100);
// Timeout after 10 seconds
setTimeout(function () {
clearInterval(checkInterval);
}, 10000);
}
// Warn on browser unload if there are unsaved changes
window.addEventListener("beforeunload", function (e) {
if (getEditorValue() !== lastSavedValue || currentConflict) {
e.preventDefault();
e.returnValue = "";
}
});
document.addEventListener("keydown", function (e) { document.addEventListener("keydown", function (e) {
if ((e.metaKey || e.ctrlKey) && e.key === "s") { if ((e.metaKey || e.ctrlKey) && e.key === "s") {
e.preventDefault(); e.preventDefault();

View File

@@ -24,7 +24,7 @@
<p class="eyebrow">Editing</p> <p class="eyebrow">Editing</p>
<h1>{{ .Title }}</h1> <h1>{{ .Title }}</h1>
</div> </div>
<a class="document-edit-link" href="/docs/{{ trimMd .Path }}">Done</a> <a class="document-edit-link" href="/docs/{{ trimMd .Path }}" data-done-link>Done</a>
</div> </div>
<details class="document-meta-panel"> <details class="document-meta-panel">
<summary> <summary>