feat(posts): import & external markdown — S1.1 (#22)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
External .md/.markdown opens and edits as-is in a single guest slot (File > Open... Cmd+O, drag-onto-window): SessionTarget extends EditorSession to write in place verbatim, no cursor sidecar, the #67 conflict alert reused for foreign files. Import... (Shift+Cmd+O) copies multi-select files in as Posts behind ONE Origin ask (writer default, immutable); external frontmatter keys ride verbatim with pass_state/origin set; slug/collision machinery shared with writeNew. Audit fixes folded in: BOM stripped + CRLF normalized at the trust boundary (a CRLF pair is one Swift grapheme — \n splits saw one line); unreadable files refuse to open (loadFailed — a failed read never becomes a write); dropped web links and severed symlinks guarded; ~24 lines of reviewer-flagged dead weight removed. 7 new tests; full unit + UI suite green; swiftlint clean. Map #22 stays open pending the live calibration pass (#88 posture).
This commit is contained in:
1 parent
683d697367
commit
0f1c4ef4fe
14 files changed
+691
-49
No files matched your search
+100
-24
@@ -19,20 +19,50 @@ enum SaveState: Equatable {
|
||||
case failed
|
||||
}
|
||||
|
||||
/// One open post's editing session (@MainActor @Observable, the PostsModel
|
||||
/// pattern). Born from a disk read; writes go out after the #64 debounce.
|
||||
/// What a session writes to (#92): a Post in the Home, or an external
|
||||
/// markdown file edited as-is — the file is the document (ADR-0004).
|
||||
enum SessionTarget {
|
||||
case post(id: String, home: URL)
|
||||
case externalFile(URL)
|
||||
}
|
||||
|
||||
/// One open document's editing session (@MainActor @Observable, the
|
||||
/// PostsModel pattern). Born from a disk read; writes go out after the #64
|
||||
/// debounce. External targets save in place and never write a cursor sidecar
|
||||
/// (#93) — nothing of ours lands next to a foreign file.
|
||||
@MainActor
|
||||
@Observable
|
||||
final class EditorSession {
|
||||
let postID: String
|
||||
private let home: URL
|
||||
let target: SessionTarget
|
||||
private let debounce: Duration
|
||||
|
||||
/// The post's folder (`<home>/posts/<id>`) — reading resolves relative
|
||||
/// image URLs against it (#71).
|
||||
/// The Post id for post targets; nil for external.
|
||||
var postID: String? {
|
||||
if case let .post(id, _) = target {
|
||||
return id
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// The external file's URL; nil for posts.
|
||||
var fileURL: URL? {
|
||||
if case let .externalFile(url) = target {
|
||||
return url
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// The document's folder — `posts/<id>/` for posts (image resolution
|
||||
/// #71); the external file's containing folder, so relative images
|
||||
/// resolve beside the file.
|
||||
var folderURL: URL {
|
||||
home.appendingPathComponent("posts", isDirectory: true)
|
||||
.appendingPathComponent(postID, isDirectory: true)
|
||||
switch target {
|
||||
case let .post(id, home):
|
||||
home.appendingPathComponent("posts", isDirectory: true)
|
||||
.appendingPathComponent(id, isDirectory: true)
|
||||
case let .externalFile(url):
|
||||
url.deletingLastPathComponent()
|
||||
}
|
||||
}
|
||||
|
||||
/// Current body text (live, per keystroke).
|
||||
@@ -59,18 +89,60 @@ final class EditorSession {
|
||||
private var lastFlushedText: String
|
||||
private var debounceTask: Task<Void, Never>?
|
||||
|
||||
/// True when the initial read failed (foreign file unreadable — wrong
|
||||
/// encoding, permissions, vanished). A session that never loaded must
|
||||
/// never write: the empty editor's first keystroke would otherwise
|
||||
/// destroy the file we failed to read.
|
||||
private(set) var loadFailed = false
|
||||
|
||||
/// Failure text for the owner's lastError strip; nil clears it (#65).
|
||||
var onError: (@MainActor @Sendable (String?) -> Void)?
|
||||
|
||||
init(postID: String, home: URL, debounce: Duration = .seconds(1)) {
|
||||
self.postID = postID
|
||||
self.home = home
|
||||
init(target: SessionTarget, debounce: Duration = .seconds(1)) {
|
||||
self.target = target
|
||||
self.debounce = debounce
|
||||
let body = PostStore.readBody(id: postID, home: home) ?? ""
|
||||
let read = Self.readDocument(target)
|
||||
loadFailed = read == nil
|
||||
let body = read ?? ""
|
||||
text = body
|
||||
loadText = body
|
||||
lastFlushedText = body
|
||||
restoreCursor = PostStore.readCursor(id: postID, home: home)
|
||||
if case let .post(id, home) = target {
|
||||
restoreCursor = PostStore.readCursor(id: id, home: home)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience: post-target session (#19 call sites).
|
||||
convenience init(postID: String, home: URL, debounce: Duration = .seconds(1)) {
|
||||
self.init(target: .post(id: postID, home: home), debounce: debounce)
|
||||
}
|
||||
|
||||
/// Whole-document read per target: posts edit body-only (frontmatter
|
||||
/// stamped back on write); external files round-trip the text — the file
|
||||
/// is the document (#91/#93). BOM stripped and CRLF normalized to LF on
|
||||
/// read (same reason as importFile: the corpus machinery splits on \n;
|
||||
/// in Swift a CRLF pair is one grapheme and every split sees one line).
|
||||
private static func readDocument(_ target: SessionTarget) -> String? {
|
||||
switch target {
|
||||
case let .post(id, home):
|
||||
return PostStore.readBody(id: id, home: home)
|
||||
case let .externalFile(url):
|
||||
guard var text = try? String(contentsOf: url, encoding: .utf8) else { return nil }
|
||||
if text.hasPrefix("\u{FEFF}") { text.removeFirst() }
|
||||
return text.replacingOccurrences(of: "\r\n", with: "\n")
|
||||
}
|
||||
}
|
||||
|
||||
/// Target-aware write: posts → `PostStore.overwrite` (+ sidecar);
|
||||
/// external → the whole text, in place, no sidecar (#93).
|
||||
private func writeDocument() throws {
|
||||
switch target {
|
||||
case let .post(id, home):
|
||||
try PostStore.overwrite(id: id, body: text, home: home)
|
||||
PostStore.writeCursor(cursor, id: id, home: home)
|
||||
case let .externalFile(url):
|
||||
try text.write(to: url, atomically: true, encoding: .utf8)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Edit + debounce (#64)
|
||||
@@ -98,19 +170,18 @@ final class EditorSession {
|
||||
|
||||
// MARK: Flush (#64/#65/#66)
|
||||
|
||||
/// Write body + sidecar now. ⌘S, debounce fire, selection change, window
|
||||
/// close, and app quit all land here. No-op when clean.
|
||||
/// Write body (+ post sidecar) now. ⌘S, debounce fire, selection change,
|
||||
/// window close, and app quit all land here. No-op when clean or when
|
||||
/// the document never loaded (a failed read must not become a write
|
||||
/// that destroys the file).
|
||||
func flush() {
|
||||
debounceTask?.cancel()
|
||||
guard text != lastFlushedText else { return }
|
||||
guard !loadFailed, text != lastFlushedText else { return }
|
||||
do {
|
||||
try PostStore.overwrite(id: postID, body: text, home: home)
|
||||
try writeDocument()
|
||||
lastFlushedText = text
|
||||
saveState = .saved
|
||||
onError?(nil)
|
||||
// Sidecar best-effort: the content write is what matters; a failed
|
||||
// cursor write must never read as a failed save.
|
||||
PostStore.writeCursor(cursor, id: postID, home: home)
|
||||
} catch {
|
||||
saveState = .failed
|
||||
onError?(error.localizedDescription)
|
||||
@@ -151,16 +222,21 @@ final class EditorSession {
|
||||
}
|
||||
|
||||
/// #67 Yes: reload from disk; cursor restored from the sidecar where
|
||||
/// still valid (the surface clamps).
|
||||
/// still valid (the surface clamps). External targets reset the cursor
|
||||
/// (#93 — no sidecar next to foreign files).
|
||||
func reloadFromDisk() {
|
||||
guard conflictAlert else { return }
|
||||
conflictAlert = false
|
||||
guard let disk = PostStore.readBody(id: postID, home: home) else { return }
|
||||
guard let disk = Self.readDocument(target) else { return }
|
||||
text = disk
|
||||
loadText = disk
|
||||
loadGeneration += 1
|
||||
lastFlushedText = disk
|
||||
restoreCursor = PostStore.readCursor(id: postID, home: home)
|
||||
if case let .post(id, home) = target {
|
||||
restoreCursor = PostStore.readCursor(id: id, home: home)
|
||||
} else {
|
||||
restoreCursor = nil
|
||||
}
|
||||
saveState = .saved
|
||||
// #83: reload resolves a failure too — shown text now equals disk.
|
||||
onError?(nil)
|
||||
@@ -174,7 +250,7 @@ final class EditorSession {
|
||||
func keepLocal() {
|
||||
guard conflictAlert else { return }
|
||||
conflictAlert = false
|
||||
if let disk = PostStore.readBody(id: postID, home: home) {
|
||||
if let disk = Self.readDocument(target) {
|
||||
lastFlushedText = disk
|
||||
}
|
||||
syncSaveState() // #83: local ≠ disk now — shows as pending changes
|
||||
|
||||
Reference in new issue
Block a user