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.
309 lines
8.7 KiB
Swift
309 lines
8.7 KiB
Swift
//
|
||
// PostsPaneView.swift
|
||
// Museion
|
||
//
|
||
// #17 (S1.1): the Posts pane — library column mirroring the Home's posts/
|
||
// on disk. #18: lifecycle — New Post draft, ⌘S first-write/save, rename,
|
||
// trash; TextEditor stub until #19–#21, mentor column (#14).
|
||
//
|
||
|
||
import SwiftUI
|
||
|
||
/// The Posts destination: the workspace's full four-column layout
|
||
/// library 232 | editor ≥420 | mentor 356 (design contract grid).
|
||
struct PostsPaneView: View {
|
||
let home: URL
|
||
/// #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 renamingPost: Post?
|
||
|
||
private var showsLibrary: Bool {
|
||
availableWidth >= 1100 - DesignTokens.railWidth
|
||
}
|
||
|
||
private var showsMentor: Bool {
|
||
availableWidth >= 920 - DesignTokens.railWidth
|
||
}
|
||
|
||
var body: some View {
|
||
HStack(spacing: 0) {
|
||
if showsLibrary {
|
||
libraryColumn
|
||
paneBorder
|
||
}
|
||
editorColumn
|
||
if showsMentor {
|
||
paneBorder
|
||
mentorColumn
|
||
}
|
||
}
|
||
.task(id: home) {
|
||
// 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)
|
||
}
|
||
}
|
||
}
|
||
|
||
private var paneBorder: some View {
|
||
Rectangle()
|
||
.fill(DesignTokens.border)
|
||
.frame(width: 1)
|
||
}
|
||
|
||
// MARK: Library column
|
||
|
||
private var libraryColumn: some View {
|
||
VStack(spacing: 0) {
|
||
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 = model.posts.filter { $0.passState == state }
|
||
if !grouped.isEmpty {
|
||
Text(state.rawValue.uppercased())
|
||
.font(.system(size: 11, design: DesignTokens.mono))
|
||
.kerning(0.6)
|
||
.foregroundStyle(DesignTokens.muted)
|
||
.padding(.horizontal, 14)
|
||
.padding(.top, 18)
|
||
.padding(.bottom, 7)
|
||
ForEach(grouped) { post in
|
||
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)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.padding(.bottom, 12)
|
||
}
|
||
}
|
||
}
|
||
.frame(width: DesignTokens.libraryWidth)
|
||
}
|
||
|
||
private var emptyState: some View {
|
||
VStack(spacing: 8) {
|
||
Text("No posts yet")
|
||
.font(.system(size: 16, weight: .semibold, design: DesignTokens.serif))
|
||
Text("Posts in this Home's posts/ folder appear here.")
|
||
.font(.callout)
|
||
.foregroundStyle(DesignTokens.muted)
|
||
.multilineTextAlignment(.center)
|
||
}
|
||
.padding(38)
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
}
|
||
|
||
// MARK: Editor column (stub until #19)
|
||
|
||
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)
|
||
}
|
||
}
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
.background(DesignTokens.surface)
|
||
}
|
||
|
||
private var mentorColumn: some View {
|
||
MentorPaneView(model: mentor, binaryPath: mentorBinaryPath)
|
||
.frame(width: DesignTokens.mentorWidth)
|
||
}
|
||
}
|
||
|
||
/// #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
|
||
let selected: Bool
|
||
let select: () -> Void
|
||
@State private var hover = false
|
||
|
||
var body: some View {
|
||
Button(action: select) {
|
||
VStack(alignment: .leading, spacing: 5) {
|
||
Text(post.title)
|
||
.font(.callout.weight(.semibold))
|
||
.lineLimit(1)
|
||
.truncationMode(.tail)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
HStack {
|
||
Text(post.passState.rawValue)
|
||
Text(post.origin.rawValue)
|
||
}
|
||
.font(.system(size: 11, design: DesignTokens.mono))
|
||
.foregroundStyle(DesignTokens.muted)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
}
|
||
.padding(10)
|
||
}
|
||
.buttonStyle(.plain)
|
||
.foregroundStyle(DesignTokens.foreground)
|
||
.background {
|
||
if selected || hover {
|
||
RoundedRectangle(cornerRadius: DesignTokens.radius)
|
||
.fill(selected ? DesignTokens.surface : DesignTokens.foreground.opacity(0.06))
|
||
.overlay(
|
||
RoundedRectangle(cornerRadius: DesignTokens.radius)
|
||
.strokeBorder(selected ? DesignTokens.border : .clear),
|
||
)
|
||
}
|
||
}
|
||
.padding(.horizontal, 8)
|
||
.onHover { hover = $0 }
|
||
.accessibilityLabel("\(post.title), \(post.passState.rawValue), origin \(post.origin.rawValue)")
|
||
}
|
||
}
|