import SwiftUI /// A collapsed "Preview" row under a markup editor. Expanded, it renders /// the draft with `FormattedBodyView`, the view the app's threads use, so /// what it shows is what the app will show once the text is sent. It sits /// under the editor rather than replacing it, so the draft stays in view. struct MarkupPreview: View { let text: String let format: String? let identifier: String @State private var expanded = false var body: some View { DisclosureGroup(isExpanded: $expanded) { if text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { Text("Nothing to preview.") .font(.gbSans(.subheadline)) .foregroundStyle(.secondary) } else { FormattedBodyView(text: text, format: format) .padding(.vertical, 4) } } label: { Text("Preview") .font(.gbSans(.subheadline)) } .accessibilityIdentifier(identifier) // The keyboard would cover the rendered text. .onChange(of: expanded) { _, isExpanded in if isExpanded { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) } } } /// The format a file's contents render in, or nil for a file shown as /// source. The same extensions the forge renders a README or wiki /// page from. static func format(forFile name: String) -> String? { switch (name as NSString).pathExtension.lowercased() { case "org": "org" case "md", "markdown": "md" default: nil } } }