fix(editor): reading-mode layout + stale-reload defects (#71 follow-up)

Five root causes behind Alexander's calibration findings:

- Wrap to column: both canvases had a fixed 640pt text container —
  clipped instead of wrapping when the editor column is narrower
  (library + mentor open). widthTracksTextView = true now; measure
  polish can return with calibration.
- Tables: TextKit2 stacks NSTextTable rows vertically (each row its
  own content-width fragment — no grid). The reading canvas forces
  the TextKit1 bridge (replaceLayoutManager); probe-verified grid +
  column borders. Read-only canvas — nothing editing-grade lost.
- Heading-tail: a chunk like "# Title" followed by body lines (single
  newline, legal markdown) dropped everything after line one. The
  heading now renders, then the remaining lines as a soft-joined
  paragraph.
- Stale reload (data-loss vector): ProseTextView seeded from
  session.loadText (open-time snapshot) — after a reading⇄editing
  swap the recreated editor showed the pre-paste text, and a keystroke
  there flushed stale content over the user's real edits. Seeds from
  session.text (live) now.
- Quote one-char-per-line: did not reproduce after the layout fixes;
  root cause was the broken layout state above.

EditorSessionTests debounce flake (50ms margin under parallel load)
passed on both re-runs — timing margin, not a regression.

Full suite green (94 unit + UI smoke); lint clean.
This commit is contained in:
AlexKucera committed 2026-10-05 17:41:34 +02:00
1 parent 36858bcf1d
commit c410c99bf8
4 files changed
+34 -9

No files matched your search

+10 -3
View File
@@ -278,8 +278,10 @@ struct ProseTextView: NSViewRepresentable {
tv.isVerticallyResizable = true
tv.isHorizontallyResizable = false
tv.autoresizingMask = [.width]
tv.textContainer?.containerSize = NSSize(width: 640, height: CGFloat.greatestFiniteMagnitude)
tv.textContainer?.widthTracksTextView = false
// Wrap to the editor column (#71 fix): the container tracks the view
// width — the fixed 640pt measure clipped in narrower columns.
tv.textContainer?.containerSize = NSSize(width: 0, height: CGFloat.greatestFiniteMagnitude)
tv.textContainer?.widthTracksTextView = true
tv.textContainer?.lineFragmentPadding = 12
tv.textContainerInset = NSSize(width: 0, height: 20)
let scroll = NSScrollView()
@@ -301,7 +303,12 @@ struct ProseTextView: NSViewRepresentable {
// per keystroke: the text view owns the live text.
if context.coordinator.loadedGeneration != session.loadGeneration {
context.coordinator.loadedGeneration = session.loadGeneration
tv.textStorage?.setAttributedString(ProsePresenter.attribute(session.loadText))
// Seed from session.text (LIVE), not session.loadText (the
// open-time snapshot): the representable is recreated on the
// reading⇄editing swap, and seeding from the snapshot showed
// pre-edit text — then session.edit on the stale view would
// have flushed it over the user's real content (#71 fix).
tv.textStorage?.setAttributedString(ProsePresenter.attribute(session.text))
// #66: restore cursor (clamped) + scroll.
let length = (tv.string as NSString).length
var cursor = session.restoreCursor ?? PostStore.CursorState()
+16 -4
View File
@@ -117,7 +117,19 @@ enum ReadingPresenter {
private static func flowContent(_ lines: [String], first: String) -> NSAttributedString {
if let level = headingLevel(first) {
let body = first.drop { $0 == "#" }.trimmingCharacters(in: .whitespaces)
return headingContent(body, level: level)
let heading = headingContent(body, level: level)
// Bug fix: a heading chunk may carry body lines under the heading
// (single newline is legal markdown) — they are part of the block.
let rest = lines.dropFirst()
.map { $0.trimmingCharacters(in: .whitespaces) }
.filter { !$0.isEmpty }
if rest.isEmpty {
return heading
}
let out = NSMutableAttributedString(attributedString: heading)
out.append(NSAttributedString(string: "\n", attributes: ProseTypography.bodyAttributes))
out.append(inlineParse(rest.joined(separator: " ")))
return out
}
if first.hasPrefix("```") {
let inner = lines.dropFirst().dropLast(hasClosingFence(lines) ? 1 : 0)
@@ -209,7 +221,7 @@ enum ReadingPresenter {
item.append(inlineParse(marker.payload.trimmingCharacters(in: .whitespaces)))
item.addAttribute(
.paragraphStyle, value: ps,
range: NSRange(location: 0, length: item.length)
range: NSRange(location: 0, length: item.length),
)
out.append(item)
if index < lines.count - 1 {
@@ -277,7 +289,7 @@ enum ReadingPresenter {
let text = column < cells.count ? cells[column] : ""
let cell = NSTextTableBlock(
table: table, startingRow: row, rowSpan: 1,
startingColumn: column, columnSpan: 1
startingColumn: column, columnSpan: 1,
)
cell.setWidth(1, type: .absolute, for: .border)
let ps = NSMutableParagraphStyle()
@@ -290,7 +302,7 @@ enum ReadingPresenter {
]
out.append(NSAttributedString(
string: text + (column == header.count - 1 ? "\n" : ""),
attributes: attrs
attributes: attrs,
))
}
row += 1
+8 -2
View File
@@ -64,8 +64,14 @@ struct ReadingSurface: NSViewRepresentable {
tv.isVerticallyResizable = true
tv.isHorizontallyResizable = false
tv.autoresizingMask = [.width]
tv.textContainer?.containerSize = NSSize(width: 640, height: CGFloat.greatestFiniteMagnitude)
tv.textContainer?.widthTracksTextView = false
// Tables (#69) lay out correctly under TextKit1 only — TK2 stacks
// NSTextTable rows vertically (probed). The canvas is read-only, so
// nothing editing-grade is lost by forcing the TK1 bridge.
tv.textContainer?.replaceLayoutManager(NSLayoutManager())
// Wrap to the editor column (#68/71 fix): the container tracks the
// view width — a fixed 640pt measure clipped in narrower columns.
tv.textContainer?.containerSize = NSSize(width: 0, height: CGFloat.greatestFiniteMagnitude)
tv.textContainer?.widthTracksTextView = true
tv.textContainer?.lineFragmentPadding = 12
tv.textContainerInset = NSSize(width: 0, height: 20)
let scroll = NSScrollView()
View File
Whitespace-only changes.