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