// WHAT THE NEW NOTES SCREENS ASK OF THE CORE beyond writing (CONTRACT.md §1, // §3, §4, §6, §7, §9, §11): the side page, pins, the notes a `[[` may name and // the note a link opens, Find, the tag icons, export, and deleting for good // (asked first, in the core's words). Every answer is the core's; the phone // decides nothing here. #if os(iOS) import CottonKit import CottonWriting import Foundation /// A line of the side page (CONTRACT.md §1): a place, a tag list, a hand-made list, Archive, Bin, Settings, Account. struct NotesPlace: Decodable, Equatable, Identifiable, Sendable { var id: String var label: String var icon: String var count: Int var list: NotesListAsk /// A screen this line opens instead of a list (`memory`: the assistant's memory, CONTRACT.md §13). var opens: String? enum CodingKeys: String, CodingKey { case id, label, name, icon, count, list, opens } init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) id = try c.decode(String.self, forKey: .id) label = try c.decodeIfPresent(String.self, forKey: .label) ?? c.decodeIfPresent(String.self, forKey: .name) ?? "" icon = try c.decodeIfPresent(String.self, forKey: .icon) ?? "notes" count = try c.decodeIfPresent(Int.self, forKey: .count) ?? 0 list = try c.decodeIfPresent(NotesListAsk.self, forKey: .list) ?? NotesListAsk(view: "all") opens = try c.decodeIfPresent(String.self, forKey: .opens) } } /// A tag on the side page, with the tags under it. struct NotesSideTag: Decodable, Equatable, Identifiable, Sendable { var tag: String var name: String var count: Int var depth: Int var icon: String var iconChosen: Bool var pinned: Bool var children: [NotesSideTag] var id: String { tag } var list: NotesListAsk { NotesListAsk(view: "tag", tag: tag) } enum CodingKeys: String, CodingKey { case tag, name, count, depth, icon, pinned, children case iconChosen = "icon_chosen" } init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) tag = try c.decode(String.self, forKey: .tag) name = try c.decodeIfPresent(String.self, forKey: .name) ?? tag count = try c.decodeIfPresent(Int.self, forKey: .count) ?? 0 depth = try c.decodeIfPresent(Int.self, forKey: .depth) ?? 0 icon = try c.decodeIfPresent(String.self, forKey: .icon) ?? "hash" iconChosen = try c.decodeIfPresent(Bool.self, forKey: .iconChosen) ?? false pinned = try c.decodeIfPresent(Bool.self, forKey: .pinned) ?? false children = try c.decodeIfPresent([NotesSideTag].self, forKey: .children) ?? [] } /// This tag and every tag under it. var flattened: [NotesSideTag] { [self] + children.flatMap(\.flattened) } } /// The whole side page (`notes.side`). struct NotesSide: Decodable, Equatable, Sendable { struct Group: Decodable, Equatable, Sendable { var label: String var items: [Item] enum CodingKeys: String, CodingKey { case label, items, tree } init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) label = try c.decodeIfPresent(String.self, forKey: .label) ?? "" items = try c.decodeIfPresent([Item].self, forKey: .items) ?? c.decodeIfPresent([Item].self, forKey: .tree) ?? [] } } var places: [NotesPlace] var tags: Group var lists: Group var foot: [NotesPlace] var more: [NotesPlace] var words: [String: String] /// Your assistant: From your assistant and Memory, between Lists and the foot (CONTRACT.md §13). var assistant: Group? /// The icon the side page draws for `tag` (its chosen one, else the catalogue's), `hash` when it has none. func icon(of tag: String) -> String { tags.items.flatMap(\.flattened).first { $0.tag.caseInsensitiveCompare(tag) == .orderedSame }?.icon ?? "hash" } } /// What a pin answered (`notes.pin`, `notes.unpin`). struct NotesPinned: Decodable, Equatable, Sendable { var said: String? } /// The icons a tag may wear (`notes.tag_icons`). struct NotesTagIcons: Decodable, Equatable, Sendable { struct Icon: Decodable, Equatable, Sendable, Identifiable { var id: String var label: String } var icons: [Icon] var title: String } /// The note a link opens (`notes.link`): its id and where its heading starts; or the offer to make it. struct NotesLinked: Decodable, Equatable, Sendable { var id: String? var headingAt: Int? var missing: Bool? var offer: String? var title: String? enum CodingKeys: String, CodingKey { case id, missing, offer, title case headingAt = "heading_at" } } /// A file made for the share sheet (`notes.export`). struct NotesExported: Decodable, Equatable, Sendable { var path: String var fileName: String var mime: String enum CodingKeys: String, CodingKey { case path, mime case fileName = "file_name" } } /// The ask the core answers before deleting for good (`notes.remove`, `notes.empty_bin` without `confirmed`). struct NotesAsk: Decodable, Equatable, Sendable { var title: String var message: String var act: String var cancel: String } /// A delete for good, asked or done: the ask to show, or what was said once done. struct NotesRemoved: Decodable, Equatable, Sendable { var ask: NotesAsk? var said: String? } extension NotesBook { private struct Zone: Encodable { let zone: String } private struct Pin: Encodable { let id: String; let tag: String? } private struct PinTag: Encodable { let tag: String; let pinned: Bool } private struct TagIcon: Encodable { let tag: String; let icon: String? } private struct LinksAsk: Encodable { let id: String?; let text: String; let cursor: Int } private struct LinkAsk: Encodable { let target: String; let make: Bool } private struct FindAsk: Encodable { let text: String; let words: String } private struct ExportAsk: Encodable { let ids: [String]?; let view: String?; let format: String; let zone: String } private struct Untag: Encodable { let id: String; let tag: String } private struct Remove: Encodable { let id: String; let confirmed: Bool } private struct Confirmed: Encodable { let confirmed: Bool } private struct Leave: Encodable { let id: String; let title: String? } private struct NoArgs: Encodable {} private struct Links: Decodable { struct Range: Decodable { var start: Int; var end: Int } struct Note: Decodable { var id: String; var title: String; var with: String; var cursor: Int } var open: Bool var replace: Range? var notes: [Note]? var none: String? } private struct Found: Decodable { struct Match: Decodable { var start: Int; var end: Int } var matches: [Match] } func side() async throws -> NotesSide { try await core.call("notes.side", Zone(zone: TimeZone.current.identifier), as: NotesSide.self) } /// The assistant's memory, read-only (`notes.memory`, CONTRACT.md §13). func memory() async throws -> NotesMemory { try await core.call("notes.memory", Zone(zone: TimeZone.current.identifier), as: NotesMemory.self) } /// Pin or unpin a note: in Notes (top of every list), or inside `tag` only. func pin(_ id: String, tag: String? = nil, _ on: Bool) async throws -> NotesPinned { try await core.call(on ? "notes.pin" : "notes.unpin", Pin(id: id, tag: tag), as: NotesPinned.self) } /// A root tag pinned to the top of the side page's tags, or unpinned. func pinTag(_ tag: String, _ on: Bool) async throws -> NotesSide { try await core.call("notes.pin_tag", PinTag(tag: tag, pinned: on), as: NotesSide.self) } func tagIcons() async throws -> NotesTagIcons { try await core.call("notes.tag_icons", NoArgs(), as: NotesTagIcons.self) } /// A tag's icon chosen (nil: back to the catalogue's). func tagIcon(_ tag: String, _ icon: String?) async throws -> NotesSide { try await core.call("notes.tag_icon", TagIcon(tag: tag, icon: icon), as: NotesSide.self) } /// The notes a `[[` being typed may name; nil when no `[[` is open at the cursor. func links(_ id: String?, text: String, cursor: Int) async throws -> WritingLinkOffers? { let answer = try await core.call("notes.links", LinksAsk(id: id, text: text, cursor: cursor), as: Links.self) guard answer.open, let replace = answer.replace else { return nil } return WritingLinkOffers(replace: NSRange(location: replace.start, length: max(0, replace.end - replace.start)), offers: (answer.notes ?? []).map { .init(id: $0.id, title: $0.title, with: $0.with, cursor: $0.cursor) }, none: answer.none) } /// The note `target` opens; with `make`, the note made when none has that title. func link(_ target: String, make: Bool = false) async throws -> NotesLinked { try await core.call("notes.link", LinkAsk(target: target, make: make), as: NotesLinked.self) } /// Where `words` stand in `text`. func find(_ text: String, _ words: String) async throws -> [NSRange] { try await core.call("notes.find", FindAsk(text: text, words: words), as: Found.self).matches .map { NSRange(location: $0.start, length: max(0, $0.end - $0.start)) } } /// One or more notes as a file in `format` (`markdown`, `textbundle`, `html`, `pdf`); `view` exports a whole list. func export(ids: [String]? = nil, view: String? = nil, format: String) async throws -> NotesExported { try await core.call("notes.export", ExportAsk(ids: ids, view: view, format: format, zone: TimeZone.current.identifier), as: NotesExported.self) } /// Delete a note for good: unconfirmed, the core answers its ask and deletes nothing. func remove(_ id: String, confirmed: Bool) async throws -> NotesRemoved { try await core.call("notes.remove", Remove(id: id, confirmed: confirmed), as: NotesRemoved.self) } /// Empty the bin: unconfirmed, the core answers its ask (nothing to ask when it is empty). func emptyBin(confirmed: Bool) async throws -> NotesRemoved { try await core.call("notes.empty_bin", Confirmed(confirmed: confirmed), as: NotesRemoved.self) } /// The writer closed, with the title the note opened with: a note with no words is not kept, and a title /// changed since rewrites the links that opened it. /// A tag taken off a note: its hashtag out of the words, the note filed again (`notes.untag`). func untag(_ id: String, _ tag: String) async throws -> NoteText { try await core.call("notes.untag", Untag(id: id, tag: tag), as: NoteText.self) } func leave(_ id: String, title: String?) async throws { _ = try await core.call("notes.leave", Leave(id: id, title: title), as: NotesPinned.self) } /// The export formats, in the order Notes offers them, each in the core's words. static let exportFormats = ["markdown", "textbundle", "html", "pdf"] } /// The assistant's memory, read-only (`notes.memory`, CONTRACT.md §13). struct NotesMemory: Decodable, Equatable, Sendable { struct Entry: Decodable, Equatable, Sendable, Identifiable { var id: String var slug: String? var sentence: String var pinned: Bool? var changed: String? } struct Group: Decodable, Equatable, Sendable, Identifiable { var id: String var label: String var count: Int var entries: [Entry] } var title: String var about: String var count: Int var empty: String? var groups: [Group] } #endif