feat(editor): reading polish — inline images, rhythm, table padding, margins (#71 follow-up 6)

- Images render inline via NSTextAttachment (scaled to 480pt, alt as
  caption); the hover popover and its ImagePopoverView are gone.
- Vertical rhythm per the author's measurements: lineHeightMultiple 1.25,
  1 line after paragraphs, 2 after headings (all block styles).
- Table cells breathe: mono pad 2→3 per side; decoration offsets measure
  with the same pad so the grid still lands on the text.
- Margins: lineFragmentPadding 12→24 (reading + editing), inset height 24.
- Image URL resolution hoisted into blocks(for:) — the attachment loads
  the resolved path, document() no longer re-resolves.
This commit is contained in:
AlexKucera committed 2026-10-06 12:49:04 +02:00
1 parent 7ed7a56642
commit 2d4e2563f7
3 files changed
+67 -18

No files matched your search

+1 -1
View File
@@ -282,7 +282,7 @@ struct ProseTextView: NSViewRepresentable {
// 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.textContainer?.lineFragmentPadding = 24
tv.textContainerInset = NSSize(width: 0, height: 20)
let scroll = NSScrollView()
scroll.documentView = tv
+64 -15
View File
@@ -19,6 +19,11 @@ import AppKit
/// Reading-mode presentation: text → rendered blocks. Pure over its input;
/// called on the main actor (NSTextTable construction is MainActor).
enum ReadingPresenter {
/// Vertical rhythm (user measurements, #71 polish): line-height ≈1.25,
/// 1 blank line after paragraphs, 2 after headings.
private static let lineHeightMultiple: CGFloat = 1.25
private static let paragraphSpacingAfter = lineHeightMultiple // 1 line
private static let headingSpacingAfter = lineHeightMultiple * 2 // 2 lines
/// One rendered block. `sourceRange` is the block's UTF-16 range in the
/// source text (delimiter-exclusive) — the #68 click→edit map.
struct Block {
@@ -63,20 +68,16 @@ enum ReadingPresenter {
static func document(for text: String, resolvingImagesIn postFolder: URL?) -> Document {
let out = NSMutableAttributedString()
var bounds: [Bound] = []
for block in blocks(for: text) {
for block in blocks(for: text, resolvingImagesIn: postFolder) {
if out.length > 0, !out.string.hasSuffix("\n") {
out.append(NSAttributedString(string: "\n", attributes: ProseTypography.bodyAttributes))
}
let start = out.length
out.append(block.content)
let image = block.imageURL.flatMap { raw -> URL? in
guard raw.scheme == nil, let postFolder else { return raw }
return postFolder.appendingPathComponent(raw.path)
}
bounds.append(Bound(
docRange: NSRange(location: start, length: out.length - start),
sourceRange: block.sourceRange,
imageURL: image,
imageURL: block.imageURL,
decoration: block.decoration,
))
}
@@ -87,7 +88,8 @@ enum ReadingPresenter {
/// Render `text` into reading blocks: blank-line split, one block per
/// chunk (a run of list items or quote lines renders as one block).
static func blocks(for text: String) -> [Block] {
/// `postFolder` resolves relative image URLs before the attachment loads.
static func blocks(for text: String, resolvingImagesIn postFolder: URL?) -> [Block] {
var out: [Block] = []
let ns = text as NSString
var loc = 0
@@ -98,7 +100,7 @@ enum ReadingPresenter {
if end > loc {
let chunkRange = NSRange(location: loc, length: end - loc)
let chunk = ns.substring(with: chunkRange)
out.append(block(chunk: chunk, sourceRange: chunkRange))
out.append(block(chunk: chunk, sourceRange: chunkRange, postFolder: postFolder))
}
loc = delim.location == NSNotFound ? ns.length : delim.location + 2
}
@@ -106,9 +108,15 @@ enum ReadingPresenter {
}
/// Classify one chunk and render it.
private static func block(chunk: String, sourceRange: NSRange) -> Block {
if let url = imageURL(in: chunk) {
return Block(content: inlineParse(chunk), sourceRange: sourceRange, imageURL: url)
private static func block(chunk: String, sourceRange: NSRange, postFolder: URL?) -> Block {
if let raw = imageURL(in: chunk) {
let url = (raw.scheme == nil && raw.path.count > 0)
? postFolder?.appendingPathComponent(raw.path) ?? raw : raw
return Block(
content: imageBlockContent(url: url, alt: altText(in: chunk)),
sourceRange: sourceRange,
imageURL: url,
)
}
let lines = chunk.split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
guard let first = lines.first else {
@@ -133,6 +141,35 @@ enum ReadingPresenter {
// MARK: Flow blocks
/// Image block: the picture inline (scaled to the column), alt as the
/// caption below, link preserved for ⌘-click. Fails soft to alt-text
/// paragraph when the file can't load.
private static func imageBlockContent(url: URL, alt: String) -> NSAttributedString {
let ps = NSMutableParagraphStyle()
ps.paragraphSpacing = 10
ps.paragraphSpacingBefore = 8
guard let image = NSImage(contentsOf: url), image.size.width > 0 else {
return inlineParse(alt.isEmpty ? "![missing](\(url.lastPathComponent))" : alt)
}
let attachment = NSTextAttachment()
attachment.image = image
let maxW: CGFloat = 480
if image.size.width > maxW {
attachment.image?.size = NSSize(width: maxW, height: maxW * image.size.height / image.size.width)
}
let cell = NSMutableAttributedString(attachment: attachment, attributes: [
.paragraphStyle: ps,
])
if !alt.isEmpty {
cell.append(NSAttributedString(string: "\n" + alt, attributes: [
.font: ProseTypography.codeFont,
.foregroundColor: ProseTypography.syntaxColor,
.paragraphStyle: ps,
]))
}
return cell
}
private static func flowContent(_ lines: [String], first: String) -> NSAttributedString {
if let level = headingLevel(first) {
let body = first.drop { $0 == "#" }.trimmingCharacters(in: .whitespaces)
@@ -178,7 +215,8 @@ enum ReadingPresenter {
default: ProseTypography.bodyFont.pointSize
}
let ps = NSMutableParagraphStyle()
ps.paragraphSpacing = level <= 2 ? 10 : 8
ps.lineHeightMultiple = lineHeightMultiple
ps.paragraphSpacing = headingSpacingAfter
ps.paragraphSpacingBefore = level == 1 ? 26 : (level == 2 ? 20 : 14)
return NSAttributedString(string: text, attributes: [
.font: ProseTypography.headingFont(size: size),
@@ -189,7 +227,8 @@ enum ReadingPresenter {
private static func codeContent(_ code: String) -> NSAttributedString {
let ps = NSMutableParagraphStyle()
ps.paragraphSpacing = 10
ps.lineHeightMultiple = lineHeightMultiple
ps.paragraphSpacing = paragraphSpacingAfter
ps.paragraphSpacingBefore = 6
ps.firstLineHeadIndent = 12
ps.headIndent = 12
@@ -203,6 +242,7 @@ enum ReadingPresenter {
private static func hrContent() -> NSAttributedString {
// Hairline drawn by the canvas (Decoration.rule).
let ps = NSMutableParagraphStyle()
ps.lineHeightMultiple = lineHeightMultiple
ps.paragraphSpacingBefore = 8
ps.paragraphSpacing = 8
return NSAttributedString(string: " ", attributes: [
@@ -223,6 +263,7 @@ enum ReadingPresenter {
guard let marker = listItemMarker(line) else { continue }
let indent: CGFloat = 16 + CGFloat(marker.level - 1) * 18
let ps = NSMutableParagraphStyle()
ps.lineHeightMultiple = lineHeightMultiple
ps.firstLineHeadIndent = indent
ps.headIndent = indent + 18
ps.paragraphSpacing = 5
@@ -258,6 +299,7 @@ enum ReadingPresenter {
let depth = blockquoteDepth(line)
let indent = CGFloat(6 + (depth - 1) * 18)
let ps = NSMutableParagraphStyle()
ps.lineHeightMultiple = lineHeightMultiple
ps.firstLineHeadIndent = indent
ps.headIndent = indent
ps.paragraphSpacing = 4
@@ -300,7 +342,7 @@ enum ReadingPresenter {
widths[column] = max(widths[column], cell.count)
}
}
let pad = 2
let pad = 3
let codeAttrs: [NSAttributedString.Key: Any] = [
.font: ProseTypography.codeFont,
.foregroundColor: ProseTypography.foregroundColor,
@@ -360,7 +402,7 @@ enum ReadingPresenter {
var running = 0.0
for width in widths {
offsets.append(running)
running += CGFloat(width + 4) * advance
running += CGFloat(width + 6) * advance // cell pad 3 + 3, matches gridLine
}
return .table(columns: offsets + [running])
}
@@ -428,6 +470,13 @@ enum ReadingPresenter {
return out
}
/// Alt text of an `![alt](url)` chunk (empty when absent).
private static func altText(in chunk: String) -> String {
let trimmed = chunk.trimmingCharacters(in: .whitespacesAndNewlines)
guard trimmed.hasPrefix("!["), let closeBracket = trimmed.firstIndex(of: "]") else { return "" }
return String(trimmed[trimmed.index(trimmed.startIndex, offsetBy: 2) ..< closeBracket])
}
// MARK: Construct matchers
private struct ListMarker {
+2 -2
View File
@@ -68,8 +68,8 @@ struct ReadingSurface: NSViewRepresentable {
// 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)
tv.textContainer?.lineFragmentPadding = 24
tv.textContainerInset = NSSize(width: 0, height: 24)
let scroll = NSScrollView()
scroll.documentView = tv
scroll.hasVerticalScroller = true