// What a note carries besides its words: pictures, films and files, under the // sheet. // // **The list is the authority, not the body.** `get_record` answers a note's // `attachments` — `{kind, id, position, caption, recordCaption, title, // contentType|mime, width, height, key, missing, bytes}` — and everything on // this screen is decided from it. A `![…](cotton:/)` reference in the // body draws the picture only when that pair is in the list; one that is not draws its caption // as text, and nothing is fetched for it. A body is text a person types, and a // typed reference must never be able to send the app looking for bytes. // // **A RENDERER ASKS `read.mediaPath`.** Every tile here goes through // `MediaImages.load` (via `KeyedPicture`), which asks where the bytes are on // THIS device and draws the empty window when they are not. Nothing in this // file may fetch: `MediaFetchSweepTests` holds the rule by name, and the one // screen allowed to fetch is the one viewer (`MediaViewer`), which a tap on a tile // opens — it takes the note's WHOLE list and the place the tap landed, so a // swipe walks the rest of them. // // **A removed picture leaves its row.** `remove_photo` deletes a photo without // touching the notes that point at it, so `get_record` answers `missing: true` // and the note draws a tile saying the picture is gone. The alternative — a // silent hole in the page — is a note quietly rewriting itself. // // ## Adding one // // Three ways in: the photo library (`PhotosPicker`, which runs out of process // so this app never asks for the library), the camera, and a file. All three // land the same way — `capture.media` copies the bytes into the store and // answers `{key, recordId}`, then `update_note { id, attachments: … }` files // that record onto the note. The upload is the sync loop's; no screen waits on // it. // // ## Captioning one, and moving one // // Both are edit-mode verbs on a STRIP tile, and both go out as the whole list // through `update_note` — the list REPLACES, so an order is only ever // expressible whole and a caption has to travel with every other entry. // // **AN ENTRY WITH NO `caption` KEY KEEPS ITS STORED CAPTION.** That absence is // what lets a move rewrite the whole list without touching a word anybody // wrote: `argument(_:)` sends `{kind, id}` alone, and `captioned(_:_:text:)` // adds the key to exactly ONE entry — a string sets it, `null` takes it off. // The read hands back two fields and they are not the same sentence: `caption` // is the note's own override, `recordCaption` the picture's own, and sending // the second back as the first would quietly make a photograph's caption this // note's. // // **A tile the BODY names is captioned in the body.** `![the wall](cotton:…)` // already says what that picture is THERE, and the reader prefers it // (`captioned(_:)`), so the strip's menu is the only place a note's own // override is written — there is no second box saying the same thing twice. // // **The camera is a CAPABILITY, never a platform branch.** A machine with no // camera surface gets `CapabilityRefusal`'s own sentence, which is the same // answer a phone whose camera was denied gets. `ScreenPlatformSweepTests` // holds the rule that no screen says `#if os(macOS)`. import CottonKit import PhotosUI import SwiftUI import UniformTypeIdentifiers #if canImport(UIKit) import UIKit #endif // MARK: - One attachment, and what this lane asks of it // `RecordAttachment` itself is `RecordAttachment.swift`'s — the viewer and the // strip read the same rows and a second struct over one JSON shape is the pair // that eventually disagrees. What is here is what the STRIP and the READER ask // of one that the pager does not. extension RecordAttachment { /// The pair a body's `cotton:` reference names. var ref: MarkdownImageRef { MarkdownImageRef(kind: kind, id: id) } /// Whether this one draws as a picture at all: its table draws its records /// as prints (`polaroid`). A film draws its poster; a file draws a titled /// card. func isPicture(in definitions: TableDefinitions) -> Bool { definitions[kind]?.polaroid != nil } /// The glyph its table badges a slip with (`slip.badge.symbol`) — a film's /// play mark — written in the print's margin; nil for a table with none. func glyph(in definitions: TableDefinitions) -> String? { definitions[kind]?.slip.badge?.symbol } } // MARK: - The rules, apart from the drawing enum RecordAttachments { /// `update_note`'s `attachments` argument. /// /// **`{kind, id}` and nothing else — an entry with NO `caption` key leaves /// the stored caption alone.** That absence is the whole reason this list /// can be rewritten by a strip that only reordered or removed a tile: a /// caption is written where it is typed and nowhere else. The read says /// which caption is whose — `caption` is the note's own override, and /// `recordCaption` the record's own — and sending either back would promote /// a photograph's own words into an override on this note. static func argument(_ list: [RecordAttachment]) -> JSONValue { .array( list.map { .object(["kind": .string($0.kind), "id": .string($0.id)]) } ) } /// What a tile writes under itself, and the ONE rule for it. /// /// A record that is GONE says so first — a caption over a hole would be the /// note claiming a picture it no longer has. Otherwise the note's own words /// about it, else the record's own caption, else what the record calls /// itself, else what it is, plainly: a print its table's name, a file its /// type's word. Both caption fields are optional: a picture nobody /// captioned carries neither, and the title carries the tile. static func caption(of attachment: RecordAttachment, in definitions: TableDefinitions) -> String { if attachment.missing { return ShellLines.line("faces", "attachments.removed") } if let caption = attachment.caption { return caption } if let recordCaption = attachment.recordCaption { return recordCaption } if let title = attachment.title { return title } if attachment.isPicture(in: definitions), let name = definitions[attachment.kind]?.name { return name } return fileWord(attachment) } /// A file's type as a word: the core's ("Web page", "PDF"), said by set /// `documents` from its type; "File" where the read said none. static func fileWord(_ attachment: RecordAttachment) -> String { attachment.format ?? ShellLines.line("documents", "format.file") } /// Which kind `capture.media` is asked for, from what the file says it is. static func captureKind(_ type: UTType?) -> CaptureMediaKind { guard let type else { return .document } if type.conforms(to: .movie) { return .video } if type.conforms(to: .image) { return .photo } return .document } } // MARK: - Adding one /// Where a new attachment comes from. enum AttachmentChoice: String, CaseIterable, Identifiable { case library case camera case file var id: String { rawValue } var label: String { switch self { case .library: return ShellLines.line("faces", "attachments.photo_library") case .camera: return ShellLines.line("faces", "attachments.take_photo") case .file: return ShellLines.line("faces", "attachments.choose_file") } } var symbol: String { switch self { case .library: return "photo.on.rectangle" case .camera: return "camera" case .file: return "doc" } } }