Two defects from the flow-knob commit: (1) edit mode got reading-mode's paragraph spacing stacked on top of the visible blank source lines — gaps tripled. Source editors render the blank line AS the separator; paragraph spacing is now 0, only lineHeightMultiple rides the knobs. (2) Knob changes never reached an open editor — updateNSView now re-attributes the whole storage (selection preserved) when ProseTypography.flowKey moves.
129 lines
4.7 KiB
Swift
129 lines
4.7 KiB
Swift
//
|
|
// ProsePresenter.swift
|
|
// Museion
|
|
//
|
|
// #19 (S1.2): line-based markdown presentation for the editor — lifted from
|
|
// the proven spike (spike/textkit2, ADR-0010: ≤1.1 ms full-doc attribute load
|
|
// on PERF-3k). Presentation-only: the plain `isRichText = false` store stays
|
|
// the source of truth; attributes ride on top and re-apply per edited
|
|
// paragraph. The Fix-Card anchor search stays with M2 (#25) — not lifted.
|
|
//
|
|
|
|
import AppKit
|
|
|
|
/// Styled-source presentation (ADR-0010 mode B): variable heading sizes,
|
|
/// paragraph spacing, dimmed syntax. Pure functions over text; no AppKit view
|
|
/// state. Colors are the spike's proven values (foreground ≈ DesignTokens
|
|
/// foreground; the 0.55 syntax dim is deliberately lighter than muted).
|
|
nonisolated enum ProsePresenter {
|
|
enum LineKind { case heading1, heading2, quote, syntaxOnly, body }
|
|
|
|
static func lineKind(_ line: String) -> LineKind {
|
|
let trimmedLine = line.drop(while: { $0 == " " })
|
|
if trimmedLine.hasPrefix("## ") {
|
|
return .heading2
|
|
}
|
|
if trimmedLine.hasPrefix("# ") {
|
|
return .heading1
|
|
}
|
|
if trimmedLine.hasPrefix("> ") {
|
|
return .quote
|
|
}
|
|
if !trimmedLine.isEmpty, trimmedLine.allSatisfy({ $0 == "*" || $0 == " " || $0 == "-" || $0 == "_" }) {
|
|
return .syntaxOnly
|
|
}
|
|
return .body
|
|
}
|
|
|
|
static let inlineSyntax: Set<Character> = ["*", "_", "`", "[", "]", "(", ")", "!", "<", ">"]
|
|
|
|
/// Paragraph spacing per line kind, applied via NSParagraphStyle.
|
|
/// ponytail: quote rides body spacing — attributes(for:) styles quotes
|
|
/// as body anyway; dedicated quote spacing waits for #20's polish pass.
|
|
static func paragraph(_: LineKind) -> NSParagraphStyle {
|
|
let style = NSMutableParagraphStyle()
|
|
// Edit mode shows blank source lines — the blank line IS the
|
|
// separator. Only line height rides the flow knobs here; the
|
|
// paragraph/heading spacing knobs are reading-mode only.
|
|
style.lineHeightMultiple = ProseTypography.lineHeightMultiple
|
|
style.paragraphSpacing = 0
|
|
style.paragraphSpacingBefore = 0
|
|
return style
|
|
}
|
|
|
|
/// Attribute a whole text (initial load). Headings/body/quote styling + dimmed syntax.
|
|
static func attribute(_ text: String) -> NSAttributedString {
|
|
let out = NSMutableAttributedString(string: text, attributes: bodyAttributes)
|
|
let nsText = text as NSString
|
|
nsText.enumerateSubstrings(in: NSRange(location: 0, length: nsText.length), options: [.byLines]) { line, range, _, _ in
|
|
guard let line else { return }
|
|
let kind = lineKind(line)
|
|
out.setAttributes(Self.attributes(for: kind), range: range)
|
|
dimSyntax(in: out, range: range)
|
|
}
|
|
return out
|
|
}
|
|
|
|
/// Attribute one paragraph (editing-time re-apply). Cheap: one line, one pass.
|
|
static func reattribute(_ out: NSMutableAttributedString, paragraphRange: NSRange) {
|
|
let line = (out.string as NSString).substring(with: paragraphRange)
|
|
let kind = lineKind(line)
|
|
out.setAttributes(Self.attributes(for: kind), range: paragraphRange)
|
|
dimSyntax(in: out, range: paragraphRange)
|
|
}
|
|
|
|
// Typography: the #70 role slots (ProseTypography) — Body / Headings /
|
|
// Code — shared with reading mode; editing chrome (dimmed syntax) rides
|
|
// the Code slot.
|
|
static var bodyFont: NSFont {
|
|
ProseTypography.bodyFont
|
|
}
|
|
|
|
static var bodyAttributes: [NSAttributedString.Key: Any] {
|
|
ProseTypography.bodyAttributes
|
|
}
|
|
|
|
static func attributes(for kind: LineKind) -> [NSAttributedString.Key: Any] {
|
|
switch kind {
|
|
case .heading1:
|
|
[.font: ProseTypography.headingFont, .paragraphStyle: paragraph(.heading1)]
|
|
case .heading2:
|
|
[
|
|
.font: ProseTypography.headingFont(size: (ProseTypography.headingFont.pointSize * 0.77).rounded()),
|
|
.paragraphStyle: paragraph(.heading2),
|
|
]
|
|
case .quote, .syntaxOnly, .body: bodyAttributes
|
|
}
|
|
}
|
|
|
|
/// Dim markdown syntax characters within a line range (marker prefixes + inline marks).
|
|
static func dimSyntax(in out: NSMutableAttributedString, range: NSRange) {
|
|
let nsText = out.string as NSString
|
|
guard range.length > 0 else { return }
|
|
let line = nsText.substring(with: range)
|
|
let kind = lineKind(line)
|
|
// prefix markers
|
|
let markerLen = switch kind {
|
|
case .heading1: 2
|
|
case .heading2: 3
|
|
case .quote: 2
|
|
default: 0
|
|
}
|
|
let dim: [NSAttributedString.Key: Any] = [
|
|
.foregroundColor: ProseTypography.syntaxColor,
|
|
.font: ProseTypography.codeFont,
|
|
]
|
|
if markerLen > 0 {
|
|
out.addAttributes(dim, range: NSRange(location: range.location, length: min(markerLen, range.length)))
|
|
}
|
|
// inline marks in body/quote
|
|
if kind == .body || kind == .quote {
|
|
for (offset, codeUnit) in line.utf16.enumerated() {
|
|
if let scalar = Unicode.Scalar(codeUnit).map({ Character($0) }), inlineSyntax.contains(scalar) {
|
|
out.addAttributes(dim, range: NSRange(location: range.location + offset, length: 1))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|