feat(editor): drop an image into the prose (#141)
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
![alt](assets/<slug>.<ext>) 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:
AlexKucera committed 2026-11-02 17:47:35 +01:00
1 parent 695ec27e11
commit de1154dacd
12 files changed
+622 -14

No files matched your search

+42 -5
View File
@@ -176,12 +176,25 @@ struct PostEditorView: View {
draftText: session == nil ? draftText : nil,
)
} else if let session {
ProseTextView(session: session, spellChecking: gate.spellChecking, focused: gate.isFocused, caretHint: flipBackCaret)
ProseTextView(
session: session,
spellChecking: gate.spellChecking,
focused: gate.isFocused,
caretHint: flipBackCaret,
dropBridge: gate.proseDrop,
)
} else {
ProseTextView(draft: draftMirror, spellChecking: gate.spellChecking, focused: gate.isFocused, onEdit: { newText in
draftMirror = newText
draftEdited?(newText)
}, caretHint: flipBackCaret)
ProseTextView(
draft: draftMirror,
spellChecking: gate.spellChecking,
focused: gate.isFocused,
onEdit: { newText in
draftMirror = newText
draftEdited?(newText)
},
caretHint: flipBackCaret,
dropBridge: gate.proseDrop,
)
}
}
@@ -309,6 +322,13 @@ private struct SaveStateDot: View {
// MARK: - TextKit2 surface (the lifted spike pane)
/// #141: the SwiftUI drop target's handle down to the live prose text
/// view (weak — dies with the editor surface).
@MainActor
final class ProseDropBridge {
weak var textView: ProseListTextView?
}
/// NSTextView in a scroll view; attributes are presentation-only over a plain
/// store. Session mode: edits → session.edit (debounce), cursor → session,
/// loads keyed by generation (open + Yes-reload). Draft mode: edits → onEdit.
@@ -322,17 +342,28 @@ struct ProseTextView: NSViewRepresentable {
/// #21: focus mode — the text ground joins the workspace background.
var focused = false
var caretHint: (location: Int, token: Int)?
/// #141: the piece folder dropped images ingest into — saved posts
/// only (drafts have no folder until first save; the external guest
/// isn't a piece). Nil = image drags refuse loudly.
var ingestFolder: URL?
/// #141: the drop bridge — the WINDOW-level SwiftUI drop target
/// reaches this text view through it (app-scoped, passed down from
/// the gate at both call sites — never optional).
var dropBridge: ProseDropBridge
init(
session: EditorSession,
spellChecking: Bool = false,
focused: Bool = false,
caretHint: (location: Int, token: Int)? = nil,
dropBridge: ProseDropBridge,
) {
self.session = session
self.spellChecking = spellChecking
self.focused = focused
self.caretHint = caretHint
ingestFolder = session.postID != nil ? session.folderURL : nil
self.dropBridge = dropBridge
}
init(
@@ -341,12 +372,14 @@ struct ProseTextView: NSViewRepresentable {
focused: Bool = false,
onEdit: @escaping (String) -> Void,
caretHint: (location: Int, token: Int)? = nil,
dropBridge: ProseDropBridge,
) {
self.draft = draft
self.spellChecking = spellChecking
self.focused = focused
self.onEdit = onEdit
self.caretHint = caretHint
self.dropBridge = dropBridge
}
/// Cursor state snapshot from the live view (#66).
@@ -361,6 +394,8 @@ struct ProseTextView: NSViewRepresentable {
func makeNSView(context: Context) -> NSScrollView {
let tv = ProseListTextView() // #115: list shortcuts + folds on the plain surface
tv.ingestFolder = ingestFolder // #141: image-drop ingest (nil on drafts/guests)
dropBridge.textView = tv // #141: the SwiftUI drop target's handle
tv.isRichText = false // plain source of truth; attributes are presentation
tv.allowsUndo = true
tv.isAutomaticQuoteSubstitutionEnabled = false
@@ -407,6 +442,8 @@ struct ProseTextView: NSViewRepresentable {
guard let tv = scroll.documentView as? ProseListTextView else { return }
// #77: posture toggle while editing — cheap idempotent set.
tv.isContinuousSpellCheckingEnabled = spellChecking
tv.ingestFolder = ingestFolder // #141: cheap idempotent set
dropBridge.textView = tv // #141: idempotent re-point
// #21: the ground follows focus in/out without a remount.
let ground = focused ? NSColor(DesignTokens.background) : NSColor(DesignTokens.surface)
tv.backgroundColor = ground