Sources/Orgstar/ExportSheet.swift
100 lines · 4823 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 /// Without a chosen destination, the App Store build passes nil, and the export asks where
12 /// to write when the default is outside the sandbox's reach.
13 let export: (_ format: String, _ destination: URL?, _ open: Bool, _ subtree: Bool, _ bodyOnly: Bool) -> Void
14 @AppStorage("exportFormat") private var format = "html"
15 @AppStorage("exportOpenAfter") private var openAfter = false
16 @AppStorage("exportBodyOnly") private var bodyOnly = false
17 @AppStorage("pdfMargin") private var pdfMargin = 50
18 @AppStorage("pdfFont") private var pdfFont = ""
19 @AppStorage("pdfPageNumbers") private var pdfPageNumbers = true
20 @State private var subtree = false
21 @State private var destination: URL?
22 @Environment(\.dismiss) private var dismiss
23
24 #if APPSTORE
25 static let formats: [(id: String, title: String, ext: String)] = [
26 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"),
27 ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("icalendar", "iCalendar", "ics"),
28 ]
29 #else
30 static let formats: [(id: String, title: String, ext: String)] = [
31 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"),
32 ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("pdf", "PDF via LaTeX (Emacs)", "pdf"),
33 ("odt", "ODT (Emacs)", "odt"), ("latex", "LaTeX (Emacs)", "tex"), ("text", "Plain text, ASCII (Emacs)", "txt"),
34 ("icalendar", "iCalendar", "ics"),
35 ]
36 #endif
37
38 private var ext: String { Self.formats.first { $0.id == format }?.ext ?? "html" }
39 private var chosen: URL { destination ?? target(ext, subtree) }
40 /// PDF and ODT are whole documents.
41 private var allowsBodyOnly: Bool { !["pdf", "print-pdf", "odt", "docx", "rtf", "plain-text", "icalendar"].contains(format) }
42
43 var body: some View {
44 Form {
45 Picker("Format", selection: $format) {
46 ForEach(Self.formats, id: \.id) { Text($0.title).tag($0.id) }
47 }
48 .onChange(of: format) { destination = destination.map { $0.deletingPathExtension().appendingPathExtension(ext) } }
49 Picker("Scope", selection: $subtree) {
50 Text("Whole file").tag(false)
51 Text("Subtree at the caret").tag(true)
52 }
53 .disabled(!inSubtree)
54 Toggle("Body only", isOn: $bodyOnly)
55 .disabled(!allowsBodyOnly)
56 .help("Leave out the title and page wrapper, as Org's body-only export")
57 if format == "print-pdf" {
58 Stepper("Margins: \(pdfMargin) pt", value: $pdfMargin, in: 18...144, step: 6)
59 Picker("Font", selection: $pdfFont) {
60 Text("The export's own").tag("")
61 Divider()
62 ForEach(NSFontManager.shared.availableFontFamilies, id: \.self) { Text($0).tag($0) }
63 }
64 Toggle("Page numbers", isOn: $pdfPageNumbers)
65 }
66 LabeledContent("Destination") {
67 HStack {
68 Text(chosen.path(percentEncoded: false)).lineLimit(1).truncationMode(.middle).foregroundStyle(.secondary)
69 Button("Choose…") { choose() }
70 }
71 }
72 Toggle("Open after export", isOn: $openAfter)
73 }
74 .formStyle(.grouped)
75 .safeAreaInset(edge: .bottom) {
76 HStack {
77 Spacer()
78 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
79 Button("Export") {
80 #if APPSTORE
81 export(format, destination, openAfter, subtree && inSubtree, bodyOnly && allowsBodyOnly)
82 #else
83 export(format, chosen, openAfter, subtree && inSubtree, bodyOnly && allowsBodyOnly)
84 #endif
85 dismiss()
86 }
87 .keyboardShortcut(.defaultAction)
88 }
89 .padding()
90 }
91 .frame(width: 520)
92 }
93
94 private func choose() {
95 let panel = NSSavePanel()
96 panel.directoryURL = chosen.deletingLastPathComponent()
97 panel.nameFieldStringValue = chosen.lastPathComponent
98 if panel.runModal() == .OK, let url = panel.url { destination = url }
99 }
100}