Cotton code
98 lines · 4.1 KB · SwiftRaw
// A LONG TEXT, WHOLE SCREEN (door row 8). A longtext column is too long// for one line, so a tap on its words opens them on the whole screen, on the// same paper in the same typewriter, the caret at the end: a note's body, a// todo's notes, any longtext a card shows. Done writes them and returns to// the same card at the same page; words left as they were write nothing.// Every other text column is written in the short-text input// (`DeckLineInput`).//// The presenter is the shell's writing layer (`DeckWritingLayer`), over the// whole stack: the deck's editing presents no sheet of its own// (`DeckNoSheetsTests`).import CottonKitimport CottonPaperimport CottonVoiceimport SwiftUIstruct DeckLongTextEditor: View {    /// The record's title, over its words.    let title: String?    /// The words as the card showed them.    let words: String    /// Where a dictation here is: the table, column and record written.    var voice: VoicePlanRequest = VoicePlanRequest(surface: "note", mode: .dictate)    /// The words to write, nil when nothing changed.    let done: (String?) -> Void    @Environment(\.theme) private var theme    @Environment(\.cottonCore) private var core    @State private var draft = ""    @State private var selection: TextSelection?    @State private var dictation: VoiceField?    @FocusState private var focused: Bool    var body: some View {        VStack(alignment: .leading, spacing: 0) {            HStack {                if let title {                    Text(title)                        .font(CottonFonts.serif(NoteFaceKnobs.titleSize))                        .foregroundStyle(theme.print(.ink))                        .lineLimit(1)                        .accessibilityAddTraits(.isHeader)                }                Spacer()                NavCircle(symbol: "checkmark", label: ShellLines.line("lists", "done")) { finish() }                    .accessibilityIdentifier("deck-note-editor-done")            }            .padding(.horizontal, DeckKnobs.lineSide)            .padding(.top, 12)            .padding(.bottom, 8)            TextEditor(text: $draft, selection: $selection)                .font(CottonFonts.type(NoteFaceKnobs.bodySize + 2))                .lineSpacing(NoteFaceKnobs.bodyLeading)                .foregroundStyle(theme.print(.ink))                .tint(theme.print(.red))                .scrollContentBackground(.hidden)                .focused($focused)                .padding(.horizontal, DeckKnobs.lineSide - 5)                .accessibilityIdentifier("deck-note-editor")                .onChange(of: draft) { _, now in dictation?.fieldChanged(now) }                .onChange(of: selection) { _, now in                    if let range = draft.utf16Range(now) { dictation?.selectionChanged(range) }                }            // The mic, where every writer has it: the trailing end of the bar            // on the keyboard.            if let dictation {                VoiceFieldBar(                    field: dictation, identifier: "deck-note-editor-mic", request: { voice },                    text: { draft }, selection: { draft.utf16Range(selection) }, write: { draft = $0 })                    .padding(.horizontal, DeckKnobs.lineSide - 12)                    .padding(.vertical, 4)                    .background(theme.colors.ground.color)            }        }        .background(theme.colors.ground.color.ignoresSafeArea())        .onAppear {            draft = words            focused = true            if dictation == nil, let core { dictation = VoiceField(session: VoiceSessions.make(core: core), inlineTentative: false) }        }        .onDisappear { dictation?.session.cancel() }    }    private func finish() {        // Done while dictating keeps what was heard.        if let dictation, dictation.isActive {            Task {                draft = await dictation.stop()                finish()            }            return        }        focused = false        let text = draft.trimmingCharacters(in: .whitespacesAndNewlines)        done(text == words.trimmingCharacters(in: .whitespacesAndNewlines) ? nil : text)    }}