Cotton code
59 lines · 2.5 KB · SwiftRaw
// The editing row (Spec 7, architect 5 Oct): while a text field on a record's// page is being edited the input box is put away, and in its place, just// above the keyboard, stands one slim row — the mic on the left, Done on the// right. The mic dictates INTO THE FIELD, at the cursor, with the box's own// engine (`DictationModel` spliced by `BodyDictation`) and the box's own// recording row: ✕, the clock and the meter, stop.//// The field says it is being edited (`FieldEditing`, in the environment of a// page that hosts the row) and hands over the three things the row needs: its// words, where its cursor stands, and how to leave it. The row never writes a// record — the field hands its words to the page when it is left, as it// always has.import CottonKitimport CottonValuesimport Observationimport SwiftUI/// The text field being edited on this page, if one is.@MainActor@Observablefinal class FieldEditing {    struct Field {        let id: String        let body: @MainActor () -> String        let setBody: @MainActor (String) -> Void        /// The selection as UTF-16 offsets, or nil for the end of the words.        let selection: @MainActor () -> (start: Int, end: Int)?        /// Leaves the field, which hands its words on.        let done: @MainActor () -> Void        /// Whether the mic is offered: never into a secret.        let speaks: Bool        /// The column the field types, and what leaving it now would hand        /// on (nil: nothing changed) — for an act that must read the words        /// as typed before the field is left (`typed()`).        var column: String? = nil        var handing: @MainActor () -> RecordValue? = { nil }    }    private(set) var field: Field?    func began(_ field: Field) { self.field = field }    /// The field still being typed in, as leaving it now would hand it on:    /// Keep on a form reads every value as typed, and the field with the    /// cursor in it has handed nothing on yet (walk, 16 Oct 2026: a note's    /// body kept with the cursor still in it was refused "a note needs a    /// title or a body", and a title typed after the body was dropped). Nil    /// when no field is open or nothing in it changed, so an untouched    /// field never blanks what is held.    func typed() -> (column: String, value: RecordValue)? {        guard let field, let column = field.column, let value = field.handing() else { return nil }        return (column, value)    }    func ended(_ id: String) {        if field?.id == id { field = nil }    }}