feat(editor): drop an image into the prose (#141)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
Dragging an image onto a saved post's prose editor copies it (never moves) into the piece's assets/ under a parser-clean slugged name (collision -2 via the shared uniqueFile helper) and inserts  at the drop point with alt selected for type-over — one edit step, one undo. Routing rides the window-level SwiftUI drop destination, field-verified to starve every deeper drag destination (nested SwiftUI targets never fire; AppKit NSDraggingDestination overrides never fire). Pure-markdown sets keep the open-as-guest behavior; mixed md+image sets refuse loudly instead of silently dropping the images behind the opened markdown. The drop point comes from NSEvent.mouseLocation mapped through the one geometry source that answers under SwiftUI hosting — firstRect(forCharacterRange:), bottom-up screen coords. Non-contiguous layout returns zero/stale rects for off-screen characters, so the solve (a pure static function, pinned by windowless tests) samples the document, keeps only viewport-band rects, brackets the drop between two in-band samples, binary-searches the bracket, and slides to the next whitespace gap — a landing never splices inside a word or an existing ref. The typewriter inset re-syncs after the edit so scrolling never freezes. The source gate refuses folders and symlinks alike: copyItem would land the LINK itself, and the #34 publish rail would then refuse the whole send. Type gate = the rail's exact set (PNG/JPEG/GIF/WebP/ SVG) — nothing enters assets/ that can't ship. Drafts, outlines, and guest surfaces refuse loudly. Finish-audit (three fresh-context reviewers) drove the mixed-set routing, the symlink refusal, and the pure-function extraction; every pin flip-verified in one batched cycle. Eleven ingest tests; suite 557. Closes #141
This commit is contained in:
1 parent
695ec27e11
commit
de1154dacd
12 files changed
+622
-14
No files matched your search
@@ -788,3 +788,88 @@ nonisolated extension PostStore {
|
||||
.write(to: file, atomically: true, encoding: .utf8)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - #141 Illustration ingest
|
||||
|
||||
/// #141's refusal reasons — the drop surface shows `localizedDescription`
|
||||
/// as its visible feedback (CardError/WriteError voice).
|
||||
struct IngestError: LocalizedError {
|
||||
let message: String
|
||||
var errorDescription: String? {
|
||||
message
|
||||
}
|
||||
|
||||
static func notPublishable(_ name: String) -> IngestError {
|
||||
IngestError(message: "“\(name.prefix(40))” isn’t publishable — PNG, JPEG, GIF, WebP, or SVG.")
|
||||
}
|
||||
|
||||
/// A dropped symlink (e.g. an unzipped archive entry) — copyItem would
|
||||
/// land the LINK itself in assets/; the #34 rail then refuses the send.
|
||||
static func isLink(_ name: String) -> IngestError {
|
||||
IngestError(message: "“\(name.prefix(40))” is a link, not an image file.")
|
||||
}
|
||||
|
||||
/// A Finder folder with an image extension — the extension gate
|
||||
/// alone would copyItem a whole directory into assets/.
|
||||
static func isFolder(_ name: String) -> IngestError {
|
||||
IngestError(message: "“\(name.prefix(40))” is a folder, not an image.")
|
||||
}
|
||||
|
||||
/// assets/ exists as a symlink or a file — never write through it
|
||||
/// (research/promote posture: the #34 rail would refuse the send).
|
||||
static func hostileAssets() -> IngestError {
|
||||
IngestError(message: "assets/ isn’t a plain folder in this piece.")
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated extension PostStore {
|
||||
/// The inserted ref for a landed asset: `` with
|
||||
/// `alt` as the selected placeholder for immediate type-over. The
|
||||
/// destination is parser-clean BY CONSTRUCTION (see `ingestImage`),
|
||||
/// so this exact string rides #34's rail — one helper so the drop
|
||||
/// surface and the tests can never drift.
|
||||
static func illustrationRef(_ destination: String) -> String {
|
||||
")"
|
||||
}
|
||||
|
||||
/// #141: copy a dropped image into the piece's `assets/` — copy,
|
||||
/// never move — and return the ref destination that names it. The
|
||||
/// base name goes through the shared `slug` (whitespace→hyphens and
|
||||
/// more: lowercase, diacritic-fold, 80-char cap) because the #34
|
||||
/// parser never matches space-bearing destinations — the ref and
|
||||
/// the file must agree at ingest. Collisions suffix `-2`/`-3`…
|
||||
/// within `assets/`. Type gate = #34's publish-able set, checked
|
||||
/// BEFORE anything lands.
|
||||
static func ingestImage(from source: URL, pieceFolder: URL) throws -> String {
|
||||
let name = source.lastPathComponent
|
||||
guard Micropub.mimeType(forDestination: name) != nil else {
|
||||
throw IngestError.notPublishable(name)
|
||||
}
|
||||
// The source must be a plain file: copyItem would land a symlink
|
||||
// AS a link (finish-audit 2026-11-02), and the #34 rail then
|
||||
// refuses the whole send — nothing enters assets/ that can't ship.
|
||||
let values = try? source.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey])
|
||||
guard source.isFileURL, let values,
|
||||
values.isDirectory != true, values.isSymbolicLink != true
|
||||
else {
|
||||
throw values?.isSymbolicLink == true ? IngestError.isLink(name) : IngestError.isFolder(name)
|
||||
}
|
||||
let assets = pieceFolder.appendingPathComponent("assets", isDirectory: true)
|
||||
if FileManager.default.fileExists(atPath: assets.path) {
|
||||
// A symlinked or file-shaped assets/ never takes the write.
|
||||
guard let values = try? assets.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey]),
|
||||
values.isDirectory == true, values.isSymbolicLink != true
|
||||
else {
|
||||
throw IngestError.hostileAssets()
|
||||
}
|
||||
} else {
|
||||
try FileManager.default.createDirectory(at: assets, withIntermediateDirectories: true)
|
||||
}
|
||||
let ext = source.pathExtension.lowercased()
|
||||
let base = slug(from: source.deletingPathExtension().lastPathComponent)
|
||||
let landed = FixCardStore.uniqueFile(assets, base: base, ext: ext)
|
||||
let destination = "assets/\(landed)"
|
||||
try FileManager.default.copyItem(at: source, to: assets.appendingPathComponent(landed))
|
||||
return destination
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user