gitbay/Views/Shared/MarkupPreview.swift
49 lines · 1764 bytes
1import SwiftUI
2
3/// A collapsed "Preview" row under a markup editor. Expanded, it renders
4/// the draft with `FormattedBodyView`, the view the app's threads use, so
5/// what it shows is what the app will show once the text is sent. It sits
6/// under the editor rather than replacing it, so the draft stays in view.
7struct MarkupPreview: View {
8
9 let text: String
10 let format: String?
11 let identifier: String
12
13 @State private var expanded = false
14
15 var body: some View {
16 DisclosureGroup(isExpanded: $expanded) {
17 if text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
18 Text("Nothing to preview.")
19 .font(.gbSans(.subheadline))
20 .foregroundStyle(.secondary)
21 } else {
22 FormattedBodyView(text: text, format: format)
23 .padding(.vertical, 4)
24 }
25 } label: {
26 Text("Preview")
27 .font(.gbSans(.subheadline))
28 }
29 .accessibilityIdentifier(identifier)
30 // The keyboard would cover the rendered text.
31 .onChange(of: expanded) { _, isExpanded in
32 if isExpanded {
33 UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder),
34 to: nil, from: nil, for: nil)
35 }
36 }
37 }
38
39 /// The format a file's contents render in, or nil for a file shown as
40 /// source. The same extensions the forge renders a README or wiki
41 /// page from.
42 static func format(forFile name: String) -> String? {
43 switch (name as NSString).pathExtension.lowercased() {
44 case "org": "org"
45 case "md", "markdown": "md"
46 default: nil
47 }
48 }
49}