Sources/Orgstar/ExportSheet.swift

5eef7a168632da902fdd0979d57258320e4fdfc2
orgstar/Sources/Orgstar/ExportSheet.swift history · blame · raw

74 lines · 3413 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    @State private var subtree = false
16    @State private var destination: URL?
17    @Environment(\.dismiss) private var dismiss
18
19    static let formats: [(id: String, title: String, ext: String)] = [
20        ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"),
21        ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("pdf", "PDF via LaTeX (Emacs)", "pdf"),
22        ("odt", "ODT (Emacs)", "odt"), ("latex", "LaTeX (Emacs)", "tex"), ("text", "Plain text, ASCII (Emacs)", "txt"),
23    ]
24
25    private var ext: String { Self.formats.first { $0.id == format }?.ext ?? "html" }
26    private var chosen: URL { destination ?? target(ext, subtree) }
27    /// PDF and ODT are whole documents.
28    private var allowsBodyOnly: Bool { !["pdf", "print-pdf", "odt", "docx", "rtf", "plain-text"].contains(format) }
29
30    var body: some View {
31        Form {
32            Picker("Format", selection: $format) {
33                ForEach(Self.formats, id: \.id) { Text($0.title).tag($0.id) }
34            }
35            .onChange(of: format) { destination = destination.map { $0.deletingPathExtension().appendingPathExtension(ext) } }
36            Picker("Scope", selection: $subtree) {
37                Text("Whole file").tag(false)
38                Text("Subtree at the caret").tag(true)
39            }
40            .disabled(!inSubtree)
41            Toggle("Body only", isOn: $bodyOnly)
42                .disabled(!allowsBodyOnly)
43                .help("Leave out the title and page wrapper, as Org's body-only export")
44            LabeledContent("Destination") {
45                HStack {
46                    Text(chosen.path(percentEncoded: false)).lineLimit(1).truncationMode(.middle).foregroundStyle(.secondary)
47                    Button("Choose…") { choose() }
48                }
49            }
50            Toggle("Open after export", isOn: $openAfter)
51        }
52        .formStyle(.grouped)
53        .safeAreaInset(edge: .bottom) {
54            HStack {
55                Spacer()
56                Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
57                Button("Export") {
58                    export(format, chosen, openAfter, subtree && inSubtree, bodyOnly && allowsBodyOnly)
59                    dismiss()
60                }
61                .keyboardShortcut(.defaultAction)
62            }
63            .padding()
64        }
65        .frame(width: 520)
66    }
67
68    private func choose() {
69        let panel = NSSavePanel()
70        panel.directoryURL = chosen.deletingLastPathComponent()
71        panel.nameFieldStringValue = chosen.lastPathComponent
72        if panel.runModal() == .OK, let url = panel.url { destination = url }
73    }
74}