feat(workspace): land Post lifecycle (S1.1)
CI / ci (push) Canceled after 0s

Posts become writable end to end per the #18 map decisions:
New Post holds an in-memory draft (folder appears only on first
save), first save stamps the flat frontmatter block the #17
reader already parses (pass_state First Draft, origin writer,
immutable after stamping), and the Post ID is the Title slug with
case-insensitive collision suffixes. Explicit Rename retitles the
# Title line and moves the folder (post.md stays); Delete goes to
Trash. A recursive FSEvents stream keeps the list mirroring
posts/ — external Finder edits included.

Editor is a deliberate TextEditor stub; #19's TextKit2 editor
replaces it in place while the lifecycle machinery below the view
survives the swap.

Two review rounds (standards/spec + finish-audit fanout) fixed:
watcher start-order bug, writeNew clobber race, overwrite
resurrecting vanished posts with reset origin, rename destroying
non-heading first lines, draft hijacking the editor, debounce
starvation under churn.

Tests: 75/75 (17 new). Closes #18.
This commit is contained in:
AlexKucera committed 2026-10-03 13:34:00 +02:00
1 parent de76a50cc6
commit 530f852f75
13 files changed
+871 -27

No files matched your search

+161 -21
View File
@@ -3,7 +3,8 @@
// Museion
//
// #17 (S1.1): the Posts pane — library column mirroring the Home's posts/
// on disk, editor placeholder until #19–#21, mentor column (#14).
// on disk. #18: lifecycle — New Post draft, ⌘S first-write/save, rename,
// trash; TextEditor stub until #19–#21, mentor column (#14).
//
import SwiftUI
@@ -15,11 +16,12 @@ struct PostsPaneView: View {
/// #14: the app-scoped mentor model + the gate's winning binary (no re-probe).
var mentor: MentorSessionModel
var mentorBinaryPath: String?
/// #18: app-scoped Posts state (watcher + lifecycle ops).
var model: PostsModel
/// Width budget after the rail — drives column collapse (export media queries:
/// library hides ≤1100, mentor hides ≤920).
var availableWidth: CGFloat
@State private var posts: [Post] = []
@State private var selectedPostID: String?
@State private var renamingPost: Post?
private var showsLibrary: Bool {
availableWidth >= 1100 - DesignTokens.railWidth
@@ -35,15 +37,20 @@ struct PostsPaneView: View {
libraryColumn
paneBorder
}
editorPlaceholder
editorColumn
if showsMentor {
paneBorder
mentorColumn
}
}
.task(id: home) {
// Scan off-main: .task inherits MainActor; a big Home must not freeze the UI.
posts = await Task.detached(priority: .userInitiated) { Post.scan(home: home) }.value
// Bind: initial off-main scan + (re)start the FSEvents watcher (#61).
model.bind(home: home)
}
.sheet(item: $renamingPost) { post in
RenamePostSheet(post: post) { title in
model.rename(post, to: title, home: home)
}
}
}
@@ -57,15 +64,24 @@ struct PostsPaneView: View {
private var libraryColumn: some View {
VStack(spacing: 0) {
// New Post arrives with #18 (Post lifecycle: lazy folder + slug).
PaneHeader(title: "Posts")
if posts.isEmpty {
PaneHeader(title: "Posts") {
Button {
model.newPost()
} label: {
Image(systemName: "plus")
}
.buttonStyle(.plain)
.foregroundStyle(DesignTokens.muted)
.help("New Post")
.accessibilityLabel("New Post")
}
if model.posts.isEmpty {
emptyState
} else {
ScrollView {
LazyVStack(alignment: .leading, spacing: 4) {
ForEach(Post.PassState.allCases, id: \.self) { state in
let grouped = posts.filter { $0.passState == state }
let grouped = model.posts.filter { $0.passState == state }
if !grouped.isEmpty {
Text(state.rawValue.uppercased())
.font(.system(size: 11, design: DesignTokens.mono))
@@ -75,8 +91,14 @@ struct PostsPaneView: View {
.padding(.top, 18)
.padding(.bottom, 7)
ForEach(grouped) { post in
PostRow(post: post, selected: post.id == selectedPostID) {
selectedPostID = post.id
PostRow(post: post, selected: post.id == model.selectedPostID) {
model.select(post.id)
}
.contextMenu {
Button("Rename…") { renamingPost = post }
Button("Move to Trash", role: .destructive) {
model.trash(post, home: home)
}
}
}
}
@@ -102,17 +124,46 @@ struct PostsPaneView: View {
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
// MARK: Editor + mentor columns
// MARK: Editor column (stub until #19)
private var editorPlaceholder: some View {
VStack(spacing: 10) {
if let post = posts.first(where: { $0.id == selectedPostID }) {
Text(post.title)
.font(.system(size: 16, weight: .semibold, design: DesignTokens.serif))
private var editorColumn: some View {
VStack(spacing: 0) {
if let draft = model.draft {
EditorStub(
initialText: draft.text,
crumb: "unsaved draft — no folder until first save",
save: { text in
model.updateDraft(text)
model.saveDraft(home: home)
},
)
.id("draft")
} else if let post = model.posts.first(where: { $0.id == model.selectedPostID }) {
// ponytail: readBody is one small file, sync, per selection
// change — #19's editor owns async loading when it lands.
EditorStub(
initialText: PostStore.readBody(id: post.id, home: home) ?? "",
crumb: "posts / \(post.id) / post.md",
save: { text in model.saveSelected(body: text, home: home) },
)
.id(post.id)
} else {
VStack(spacing: 10) {
Image(systemName: "square.and.pencil")
.font(.largeTitle)
.foregroundStyle(DesignTokens.muted)
Text("Select a post, or start a new one.")
.font(.callout)
.foregroundStyle(DesignTokens.muted)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
if let error = model.lastError {
Text(error)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.accent)
.padding(.vertical, 6)
}
Text("The editor arrives with S1.2 (#19–#21).")
.font(.callout)
.foregroundStyle(DesignTokens.muted)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(DesignTokens.surface)
@@ -124,6 +175,95 @@ struct PostsPaneView: View {
}
}
/// #18 stub editor (#62): plain TextEditor in the placeholder slot; ⌘S is
/// the first-write trigger (lazy folder + slug + stamp). Throwaway at the
/// view layer — #19's TextKit2 editor replaces it in place; the lifecycle
/// machinery lives in PostsModel/PostStore and survives the swap.
private struct EditorStub: View {
/// Typed text is local @State — disk is written only on save; `.id()` at
/// the call site swaps identity (and thus reloads) per post/draft.
let crumb: String
let save: (String) -> Void
@State private var text: String
init(
initialText: String,
crumb: String,
save: @escaping (String) -> Void,
) {
self.crumb = crumb
self.save = save
_text = State(initialValue: initialText)
}
var body: some View {
VStack(spacing: 0) {
HStack {
Text(crumb)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
.lineLimit(1)
.truncationMode(.head)
Spacer()
Button("Save") { save(text) }
.keyboardShortcut("s", modifiers: .command)
}
.padding(.horizontal, 16)
.frame(minHeight: 40)
.overlay(alignment: .bottom) {
Rectangle().fill(DesignTokens.border).frame(height: 1)
}
TextEditor(text: $text)
.font(.system(size: 15, design: DesignTokens.serif))
.scrollContentBackground(.hidden)
.padding(EdgeInsets(top: 20, leading: 24, bottom: 20, trailing: 24))
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
/// #60: Explicit Rename dialog — asks the new Title (not a slug).
private struct RenamePostSheet: View {
let rename: (String) -> Void
@Environment(\.dismiss) private var dismiss
@State private var title: String
init(post: Post, rename: @escaping (String) -> Void) {
self.rename = rename
_title = State(initialValue: post.title)
}
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Text("Rename Post")
.font(.system(size: 15, weight: .semibold, design: DesignTokens.serif))
Text("The folder moves to match the new Title. The file stays post.md.")
.font(.callout)
.foregroundStyle(DesignTokens.muted)
TextField("Title", text: $title)
.textFieldStyle(.roundedBorder)
.onSubmit { confirm() }
HStack {
Spacer()
Button("Cancel", role: .cancel) { dismiss() }
.keyboardShortcut(.cancelAction)
Button("Rename", action: confirm)
.keyboardShortcut(.defaultAction)
.disabled(title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
}
}
.padding(20)
.frame(width: 360)
}
private func confirm() {
let trimmed = title.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return }
rename(trimmed)
dismiss()
}
}
/// One post row per the export: title, Pass State + Origin in mono 11.
private struct PostRow: View {
let post: Post