// 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) } } }