spike(textkit2): Spike B — NSTextView/TextKit2 custom-attribute prose editor (#53)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
- ProsePresenter: line-kind markdown→attributes (serif body 17pt, H1/H2/H3 26/21/18pt bold, dimmed mono syntax, paragraph spacing), whitespace-tolerant quote-anchor search - EditorPane: NSViewRepresentable NSTextView, per-paragraph reattribute on edit, undo, non-contiguous layout, 640pt measure - SpikeApp: Reading toggle (inline md render), anchor/stale-anchor demo, exact web-proto alert wording, --check self-test - run.sh wraps SPM binary in .app (bare executables show no windows) - assets/: verification screenshots Wayfinder map #16 — throwaway spike, not for main.
This commit is contained in:
1 parent
3b3544e84d
commit
dd3542a720
12 files changed
+372
No files matched your search
@@ -0,0 +1,85 @@
|
||||
// Spike B (#53) — THROWAWAY. AppKit text editor with TextKit2 custom attributes.
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
// MARK: - TextKit2-backed editor (NSTextView with custom attribute re-apply on edit)
|
||||
|
||||
/// NSViewRepresentable wrapper around a scrollable NSTextView, restyling the edited
|
||||
/// paragraph after each edit burst (textDidChange), preserving undo/IME natively.
|
||||
struct EditorPane: NSViewRepresentable {
|
||||
let attributed: NSAttributedString
|
||||
var onEdit: ((String) -> Void)?
|
||||
/// When set, highlight this range (Fix-Card anchor).
|
||||
var anchorHighlight: NSRange?
|
||||
|
||||
func makeNSView(context: Context) -> NSScrollView {
|
||||
let tv = NSTextView()
|
||||
tv.isRichText = false // plain source of truth; attributes are presentation-only
|
||||
tv.allowsUndo = true
|
||||
tv.isAutomaticQuoteSubstitutionEnabled = false
|
||||
tv.isAutomaticDashSubstitutionEnabled = false
|
||||
tv.isAutomaticTextReplacementEnabled = false
|
||||
tv.isAutomaticSpellingCorrectionEnabled = false
|
||||
tv.usesFindBar = true
|
||||
tv.backgroundColor = NSColor(white: 0.98, alpha: 1)
|
||||
tv.typingAttributes = ProsePresenter.bodyAttributes
|
||||
tv.delegate = context.coordinator
|
||||
tv.layoutManager?.allowsNonContiguousLayout = true
|
||||
let scroll = NSScrollView()
|
||||
scroll.documentView = tv
|
||||
scroll.hasVerticalScroller = true
|
||||
scroll.autohidesScrollers = true
|
||||
scroll.drawsBackground = true
|
||||
scroll.backgroundColor = NSColor(white: 0.98, alpha: 1)
|
||||
// centered measure: text container inset
|
||||
let containerWidth: CGFloat = 640
|
||||
tv.minSize = NSSize(width: 0, height: 0)
|
||||
tv.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
|
||||
tv.isVerticallyResizable = true
|
||||
tv.isHorizontallyResizable = false
|
||||
tv.autoresizingMask = [.width]
|
||||
tv.textContainer?.containerSize = NSSize(width: containerWidth, height: .greatestFiniteMagnitude)
|
||||
tv.textContainer?.widthTracksTextView = false
|
||||
tv.textContainer?.lineFragmentPadding = 12
|
||||
context.coordinator.textView = tv
|
||||
return scroll
|
||||
}
|
||||
|
||||
func updateNSView(_ scroll: NSScrollView, context: Context) {
|
||||
guard let tv = scroll.documentView as? NSTextView else { return }
|
||||
if context.coordinator.loadedHash != attributed.hash {
|
||||
context.coordinator.loadedHash = attributed.hash
|
||||
tv.textStorage?.setAttributedString(attributed)
|
||||
}
|
||||
applyAnchor(to: tv)
|
||||
}
|
||||
|
||||
private func applyAnchor(to tv: NSTextView) {
|
||||
let storage = tv.textStorage!
|
||||
storage.removeAttribute(.backgroundColor, range: NSRange(location: 0, length: storage.length))
|
||||
guard let anchorHighlight else { return }
|
||||
storage.addAttribute(.backgroundColor, value: NSColor(calibratedRed: 0.79, green: 0.39, blue: 0.26, alpha: 0.18), range: anchorHighlight)
|
||||
tv.scrollRangeToVisible(anchorHighlight)
|
||||
}
|
||||
|
||||
func makeCoordinator() -> Coordinator { Coordinator(onEdit: onEdit) }
|
||||
|
||||
final class Coordinator: NSObject, NSTextViewDelegate {
|
||||
let onEdit: ((String) -> Void)?
|
||||
weak var textView: NSTextView?
|
||||
var loadedHash: Int = 0
|
||||
|
||||
init(onEdit: ((String) -> Void)?) { self.onEdit = onEdit }
|
||||
|
||||
func textDidChange(_ notification: Notification) {
|
||||
guard let tv = notification.object as? NSTextView, let storage = tv.textStorage else { return }
|
||||
let sel = tv.selectedRange()
|
||||
// Re-attribute the paragraph(s) touching the selection — one paragraph, one pass.
|
||||
let paragraphRange = (storage.string as NSString).paragraphRange(for: sel)
|
||||
ProsePresenter.reattribute(storage as NSMutableAttributedString, paragraphRange: paragraphRange)
|
||||
// re-dim inline syntax across the edited line (already done in reattribute)
|
||||
tv.typingAttributes = ProsePresenter.bodyAttributes
|
||||
onEdit?(storage.string)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user