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