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

+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