feat(library): projects & archive UI — S2 (#108)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
Variant B per the #104 prototype verdict: the library column groups pieces by project (serif lowercase heads, collapse toggle, Pass-State groups inside, 2px edge-bar selection shared with the new Archive pane via LibraryPostRow). Move to Project / Archive / Move Back / whole-project Unarchive are folder moves — post.md bytes untouched, never clobbering, ids globally unique. File ▸ New Post ⌘N and New Project… ⌥⌘N (CommandGroup, never CommandMenu per #84). Every folder move flushes pending edits first and aborts when the write failed — nothing moves without its text. Project membership and the archived-refusal derive from freshly resolved disk paths, never the scan snapshot. Slug and project-name comparisons fold through one canonical key (NFC + lowercase). Two reviewer fanouts this session; their findings are all fixed or visibly parked. 144/144 tests green (run twice), swiftformat/swiftlint clean. Closes #108
This commit is contained in:
1 parent
9da3a07f4a
commit
e55747e7fe
15 files changed
+1205
-68
No files matched your search
+180
-7
@@ -6,6 +6,8 @@
|
||||
// roots (projects/ + archive/, #107) with a ~0.5 s debounced rescan (#61),
|
||||
// selection, and the New Post draft lifecycle (#62). Own ops route through
|
||||
// the same rescan: one path.
|
||||
// #108: project targeting + the move/archive/restore ops behind the
|
||||
// variant-B library and the Archive pane.
|
||||
//
|
||||
|
||||
import CoreServices
|
||||
@@ -96,6 +98,16 @@ final class PostsModel {
|
||||
/// answers, then the batch runs.
|
||||
private(set) var pendingImportURLs: [URL]?
|
||||
|
||||
/// #108: the explicit target for the NEXT new piece — set by New Project
|
||||
/// once the folder exists; outranks selection. Cleared when the draft
|
||||
/// lands (saveDraft) or the ask is canceled.
|
||||
private(set) var pendingProject: String?
|
||||
|
||||
/// #108: File ▸ New Project… stages its ask here — commands can't
|
||||
/// present sheets, so the model holds the flag and WorkspaceView
|
||||
/// presents (the pendingImportURLs pattern, #94).
|
||||
private(set) var askNewProject = false
|
||||
|
||||
/// Last failed-op message surfaced in the editor (nil = clean).
|
||||
private(set) var lastError: String?
|
||||
|
||||
@@ -163,11 +175,13 @@ final class PostsModel {
|
||||
// writes and same-volume moves; if a big Home ever stalls the UI, wrap
|
||||
// them in Task.detached like rescan.
|
||||
|
||||
/// #107: the project a NEW piece lands in — the selected non-archived
|
||||
/// post's project, else the default. One home; newPost/importFiles must
|
||||
/// not drift.
|
||||
/// #107: the project a NEW piece lands in — #108: an explicit pending
|
||||
/// target first, else the selected non-archived post's project, else the
|
||||
/// default. One home; newPost/importFiles must not drift.
|
||||
private var targetProject: String {
|
||||
posts.first(where: { $0.id == selectedPostID && !$0.isArchived })?.project ?? Post.defaultProject
|
||||
pendingProject
|
||||
?? posts.first(where: { $0.id == selectedPostID && !$0.isArchived })?.project
|
||||
?? Post.defaultProject
|
||||
}
|
||||
|
||||
/// Tear the watcher down on the main actor (its scheduling thread) —
|
||||
@@ -211,6 +225,7 @@ final class PostsModel {
|
||||
do {
|
||||
let post = try PostStore.writeNew(body: draft.text, project: draft.project, home: home)
|
||||
self.draft = nil
|
||||
pendingProject = nil // #108: the explicit target landed with the draft
|
||||
selectedPostID = post.id
|
||||
session = EditorSession(postID: post.id, home: home)
|
||||
wireSession()
|
||||
@@ -233,6 +248,9 @@ final class PostsModel {
|
||||
}
|
||||
session = id == session?.postID ? session : EditorSession(postID: id, home: home)
|
||||
draft = nil
|
||||
// #108: abandoning the draft kills its explicit project target too —
|
||||
// a later New Post must not silently land in the abandoned project.
|
||||
pendingProject = nil
|
||||
selectedPostID = id
|
||||
wireSession()
|
||||
}
|
||||
@@ -247,9 +265,10 @@ final class PostsModel {
|
||||
/// 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
|
||||
}
|
||||
// #108 audit 2: rename reads the file from disk — a failed write
|
||||
// aborts (moving the folder would strand unflushed edits), and the
|
||||
// session stays up, dirty and honest.
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
do {
|
||||
let moved = try PostStore.rename(post, to: title, home: home)
|
||||
if selectedPostID == post.id {
|
||||
@@ -286,6 +305,159 @@ final class PostsModel {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Projects (#108)
|
||||
|
||||
/// #108: New Project's confirmed sheet — the folder exists, the next
|
||||
/// draft targets it, and opens now.
|
||||
func newProjectPending(named name: String) {
|
||||
pendingProject = name
|
||||
newPost()
|
||||
}
|
||||
|
||||
/// #108: raw create — the sheet composes on it. The failure reason on
|
||||
/// false (shown inline in the sheet; never routed into lastError — the
|
||||
/// strip behind the sheet must not double-message one error).
|
||||
func createProject(named name: String, home: URL) -> String? {
|
||||
do {
|
||||
try PostStore.createProject(named: name, home: home)
|
||||
return nil // success
|
||||
} catch {
|
||||
return error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// #108: File ▸ New Project… — stage the ask (WorkspaceView presents).
|
||||
func stageNewProject() {
|
||||
askNewProject = true
|
||||
}
|
||||
|
||||
/// #108: the ask's Cancel.
|
||||
func cancelNewProject() {
|
||||
askNewProject = false
|
||||
}
|
||||
|
||||
/// #108 audit 2: the Move-to-Project list — every project on disk under
|
||||
/// projects/ plus the default (bind creates the roots, not posts/ — an
|
||||
/// empty Home must still offer it), localized-sorted to match the
|
||||
/// library's section order.
|
||||
func moveTargets(home: URL) -> [String] {
|
||||
let root = home.appendingPathComponent("projects", isDirectory: true)
|
||||
let onDisk = (try? FileManager.default.contentsOfDirectory(atPath: root.path)) ?? []
|
||||
var names = Set(onDisk.filter { !$0.hasPrefix(".") })
|
||||
names.insert(Post.defaultProject)
|
||||
return names.sorted { $0.localizedStandardCompare($1) == .orderedAscending }
|
||||
}
|
||||
|
||||
/// #108 audit: flush the open piece, aborting when the write failed — a
|
||||
/// folder move must never carry away text that didn't land (the move
|
||||
/// itself succeeds exactly when a file write fails). Returns false with
|
||||
/// lastError set; the session stays up, dirty and honest.
|
||||
private func flushOrAbort(_ postID: String) -> Bool {
|
||||
guard let session, session.postID == postID else { return true }
|
||||
if !session.flush() {
|
||||
// Append, don't replace — the flush already routed the write's
|
||||
// localizedDescription into lastError; only say what was skipped.
|
||||
lastError = (lastError.map { "\($0) " } ?? "") + "— nothing was moved."
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/// #108: Move to Project — flush first (move reads disk), then the
|
||||
/// folder moves under projects/. Id stays the same; the session is
|
||||
/// reopened anyway so folderURL re-resolves to the new parent.
|
||||
func moveToProject(_ post: Post, project: String, home: URL) {
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
do {
|
||||
try PostStore.move(post, to: project, home: home)
|
||||
if session?.postID == post.id {
|
||||
session = EditorSession(postID: post.id, home: home)
|
||||
wireSession()
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// #108: Move to Archive. Archiving the open piece clears it — the
|
||||
/// editor is empty until the next selection (archived pieces open from
|
||||
/// the Archive pane, where they're listed).
|
||||
func archivePost(_ post: Post, home: URL) {
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
do {
|
||||
try PostStore.archive(post, home: home)
|
||||
if selectedPostID == post.id {
|
||||
selectedPostID = nil
|
||||
}
|
||||
if let open = session, open.postID == post.id {
|
||||
open.teardown(flushFirst: false) // already flushed above
|
||||
session = nil
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// #108: Move Back — the restored piece lands in its project and opens;
|
||||
/// the writer stays in the piece they just revived. A session already
|
||||
/// open on the piece survives `select` — reopen it so folderURL points
|
||||
/// at projects/, not the archive path it was born with.
|
||||
func restorePost(_ post: Post, home: URL) {
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
do {
|
||||
try PostStore.restore(post, home: home)
|
||||
if let open = session, open.postID == post.id {
|
||||
session = EditorSession(postID: post.id, home: home)
|
||||
wireSession()
|
||||
} else {
|
||||
select(post.id, home: home)
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// #108: whole-project unarchive from the Archive pane's section header.
|
||||
/// Flush-first like every folder move (a dirty open piece inside the
|
||||
/// project would otherwise race the move), and its session is reopened so
|
||||
/// folderURL re-resolves off archive/ (same reason as restorePost).
|
||||
func unarchiveProject(_ name: String, home: URL) {
|
||||
/// #108 audit 2: disk-derived membership — the posts snapshot is stale
|
||||
/// the moment a Finder move lands. Captured BEFORE the move: after it
|
||||
/// the piece resolves under projects/ and its archive membership is
|
||||
/// invisible. dir − 1 = project dir (…/archive/<name>), − 2 = root.
|
||||
func openPieceInArchivedProject() -> String? {
|
||||
guard let id = session?.postID,
|
||||
let projectDir = PostStore.folder(of: id, home: home)?.deletingLastPathComponent(),
|
||||
projectDir.lastPathComponent == name,
|
||||
projectDir.deletingLastPathComponent().lastPathComponent == "archive"
|
||||
else { return nil }
|
||||
return id
|
||||
}
|
||||
let openID = openPieceInArchivedProject()
|
||||
if let openID {
|
||||
// #108 audit 2: the only #108 folder move that skipped the gate.
|
||||
guard flushOrAbort(openID) else { return }
|
||||
}
|
||||
do {
|
||||
try PostStore.unarchiveProject(name, home: home)
|
||||
if let openID {
|
||||
session = EditorSession(postID: openID, home: home)
|
||||
wireSession()
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: External buffer (#91–#93) + Import (#94/#95)
|
||||
|
||||
/// #91/#92: open an external markdown file in the guest slot — one at a
|
||||
@@ -302,6 +474,7 @@ final class PostsModel {
|
||||
session?.teardown(flushFirst: true) // #92: the guest takes the column
|
||||
session = nil
|
||||
draft = nil
|
||||
pendingProject = nil // #108 audit: the draft died here too — its target dies with it
|
||||
externalSession = EditorSession(target: .externalFile(target))
|
||||
if externalSession?.loadFailed == true {
|
||||
lastError = "Couldn't read \(target.lastPathComponent) — not UTF-8 text or unreadable. Nothing was changed."
|
||||
|
||||
Reference in new issue
Block a user