fix(editor): typography knob changes reach open surfaces (#71 follow-up 9)
updateNSView never fires on UserDefaults writes from the Settings pane, so the flowKey check there was dead code. Both surfaces now observe UserDefaults.didChangeNotification directly: the editor re-attributes (selection preserved), the reading canvas rebuilds its document. Also closes the parked stale-reading-view defect from the handoff notes.
This commit is contained in:
1 parent
677dc8c073
commit
0d3d5af78a
2 files changed
+52
-8
No files matched your search
@@ -297,21 +297,18 @@ struct ProseTextView: NSViewRepresentable {
|
|||||||
|
|
||||||
func updateNSView(_ scroll: NSScrollView, context: Context) {
|
func updateNSView(_ scroll: NSScrollView, context: Context) {
|
||||||
guard let tv = scroll.documentView as? NSTextView else { return }
|
guard let tv = scroll.documentView as? NSTextView else { return }
|
||||||
// Typography/flow knob change: re-attribute everything, keep text
|
// Typography/flow knob changes arrive via the coordinator's
|
||||||
// and selection (settings live in another pane; the editor is idle).
|
// UserDefaults observer (SwiftUI doesn't invalidate on defaults
|
||||||
if context.coordinator.appliedFlowKey != ProseTypography.flowKey {
|
// writes from another pane).
|
||||||
|
context.coordinator.watchDefaults(tv)
|
||||||
context.coordinator.appliedFlowKey = ProseTypography.flowKey
|
context.coordinator.appliedFlowKey = ProseTypography.flowKey
|
||||||
let sel = tv.selectedRange()
|
|
||||||
let storage = tv.textStorage as! NSMutableAttributedString
|
|
||||||
storage.setAttributedString(ProsePresenter.attribute(tv.string))
|
|
||||||
tv.setSelectedRange(sel)
|
|
||||||
}
|
|
||||||
if let session {
|
if let session {
|
||||||
context.coordinator.sessionRef = session
|
context.coordinator.sessionRef = session
|
||||||
// Load only when the generation moved (open, Yes-reload) — never
|
// Load only when the generation moved (open, Yes-reload) — never
|
||||||
// per keystroke: the text view owns the live text.
|
// per keystroke: the text view owns the live text.
|
||||||
if context.coordinator.loadedGeneration != session.loadGeneration {
|
if context.coordinator.loadedGeneration != session.loadGeneration {
|
||||||
context.coordinator.loadedGeneration = session.loadGeneration
|
context.coordinator.loadedGeneration = session.loadGeneration
|
||||||
|
context.coordinator.appliedFlowKey = ProseTypography.flowKey
|
||||||
// Seed from session.text (LIVE), not session.loadText (the
|
// Seed from session.text (LIVE), not session.loadText (the
|
||||||
// open-time snapshot): the representable is recreated on the
|
// open-time snapshot): the representable is recreated on the
|
||||||
// reading⇄editing swap, and seeding from the snapshot showed
|
// reading⇄editing swap, and seeding from the snapshot showed
|
||||||
@@ -358,6 +355,29 @@ struct ProseTextView: NSViewRepresentable {
|
|||||||
var loadedGeneration = -1
|
var loadedGeneration = -1
|
||||||
var appliedCaretToken = -1
|
var appliedCaretToken = -1
|
||||||
var appliedFlowKey = ""
|
var appliedFlowKey = ""
|
||||||
|
private var defaultsObserver: NSObjectProtocol?
|
||||||
|
|
||||||
|
func watchDefaults(_ tv: NSTextView) {
|
||||||
|
guard defaultsObserver == nil else { return }
|
||||||
|
defaultsObserver = NotificationCenter.default.addObserver(
|
||||||
|
forName: UserDefaults.didChangeNotification,
|
||||||
|
object: nil,
|
||||||
|
queue: .main,
|
||||||
|
) { [weak self] _ in
|
||||||
|
self?.applyTypographyIfChanged(tv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func applyTypographyIfChanged(_ tv: NSTextView) {
|
||||||
|
let key = ProseTypography.flowKey
|
||||||
|
guard key != appliedFlowKey else { return }
|
||||||
|
appliedFlowKey = key
|
||||||
|
let sel = tv.selectedRange()
|
||||||
|
if let storage = tv.textStorage as? NSMutableAttributedString {
|
||||||
|
storage.setAttributedString(ProsePresenter.attribute(tv.string))
|
||||||
|
tv.setSelectedRange(sel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
init(representable: ProseTextView) {
|
init(representable: ProseTextView) {
|
||||||
onEdit = representable.onEdit
|
onEdit = representable.onEdit
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ struct ReadingSurface: NSViewRepresentable {
|
|||||||
tv.onEditIntent = onEditIntent
|
tv.onEditIntent = onEditIntent
|
||||||
tv.stateBox = stateBox
|
tv.stateBox = stateBox
|
||||||
context.coordinator.canvas = tv
|
context.coordinator.canvas = tv
|
||||||
|
context.coordinator.watchDefaults(tv, representable: self)
|
||||||
return scroll
|
return scroll
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,6 +111,29 @@ struct ReadingSurface: NSViewRepresentable {
|
|||||||
weak var canvas: ReadingCanvasView?
|
weak var canvas: ReadingCanvasView?
|
||||||
var builtGeneration = -1
|
var builtGeneration = -1
|
||||||
var builtText = ""
|
var builtText = ""
|
||||||
|
private var defaultsObserver: NSObjectProtocol?
|
||||||
|
private var appliedFlowKey = ""
|
||||||
|
|
||||||
|
/// Rebuild the rendered document when typography/flow knobs move
|
||||||
|
/// (SwiftUI doesn't re-call updateNSView on UserDefaults writes).
|
||||||
|
func watchDefaults(_: ReadingCanvasView, representable: ReadingSurface) {
|
||||||
|
guard defaultsObserver == nil else { return }
|
||||||
|
appliedFlowKey = ProseTypography.flowKey
|
||||||
|
defaultsObserver = NotificationCenter.default.addObserver(
|
||||||
|
forName: UserDefaults.didChangeNotification,
|
||||||
|
object: nil,
|
||||||
|
queue: .main,
|
||||||
|
) { [weak self] _ in
|
||||||
|
guard let self, let canvas else { return }
|
||||||
|
let key = ProseTypography.flowKey
|
||||||
|
guard key != appliedFlowKey else { return }
|
||||||
|
appliedFlowKey = key
|
||||||
|
let doc = ReadingPresenter.document(for: representable.text, resolvingImagesIn: representable.postFolder)
|
||||||
|
canvas.textStorage?.setAttributedString(doc.content)
|
||||||
|
canvas.blockBounds = doc.bounds
|
||||||
|
canvas.needsDisplay = true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user