Jump on a Fix Card selects the anchor's span in the piece's editor and makes the main window key (#204). The span is the card's quote at its 1-based occurrence, resolved against the LIVE session text so a dirty buffer still lands, and it rides the reading flip-back's caret rail: caretHint widens from a (location, token) tuple to FixCardRevealRequest (SwiftLint large_tuple), one shared token counter so a jump and a flip-back can never collide, a zero-length range stays a bare caret and a non-zero range selects. A jump leaves reading mode first (reading = false, not exitReading, which stamps its own zero-length hint), writes nothing, and re-fires per click. The palette offers Jump on every non-stale card that still shows an action row; stale anchors have nothing to land on. The landing math is ProseTextView.landingSpan - a bare caret hops past leading blanks, a span never does (a quote that IS the leading blanks selects those blanks) - and appliedText now routes through anchorRange so Apply and Jump share one occurrence math. Two review rounds closed the view-layer gaps the pure pins could not see: a jump dropped in reading mode; a main window that never became key (NSApp.activate is a no-op once the palette's click activated the app - the Coordinator makes tv.window key instead); a cursor record taken before the scroll. +3 tests (anchorRange occurrence math with a UTF-16 fixture, the model's reveal stamp, landingSpan), each flip-verified. Rail 732/732; SwiftFormat and SwiftLint clean. DOX: Museion/AGENTS, MuseionTests/AGENTS, GLOSSARY gains Jump.
191 lines
7.5 KiB
Swift
191 lines
7.5 KiB
Swift
//
|
|
// PostsModelCards.swift
|
|
// Museion
|
|
//
|
|
// #146: the fix-card extensions of PostsModel — stored-card surfacing
|
|
// (#23), apply/skip/done (#25, S3.2/S3.3), and promote (#26, S4.1) —
|
|
// moved from PostsModel.swift (file-length ceiling), code unchanged;
|
|
// #204: the jump-to-the-anchor reveal (`revealCard`, `FixCardRevealRequest`).
|
|
//
|
|
|
|
import Foundation
|
|
|
|
// MARK: Stored fix cards (#23, M2 slice 3)
|
|
|
|
extension PostsModel {
|
|
/// #23 (M2, slice 3): re-derive `openPieceCards` from the OPEN piece's
|
|
/// `<piece>/cards/` (FixCardStore.scan — any MCP client's submissions,
|
|
/// newest-first; archive pieces resolve through folder(of:) too). The
|
|
/// nil-session rule clears: draft, external guest, held guard ask,
|
|
/// nothing open → []. Called at the rescan tail and the open
|
|
/// transitions — the piece-roots watcher already fires on cards/ writes.
|
|
/// #122: the same pass also derives `openPieceHasSnapshot` (the Blind
|
|
/// Read's base) — one detached read of the open piece's disk facts.
|
|
/// brief-28: identity at BOTH ends. At launch the open session must
|
|
/// target (watchedHome, id) — a session born under another Home has no
|
|
/// cards here. At COMPLETION the re-check is identity too: the old
|
|
/// ID-only guard let a scan started under the previous library land its
|
|
/// cards on the new library's same-slug piece after a repoint.
|
|
func refreshCards() {
|
|
guard let session, let id = session.postID, let home = watchedHome,
|
|
session.targets(postID: id, home: home), // brief-28
|
|
let folder = PostStore.folder(of: id, home: home)
|
|
else {
|
|
openPieceCards = []
|
|
openPieceHasSnapshot = false
|
|
return
|
|
}
|
|
// Plain disk reads re-run on every watcher burst (typing retriggers
|
|
// rescan) — keep the I/O off MainActor, same posture as rescan's
|
|
// Post.scan. The identity re-check on hop-back drops a scan that
|
|
// raced a piece switch or a Home repoint.
|
|
let scan = scanPieceCards // detached capture, not a property read
|
|
Task.detached { [weak self] in
|
|
let cards = scan(folder)
|
|
let hasSnapshot = PostStore.latestPassSnapshot(id: id, home: home) != nil
|
|
await MainActor.run { [weak self] in
|
|
// brief-28: (Home, ID) at completion, never the id alone.
|
|
guard let self, self.session?.targets(postID: id, home: home) == true else { return }
|
|
openPieceCards = cards
|
|
openPieceHasSnapshot = hasSnapshot
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The piece's display title (fallback: id) — runPass, the pass bar,
|
|
/// and the Fix Cards palette share this one lookup (review: three
|
|
/// hand-copies).
|
|
func title(of id: String) -> String {
|
|
posts.first(where: { $0.id == id })?.title ?? id
|
|
}
|
|
}
|
|
|
|
// MARK: Fix Card apply/skip/done (#25, S3.2/S3.3)
|
|
|
|
/// #25 (S3.2/S3.3): what Apply did — the edit landed, the anchor went
|
|
/// stale (no text touched, card flagged), or the operation refused with
|
|
/// a reason for the error strip.
|
|
enum FixCardApplyOutcome: Equatable {
|
|
case applied
|
|
case stale
|
|
case refused(String)
|
|
}
|
|
|
|
extension PostsModel {
|
|
/// #25 (S3.2/S3.3): Apply a mechanical card — the blocking ask already
|
|
/// happened in the UI (ask-before trust level); this lands the edit.
|
|
/// Dirty buffer lands first (Apply reads live text); an unanswered #67
|
|
/// conflict aborts here with the existing wording. A stale anchor
|
|
/// flags the card and touches no text. A failed close after a landed
|
|
/// edit is reported via lastError, never silent.
|
|
@discardableResult
|
|
func applyCard(_ card: FixCardStore.StoredFixCard) -> FixCardApplyOutcome {
|
|
guard card.status == .open else { return .refused("card already closed") }
|
|
guard card.lane == .mechanical else { return .refused("demonstration cards close Done or Skip") }
|
|
guard let session, let postID = session.postID else {
|
|
return .refused("open the piece to work its cards")
|
|
}
|
|
guard flushOrAbort(postID) else { return .refused(lastError ?? "edits did not land") }
|
|
guard let newText = FixCardStore.appliedText(of: card, in: session.text) else {
|
|
// Stale anchor — the Post changed after the card was issued:
|
|
// persist the honest flag, select and change nothing. A failed
|
|
// flag write surfaces like the applied path's — never silent.
|
|
do {
|
|
_ = try FixCardStore.close(
|
|
pieceURL: session.passPieceURL, id: card.id, status: .open, anchorStatus: .stale,
|
|
)
|
|
} catch {
|
|
lastError = "card close failed: \(error.localizedDescription)"
|
|
}
|
|
refreshCards()
|
|
return .stale
|
|
}
|
|
guard session.adoptText(newText) else { return .refused(lastError ?? "write failed") }
|
|
closeCard(card, status: .applied)
|
|
return .applied
|
|
}
|
|
|
|
/// #25 (S3.3): Skip — not this instance. Closes the card recording
|
|
/// NOTHING beyond the status flip (CONTEXT.md).
|
|
func skipCard(_ card: FixCardStore.StoredFixCard) {
|
|
closeCard(card, status: .skipped)
|
|
}
|
|
|
|
/// #25 (S3.3): Done — the writer rewrote the span themselves. Same
|
|
/// flip, different terminal.
|
|
func doneCard(_ card: FixCardStore.StoredFixCard) {
|
|
closeCard(card, status: .done)
|
|
}
|
|
|
|
private func closeCard(
|
|
_ card: FixCardStore.StoredFixCard,
|
|
status: FixCardStore.StoredFixCard.Status,
|
|
) {
|
|
guard let session else {
|
|
lastError = "open the piece to work its cards"
|
|
return
|
|
}
|
|
do {
|
|
_ = try FixCardStore.close(pieceURL: session.passPieceURL, id: card.id, status: status)
|
|
} catch {
|
|
lastError = "card close failed: \(error.localizedDescription)"
|
|
}
|
|
refreshCards()
|
|
}
|
|
}
|
|
|
|
// MARK: Jump to the anchor (#204)
|
|
|
|
/// #204: one jump-to-the-anchor request — the anchor's span in the live
|
|
/// session text plus a token that advances per jump so the editor column
|
|
/// (which applies once per token) re-fires on a repeated jump. Ephemeral
|
|
/// view state: nothing here ever reaches disk.
|
|
struct FixCardRevealRequest: Equatable {
|
|
var range: NSRange
|
|
var token: Int
|
|
}
|
|
|
|
extension PostsModel {
|
|
/// #204: jump to a card's anchor — the palette's Jump. Resolves the
|
|
/// anchor against the LIVE session text (a dirty buffer still lands
|
|
/// where the writer sees it) and stamps one reveal request; the editor
|
|
/// column observes it and selects + reveals the span. A stale anchor
|
|
/// (quote gone, occurrence short) stamps nothing — the previous
|
|
/// request stands. Touches no text, no card, no disk: a jump is a view
|
|
/// act, the writer's hands stay on the wheel.
|
|
func revealCard(_ card: FixCardStore.StoredFixCard) {
|
|
guard let session, let range = FixCardStore.anchorRange(of: card, in: session.text) else { return }
|
|
revealRequest = FixCardRevealRequest(range: range, token: (revealRequest?.token ?? 0) + 1)
|
|
}
|
|
}
|
|
|
|
// MARK: Fix Card promote (#26, S4.1/S4.3)
|
|
|
|
/// #26 (S4.1): what Promote did — the Learning landed, or the operation
|
|
/// refused with a reason (the ask sheet shows it; lastError carries it).
|
|
enum FixCardPromoteOutcome: Equatable {
|
|
case promoted
|
|
case refused(String)
|
|
}
|
|
|
|
extension PostsModel {
|
|
/// #26 (S4.1/S4.3): Promote a card into ONE plain markdown Learning
|
|
/// under `<home>/learnings/<type>s/` — the writer's EXPLICIT
|
|
/// confirmation only (one confirm = one entry, never a repetition
|
|
/// counter; the store guarantees the one-stamp rule). Touches no text
|
|
/// and flushes nothing: this writes a Learning, not an edit.
|
|
@discardableResult
|
|
func promoteCard(_ card: FixCardStore.StoredFixCard) -> FixCardPromoteOutcome {
|
|
guard card.promotedAt == nil else { return .refused("card already promoted") }
|
|
guard let session else { return .refused("open the piece to work its cards") }
|
|
do {
|
|
_ = try FixCardStore.promote(pieceURL: session.passPieceURL, card: card)
|
|
} catch {
|
|
lastError = "learning write failed: \(error.localizedDescription)"
|
|
return .refused("learning write failed: \(error.localizedDescription)")
|
|
}
|
|
refreshCards()
|
|
return .promoted
|
|
}
|
|
}
|