feat(fix-cards): jump to the anchor

Jump on a Fix Card selects the anchor's span in the piece's
editor and makes the main window key (#204). The span is the
card's quote at its 1-based occurrence, resolved against the
LIVE session text so a dirty buffer still lands, and it rides
the reading flip-back's caret rail: caretHint widens from a
(location, token) tuple to FixCardRevealRequest (SwiftLint
large_tuple), one shared token counter so a jump and a flip-back
can never collide, a zero-length range stays a bare caret and a
non-zero range selects. A jump leaves reading mode first
(reading = false, not exitReading, which stamps its own
zero-length hint), writes nothing, and re-fires per click.

The palette offers Jump on every non-stale card that still shows
an action row; stale anchors have nothing to land on. The
landing math is ProseTextView.landingSpan - a bare caret hops
past leading blanks, a span never does (a quote that IS the
leading blanks selects those blanks) - and appliedText now
routes through anchorRange so Apply and Jump share one
occurrence math.

Two review rounds closed the view-layer gaps the pure pins
could not see: a jump dropped in reading mode; a main window
that never became key (NSApp.activate is a no-op once the
palette's click activated the app - the Coordinator makes
tv.window key instead); a cursor record taken before the scroll.

+3 tests (anchorRange occurrence math with a UTF-16 fixture, the
model's reveal stamp, landingSpan), each flip-verified. Rail
732/732; SwiftFormat and SwiftLint clean. DOX: Museion/AGENTS,
MuseionTests/AGENTS, GLOSSARY gains Jump.
This commit is contained in:
AlexKucera committed 2026-12-03 17:48:55 +01:00
1 parent d183d735d7
commit 97893147a6
10 files changed
+259 -52

No files matched your search

+71 -29
View File
@@ -47,9 +47,10 @@ struct PostEditorView: View {
/// #68: reading is a transient act of inspection — no persistence; a
/// fresh open always lands in editing.
@State private var reading = false
/// Flip-back caret (#68): source location + a token so repeated flips to
/// the same location re-apply. Consumed by ProseTextView's coordinator.
@State private var flipBackCaret: (location: Int, token: Int)?
/// Flip-back caret (#68) and #204 jump span: a zero-length range is a
/// bare caret landing, a non-zero range selects the anchor; the token
/// re-applies repeats. Consumed by ProseTextView's coordinator.
@State private var flipBackCaret: FixCardRevealRequest?
@State private var flipToken = 0
/// Reading scroll state (#68 mapping): entry caret + live top block.
@State private var readingState = ReadingStateBox()
@@ -108,6 +109,22 @@ struct PostEditorView: View {
}
}
}
// #204: a Fix Card jump (the palette's Jump) lands here — the model's
// reveal request rides the SAME caret rail as the reading flip-back,
// through the ONE token counter (two counters could collide and a
// jump would silently never land): the span selected, revealed, and
// the main window made key so the writer can type where the card
// pointed (the coordinator does that — it owns tv.window). A jump
// while READING leaves reading first — plain `reading = false`, not
// exitReading (which would stamp its own zero-length hint over the
// jump); the remounted coordinator applies the jump after the load.
// Outline pieces have no prose surface — the request is dropped.
.onChange(of: gate.posts.revealRequest) { _, request in
guard let request, !outline else { return }
flipToken += 1
flipBackCaret = FixCardRevealRequest(range: request.range, token: flipToken)
reading = false
}
.onAppear {
if draftMirror.isEmpty {
draftMirror = draftText ?? ""
@@ -220,7 +237,7 @@ struct PostEditorView: View {
flipToken += 1
// Toggle-back maps the reading scroll back (top visible block);
// click-to-edit lands at the clicked paragraph (survives always).
flipBackCaret = (caret ?? readingState.topSourceLocation ?? 0, flipToken)
flipBackCaret = FixCardRevealRequest(range: NSRange(location: caret ?? readingState.topSourceLocation ?? 0, length: 0), token: flipToken)
reading = false
}
@@ -350,8 +367,9 @@ final class ProseDropBridge {
/// 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.
/// #71: `caretHint` (location + token) lands the caret after a flip back
/// from reading — applied once per token, after any generation load.
/// #71/#204: `caretHint` (range + token) lands a bare caret after a flip
/// back from reading or SELECTS a card's anchor span after a Jump — applied
/// once per token, after any generation load.
struct ProseTextView: NSViewRepresentable {
var session: EditorSession?
var draft: String?
@@ -363,7 +381,7 @@ struct ProseTextView: NSViewRepresentable {
var spellChecking = false
/// #21: focus mode — the text ground joins the workspace background.
var focused = false
var caretHint: (location: Int, token: Int)?
var caretHint: FixCardRevealRequest?
/// #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.
@@ -378,7 +396,7 @@ struct ProseTextView: NSViewRepresentable {
session: EditorSession,
spellChecking: Bool = false,
focused: Bool = false,
caretHint: (location: Int, token: Int)? = nil,
caretHint: FixCardRevealRequest? = nil,
dropBridge: ProseDropBridge,
routeDrop: @escaping ([URL]) -> Bool,
onTextChange: ((String) -> Void)? = nil,
@@ -398,7 +416,7 @@ struct ProseTextView: NSViewRepresentable {
spellChecking: Bool = false,
focused: Bool = false,
onEdit: @escaping (String) -> Void,
caretHint: (location: Int, token: Int)? = nil,
caretHint: FixCardRevealRequest? = nil,
dropBridge: ProseDropBridge,
routeDrop: @escaping ([URL]) -> Bool,
onTextChange: ((String) -> Void)? = nil,
@@ -428,12 +446,25 @@ struct ProseTextView: NSViewRepresentable {
/// location 0 then rests inside the invisible 9pt blank fragment ABOVE
/// the title, rendering as a detached terracotta stroke at the canvas
/// top. Programmatic landings (cursor restore, reading flip-back) hop
/// over leading blanks to the first content line; interactive
/// placements (click, drag, typing) never route through here.
/// over leading blanks to the first content line — zero-length landings
/// only, a #204 span never hops (`landingSpan`); interactive placements
/// (click, drag, typing) never route through here.
static func caretPastLeadingBlanks(_ location: Int, in text: String) -> Int {
max(location, ProsePresenter.firstContentLocation(text as NSString) ?? location)
}
/// Where a caret-hint lands, clamped to the text: a zero-length range
/// (#71 flip-back) is a bare caret and hops past leading blanks; a
/// non-zero range (#204 jump) SELECTS the anchor exactly where it
/// resolved — a quote that IS the leading blanks selects those blanks,
/// never the title (the load restore gates its hop the same way).
static func landingSpan(for hint: FixCardRevealRequest, in text: String) -> NSRange {
let length = (text as NSString).length
let start = hint.range.length == 0 ? caretPastLeadingBlanks(max(0, hint.range.location), in: text) : max(0, hint.range.location)
let loc = min(start, length)
return NSRange(location: loc, length: min(max(0, hint.range.length), length - loc))
}
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)
@@ -541,25 +572,9 @@ struct ProseTextView: NSViewRepresentable {
tv.textStorage?.setAttributedString(ProsePresenter.attribute(draft))
tv.refreshFolds() // #115: re-key folds over the swapped content
}
// #71: flip-back caret — apply once per token, after any load above.
// #71 flip-back caret / #204 jump span — apply once per token, after any load above.
if let hint = caretHint, context.coordinator.appliedCaretToken != hint.token {
context.coordinator.appliedCaretToken = hint.token
let length = (tv.string as NSString).length
// #ui-r1: same leading-blank hop as the load restore — a reading
// flip-back must never park the caret above the first content line.
let loc = min(Self.caretPastLeadingBlanks(max(0, hint.location), in: tv.string), length)
tv.setSelectedRange(NSRange(location: loc, length: 0))
// ponytail: same layout race as the cursor-restore scroll above —
// this was the click-to-edit "always lands at top" defect: the sync
// scrollRangeToVisible fired before the target region was laid out.
// One runloop hop fixes it (offscreen-verified, 3000-para doc).
DispatchQueue.main.async { [weak tv] in
tv?.scrollRangeToVisible(NSRange(location: loc, length: 0))
tv?.window?.makeFirstResponder(tv)
}
if let session {
session.cursorUpdate(ProseTextView.cursorState(of: tv))
}
context.coordinator.applyCaretHint(hint, tv: tv, session: session)
}
}
@@ -585,6 +600,33 @@ struct ProseTextView: NSViewRepresentable {
onTextChange = representable.onTextChange
}
/// The caret rail's landing (#71 flip-back, #204 jump). Called by the
/// representable once per token.
func applyCaretHint(_ hint: FixCardRevealRequest, tv: NSTextView, session: EditorSession?) {
appliedCaretToken = hint.token
let landing = ProseTextView.landingSpan(for: hint, in: tv.string)
tv.setSelectedRange(landing)
// ponytail: same layout race as the cursor-restore scroll — this was
// the click-to-edit "always lands at top" defect: the sync
// scrollRangeToVisible fired before the target region was laid out.
// One runloop hop fixes it (offscreen-verified, 3000-para doc).
DispatchQueue.main.async { [weak tv] in
guard let tv else { return }
// #204: a jump (span > 0) comes from the palette window, which
// holds key after the click — make the main window key or the
// first responder never sees keystrokes. The #71 flip-back
// (span 0) is already in the key window; untouched.
if landing.length > 0 {
tv.window?.makeKeyAndOrderFront(nil)
}
tv.scrollRangeToVisible(landing)
tv.window?.makeFirstResponder(tv)
// Recorded AFTER the scroll: the sidecar's scrollOffset must be
// the landed one, or a reopen restores an off-screen selection.
session?.cursorUpdate(ProseTextView.cursorState(of: tv))
}
}
func textDidChange(_ notification: Notification) {
guard let tv = notification.object as? NSTextView, let storage = tv.textStorage else { return }
let sel = tv.selectedRange()