feat(outline): convert to post — S4 (#114)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
In-place outline→post conversion on the piece: id, folder, Pass State, origin kept, type frontmatter flips to post, body transforms once. The ask picks the transform — Verbatim Bullets (default, lossless) or Headings ladder (roots ##, stepping to the ###### floor, deeper stays bullets), with a warning when the ladder would drop paragraph lines between bullets. The open session flushes first, then reopens routed to prose via proseOverridePostID (lastDraftWasOutline's machinery reversed). An outline that went lossy on disk behind an unwarned ask aborts the convert; an unanswered #67 conflict aborts every flush-first op (shared flushOrAbort seam); rescan no longer hands a same-id reopened session its predecessor's stale disk text; a held #111 guard ask dies with the outline it pointed at. Audit round (finish-audit, three reviewers) included: 13 new pins, 206 tests green, pin discrimination verified. Record: docs/devlog/20261016-issue114-convert-to-post_log.md
This commit is contained in:
1 parent
21e2f27450
commit
c0513aae85
14 files changed
+547
-19
No files matched your search
@@ -131,6 +131,12 @@ final class PostsModel {
|
||||
/// a routing override with lastDraftWasOutline's exact lifecycle (dies
|
||||
/// on the next open/draft/guest; the rescan lag window is covered).
|
||||
private(set) var proseOverridePostID: String?
|
||||
|
||||
/// #114 (S4): the staged Convert to Post ask (row menu → sheet). The
|
||||
/// ask targets a piece id, not the session — it survives background
|
||||
/// selects; the sheet is modal in its window, and the convert itself
|
||||
/// re-derives everything (flush, disk read) at answer time.
|
||||
private(set) var pendingConversion: PendingConversion?
|
||||
/// Last failed-op message surfaced in the editor (nil = clean).
|
||||
private(set) var lastError: String?
|
||||
/// Home the watcher is bound to; also the rescan target.
|
||||
@@ -170,6 +176,10 @@ final class PostsModel {
|
||||
// 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
|
||||
// #114 audit: identity, not id — a same-id reopen (convert/restore/
|
||||
// move) must not receive the OLD session's stale disk text as a
|
||||
// spurious #67 conflict.
|
||||
let openSession = session
|
||||
let (scanned, openDiskText) = await Task.detached(priority: .userInitiated) {
|
||||
let posts = Post.scan(home: home)
|
||||
let disk = openPostID.flatMap { PostStore.readBody(id: $0, home: home) }
|
||||
@@ -186,8 +196,8 @@ final class PostsModel {
|
||||
}
|
||||
// #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)
|
||||
if openSession === session, let openDiskText {
|
||||
openSession?.diskDidChange(diskText: openDiskText)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -414,10 +424,18 @@ final class PostsModel {
|
||||
/// lastError set; the session stays up, dirty and honest.
|
||||
private func flushOrAbort(_ postID: String) -> Bool {
|
||||
guard let session, session.postID == postID else { return true }
|
||||
// #114 audit: an unanswered #67 conflict means disk holds foreign
|
||||
// text with the session's version unwritten — proceeding would act
|
||||
// on the foreign body and discard the editor's. Abort; the alert
|
||||
// stays up for the writer to answer, then the op retries.
|
||||
if session.conflictAlert {
|
||||
lastError = "This piece changed on disk underneath you — answer the reload question first."
|
||||
return false
|
||||
}
|
||||
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."
|
||||
lastError = (lastError.map { "\($0) " } ?? "") + "— nothing was changed."
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -673,3 +691,74 @@ extension PostsModel {
|
||||
wireSession()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Convert to Post (#114, S4)
|
||||
|
||||
/// The staged ask: which piece, and whether the headings ladder would
|
||||
/// drop prose lines (mixed content — the sheet warns, verbatim keeps
|
||||
/// everything).
|
||||
struct PendingConversion {
|
||||
let id: String
|
||||
let lossy: Bool
|
||||
}
|
||||
|
||||
extension PostsModel {
|
||||
/// #114 (S4): row-menu entry — stage the ask. Outline pieces only.
|
||||
/// The open session's text is the truth when the piece is open — disk
|
||||
/// lags the debounce, and a stale-clean read would drop the warning
|
||||
/// for prose typed since the last flush (#114 audit).
|
||||
func stageConversion(_ post: Post, home: URL) {
|
||||
guard post.type == .outline else { return }
|
||||
let body: String = if let session, session.postID == post.id {
|
||||
session.text
|
||||
} else {
|
||||
PostStore.readBody(id: post.id, home: home) ?? ""
|
||||
}
|
||||
pendingConversion = PendingConversion(id: post.id, lossy: OutlineTree.parsesWithLoss(body))
|
||||
}
|
||||
|
||||
/// #114 (S4): the ask's Cancel (binding dismissal).
|
||||
func cancelConversion() {
|
||||
pendingConversion = nil
|
||||
}
|
||||
|
||||
/// #114 (S4): the answer. Flush-first when the piece is open (the
|
||||
/// transform reads disk), flip `type` + transform the body once, then
|
||||
/// reopen the session routed to prose — proseOverridePostID covers the
|
||||
/// rescan lag (lastDraftWasOutline's machinery, reversed). A piece that
|
||||
/// isn't open just converts; nothing else moves. One-way in v1.
|
||||
func convertToPost(headingsLadder: Bool, home: URL) {
|
||||
guard let pending = pendingConversion else { return }
|
||||
pendingConversion = nil
|
||||
guard flushOrAbort(pending.id) else { return }
|
||||
do {
|
||||
// The sheet's warning covered the STAGED body. If the
|
||||
// answer-time body went lossy behind the ask (foreign write,
|
||||
// late edit) and the writer was never warned, Headings would
|
||||
// drop unconsented prose — abort, nothing converted (#114 audit).
|
||||
if headingsLadder, !pending.lossy,
|
||||
let body = PostStore.readBody(id: pending.id, home: home),
|
||||
OutlineTree.parsesWithLoss(body)
|
||||
{
|
||||
lastError = "The outline changed since the sheet opened — nothing was converted."
|
||||
return
|
||||
}
|
||||
try PostStore.convert(id: pending.id, headingsLadder: headingsLadder, home: home)
|
||||
if session?.postID == pending.id {
|
||||
session?.teardown(flushFirst: false) // flushOrAbort already wrote
|
||||
session = nil
|
||||
proseOverridePostID = pending.id
|
||||
openUnguarded(pending.id, home: home)
|
||||
}
|
||||
if outlineGuardPieceID == pending.id {
|
||||
// Multi-window: the held #111 ask targets an outline that no
|
||||
// longer is — dismiss it as a cancel (the ask's own teardown).
|
||||
cancelOutlineGuard()
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user