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

+23 -39
View File
@@ -63,32 +63,26 @@ struct ReadingPresenterTests {
#expect(doc.bounds[3].sourceRange.location == expected)
}
/// Tables render as NSTextTable blocks with header bold.
/// Tables render as a mono grid: decoration carries column offsets,
/// header/body rows are padded columns of text.
@Test
func `table renders`() {
func `table renders`() throws {
let src = "| A | B |\n| --- | --- |\n| one | two |"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(doc.bounds.count == 1)
#expect(doc.content.string.contains("one"))
#expect(doc.content.string.contains("two"))
let hasTableBlock = doc.content.enumerateAttribute(
.paragraphStyle, in: NSRange(location: 0, length: doc.content.length),
) { value, _, stop in
if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is NSTextTableBlock }) {
stop.pointee = true
}
let lines = doc.content.string.components(separatedBy: "\n")
#expect(lines.count == 2)
#expect(lines[0].contains("A"))
#expect(lines[1].contains("one"))
// alignment: same column starts across rows
let colA0 = try lines[0].distance(from: lines[0].startIndex, to: #require(lines[0].firstIndex(of: "A")))
let colA1 = try lines[1].distance(from: lines[1].startIndex, to: #require(lines[1].firstIndex(of: "o")))
#expect(colA0 == colA1)
guard case let .table(columns)? = doc.bounds[0].decoration else {
Issue.record("expected table decoration")
return
}
_ = hasTableBlock // enumerate returns Void; assertion below is the check
// direct check: at least one NSTextTableBlock anywhere
var found = false
doc.content.enumerateAttribute(
.paragraphStyle, in: NSRange(location: 0, length: doc.content.length),
) { value, _, _ in
if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is NSTextTableBlock }) {
found = true
}
}
#expect(found)
#expect(columns.count == 3) // two columns + closing edge
}
/// Fenced code: mono face, markers hidden, content intact.
@@ -102,22 +96,17 @@ struct ReadingPresenterTests {
#expect((attrs[.font] as? NSFont)?.fontName == ProseTypography.codeFont.fontName)
}
/// Blockquote: markers hidden, hairline block attached.
/// Blockquote: markers hidden, quote decoration on the bound.
@Test
func blockquote() {
let src = "> quoted _words_\n>> deeper"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(!doc.content.string.contains(">"))
#expect(doc.content.string.contains("quoted words"))
var foundQuoteBlock = false
doc.content.enumerateAttribute(
.paragraphStyle, in: NSRange(location: 0, length: doc.content.length),
) { value, _, _ in
if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is QuoteTextBlock }) {
foundQuoteBlock = true
}
guard case .quote? = doc.bounds[0].decoration else {
Issue.record("expected quote decoration")
return
}
#expect(foundQuoteBlock)
}
/// Links styled + attribute retained (⌘-click opens).
@@ -158,21 +147,16 @@ struct ReadingPresenterTests {
#expect(doc.content.string.contains("[^1]"))
}
/// HR: hairline block, matcher shared with editing.
/// HR: rule decoration, matcher shared with editing.
@Test
func `horizontal rule`() {
let src = "above\n\n* * *\n\nbelow"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(doc.bounds.count == 3)
var foundRule = false
doc.content.enumerateAttribute(
.paragraphStyle, in: NSRange(location: 0, length: doc.content.length),
) { value, _, _ in
if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is RuleTextBlock }) {
foundRule = true
}
guard case .rule? = doc.bounds[1].decoration else {
Issue.record("expected rule decoration")
return
}
#expect(foundRule)
}
/// Soft-joined lines: one paragraph, single spaces.