feat(editor): land editor core — open, edit, autosave (S1.2)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
Posts open into the TextKit2 surface (ADR-0010 mode B) lifted from the proven spike: styled source, scaled headings, dimmed syntax, native undo/IME. One EditorSession per open post drives a ~1 s debounced autosave (flush on selection change, window close, app quit; ⌘S immediate), the Saved/Saving/Failed dot, and the per-post .museion-state.json cursor sidecar that dies with the folder. External edits raise an explicit Reload/Keep-Local alert — the debounce never answers it for the writer, and drafts keep the lazy-folder first-⌘S rule (#62). Reviewer fanout fixes folded in: session teardown at every lifecycle exit (rename flushes before the folder move; trash cancels the debounce), draft ⌘S continues in place, conflict holds the debounce, rescan's disk read off-main, ~35 lines of dead code out. Closes #19
This commit is contained in:
1 parent
530f852f75
commit
f2953ee07e
13 files changed
+893
-87
No files matched your search
+61
-20
@@ -78,6 +78,10 @@ final class PostsModel {
|
||||
/// The in-memory draft for New Post (#62): no folder until first save.
|
||||
private(set) var draft: PostDraft?
|
||||
|
||||
/// #19: the open post's editing session; nil while a draft is open or
|
||||
/// nothing is selected.
|
||||
private(set) var session: EditorSession?
|
||||
|
||||
/// Last failed-op message surfaced in the editor (nil = clean).
|
||||
private(set) var lastError: String?
|
||||
|
||||
@@ -112,11 +116,27 @@ final class PostsModel {
|
||||
/// no second debounce on top.
|
||||
private func rescan() async {
|
||||
guard let home = watchedHome else { return }
|
||||
let scanned = await Task.detached(priority: .userInitiated) { Post.scan(home: home) }.value
|
||||
// Scan + the #67 disk read both run off-main (a synced large post.md
|
||||
// must never stall the UI on a watcher burst).
|
||||
let openPostID = session?.postID
|
||||
let (scanned, openDiskText) = await Task.detached(priority: .userInitiated) {
|
||||
let posts = Post.scan(home: home)
|
||||
let disk = openPostID.flatMap { PostStore.readBody(id: $0, home: home) }
|
||||
return (posts, disk)
|
||||
}.value
|
||||
guard !Task.isCancelled else { return }
|
||||
posts = scanned
|
||||
if let id = selectedPostID, !posts.contains(where: { $0.id == id }) {
|
||||
selectedPostID = nil // the post vanished (deleted in Finder or here)
|
||||
if let open = session, open.postID == id {
|
||||
open.teardown(flushFirst: false) // post is gone; cancel the debounce
|
||||
self.session = nil
|
||||
}
|
||||
}
|
||||
// #67: watcher echo check on the open post — equality against the
|
||||
// session's lastFlushedText suppresses our own writes exactly.
|
||||
if let session, let openDiskText {
|
||||
session.diskDidChange(diskText: openDiskText)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,37 +147,33 @@ final class PostsModel {
|
||||
// them in Task.detached like rescan.
|
||||
|
||||
/// Tear the watcher down on the main actor (its scheduling thread) —
|
||||
/// deinit of a @MainActor-held object may run elsewhere.
|
||||
/// deinit of a @MainActor-held object may run elsewhere. #64: app quit
|
||||
/// flushes the open session first (sidecar rides along).
|
||||
func shutdown() {
|
||||
session?.flush()
|
||||
watcher.stop()
|
||||
}
|
||||
|
||||
/// New Post (#62): fresh in-memory draft, no folder, editor takes it.
|
||||
func newPost() {
|
||||
session?.teardown(flushFirst: true) // #64: selection change flushes
|
||||
session = nil
|
||||
draft = PostDraft()
|
||||
selectedPostID = nil
|
||||
}
|
||||
|
||||
/// First save of the draft (#62): lazy folder, slug, #59 stamp all fire
|
||||
/// here. Rescans immediately (the watcher would too, after its window).
|
||||
/// here. The saved post opens straight into an editing session — the
|
||||
/// writer keeps typing where they were. Rescans immediately (the watcher
|
||||
/// would too, after its window).
|
||||
func saveDraft(home: URL) {
|
||||
guard let draft else { return }
|
||||
do {
|
||||
let post = try PostStore.writeNew(body: draft.text, home: home)
|
||||
self.draft = nil
|
||||
selectedPostID = post.id
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// Save an edited body for the selected post (preserves its frontmatter).
|
||||
func saveSelected(body: String, home: URL) {
|
||||
guard let id = selectedPostID else { return }
|
||||
do {
|
||||
try PostStore.overwrite(id: id, body: body, home: home)
|
||||
session = EditorSession(postID: post.id, home: home)
|
||||
wireSession()
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
@@ -167,23 +183,43 @@ final class PostsModel {
|
||||
|
||||
/// Select a Post; selecting anything abandons an unsaved draft (the
|
||||
/// draft otherwise hijacks the editor — its only exits are save or
|
||||
/// selecting away).
|
||||
func select(_ id: String) {
|
||||
/// selecting away). #19: opening a post ends the previous session
|
||||
/// (flush-on-selection-change, #64) and starts a new one.
|
||||
func select(_ id: String, home: URL) {
|
||||
if let session, session.postID != id {
|
||||
session.teardown(flushFirst: true)
|
||||
}
|
||||
session = id == session?.postID ? session : EditorSession(postID: id, home: home)
|
||||
draft = nil
|
||||
selectedPostID = id
|
||||
wireSession()
|
||||
}
|
||||
|
||||
/// Draft typing from the editor stub (private(set) barrier).
|
||||
private func wireSession() {
|
||||
session?.onError = { [weak self] message in self?.lastError = message }
|
||||
}
|
||||
|
||||
/// Draft typing from the editor (private(set) barrier).
|
||||
func updateDraft(_ text: String) {
|
||||
draft?.text = text
|
||||
}
|
||||
|
||||
/// Explicit Rename = retitle + move (#60). Sets lastError on failure.
|
||||
/// Explicit Rename = retitle + move (#60). Renaming the open post flushes
|
||||
/// first (rename reads the file from disk — unflushed edits must land
|
||||
/// before the move), then reopens a session at the new id. Sets lastError
|
||||
/// on failure.
|
||||
func rename(_ post: Post, to title: String, home: URL) {
|
||||
if let session, session.postID == post.id {
|
||||
session.flush() // before PostStore.rename reads the old file
|
||||
}
|
||||
do {
|
||||
let moved = try PostStore.rename(post, to: title, home: home)
|
||||
if selectedPostID == post.id {
|
||||
selectedPostID = moved.id
|
||||
if session?.postID == post.id {
|
||||
session = EditorSession(postID: moved.id, home: home)
|
||||
wireSession()
|
||||
}
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
@@ -193,12 +229,17 @@ final class PostsModel {
|
||||
}
|
||||
|
||||
/// Delete a Post (Trash, recoverable — #18 AC). Clears selection if it
|
||||
/// was the post.
|
||||
/// was the post; the session is torn down (cancel only — the folder is
|
||||
/// leaving, a flush would resurrect nothing but churn the watcher).
|
||||
func trash(_ post: Post, home: URL) {
|
||||
do {
|
||||
try PostStore.trash(post, home: home)
|
||||
if selectedPostID == post.id {
|
||||
selectedPostID = nil
|
||||
if let open = session, open.postID == post.id {
|
||||
open.teardown(flushFirst: false)
|
||||
self.session = nil
|
||||
}
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
|
||||
Reference in new issue
Block a user