fix(editor): draw quote/HR/table decorations on the canvas (#71 follow-up 2)

NSTextBlock/NSTextTable layout is broken on this TextKit build regardless
of engine: textBlocks zero-width the line rect (quotes wrapped one
character per line), NSTextTable rows stack vertically with no grid
(tables rendered as concatenated text with a stray outline). The TextKit1
bridge attempt failed live too. Stop fighting the API:

- Presenter emits plain content: quotes indent-only, HR a thin invisible
  line, tables mono-space-padded grid text with alignment honored.
- Each Block/Bound carries a Decoration payload (quote indent, rule,
  table column offsets); ReadingCanvasView.draw renders hairlines,
  rules, and table grids from measured layoutManager geometry.
- Tests assert decorations + column alignment instead of textBlocks.

Full suite green; lint clean.
This commit is contained in:
AlexKucera committed 2026-10-05 18:05:16 +02:00
1 parent 8992e2ade5
commit 4138bf22dd
3 files changed
+186 -116

No files matched your search

+66 -4
View File
@@ -64,10 +64,6 @@ struct ReadingSurface: NSViewRepresentable {
tv.isVerticallyResizable = true
tv.isHorizontallyResizable = false
tv.autoresizingMask = [.width]
// 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)
@@ -136,6 +132,72 @@ final class ReadingCanvasView: NSTextView {
blockBounds.last { $0.docRange.location <= index }
}
// MARK: Decorations (#69 fix: quote hairlines, HR rules, table grids —
// drawn from measured geometry; NSTextBlock/NSTextTable layout is
// broken on modern TextKit)
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
guard let layoutManager, let container = textContainer else { return }
for bound in blockBounds {
guard let decoration = bound.decoration else { continue }
let glyphRange = layoutManager.glyphRange(
forCharacterRange: bound.docRange, actualCharacterRange: nil,
)
switch decoration {
case let .quote(indent):
let rect = layoutManager.boundingRect(forGlyphRange: glyphRange, in: container)
let x = textContainerOrigin.x + container.lineFragmentPadding + indent - 6
let path = NSBezierPath()
path.move(to: NSPoint(x: x, y: rect.minY))
path.line(to: NSPoint(x: x, y: rect.maxY))
path.lineWidth = 2
ProseTypography.quoteRuleColor.setStroke()
path.stroke()
case .rule:
let rect = layoutManager.boundingRect(forGlyphRange: glyphRange, in: container)
let y = rect.midY
let path = NSBezierPath()
path.move(to: NSPoint(x: textContainerOrigin.x, y: y))
path.line(to: NSPoint(x: textContainerOrigin.x + container.size.width, y: y))
path.lineWidth = 1
ProseTypography.tableBorderColor.setStroke()
path.stroke()
case let .table(columns):
let rect = layoutManager.boundingRect(forGlyphRange: glyphRange, in: container)
let originX = textContainerOrigin.x + container.lineFragmentPadding
let path = NSBezierPath()
for offset in columns {
let x = originX + offset
path.move(to: NSPoint(x: x, y: rect.minY))
path.line(to: NSPoint(x: x, y: rect.maxY))
}
var lineStart = glyphRange.location
var rowLines: [CGFloat] = []
while lineStart < NSMaxRange(glyphRange) {
var effective = NSRange(location: 0, length: 0)
let lineRect = layoutManager.lineFragmentRect(
forGlyphAt: lineStart, effectiveRange: &effective,
withoutAdditionalLayout: true,
)
if effective.length == 0 {
break
}
rowLines.append(lineRect.maxY)
lineStart = NSMaxRange(effective)
}
for y in rowLines {
path.move(to: NSPoint(x: originX, y: y))
path.line(to: NSPoint(x: originX + (columns.last ?? 0), y: y))
}
path.lineWidth = 1
ProseTypography.tableBorderColor.setStroke()
path.stroke()
}
}
}
override func updateTrackingAreas() {
super.updateTrackingAreas()
if hoverTrackingArea == nil {