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
+114
-8
@@ -82,12 +82,24 @@ final class PostsModel {
|
||||
/// nothing is selected.
|
||||
private(set) var session: EditorSession?
|
||||
|
||||
/// #92: the external buffer's guest slot — one at a time, transient per
|
||||
/// launch. Takes the editor column; opening a Post or draft dismisses it
|
||||
/// (flush first).
|
||||
private(set) var externalSession: EditorSession?
|
||||
|
||||
/// #94: a multi-file import waiting on its ONE Origin ask; the sheet
|
||||
/// answers, then the batch runs.
|
||||
private(set) var pendingImportURLs: [URL]?
|
||||
|
||||
/// 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.
|
||||
private var watchedHome: URL?
|
||||
private var watcher = PostsWatcher()
|
||||
/// #93: per-file watcher on the open external file — the #67 conflict
|
||||
/// check reused verbatim for foreign files.
|
||||
private var externalWatcher = PostsWatcher()
|
||||
|
||||
/// Bind a Home: ensure `posts/` exists (FSEvents on a missing path is
|
||||
/// unreliable; the AC's lazy rule is about post folders, not this root),
|
||||
@@ -130,7 +142,7 @@ final class PostsModel {
|
||||
selectedPostID = nil // the post vanished (deleted in Finder or here)
|
||||
if let open = session, open.postID == id {
|
||||
open.teardown(flushFirst: false) // post is gone; cancel the debounce
|
||||
self.session = nil
|
||||
session = nil
|
||||
}
|
||||
}
|
||||
// #67: watcher echo check on the open post — equality against the
|
||||
@@ -148,20 +160,33 @@ final class PostsModel {
|
||||
|
||||
/// Tear the watcher down on the main actor (its scheduling thread) —
|
||||
/// deinit of a @MainActor-held object may run elsewhere. #64: app quit
|
||||
/// flushes the open session first (sidecar rides along).
|
||||
/// flushes the open session first (sidecar rides along). #96: quit also
|
||||
/// flushes the external buffer — same posture, same moment.
|
||||
func shutdown() {
|
||||
session?.flush()
|
||||
externalSession?.flush()
|
||||
watcher.stop()
|
||||
externalWatcher.stop()
|
||||
}
|
||||
|
||||
/// New Post (#62): fresh in-memory draft, no folder, editor takes it.
|
||||
func newPost() {
|
||||
dismissExternal() // #92: opening a draft dismisses the guest
|
||||
session?.teardown(flushFirst: true) // #64: selection change flushes
|
||||
session = nil
|
||||
draft = PostDraft()
|
||||
selectedPostID = nil
|
||||
}
|
||||
|
||||
/// #92: dismiss the external guest slot, flushing pending edits (the
|
||||
/// guest is always leaving for somewhere — same posture as post
|
||||
/// selection change).
|
||||
private func dismissExternal() {
|
||||
externalSession?.teardown(flushFirst: true)
|
||||
externalSession = nil
|
||||
externalWatcher.stop()
|
||||
}
|
||||
|
||||
/// First save of the draft (#62): lazy folder, slug, #59 stamp all fire
|
||||
/// here. The saved post opens straight into an editing session — the
|
||||
/// writer keeps typing where they were. Rescans immediately (the watcher
|
||||
@@ -184,8 +209,10 @@ final class PostsModel {
|
||||
/// Select a Post; selecting anything abandons an unsaved draft (the
|
||||
/// draft otherwise hijacks the editor — its only exits are save or
|
||||
/// selecting away). #19: opening a post ends the previous session
|
||||
/// (flush-on-selection-change, #64) and starts a new one.
|
||||
/// (flush-on-selection-change, #64) and starts a new one. #92: it also
|
||||
/// dismisses the external guest (flush first).
|
||||
func select(_ id: String, home: URL) {
|
||||
dismissExternal()
|
||||
if let session, session.postID != id {
|
||||
session.teardown(flushFirst: true)
|
||||
}
|
||||
@@ -195,10 +222,6 @@ final class PostsModel {
|
||||
wireSession()
|
||||
}
|
||||
|
||||
private func wireSession() {
|
||||
session?.onError = { [weak self] message in self?.lastError = message }
|
||||
}
|
||||
|
||||
/// Draft typing from the editor (private(set) barrier).
|
||||
func updateDraft(_ text: String) {
|
||||
draft?.text = text
|
||||
@@ -238,7 +261,7 @@ final class PostsModel {
|
||||
selectedPostID = nil
|
||||
if let open = session, open.postID == post.id {
|
||||
open.teardown(flushFirst: false)
|
||||
self.session = nil
|
||||
session = nil
|
||||
}
|
||||
}
|
||||
lastError = nil
|
||||
@@ -247,4 +270,87 @@ final class PostsModel {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: External buffer (#91–#93) + Import (#94/#95)
|
||||
|
||||
/// #91/#92: open an external markdown file in the guest slot — one at a
|
||||
/// time; opening anything else (file, Post, draft) flushed and replaced
|
||||
/// it first. A Post session/draft stands down while the guest is up
|
||||
/// (the editor column is the guest's; the selection stays for return).
|
||||
/// Local files only (a dragged web link is not a document) and symlinks
|
||||
/// resolve to their real target so the in-place write never severs the
|
||||
/// link (rename over a symlink replaces it).
|
||||
func openExternal(_ url: URL) {
|
||||
guard url.isFileURL else { return }
|
||||
let target = url.resolvingSymlinksInPath()
|
||||
dismissExternal()
|
||||
session?.teardown(flushFirst: true) // #92: the guest takes the column
|
||||
session = nil
|
||||
draft = nil
|
||||
externalSession = EditorSession(target: .externalFile(target))
|
||||
if externalSession?.loadFailed == true {
|
||||
lastError = "Couldn't read \(target.lastPathComponent) — not UTF-8 text or unreadable. Nothing was changed."
|
||||
externalSession = nil
|
||||
return
|
||||
}
|
||||
wireSession()
|
||||
startExternalWatch(url: target)
|
||||
}
|
||||
|
||||
/// #93: watch the open external file — its rescan is the #67 disk-change
|
||||
/// check, reused verbatim for foreign files. PostsWatcher fires on the
|
||||
/// main actor already; no re-hop needed.
|
||||
private func startExternalWatch(url: URL) {
|
||||
externalWatcher.onEvent = { [weak self] in
|
||||
guard let self, let session = self.externalSession else { return }
|
||||
guard let disk = try? String(contentsOf: url, encoding: .utf8) else { return }
|
||||
session.diskDidChange(diskText: disk)
|
||||
}
|
||||
externalWatcher.start(url: url.deletingLastPathComponent())
|
||||
}
|
||||
|
||||
/// #94: stage a multi-file import — the ONE Origin ask presents; the
|
||||
/// sheet's answer runs the batch.
|
||||
func stageImport(_ urls: [URL]) {
|
||||
guard !urls.isEmpty else { return }
|
||||
pendingImportURLs = urls
|
||||
}
|
||||
|
||||
/// #94: dismiss the ask without an answer — the batch drops.
|
||||
func cancelImport() {
|
||||
pendingImportURLs = nil
|
||||
}
|
||||
|
||||
/// #94/#95: run the batch after the Origin answer — copy each file in as
|
||||
/// a Post; a per-file failure continues the batch into lastError (stated
|
||||
/// default, reversible; never an alert, never an abort). The last
|
||||
/// imported post opens; a failure anywhere keeps its message up even
|
||||
/// when other files landed.
|
||||
func importFiles(origin: Post.Origin, home: URL) {
|
||||
guard let urls = pendingImportURLs else { return }
|
||||
pendingImportURLs = nil
|
||||
var last: Post?
|
||||
var failures = 0
|
||||
for url in urls {
|
||||
do {
|
||||
last = try PostStore.importFile(at: url, origin: origin, home: home)
|
||||
} catch {
|
||||
failures += 1
|
||||
lastError = "Import failed — \(url.lastPathComponent): \(error.localizedDescription)"
|
||||
}
|
||||
}
|
||||
if let last {
|
||||
select(last.id, home: home)
|
||||
}
|
||||
if failures == 0 {
|
||||
lastError = nil
|
||||
}
|
||||
Task { await rescan() }
|
||||
}
|
||||
|
||||
private func wireSession() {
|
||||
let route: (@MainActor @Sendable (String?) -> Void)? = { [weak self] message in self?.lastError = message }
|
||||
session?.onError = route
|
||||
externalSession?.onError = route
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user