// // PostsModelImports.swift // Museion // // #193: the external guest (#91–#93) and the import batch (#94/#95), // moved from PostsModel.swift (file-length ceiling) — openExternal, // dismissExternal, the guest watch, and stageImport/cancelImport/ // importFiles, code unchanged. // import Foundation extension PostsModel { /// #92: dismiss the external guest slot, flushing pending edits (the /// guest is always leaving for somewhere — same posture as post /// selection change). brief-25: the dismissal is GUARDED — a guest /// whose flush fails (or whose #67 conflict is unanswered) STAYS, /// buffer and watcher live: teardown would drop the only retained /// copy of the unsaved edits. Callers abort their navigation on /// false, before clearing anything. /// #146: internal — the split-out domain extensions dismiss too. @discardableResult func dismissExternal() -> Bool { guard let guest = externalSession else { externalWatcher.stop() return true } guard sessionMayDepart(guest) else { return false } guest.teardown(flushFirst: false) // the gate already flushed externalSession = nil externalWatcher.stop() return true } // 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() guard departOpenDocuments() else { return } // #92 + brief-25: the column's edits land or the drop aborts draft = nil pendingProject = nil // #108 audit: the draft died here too — its target dies with it lastDraftWasOutline = false // #109: same death, same clearing proseOverridePostID = nil // #111: same death, same clearing outlineGuardPieceID = nil // #111: the guest displaces any held ask openPieceCards = [] // #23: the guest is not a piece 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. The disk read goes through /// the shared external normalizer (brief-32): the session's text is /// normalized on open, so a raw read raised a false conflict on every /// directory event over an unchanged BOM/CRLF file. 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 = externalSession, let disk = EditorSession.readExternalDocument(at: url) else { return } session.diskDidChange(diskText: disk) } externalWatcher.start(urls: [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. #107: the batch lands in the selected /// post's project (newPost's target rule). #179: the batch rides the /// rail — the strip was cleared up front, so the per-file failure /// messages the loop sets survive the rail's success tail. func importFiles(origin: Post.Origin) { guard let urls = pendingImportURLs else { return } pendingImportURLs = nil let project = targetProject var last: Post? performWrite { home in for url in urls { do { last = try PostStore.importFile(at: url, origin: origin, project: project, home: home) } catch { lastError = "Import failed — \(url.lastPathComponent): \(error.localizedDescription)" } } } if let last { select(last.id) } } }