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:
1 parent
8992e2ade5
commit
4138bf22dd
3 files changed
+186
-116
No files matched your search
@@ -26,6 +26,8 @@ enum ReadingPresenter {
|
||||
let sourceRange: NSRange
|
||||
/// Popover image URL for an `` chunk (#69 hover floor).
|
||||
let imageURL: URL?
|
||||
/// Canvas-drawn decoration (quote hairline / rule / table grid).
|
||||
var decoration: Decoration?
|
||||
}
|
||||
|
||||
/// A block's placement in the built document: where its content landed
|
||||
@@ -34,6 +36,19 @@ enum ReadingPresenter {
|
||||
let docRange: NSRange
|
||||
let sourceRange: NSRange
|
||||
let imageURL: URL?
|
||||
var decoration: Decoration?
|
||||
}
|
||||
|
||||
/// Canvas-drawn decoration for constructs whose layout NSTextTable /
|
||||
/// NSTextBlock can't deliver on modern TextKit (#69 fix: draw it
|
||||
/// ourselves from measured geometry instead).
|
||||
enum Decoration {
|
||||
/// Left hairline at the given indent.
|
||||
case quote(indent: CGFloat)
|
||||
/// Full-width horizontal rule.
|
||||
case rule
|
||||
/// Column x-offsets (incl. closing edge) in mono advance.
|
||||
case table(columns: [CGFloat])
|
||||
}
|
||||
|
||||
/// The reading document: blocks concatenated with paragraph-rhythm
|
||||
@@ -62,6 +77,7 @@ enum ReadingPresenter {
|
||||
docRange: NSRange(location: start, length: out.length - start),
|
||||
sourceRange: block.sourceRange,
|
||||
imageURL: image,
|
||||
decoration: block.decoration,
|
||||
))
|
||||
}
|
||||
return Document(content: out, bounds: bounds)
|
||||
@@ -101,13 +117,16 @@ enum ReadingPresenter {
|
||||
// HR first (#69: `* * *`/`- - -` would otherwise match the bullet
|
||||
// marker `* `/`- ` — same predicate as editing, checked earlier).
|
||||
if lines.count == 1, ProsePresenter.lineKind(first) == .syntaxOnly {
|
||||
return Block(content: hrContent(), sourceRange: sourceRange, imageURL: nil)
|
||||
return Block(content: hrContent(), sourceRange: sourceRange, imageURL: nil, decoration: .rule)
|
||||
}
|
||||
if lines.allSatisfy({ listItemMarker($0) != nil }) {
|
||||
return Block(content: listContent(lines), sourceRange: sourceRange, imageURL: nil)
|
||||
}
|
||||
if lines.allSatisfy({ blockquoteDepth($0) > 0 }) {
|
||||
return Block(content: quoteContent(lines), sourceRange: sourceRange, imageURL: nil)
|
||||
return Block(content: quoteContent(lines), sourceRange: sourceRange, imageURL: nil, decoration: quoteDecoration(lines))
|
||||
}
|
||||
if lines.count >= 2, isTableDelimiterRow(lines[1]), let table = tableContent(lines) {
|
||||
return Block(content: table, sourceRange: sourceRange, imageURL: nil, decoration: tableDecoration(lines))
|
||||
}
|
||||
return Block(content: flowContent(lines, first: first), sourceRange: sourceRange, imageURL: nil)
|
||||
}
|
||||
@@ -135,9 +154,6 @@ enum ReadingPresenter {
|
||||
let inner = lines.dropFirst().dropLast(hasClosingFence(lines) ? 1 : 0)
|
||||
return codeContent(inner.joined(separator: "\n"))
|
||||
}
|
||||
if lines.count >= 2, isTableDelimiterRow(lines[1]), let table = tableContent(lines) {
|
||||
return table
|
||||
}
|
||||
// HR — the editing presenter's syntaxOnly matcher reused verbatim
|
||||
// (#69: same predicate both modes = one mental model).
|
||||
if ProsePresenter.lineKind(first) == .syntaxOnly {
|
||||
@@ -185,8 +201,8 @@ enum ReadingPresenter {
|
||||
}
|
||||
|
||||
private static func hrContent() -> NSAttributedString {
|
||||
// Hairline drawn by the canvas (Decoration.rule).
|
||||
let ps = NSMutableParagraphStyle()
|
||||
ps.textBlocks = [RuleTextBlock()]
|
||||
ps.paragraphSpacingBefore = 8
|
||||
ps.paragraphSpacing = 8
|
||||
return NSAttributedString(string: " ", attributes: [
|
||||
@@ -234,13 +250,14 @@ enum ReadingPresenter {
|
||||
// MARK: Blockquotes (#69: `>` never visible, hairline rule, nesting indents)
|
||||
|
||||
private static func quoteContent(_ lines: [String]) -> NSAttributedString {
|
||||
// Hairline drawn by the canvas (Decoration.quote) — NSTextBlock
|
||||
// drawing zero-widths the line rect on modern TextKit, wrapping
|
||||
// content one character per line.
|
||||
let out = NSMutableAttributedString()
|
||||
let quoteBlock = QuoteTextBlock()
|
||||
for (index, line) in lines.enumerated() {
|
||||
let depth = blockquoteDepth(line)
|
||||
let indent = CGFloat(6 + (depth - 1) * 18)
|
||||
let ps = NSMutableParagraphStyle()
|
||||
ps.textBlocks = [quoteBlock]
|
||||
ps.firstLineHeadIndent = indent
|
||||
ps.headIndent = indent
|
||||
ps.paragraphSpacing = 4
|
||||
@@ -265,51 +282,89 @@ enum ReadingPresenter {
|
||||
return String(line.dropFirst(prefix.count)).trimmingCharacters(in: .whitespaces)
|
||||
}
|
||||
|
||||
// MARK: Tables (#69: simple 1px-bordered NSTextTable; delimiter-row
|
||||
// MARK: Tables (#69: mono grid — plain padded text; the canvas draws the
|
||||
|
||||
// alignment honored — cheap once the delimiter is parsed anyway)
|
||||
// grid lines from Decoration.table. NSTextTable cannot lay out on modern
|
||||
// TextKit: rows stack vertically, columns never align.)
|
||||
|
||||
private static func tableContent(_ lines: [String]) -> NSAttributedString? {
|
||||
let header = tableCells(lines[0])
|
||||
let aligns = tableAlignments(lines[1])
|
||||
guard !header.isEmpty, header.count == aligns.count else { return nil }
|
||||
let table = NSTextTable()
|
||||
table.numberOfColumns = header.count
|
||||
table.collapsesBorders = true
|
||||
table.setBorderColor(ProseTypography.tableBorderColor)
|
||||
table.setWidth(1, type: .absolute, for: .border)
|
||||
let out = NSMutableAttributedString()
|
||||
var row = 0
|
||||
for (index, line) in lines.enumerated() {
|
||||
if index == 1 {
|
||||
continue
|
||||
} // delimiter row
|
||||
let cells = tableCells(line)
|
||||
for column in 0 ..< header.count {
|
||||
let text = column < cells.count ? cells[column] : ""
|
||||
let cell = NSTextTableBlock(
|
||||
table: table, startingRow: row, rowSpan: 1,
|
||||
startingColumn: column, columnSpan: 1,
|
||||
)
|
||||
cell.setWidth(1, type: .absolute, for: .border)
|
||||
let ps = NSMutableParagraphStyle()
|
||||
ps.textBlocks = [cell]
|
||||
ps.alignment = aligns[column]
|
||||
let attrs: [NSAttributedString.Key: Any] = [
|
||||
.font: row == 0 ? ProseTypography.boldBodyFont : ProseTypography.bodyFont,
|
||||
.paragraphStyle: ps,
|
||||
.foregroundColor: ProseTypography.foregroundColor,
|
||||
]
|
||||
out.append(NSAttributedString(
|
||||
string: text + (column == header.count - 1 ? "\n" : ""),
|
||||
attributes: attrs,
|
||||
))
|
||||
let dataRows = lines.enumerated().filter { $0.offset != 1 }.map(\.element)
|
||||
let cellTexts = ([lines[0]] + dataRows.dropFirst()).map(tableCells)
|
||||
let columnCount = header.count
|
||||
var widths = Array(repeating: 0, count: columnCount)
|
||||
for row in cellTexts {
|
||||
for (column, cell) in row.enumerated() where column < columnCount {
|
||||
widths[column] = max(widths[column], cell.count)
|
||||
}
|
||||
row += 1
|
||||
}
|
||||
let pad = 2
|
||||
let codeAttrs: [NSAttributedString.Key: Any] = [
|
||||
.font: ProseTypography.codeFont,
|
||||
.foregroundColor: ProseTypography.foregroundColor,
|
||||
]
|
||||
let out = NSMutableAttributedString()
|
||||
func gridLine(_ cells: [String]) -> NSAttributedString {
|
||||
var line = ""
|
||||
for column in 0 ..< columnCount {
|
||||
let text = column < cells.count ? cells[column] : ""
|
||||
let width = widths[column]
|
||||
let padded: String
|
||||
switch aligns[column] {
|
||||
case .center where text.count < width:
|
||||
let left = (width - text.count) / 2
|
||||
padded = String(repeating: " ", count: left) + text
|
||||
+ String(repeating: " ", count: width - text.count - left)
|
||||
case .right:
|
||||
padded = String(repeating: " ", count: max(0, width - text.count)) + text
|
||||
default:
|
||||
padded = text + String(repeating: " ", count: max(0, width - text.count))
|
||||
}
|
||||
line += String(repeating: " ", count: pad) + padded + String(repeating: " ", count: pad)
|
||||
}
|
||||
return NSAttributedString(string: line, attributes: codeAttrs)
|
||||
}
|
||||
out.append(gridLine(cellTexts[0]))
|
||||
for row in cellTexts.dropFirst() {
|
||||
out.append(NSAttributedString(string: "\n", attributes: codeAttrs))
|
||||
out.append(gridLine(row))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/// Quote decoration: left hairline at the shallowest quote indent.
|
||||
private static func quoteDecoration(_ lines: [String]) -> Decoration {
|
||||
let minIndent = lines.compactMap { line -> CGFloat? in
|
||||
blockquoteDepth(line) > 0 ? CGFloat(6 + (blockquoteDepth(line) - 1) * 18) : nil
|
||||
}.min() ?? 6
|
||||
return .quote(indent: minIndent)
|
||||
}
|
||||
|
||||
/// Table decoration: column x-offsets (+ closing edge) in mono advance.
|
||||
private static func tableDecoration(_ lines: [String]) -> Decoration {
|
||||
let header = tableCells(lines[0])
|
||||
let dataRows = lines.enumerated().filter { $0.offset != 1 }.map(\.element)
|
||||
let cellTexts = ([lines[0]] + dataRows.dropFirst()).map(tableCells)
|
||||
var widths = Array(repeating: 0, count: header.count)
|
||||
for row in cellTexts {
|
||||
for (column, cell) in row.enumerated() where column < header.count {
|
||||
widths[column] = max(widths[column], cell.count)
|
||||
}
|
||||
}
|
||||
let advance = ("0" as NSString).size(withAttributes: [
|
||||
.font: ProseTypography.codeFont,
|
||||
]).width
|
||||
var offsets: [CGFloat] = []
|
||||
var running = 0.0
|
||||
for width in widths {
|
||||
offsets.append(running)
|
||||
running += CGFloat(width + 4) * advance
|
||||
}
|
||||
return .table(columns: offsets + [running])
|
||||
}
|
||||
|
||||
private static func tableCells(_ line: String) -> [String] {
|
||||
var trimmed = line.trimmingCharacters(in: .whitespaces)
|
||||
if trimmed.hasPrefix("|") {
|
||||
@@ -444,34 +499,3 @@ enum ReadingPresenter {
|
||||
return URL(string: String(trimmed[start ..< closeParen]))
|
||||
}
|
||||
}
|
||||
|
||||
/// Hairline-left text block for blockquotes (#69).
|
||||
final nonisolated class QuoteTextBlock: NSTextBlock {
|
||||
override func drawBackground(
|
||||
withFrame frameRect: NSRect, in _: NSView?,
|
||||
characterRange _: NSRange, layoutManager _: NSLayoutManager,
|
||||
) {
|
||||
let path = NSBezierPath()
|
||||
path.move(to: NSPoint(x: frameRect.minX + 2, y: frameRect.minY))
|
||||
path.line(to: NSPoint(x: frameRect.minX + 2, y: frameRect.maxY))
|
||||
path.lineWidth = 2
|
||||
ProseTypography.quoteRuleColor.setStroke()
|
||||
path.stroke()
|
||||
}
|
||||
}
|
||||
|
||||
/// Full-width hairline text block for horizontal rules (#69).
|
||||
final nonisolated class RuleTextBlock: NSTextBlock {
|
||||
override func drawBackground(
|
||||
withFrame frameRect: NSRect, in _: NSView?,
|
||||
characterRange _: NSRange, layoutManager _: NSLayoutManager,
|
||||
) {
|
||||
let midY = frameRect.midY
|
||||
let path = NSBezierPath()
|
||||
path.move(to: NSPoint(x: frameRect.minX, y: midY))
|
||||
path.line(to: NSPoint(x: frameRect.maxX, y: midY))
|
||||
path.lineWidth = 1
|
||||
ProseTypography.tableBorderColor.setStroke()
|
||||
path.stroke()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user