fix(editor): honest save indicator — Changed while dirty (#83)
The dot claimed "Saved" for open posts even with unflushed keystrokes; only draft mode told the truth. SaveState now mirrors cleanliness: Changed while text differs from the last flush, Saved when clean. The Saving… state is deleted — the synchronous write never lasts a render pass. Save failed stays sticky until a write actually succeeds, and Keep Local no longer adopts local text as flushed when the post vanished mid-alert (that path silently killed ⌘S with "Saved" showing). Closes #83 [Generated by AI]
This commit is contained in:
1 parent
c1f5d707f8
commit
fc0dde4f7c
5 files changed
+79
-12
No files matched your search
@@ -11,11 +11,11 @@
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
/// The save-state indicator (#65): Saved → Saving… → Saved, plus Save failed
|
||||
/// in accent on write error. Never silent, never modal.
|
||||
/// Save-state indicator (#65/#83): Saved = clean, Changed = dirty (text ≠
|
||||
/// last flushed), failed = last write threw (sticky until one succeeds).
|
||||
enum SaveState: Equatable {
|
||||
case saved
|
||||
case saving
|
||||
case changed
|
||||
case failed
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ final class EditorSession {
|
||||
/// Keystroke from the editor surface; re-arms the ~1 s idle debounce.
|
||||
func edit(_ newText: String) {
|
||||
text = newText
|
||||
syncSaveState() // #83: dirty shows while the debounce holds
|
||||
debounceTask?.cancel()
|
||||
debounceTask = Task { [debounce] in
|
||||
try? await Task.sleep(for: debounce)
|
||||
@@ -102,7 +103,6 @@ final class EditorSession {
|
||||
func flush() {
|
||||
debounceTask?.cancel()
|
||||
guard text != lastFlushedText else { return }
|
||||
saveState = .saving
|
||||
do {
|
||||
try PostStore.overwrite(id: postID, body: text, home: home)
|
||||
lastFlushedText = text
|
||||
@@ -117,6 +117,17 @@ final class EditorSession {
|
||||
}
|
||||
}
|
||||
|
||||
/// #83: the indicator mirrors cleanliness — text ≠ last flushed = Changed.
|
||||
private func syncSaveState() {
|
||||
if text != lastFlushedText {
|
||||
if saveState == .saved {
|
||||
saveState = .changed
|
||||
}
|
||||
} else if saveState == .changed {
|
||||
saveState = .saved
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: External change (#67)
|
||||
|
||||
/// Owner teardown when the post leaves the editor (rename away, trash,
|
||||
@@ -151,15 +162,21 @@ final class EditorSession {
|
||||
lastFlushedText = disk
|
||||
restoreCursor = PostStore.readCursor(id: postID, home: home)
|
||||
saveState = .saved
|
||||
// #83: reload resolves a failure too — shown text now equals disk.
|
||||
onError?(nil)
|
||||
}
|
||||
|
||||
/// #67 No: keep local — the next autosave overwrites disk (the writer's
|
||||
/// explicit choice). Marks the disk state as seen so the same external
|
||||
/// content doesn't re-alert on every watcher tick; a new external write
|
||||
/// still does.
|
||||
/// still does. An unreadable disk (post vanished mid-alert) keeps the
|
||||
/// previous flushed text — the session stays dirty, ⌘S stays armed.
|
||||
func keepLocal() {
|
||||
guard conflictAlert else { return }
|
||||
conflictAlert = false
|
||||
lastFlushedText = PostStore.readBody(id: postID, home: home) ?? text
|
||||
if let disk = PostStore.readBody(id: postID, home: home) {
|
||||
lastFlushedText = disk
|
||||
}
|
||||
syncSaveState() // #83: local ≠ disk now — shows as pending changes
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user