Sources/Orgstar/ExportSheet.swift

87 lines · 4114 bytes

 1import AppKit
 2import SwiftUI
 3
 4/// Export… (`app.export-dialog`): the format, where the file goes, and whether to open it after.
 5struct ExportSheet: View {
 6    let source: URL
 7    /// Whether the caret is in a subtree that can be exported alone.
 8    let inSubtree: Bool
 9    /// The default destination for an extension, with or without the subtree.
10    let target: (_ ext: String, _ subtree: Bool) -> URL
11    let export: (_ format: String, _ destination: URL, _ open: Bool, _ subtree: Bool, _ bodyOnly: Bool) -> Void
12    @AppStorage("exportFormat") private var format = "html"
13    @AppStorage("exportOpenAfter") private var openAfter = false
14    @AppStorage("exportBodyOnly") private var bodyOnly = false
15    @AppStorage("pdfMargin") private var pdfMargin = 50
16    @AppStorage("pdfFont") private var pdfFont = ""
17    @AppStorage("pdfPageNumbers") private var pdfPageNumbers = true
18    @State private var subtree = false
19    @State private var destination: URL?
20    @Environment(\.dismiss) private var dismiss
21
22    static let formats: [(id: String, title: String, ext: String)] = [
23        ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"),
24        ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("pdf", "PDF via LaTeX (Emacs)", "pdf"),
25        ("odt", "ODT (Emacs)", "odt"), ("latex", "LaTeX (Emacs)", "tex"), ("text", "Plain text, ASCII (Emacs)", "txt"),
26        ("icalendar", "iCalendar", "ics"),
27    ]
28
29    private var ext: String { Self.formats.first { $0.id == format }?.ext ?? "html" }
30    private var chosen: URL { destination ?? target(ext, subtree) }
31    /// PDF and ODT are whole documents.
32    private var allowsBodyOnly: Bool { !["pdf", "print-pdf", "odt", "docx", "rtf", "plain-text", "icalendar"].contains(format) }
33
34    var body: some View {
35        Form {
36            Picker("Format", selection: $format) {
37                ForEach(Self.formats, id: \.id) { Text($0.title).tag($0.id) }
38            }
39            .onChange(of: format) { destination = destination.map { $0.deletingPathExtension().appendingPathExtension(ext) } }
40            Picker("Scope", selection: $subtree) {
41                Text("Whole file").tag(false)
42                Text("Subtree at the caret").tag(true)
43            }
44            .disabled(!inSubtree)
45            Toggle("Body only", isOn: $bodyOnly)
46                .disabled(!allowsBodyOnly)
47                .help("Leave out the title and page wrapper, as Org's body-only export")
48            if format == "print-pdf" {
49                Stepper("Margins: \(pdfMargin) pt", value: $pdfMargin, in: 18...144, step: 6)
50                Picker("Font", selection: $pdfFont) {
51                    Text("The export's own").tag("")
52                    Divider()
53                    ForEach(NSFontManager.shared.availableFontFamilies, id: \.self) { Text($0).tag($0) }
54                }
55                Toggle("Page numbers", isOn: $pdfPageNumbers)
56            }
57            LabeledContent("Destination") {
58                HStack {
59                    Text(chosen.path(percentEncoded: false)).lineLimit(1).truncationMode(.middle).foregroundStyle(.secondary)
60                    Button("Choose…") { choose() }
61                }
62            }
63            Toggle("Open after export", isOn: $openAfter)
64        }
65        .formStyle(.grouped)
66        .safeAreaInset(edge: .bottom) {
67            HStack {
68                Spacer()
69                Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
70                Button("Export") {
71                    export(format, chosen, openAfter, subtree && inSubtree, bodyOnly && allowsBodyOnly)
72                    dismiss()
73                }
74                .keyboardShortcut(.defaultAction)
75            }
76            .padding()
77        }
78        .frame(width: 520)
79    }
80
81    private func choose() {
82        let panel = NSSavePanel()
83        panel.directoryURL = chosen.deletingLastPathComponent()
84        panel.nameFieldStringValue = chosen.lastPathComponent
85        if panel.runModal() == .OK, let url = panel.url { destination = url }
86    }
87}