// Keys a record that is handed on reads, each drawn once for any table that// names it (Thoughts was the first: a capture that became one todo opens as// that todo, a filed capture names under its slip what it became, a capture's// pictures are added and taken off on its page)://// * `hand_over` — a record whose `links` hold exactly ONE record opens as// that record: its page replaces itself with that record's page, while// the record holds what `when` names;// * slip `links` — the record's `links` as hand links under its slip// ("Todo ›"), each opening its record;// * `attachments` — the strip under the page is editable: a picture added// from where `sources` says, moved, captioned and taken off, the whole// list written as the record's `attachments` through `update_record`.//// The reading lives here, pure, so what opens is settled by tests; the faces and surfaces only draw them.importCottonKitimportCottonValuesimportFoundation/// A record that opens as the one record it became (`hand_over`).publictypealiasRecordHandOver = DefinitionRecordHandOverextensionDefinitionRecordHandOver {publicinit(when: SectionWhen? = nil) {self.init(_generated: ())self.when = when }}/// Where a picture added to a record's strip comes from.publictypealiasAttachmentSourceKey = DefinitionAttachmentSource/// A record's strip, editable (`attachments`).publictypealiasAttachmentsLayout = DefinitionAttachmentsLayoutextensionDefinitionAttachmentsLayout {publicinit(editable: Bool = true, sources: [AttachmentSourceKey] = [.library, .camera]) {self.init(_generated: ())self.editable = editableself.sources = sources }}extensionTableDefinition {/// The hand links under a slip: the record's links less the tables the/// slip leaves out, none without slip `links`.publicfunc slipLinks(of record: TableRecord) -> [LinkedRecord] {guardlet links = slip.links else { return [] }return linkedRecords(of: record, in: links) }/// The lines under a slip from another table (`slip.lines`): the core's/// words, one string each, never composed here; none without the key.publicfunc slipRowLines(of record: TableRecord) -> [String] {guard slip.lines != nil, case .list(let lines) = record["@slip_lines"] else { return [] }return lines.compactMap(\.text).filter { !$0.isEmpty } }/// `update_record`'s patch for a strip: the whole list, in order, as the/// record's `attachments` (each `{kind, id}`, and its caption).staticfunc attachmentsPatch(_ list: [RecordAttachment]) -> [String: RecordValue] { attachmentsPatch(RecordAttachments.argument(list)) }/// The same patch from the argument as written (a caption's list).staticfunc attachmentsPatch(_ argument: JSONValue) -> [String: RecordValue] { ["attachments": RecordValue(argument)] }}extensionRecordValue {/// A JSON value as a record holds it.init(_ json: JSONValue) {switch json {case .null: self = .nullcase .bool(let b): self = .bool(b)case .number(let n): self = .number(n)case .string(let s): self = .text(s)case .array(let items): self = .list(items.map(RecordValue.init))case .object(let fields): self = .object(fields.mapValues(RecordValue.init)) } }}extensionLinkedRecord {/// A hand link's words: the kind's word, then the arrow ("Todo ›").publicvar handLink: String { "\(BadgeKind.label(forKind: table)) \u{203A}" }}