gitbay/Views/Shared/MarkupPreview.swift
42 lines · 1435 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 }
31
32 /// The format a file's contents render in, or nil for a file shown as
33 /// source. The same extensions the forge renders a README or wiki
34 /// page from.
35 static func format(forFile name: String) -> String? {
36 switch (name as NSString).pathExtension.lowercased() {
37 case "org": "org"
38 case "md", "markdown": "md"
39 default: nil
40 }
41 }
42}