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
56
57
58
import SwiftUI
extension ComposeView {
func updateEmojiSuggestions(_ shortcode: ComposerEmoji?) {
guard activeEmoji != shortcode else { return }
activeEmoji = shortcode
emojiResults = []
}
func loadEmojiSuggestions() async {
guard let shortcode = activeEmoji else { return }
let results = await Task.detached(priority: .userInitiated) {
EmojiCatalog.suggestions(for: shortcode.query)
}.value
guard !Task.isCancelled, activeEmoji == shortcode else { return }
emojiResults = results
}
func emojiDropdown(editorFrame: CGRect, viewport: CGSize) -> some View {
ComposerSuggestionDropdown(
caret: caretRect.offsetBy(dx: editorFrame.minX, dy: editorFrame.minY),
viewport: viewport, contentHeight: CGFloat(emojiResults.count) * emojiRowHeight
) {
VStack(alignment: .leading, spacing: 0) {
ForEach(emojiResults) { entry in
Button {
guard let shortcode = activeEmoji,
ComposerEmoji.active(in: text, selection: textSelection) == shortcode,
let replacement = shortcode.inserting(emoji: entry.emoji, in: text)
else { return }
text = replacement.text
textSelection = replacement.selection
updateEmojiSuggestions(nil)
textIsFocused = true
} label: {
HStack(spacing: 12) {
Text(entry.emoji)
.font(.title2)
.accessibilityHidden(true)
Text(":\(entry.shortcode):")
.appFont(.body)
.foregroundStyle(.primary)
.lineLimit(1)
Spacer(minLength: 0)
}
.padding(.horizontal, 12)
.frame(height: emojiRowHeight)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel("Insert \(entry.shortcode.replacingOccurrences(of: "_", with: " ")) emoji")
.accessibilityIdentifier("emojiSuggestion-\(entry.shortcode)")
}
}
}
.accessibilityIdentifier("emojiSuggestions")
}
}