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