~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
55 lines · 2.2 KB Raw History
import SwiftUI

struct PostText: View {
    let text: String
    var facets: [PostFacet] = []
    var preparedText: AttributedString?
    var font: Font.TextStyle?
    var accessibilityID = ""
    let openPost: () -> Void
    let openActor: (String) -> Void
    let openLinkedPost: (String) -> Void

    var body: some View {
        if preparedText != nil || !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
            let rendered = preparedText ?? PostTextFacets.render(
                text, facets: facets, defaultLink: PostTextFacets.openContainingPostURL)
            Text(rendered)
                .appFontIfSet(font)
                .foregroundStyle(.primary)
                .fixedSize(horizontal: false, vertical: true)
                .frame(maxWidth: .infinity, alignment: .leading)
                .contentShape(Rectangle())
                .accessibilityAddTraits(.isButton)
                .accessibilityAction { openPost() }
                .externalLinkBrowser {
                    if $0 == PostTextFacets.openContainingPostURL {
                        openPost()
                        return true
                    }
                    return PostTextFacets.openInternalURL($0, openActor: openActor, openPost: openLinkedPost)
                }
                .accessibilityIdentifier(accessibilityID)
        }
    }
}

#if DEBUG
    #Preview("Post text") {
        VStack(alignment: .leading, spacing: 20) {
            PostText(text: PreviewContent.post.text, openPost: {}, openActor: { _ in }, openLinkedPost: { _ in })
            PostText(
                text: "Hello @mae.example, see https://example.com",
                facets: [
                    PostFacet(byteStart: 6, byteEnd: 18, feature: .mention("mae")),
                    PostFacet(byteStart: 24, byteEnd: 43, feature: .link(URL(string: "https://example.com")!))
                ], font: .subheadline, openPost: {}, openActor: { _ in }, openLinkedPost: { _ in })
            PostText(
                text: "A longer post can wrap across several lines. "
                    + "The text stays readable even when a person uses a larger text size.",
                openPost: {}, openActor: { _ in }, openLinkedPost: { _ in }
            )
        }
        .padding()
    }
#endif