Sources/OrgstarMobile/ExportMenu.swift
129 lines · 5926 bytes
1#if os(iOS)
2import CoreTransferable
3import OrgApp
4import OrgCore
5import OrgPresentation
6import SwiftUI
7import UniformTypeIdentifiers
8
9/// A file exported as HTML, Markdown, PDF, RTF, plain text or iCalendar, written when the share
10/// sheet takes it.
11struct OrgExport: Transferable {
12 enum Format: CaseIterable {
13 case html, markdown, pdf, rtf, plainText, icalendar
14
15 var name: String {
16 switch self {
17 case .html: "HTML"
18 case .markdown: "Markdown"
19 case .pdf: "PDF"
20 case .rtf: "Rich Text (RTF)"
21 case .plainText: "Plain Text"
22 case .icalendar: "iCalendar"
23 }
24 }
25
26 var fileExtension: String {
27 switch self {
28 case .html: "html"
29 case .markdown: "md"
30 case .pdf: "pdf"
31 case .rtf: "rtf"
32 case .plainText: "txt"
33 case .icalendar: "ics"
34 }
35 }
36 }
37
38 let text: String
39 let path: String
40 let format: Format
41 let settings: OrgSettings
42 /// Made on the main actor, where the editor's highlighter is.
43 let codeStyle: HTMLCodeStyle
44
45 /// `#+EXPORT_FILE_NAME`'s name, or the file's, with the format's extension.
46 var fileName: String {
47 ExportScope.output(for: URL(fileURLWithPath: path), name: ExportScope.fileName(text, settings: settings), extension: format.fileExtension)
48 .lastPathComponent
49 }
50
51 static var transferRepresentation: some TransferRepresentation {
52 FileRepresentation(exportedContentType: .html) { try await $0.write() }
53 .exportingCondition { $0.format == .html }
54 FileRepresentation(exportedContentType: UTType(filenameExtension: "md") ?? .plainText) { try await $0.write() }
55 .exportingCondition { $0.format == .markdown }
56 FileRepresentation(exportedContentType: .pdf) { try await $0.write() }
57 .exportingCondition { $0.format == .pdf }
58 FileRepresentation(exportedContentType: .rtf) { try await $0.write() }
59 .exportingCondition { $0.format == .rtf }
60 FileRepresentation(exportedContentType: .plainText) { try await $0.write() }
61 .exportingCondition { $0.format == .plainText }
62 FileRepresentation(exportedContentType: .calendarEvent) { try await $0.write() }
63 .exportingCondition { $0.format == .icalendar }
64 }
65
66 private func write() async throws -> SentTransferredFile {
67 let base = ((path as NSString).lastPathComponent as NSString).deletingPathExtension
68 let settings = settings.resolvingSetupFiles(in: text, path: path, read: OrgSettings.readFile)
69 let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
70 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
71 let url = folder.appendingPathComponent(fileName)
72 switch format {
73 case .html:
74 try HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle)
75 .write(to: url, atomically: true, encoding: .utf8)
76 case .markdown:
77 try MarkdownExport.document(text, settings: settings, path: path).write(to: url, atomically: true, encoding: .utf8)
78 case .icalendar:
79 try ICalendarFiles.export(text, path: path, settings: settings, options: OrgPreferences.icalendarOptions(), destination: url)
80 case .pdf, .rtf, .plainText:
81 let html = HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle)
82 let directory = URL(fileURLWithPath: path).deletingLastPathComponent()
83 let data: Data = switch format {
84 case .pdf: try await PDFExport.pdf(html: html, directory: directory)
85 case .rtf: try await RichTextExport.rtf(html: html, directory: directory)
86 default: Data(try await RichTextExport.plain(html: html, directory: directory).utf8)
87 }
88 try data.write(to: url, options: .atomic)
89 }
90 return SentTransferredFile(url)
91 }
92}
93
94/// Sharing the file as HTML, Markdown, PDF, RTF, plain text or iCalendar: to another app, or saved with
95/// Save to Files; or copying it as rich text.
96struct ExportMenu: View {
97 let path: String
98 let text: String
99 @Environment(MobileModel.self) private var model
100 /// Shows what happened, as the screen's message line does.
101 var report: (String) -> Void = { _ in }
102
103 var body: some View {
104 Menu("Export", systemImage: "square.and.arrow.up") {
105 ForEach(OrgExport.Format.allCases, id: \.self) { format in
106 let export = OrgExport(text: text, path: path, format: format, settings: OrgPreferences.orgSettings(),
107 codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme))
108 ShareLink(item: export, preview: SharePreview(export.fileName)) { Text(format.name) }
109 }
110 Divider()
111 Button("Copy as Rich Text", systemImage: "doc.on.doc") { copy() }
112 }
113 }
114
115 /// The file on the clipboard as HTML, RTF and plain text, to paste formatted.
116 private func copy() {
117 let base = ((path as NSString).lastPathComponent as NSString).deletingPathExtension
118 let settings = OrgPreferences.orgSettings().resolvingSetupFiles(in: text, path: path, read: OrgSettings.readFile)
119 let html = HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path,
120 codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme))
121 do {
122 try RichTextExport.copy(html: html, directory: URL(fileURLWithPath: path).deletingLastPathComponent())
123 report("Copied as rich text")
124 } catch {
125 report("\(error)")
126 }
127 }
128}
129#endif