Sources/Orgstar/ExportSheet.swift
122 lines · 5895 bytes
1import AppKit
2import OrgApp
3import SwiftUI
4
5/// Export… (`app.export-dialog`): the format, where the file goes, and whether to open it after.
6struct ExportSheet: View {
7 let source: URL
8 /// Whether the caret is in a subtree that can be exported alone.
9 let inSubtree: Bool
10 /// The default destination for an extension, with or without the subtree.
11 let target: (_ ext: String, _ subtree: Bool) -> URL
12 /// Without a chosen destination, the App Store build passes nil, and the export asks where
13 /// to write when the default is outside the sandbox's reach.
14 let export: (_ format: String, _ destination: URL?, _ open: Bool, _ subtree: Bool, _ bodyOnly: Bool) -> Void
15 /// The file's settings, kept when it is exported.
16 @State private var format: String
17 @State private var openAfter: Bool
18 @State private var bodyOnly: Bool
19 @State private var subtree: Bool
20 @AppStorage("pdfMargin") private var pdfMargin = 50
21 @AppStorage("pdfFont") private var pdfFont = ""
22 @AppStorage("pdfPageNumbers") private var pdfPageNumbers = true
23 @State private var destination: URL?
24 @Environment(\.dismiss) private var dismiss
25
26 init(
27 source: URL, inSubtree: Bool, target: @escaping (_ ext: String, _ subtree: Bool) -> URL,
28 export: @escaping (_ format: String, _ destination: URL?, _ open: Bool, _ subtree: Bool, _ bodyOnly: Bool) -> Void
29 ) {
30 self.source = source
31 self.inSubtree = inSubtree
32 self.target = target
33 self.export = export
34 let saved = ExportSettings.saved(for: source.path)
35 _format = State(initialValue: saved.format)
36 _openAfter = State(initialValue: saved.openAfter)
37 _bodyOnly = State(initialValue: saved.bodyOnly)
38 _subtree = State(initialValue: saved.subtree)
39 }
40
41 #if APPSTORE
42 static let formats: [(id: String, title: String, ext: String)] = [
43 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"),
44 ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("odt", "ODT", "odt"),
45 ("icalendar", "iCalendar", "ics"), ("latex", "LaTeX", "tex"), ("text", "Plain text, ASCII", "txt"),
46 ("reveal", "reveal.js slides", "html"),
47 ]
48 #else
49 static let formats: [(id: String, title: String, ext: String)] = [
50 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"),
51 ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("pdf", "PDF via LaTeX (Emacs)", "pdf"),
52 ("odt", "ODT", "odt"), ("latex", "LaTeX", "tex"), ("text", "Plain text, ASCII", "txt"),
53 ("icalendar", "iCalendar", "ics"), ("reveal", "reveal.js slides", "html"),
54 ]
55 #endif
56
57 private var ext: String { Self.formats.first { $0.id == format }?.ext ?? "html" }
58 private var chosen: URL { destination ?? target(ext, subtree && inSubtree) }
59 /// PDF and ODT are whole documents.
60 private var allowsBodyOnly: Bool { ExportSettings.allowsBodyOnly(format) }
61
62 var body: some View {
63 Form {
64 Picker("Format", selection: $format) {
65 ForEach(Self.formats, id: \.id) { Text($0.title).tag($0.id) }
66 }
67 .onChange(of: format) { destination = destination.map { $0.deletingPathExtension().appendingPathExtension(ext) } }
68 Picker("Scope", selection: Binding(get: { subtree && inSubtree }, set: { subtree = $0 })) {
69 Text("Whole file").tag(false)
70 Text("Subtree at the caret").tag(true)
71 }
72 .disabled(!inSubtree)
73 Toggle("Body only", isOn: $bodyOnly)
74 .disabled(!allowsBodyOnly)
75 .help("Leave out the title and page wrapper, as Org's body-only export")
76 if format == "print-pdf" {
77 Stepper("Margins: \(pdfMargin) pt", value: $pdfMargin, in: 18...144, step: 6)
78 Picker("Font", selection: $pdfFont) {
79 Text("The export's own").tag("")
80 Divider()
81 ForEach(NSFontManager.shared.availableFontFamilies, id: \.self) { Text($0).tag($0) }
82 }
83 Toggle("Page numbers", isOn: $pdfPageNumbers)
84 }
85 LabeledContent("Destination") {
86 HStack {
87 Text(chosen.path(percentEncoded: false)).lineLimit(1).truncationMode(.middle).foregroundStyle(.secondary)
88 Button("Choose…") { choose() }
89 }
90 }
91 Toggle("Open after export", isOn: $openAfter)
92 }
93 .formStyle(.grouped)
94 .safeAreaInset(edge: .bottom) {
95 HStack {
96 Spacer()
97 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
98 Button("Export") {
99 var settings = ExportSettings()
100 (settings.format, settings.subtree, settings.bodyOnly, settings.openAfter) = (format, subtree, bodyOnly, openAfter)
101 settings.save(for: source.path, lastUsed: true)
102 #if APPSTORE
103 export(format, destination, openAfter, subtree && inSubtree, bodyOnly && allowsBodyOnly)
104 #else
105 export(format, chosen, openAfter, subtree && inSubtree, bodyOnly && allowsBodyOnly)
106 #endif
107 dismiss()
108 }
109 .keyboardShortcut(.defaultAction)
110 }
111 .padding()
112 }
113 .frame(width: 520)
114 }
115
116 private func choose() {
117 let panel = NSSavePanel()
118 panel.directoryURL = chosen.deletingLastPathComponent()
119 panel.nameFieldStringValue = chosen.lastPathComponent
120 if panel.runModal() == .OK, let url = panel.url { destination = url }
121 }
122}