fix done btn on edit
This commit is contained in:
@@ -226,34 +226,45 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isSaving = true;
|
||||||
hideConflict();
|
hideConflict();
|
||||||
setStatus("Saving");
|
setStatus("Saving");
|
||||||
|
|
||||||
if (!window.MDHubSync) {
|
if (!window.MDHubSync) {
|
||||||
|
isSaving = false;
|
||||||
setStatus("Queued");
|
setStatus("Queued");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = await window.MDHubSync.saveDocument(path, value, baseHash);
|
try {
|
||||||
if (result.status === "saved") {
|
var result = await window.MDHubSync.saveDocument(path, value, baseHash);
|
||||||
lastSavedValue = value;
|
if (result.status === "saved") {
|
||||||
if (hashEl && result.result && result.result.hash) {
|
lastSavedValue = value;
|
||||||
hashEl.textContent = result.result.hash;
|
if (hashEl && result.result && result.result.hash) {
|
||||||
baseHash = result.result.hash;
|
hashEl.textContent = result.result.hash;
|
||||||
if (documentShell) {
|
baseHash = result.result.hash;
|
||||||
documentShell.setAttribute("data-document-hash", result.result.hash);
|
if (documentShell) {
|
||||||
|
documentShell.setAttribute("data-document-hash", result.result.hash);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
setStatus("Saved");
|
||||||
|
isSaving = false;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
setStatus("Saved");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.status === "conflict") {
|
if (result.status === "conflict") {
|
||||||
showConflict(result.conflict, value);
|
showConflict(result.conflict, value);
|
||||||
return;
|
isSaving = false;
|
||||||
}
|
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();
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user