Cotton code
54 lines · 2.3 KB · SwiftRaw
// The tray's answer as the value a command's pick sends. It reads the command// alone — never a table's id and never whether the tool shipped with the app.import CottonKitimport CottonValuesimport Foundationenum RecordCardPage {    /// The tray's answer as the value `command`'s pick sends: a moment as an    /// instant (a day alone is that day's start), a record, a database or a    /// table as its id, words as they were typed.    static func picked(_ value: FieldValue, for command: RecordCommand, calendar: Calendar = .current) -> String? {        guard let pick = command.pick else {            if command.needsPick, case .link(let id) = value { return id }            return nil        }        switch (pick.kind, value) {        case (.number, _):            let number: Double?            switch value {            case .number(let value): number = value            case .text(let words): number = NumberValue.parse(words)            default: number = nil            }            guard let number, number.isFinite,                  pick.min.map({ number >= $0 }) ?? true,                  pick.max.map({ number <= $0 }) ?? true else { return nil }            return String(number)        case (.datetime, .when(let when)):            let at = WhenValue(day: when.day, time: when.time ?? ClockTime(hour: 0, minute: 0))            return at.stored(calendar: calendar).text        case (.record, .link(let id)), (.database, .link(let id)):            return id        case (.text, .text(let words)):            let words = words.trimmingCharacters(in: .whitespacesAndNewlines)            return words.isEmpty ? nil : words        default:            return nil        }    }}extension ColumnDefinition {    /// The column's `placeholder` reads as its value when it holds nothing —    /// a default the person did not choose ("Once", "No subject", "Cotton's    /// default") — on a column the person does not type: read-only,    /// computed, a choice or a size. On a column they type it is a hint    /// sentence ("Add a summary — …", "What needs doing?"), which belongs to    /// the editor, never the page; on a link it is the add card's (r6).    var placeholderIsValue: Bool {        guard placeholder != nil, type != .link else { return false }        return readonly || computed != nil || type == .choice || type == .bytes    }}