feat(editor): land editor core — open, edit, autosave (S1.2)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
Posts open into the TextKit2 surface (ADR-0010 mode B) lifted from the proven spike: styled source, scaled headings, dimmed syntax, native undo/IME. One EditorSession per open post drives a ~1 s debounced autosave (flush on selection change, window close, app quit; ⌘S immediate), the Saved/Saving/Failed dot, and the per-post .museion-state.json cursor sidecar that dies with the folder. External edits raise an explicit Reload/Keep-Local alert — the debounce never answers it for the writer, and drafts keep the lazy-folder first-⌘S rule (#62). Reviewer fanout fixes folded in: session teardown at every lifecycle exit (rename flushes before the folder move; trash cancels the debounce), draft ⌘S continues in place, conflict holds the debounce, rescan's disk read off-main, ~35 lines of dead code out. Closes #19
This commit is contained in:
1 parent
530f852f75
commit
f2953ee07e
13 files changed
+893
-87
No files matched your search
@@ -0,0 +1,127 @@
|
||||
//
|
||||
// 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(_ kind: LineKind) -> NSParagraphStyle {
|
||||
let style = NSMutableParagraphStyle()
|
||||
switch kind {
|
||||
case .heading1: style.paragraphSpacing = 18; style.paragraphSpacingBefore = 26
|
||||
case .heading2: style.paragraphSpacing = 12; style.paragraphSpacingBefore = 20
|
||||
case .quote, .syntaxOnly, .body: style.paragraphSpacing = 10
|
||||
}
|
||||
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: Iowan Old Style prose stance (the export's stack), 17pt body,
|
||||
// scaled headings.
|
||||
static var bodyFont: NSFont {
|
||||
NSFontManager.shared.font(withFamily: "Iowan Old Style", traits: [], weight: 5, size: 17)
|
||||
?? NSFont.systemFont(ofSize: 17)
|
||||
}
|
||||
|
||||
static var bodyAttributes: [NSAttributedString.Key: Any] {
|
||||
[
|
||||
.font: bodyFont,
|
||||
.paragraphStyle: paragraph(.body),
|
||||
.foregroundColor: NSColor(red: 0.10, green: 0.10, blue: 0.09, alpha: 1),
|
||||
]
|
||||
}
|
||||
|
||||
static func attributes(for kind: LineKind) -> [NSAttributedString.Key: Any] {
|
||||
func headingFont(size: CGFloat, weight: Int) -> NSFont {
|
||||
NSFontManager.shared.font(withFamily: "Iowan Old Style", traits: [.boldFontMask], weight: weight, size: size)
|
||||
?? .boldSystemFont(ofSize: size)
|
||||
}
|
||||
switch kind {
|
||||
case .heading1: return [.font: headingFont(size: 26, weight: 9), .paragraphStyle: paragraph(.heading1)]
|
||||
case .heading2: return [.font: headingFont(size: 20, weight: 8), .paragraphStyle: paragraph(.heading2)]
|
||||
case .quote, .syntaxOnly, .body: return 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: NSColor(white: 0.55, alpha: 1), .font: NSFont.monospacedSystemFont(ofSize: 13, weight: .regular)]
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user