feat(editor): land reading toggle — render, interact, typography slots (S1.2 #71)
Map #20 execution child, built per decisions #68/#69/#70: - ReadingPresenter: block-aware renderer (ADR-0010 stance) — blank-line split, one block per chunk; lists (bullets + ordered + nesting + auto-renumber via per-level counters — one rendering path), headings by level (h1 = slot size, h2 = 0.77x, ### = body+1, #### floors at bold body), HR via the editing syntaxOnly predicate, fenced code, blockquotes (QuoteTextBlock hairline), tables (NSTextTable 1px border, delimiter-row alignment), images (hover payload), links styled + retained. Footnotes stay fallback. Source ranges ride every block (click→edit map). - ReadingSurface: read-only selectable NSTextView canvas (#68: a presentation of the same TextKit2 document). Tap with no selection after mouseDown returns = edit intent at the clicked block (the tracking-loop fix); ⌘-click opens links; scroll tracking feeds the flip-back caret; image hover NSPopover loads off-main. - ProseTypography (#70): three role slots — Body / Headings / Code — each {face, size}, UserDefaults six values, member-based font resolution (family+weight lookup misresolves Concourse Book→Thin), silent system fallback; both prose surfaces read them; editing presenter's headings + dimmed syntax rewired through the slots. - PostEditorView: Read⇄Edit toolbar button (far left) + View menu "Toggle Reading Mode" + ⌘R; reading state transient per open; drafts included (draft mirror); flip-back caret hint applied once per token. - SettingsPaneView: Typography section — three slot rows (family picker incl. current face not in the installed list + size field). - kitchen-sink.md (+png): corpus piece exercising constructs absent from the eight posts, per #69. Verified: 94 unit tests (renderer classification, renumbering, source ranges, fallbacks) + UI smoke; PERF-3k renders 128 blocks in ~17 ms; live-app probe — Read renders kitchen-sink markers-hidden, renumbered, byte-stable file after full round-trip; click-to-edit flips at the clicked paragraph; typography ladder pixel-verified (20/15 ink px).
This commit is contained in:
1 parent
f2953ee07e
commit
4c91733e78
12 files changed
+1393
-33
No files matched your search
+112
-10
@@ -7,12 +7,23 @@
|
||||
// attributes re-applied per edited paragraph; native undo/IME/find. Session
|
||||
// mode autosaves via EditorSession (#64–#67); draft mode stays #62's
|
||||
// explicit-⌘S path. The #18 stub's Save button died here (#65).
|
||||
// #71 (map #20): the reading toggle (#68) — Read⇄Edit toolbar button (far
|
||||
// left, before the crumb) + View menu item + ⌘R; reading renders
|
||||
// ReadingPresenter output in a read-only canvas; a tap with no selection
|
||||
// drops to editing at that paragraph; drafts included; no persistence —
|
||||
// every fresh open lands in editing.
|
||||
//
|
||||
|
||||
import AppKit
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
extension Notification.Name {
|
||||
/// #68: the View menu's "Toggle Reading Mode" item — the visible editor
|
||||
/// listens (one editor column exists at a time).
|
||||
static let toggleReadingMode = Notification.Name("museion.toggleReadingMode")
|
||||
}
|
||||
|
||||
/// Editor column content: toolbar row (crumb + save-state widget) + surface.
|
||||
/// `session` nil = draft mode (crumb + ⌘S first-write, no autosave, no dot).
|
||||
struct PostEditorView: View {
|
||||
@@ -23,29 +34,58 @@ struct PostEditorView: View {
|
||||
var draftSave: (() -> Void)?
|
||||
|
||||
@State private var closeObserver: AnyCancellable?
|
||||
@State private var toggleObserver: AnyCancellable?
|
||||
/// #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)?
|
||||
@State private var flipToken = 0
|
||||
/// Reading scroll state (#68 mapping): entry caret + live top block.
|
||||
@State private var readingState = ReadingStateBox()
|
||||
/// Draft text mirror — reading renders it; edits flow back via onEdit.
|
||||
@State private var draftMirror = ""
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
toolbar
|
||||
Rectangle().fill(DesignTokens.border).frame(height: 1)
|
||||
if let session {
|
||||
ProseTextView(session: session)
|
||||
if reading {
|
||||
ReadingSurface(
|
||||
text: renderText,
|
||||
generation: session?.loadGeneration ?? 0,
|
||||
postFolder: session?.folderURL,
|
||||
entryCaretLocation: readingState.entryLocation,
|
||||
onEditIntent: { loc in exitReading(caret: loc) },
|
||||
stateBox: readingState,
|
||||
)
|
||||
} else {
|
||||
ProseTextView(draft: draftText ?? "", onEdit: { draftEdited?($0) })
|
||||
editingSurface
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
.background(DesignTokens.surface)
|
||||
// #64: flush on window close (app quit rides willTerminate in MuseionApp).
|
||||
// ponytail: no object filter — flush is idempotent; any-window close is fine.
|
||||
.onAppear {
|
||||
if draftMirror.isEmpty {
|
||||
draftMirror = draftText ?? ""
|
||||
}
|
||||
closeObserver = NotificationCenter.default.publisher(
|
||||
for: NSWindow.willCloseNotification,
|
||||
).sink { _ in
|
||||
session?.flush()
|
||||
}
|
||||
// #68: View menu / ⌘R reaches the visible editor.
|
||||
toggleObserver = NotificationCenter.default.publisher(
|
||||
for: .toggleReadingMode,
|
||||
).sink { [weak session] _ in
|
||||
toggleReading()
|
||||
_ = session
|
||||
}
|
||||
}
|
||||
// #67: explicit alert — never silent in either direction.
|
||||
// #67: explicit alert — never silent in either direction. It hangs
|
||||
// above the surface swap, so it shows while reading too; Yes-reload
|
||||
// bumps loadGeneration and the reading surface re-renders (#68 Q6).
|
||||
.alert("File on Disk changed", isPresented: conflictBinding) {
|
||||
Button("Reload") { session?.reloadFromDisk() }
|
||||
Button("Keep Local") { session?.keepLocal() }
|
||||
@@ -54,6 +94,43 @@ struct PostEditorView: View {
|
||||
}
|
||||
}
|
||||
|
||||
/// The text reading renders: live session text or the draft mirror.
|
||||
private var renderText: String {
|
||||
session?.text ?? draftMirror
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var editingSurface: some View {
|
||||
if let session {
|
||||
ProseTextView(session: session, caretHint: flipBackCaret)
|
||||
} else {
|
||||
ProseTextView(draft: draftMirror, onEdit: { newText in
|
||||
draftMirror = newText
|
||||
draftEdited?(newText)
|
||||
}, caretHint: flipBackCaret)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Reading toggle (#68)
|
||||
|
||||
private func toggleReading() {
|
||||
if reading {
|
||||
exitReading(caret: nil)
|
||||
} else {
|
||||
// Entry mapping: the editing caret's block scrolls to the top.
|
||||
readingState.entryLocation = session?.cursor.selectionLocation
|
||||
reading = true
|
||||
}
|
||||
}
|
||||
|
||||
private func exitReading(caret: Int?) {
|
||||
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)
|
||||
reading = false
|
||||
}
|
||||
|
||||
/// Extracted binding (SwiftFormat/SwiftLint closure_position peace).
|
||||
/// Dismissal without a button (Esc) = keep-local semantics, same as No.
|
||||
private var conflictBinding: Binding<Bool> {
|
||||
@@ -72,6 +149,9 @@ struct PostEditorView: View {
|
||||
|
||||
private var toolbar: some View {
|
||||
HStack {
|
||||
// #68: labeled toggle, far left, before the crumb.
|
||||
Button(reading ? "Edit" : "Read") { toggleReading() }
|
||||
.help(reading ? "Return to editing (⌘R)" : "Render this post (⌘R)")
|
||||
Text(crumb)
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
@@ -147,18 +227,27 @@ private struct SaveStateDot: View {
|
||||
/// 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.
|
||||
struct ProseTextView: NSViewRepresentable {
|
||||
var session: EditorSession?
|
||||
var draft: String?
|
||||
var onEdit: ((String) -> Void)?
|
||||
var caretHint: (location: Int, token: Int)?
|
||||
|
||||
init(session: EditorSession) {
|
||||
init(session: EditorSession, caretHint: (location: Int, token: Int)? = nil) {
|
||||
self.session = session
|
||||
self.caretHint = caretHint
|
||||
}
|
||||
|
||||
init(draft: String, onEdit: @escaping (String) -> Void) {
|
||||
init(
|
||||
draft: String,
|
||||
onEdit: @escaping (String) -> Void,
|
||||
caretHint: (location: Int, token: Int)? = nil,
|
||||
) {
|
||||
self.draft = draft
|
||||
self.onEdit = onEdit
|
||||
self.caretHint = caretHint
|
||||
}
|
||||
|
||||
/// Cursor state snapshot from the live view (#66).
|
||||
@@ -181,7 +270,7 @@ struct ProseTextView: NSViewRepresentable {
|
||||
tv.isAutomaticSpellingCorrectionEnabled = false
|
||||
tv.usesFindBar = true
|
||||
tv.backgroundColor = NSColor.white
|
||||
tv.typingAttributes = ProsePresenter.bodyAttributes
|
||||
tv.typingAttributes = ProseTypography.bodyAttributes
|
||||
tv.delegate = context.coordinator
|
||||
tv.layoutManager?.allowsNonContiguousLayout = true
|
||||
tv.minSize = NSSize(width: 0, height: 0)
|
||||
@@ -228,6 +317,18 @@ struct ProseTextView: NSViewRepresentable {
|
||||
// only reload when the textview genuinely diverges (new draft).
|
||||
tv.textStorage?.setAttributedString(ProsePresenter.attribute(draft))
|
||||
}
|
||||
// #71: flip-back caret — 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
|
||||
let loc = min(max(0, hint.location), length)
|
||||
tv.setSelectedRange(NSRange(location: loc, length: 0))
|
||||
tv.scrollRangeToVisible(NSRange(location: loc, length: 0))
|
||||
tv.window?.makeFirstResponder(tv)
|
||||
if let session {
|
||||
session.cursorUpdate(ProseTextView.cursorState(of: tv))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
@@ -239,6 +340,7 @@ struct ProseTextView: NSViewRepresentable {
|
||||
weak var textView: NSTextView?
|
||||
var sessionRef: EditorSession?
|
||||
var loadedGeneration = -1
|
||||
var appliedCaretToken = -1
|
||||
|
||||
init(representable: ProseTextView) {
|
||||
onEdit = representable.onEdit
|
||||
@@ -250,7 +352,7 @@ struct ProseTextView: NSViewRepresentable {
|
||||
// Re-attribute the paragraph(s) touching the selection — one pass.
|
||||
let paragraphRange = (storage.string as NSString).paragraphRange(for: sel)
|
||||
ProsePresenter.reattribute(storage as NSMutableAttributedString, paragraphRange: paragraphRange)
|
||||
tv.typingAttributes = ProsePresenter.bodyAttributes
|
||||
tv.typingAttributes = ProseTypography.bodyAttributes
|
||||
if let sessionRef {
|
||||
sessionRef.edit(storage.string)
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user