// Send (wireframes 6 and 7, from Read's ↗ and from About): the script handed// on as a file, through the system's share sheet. Three ways, each a word and// a quiet line saying what it is for: a PDF laid out by the core's pages// (`StoryPDF`), a Final Draft file (`story.fdx.write`) and a Fountain file// (`story.fountain.write`). A marked draft sends its own pages and Fountain// (`story.drafts.read`); the core writes no Final Draft file of a draft, so a// draft offers the other two.#if os(iOS)importCottonKitimportSwiftUIstructInkSendSheet: View {let story: InkStorylet script: String/// The script's title, as the sheet's title says it.let title: String/// A marked draft to send instead of the script as it stands.var draft: InkReadScreen.Draft? = nil @Environment(\.closeCard) privatevar closeCardvar body: someView {InkActsSheet(title: InkWords.line("ink.send_title", ["title": title]), acts: acts) }privatevar acts: [InkAct] {var acts = [InkAct(words: InkWords.line("ink.send_pdf_file"), detail: InkWords.line("ink.send_pdf_line")) { run { tryawaitInkSending.pdf(story: story, script: script, draft: draft) } }, ]if draft == nil { acts.append(InkAct(words: InkWords.line("ink.send_fdx"), detail: InkWords.line("ink.send_fdx_line")) { run {let file = tryawait story.shelf.fdx(story: script)tryInkSending.share(file.text, named: file.name) } }) } acts.append(InkAct(words: InkWords.line("ink.send_fountain_file"), detail: InkWords.line("ink.send_fountain_line")) { run {iflet draft {let read = tryawait story.shelf.readDraft(draft.id)tryInkSending.share(read.fountainText, named: read.fountainName) } else {let file = tryawait story.shelf.fountain(story: script)tryInkSending.share(file.text, named: file.name) } } })return acts }privatefunc run(_ work: @escaping () asyncthrows -> Void) { closeCard?()Task { await story.attempt { tryawait work() } } }}/// The files Send hands to the share sheet.@MainActorenumInkSending {/// The script's pages, or a draft's, as the PDF the core's layout draws.staticfunc pdf(story: InkStory, script: String, draft: InkReadScreen.Draft?) asyncthrows {let working = tryawait story.book.draft(story: script)guardlet draft else {iflet url = StoryPDF.write(working.script, named: working.title) { ShareSheet.present(url) }return }let read = tryawait story.shelf.readDraft(draft.id)let pages = StoryScript(paper: working.script.paper, titlePage: nil, pages: read.pages, scenes: [])let title = [working.title, draft.name].filter { !$0.isEmpty }.joined(separator: " - ")iflet url = StoryPDF.write(pages, named: title) { ShareSheet.present(url) } }/// A file's words written under Story's own folder, then shared.staticfunc share(_ text: String, named name: String) throws {let url = StoryFiles.folder().appendingPathComponent(StoryFiles.safe(name))tryData(text.utf8).write(to: url, options: .atomic)ShareSheet.present(url) }}#endif