import AppKit import EquipoKit /// The text belongs to the card, so replacing a row never discards an edit. @MainActor final class QuestionAnswerField: NSTextField, NSTextFieldDelegate { private let draft: QuestionAnswerDraft private let onSubmit: (String) -> Void init(draft: QuestionAnswerDraft, onSubmit: @escaping (String) -> Void) { self.draft = draft; self.onSubmit = onSubmit super.init(frame: .zero) stringValue = draft.typedChoice delegate = self } required init?(coder: NSCoder) { fatalError() } func controlTextDidChange(_ notification: Notification) { draft.typedChoice = stringValue } func control(_ control: NSControl, textView: NSTextView, doCommandBy selector: Selector) -> Bool { guard selector == #selector(NSResponder.insertNewline(_:)) else { return false } submit() return true } func submit() { guard isEnabled else { return } let text = stringValue.trimmingCharacters(in: .whitespacesAndNewlines) guard !text.isEmpty else { return } draft.typedChoice = stringValue onSubmit(text) } }