From 6f646c2505f6dbdd2636a6d12a75c1431e549c61 Mon Sep 17 00:00:00 2001 From: Tim Bendt Date: Tue, 9 Jun 2026 21:06:26 -0400 Subject: [PATCH] fix done btn on edit --- .../internal/httpserver/static/editor.js | 120 +++++++++++++++--- .../httpserver/templates/document_edit.gohtml | 2 +- 2 files changed, 105 insertions(+), 17 deletions(-) diff --git a/apps/server/internal/httpserver/static/editor.js b/apps/server/internal/httpserver/static/editor.js index 110423e..f86851e 100644 --- a/apps/server/internal/httpserver/static/editor.js +++ b/apps/server/internal/httpserver/static/editor.js @@ -226,34 +226,45 @@ return; } + isSaving = true; hideConflict(); setStatus("Saving"); if (!window.MDHubSync) { + isSaving = false; setStatus("Queued"); return; } - var result = await window.MDHubSync.saveDocument(path, value, baseHash); - if (result.status === "saved") { - lastSavedValue = value; - if (hashEl && result.result && result.result.hash) { - hashEl.textContent = result.result.hash; - baseHash = result.result.hash; - if (documentShell) { - documentShell.setAttribute("data-document-hash", result.result.hash); + try { + var result = await window.MDHubSync.saveDocument(path, value, baseHash); + if (result.status === "saved") { + lastSavedValue = value; + if (hashEl && result.result && result.result.hash) { + hashEl.textContent = result.result.hash; + baseHash = 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") { - showConflict(result.conflict, value); - return; - } + if (result.status === "conflict") { + showConflict(result.conflict, value); + isSaving = false; + return; + } - setStatus("Queued"); + setStatus("Queued"); + } catch (err) { + setStatus("Queued"); + throw err; + } finally { + isSaving = false; + } } async function applyResolvedConflict() { @@ -571,6 +582,83 @@ 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) { if ((e.metaKey || e.ctrlKey) && e.key === "s") { e.preventDefault(); diff --git a/apps/server/internal/httpserver/templates/document_edit.gohtml b/apps/server/internal/httpserver/templates/document_edit.gohtml index 9d13652..9bc6d1f 100644 --- a/apps/server/internal/httpserver/templates/document_edit.gohtml +++ b/apps/server/internal/httpserver/templates/document_edit.gohtml @@ -24,7 +24,7 @@

Editing

{{ .Title }}

- Done + Done