1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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