Sources/OrgstarMobile/ExportMenu.swift

a9ec94b75caa22bf47461bc180158d2b961bc747
orgstar/Sources/OrgstarMobile/ExportMenu.swift

90 lines · 3721 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 or PDF, written when the share sheet takes it.
10struct OrgExport: Transferable {
11    enum Format: CaseIterable {
12        case html, markdown, pdf
13
14        var name: String {
15            switch self {
16            case .html: "HTML"
17            case .markdown: "Markdown"
18            case .pdf: "PDF"
19            }
20        }
21
22        var fileExtension: String {
23            switch self {
24            case .html: "html"
25            case .markdown: "md"
26            case .pdf: "pdf"
27            }
28        }
29    }
30
31    let text: String
32    let path: String
33    let format: Format
34    let settings: OrgSettings
35    /// Made on the main actor, where the editor's highlighter is.
36    let codeStyle: HTMLCodeStyle
37
38    /// `#+EXPORT_FILE_NAME`'s name, or the file's, with the format's extension.
39    var fileName: String {
40        ExportScope.output(for: URL(fileURLWithPath: path), name: ExportScope.fileName(text, settings: settings), extension: format.fileExtension)
41            .lastPathComponent
42    }
43
44    static var transferRepresentation: some TransferRepresentation {
45        FileRepresentation(exportedContentType: .html) { try await $0.write() }
46            .exportingCondition { $0.format == .html }
47        FileRepresentation(exportedContentType: UTType(filenameExtension: "md") ?? .plainText) { try await $0.write() }
48            .exportingCondition { $0.format == .markdown }
49        FileRepresentation(exportedContentType: .pdf) { try await $0.write() }
50            .exportingCondition { $0.format == .pdf }
51    }
52
53    private func write() async throws -> SentTransferredFile {
54        let base = ((path as NSString).lastPathComponent as NSString).deletingPathExtension
55        let settings = settings.resolvingSetupFiles(in: text, path: path, read: OrgSettings.readFile)
56        let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
57        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
58        let url = folder.appendingPathComponent(fileName)
59        switch format {
60        case .html:
61            try HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle)
62                .write(to: url, atomically: true, encoding: .utf8)
63        case .markdown:
64            try MarkdownExport.document(text, settings: settings, path: path).write(to: url, atomically: true, encoding: .utf8)
65        case .pdf:
66            let html = HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle)
67            let directory = URL(fileURLWithPath: path).deletingLastPathComponent()
68            try await PDFExport.pdf(html: html, directory: directory).write(to: url, options: .atomic)
69        }
70        return SentTransferredFile(url)
71    }
72}
73
74/// Sharing the file as HTML, Markdown or PDF: to another app, or saved with Save to Files.
75struct ExportMenu: View {
76    let path: String
77    let text: String
78    @Environment(MobileModel.self) private var model
79
80    var body: some View {
81        Menu("Export", systemImage: "square.and.arrow.up") {
82            ForEach(OrgExport.Format.allCases, id: \.self) { format in
83                let export = OrgExport(text: text, path: path, format: format, settings: OrgPreferences.orgSettings(),
84                                       codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme))
85                ShareLink(item: export, preview: SharePreview(export.fileName)) { Text(format.name) }
86            }
87        }
88    }
89}
90#endif
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form