-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathEnrichedTextScreen.tsx
More file actions
186 lines (177 loc) · 5.17 KB
/
Copy pathEnrichedTextScreen.tsx
File metadata and controls
186 lines (177 loc) · 5.17 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { useState } from 'react';
import { View, StyleSheet, ScrollView, Text } from 'react-native';
import {
EnrichedText,
type EnrichedTextProps,
type OnImagePressEvent,
type OnLinkPressEvent,
type OnMentionPressEvent,
} from 'react-native-enriched-html';
import { Button } from '../components/Button';
import { ValueModal } from '../components/ValueModal';
import { enrichedTextHtmlStyle } from '../constants/editorConfig';
type EllipsizeMode = EnrichedTextProps['ellipsizeMode'];
interface EnrichedTextScreenProps {
onSwitch: () => void;
}
export function EnrichedTextScreen({ onSwitch }: EnrichedTextScreenProps) {
const [isModalOpen, setIsModalOpen] = useState(false);
const [html, setHtml] = useState<string | null>(null);
const [ellipsizeMode, setEllipsizeMode] = useState<EllipsizeMode>('tail');
const [numberOfLines, setNumberOfLines] = useState<number>(0);
const handleSubmit = (value: string) => {
setHtml(value);
setIsModalOpen(false);
};
const handleLinkPress = (e: OnLinkPressEvent) => {
setHtml(`You pressed the link: ${e.url}`);
};
const handleMentionPress = (e: OnMentionPressEvent) => {
setHtml(
`You pressed the mention: text: ${e.text}, type: ${e.indicator}, attributes: ${JSON.stringify(e.attributes)}`
);
};
const handleImagePress = (e: OnImagePressEvent) => {
setHtml(`You pressed the image: ${JSON.stringify(e.image)}`);
};
return (
<>
<ScrollView
style={styles.container}
contentContainerStyle={styles.content}
>
<View style={styles.buttonRow}>
<Button
title="Test Screen"
onPress={onSwitch}
style={styles.rowButton}
testID="toggle-screen-button"
/>
<Button
title="Set Text"
onPress={() => setIsModalOpen(true)}
style={styles.rowButton}
testID="set-enriched-text-button"
/>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle} testID="ellipsize-current-mode">
ellipsizeMode: {ellipsizeMode}
</Text>
<View style={styles.buttonRow}>
<Button
title="Head"
onPress={() => setEllipsizeMode('head')}
style={styles.rowButton}
testID="ellipsize-head-button"
/>
<Button
title="Middle"
onPress={() => setEllipsizeMode('middle')}
style={styles.rowButton}
testID="ellipsize-middle-button"
/>
<Button
title="Tail"
onPress={() => setEllipsizeMode('tail')}
style={styles.rowButton}
testID="ellipsize-tail-button"
/>
</View>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle} testID="number-of-lines-current">
numberOfLines: {numberOfLines === 0 ? 'unset' : numberOfLines}
</Text>
<View style={styles.buttonRow}>
<Button
title="All"
onPress={() => setNumberOfLines(0)}
style={styles.rowButton}
testID="number-of-lines-all-button"
/>
<Button
title="1"
onPress={() => setNumberOfLines(1)}
style={styles.rowButton}
testID="number-of-lines-1-button"
/>
<Button
title="2"
onPress={() => setNumberOfLines(2)}
style={styles.rowButton}
testID="number-of-lines-2-button"
/>
</View>
</View>
{html !== null && (
<View style={styles.rendererContainer} testID="enriched-text">
<EnrichedText
style={styles.text}
htmlStyle={enrichedTextHtmlStyle}
numberOfLines={numberOfLines}
ellipsizeMode={ellipsizeMode}
onLinkPress={handleLinkPress}
onMentionPress={handleMentionPress}
onImagePress={handleImagePress}
useHtmlNormalizer
>
{html}
</EnrichedText>
</View>
)}
</ScrollView>
<ValueModal
avoidKeyboard
isOpen={isModalOpen}
onSubmit={handleSubmit}
onClose={() => setIsModalOpen(false)}
/>
</>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
content: {
flexGrow: 1,
padding: 16,
paddingTop: 50,
alignItems: 'center',
},
buttonRow: {
flexDirection: 'row',
width: '100%',
gap: 8,
},
sectionContainer: {
borderWidth: StyleSheet.hairlineWidth,
borderColor: 'gray',
borderRadius: 8,
padding: 16,
marginTop: 8,
},
sectionTitle: {
alignSelf: 'flex-start',
fontSize: 20,
fontWeight: '600',
color: 'black',
},
rowButton: {
flex: 1,
},
rendererContainer: {
width: '100%',
padding: 16,
borderWidth: StyleSheet.hairlineWidth,
marginVertical: 16,
borderRadius: 8,
},
text: {
fontSize: 18,
color: 'black',
fontFamily: 'Nunito-Regular',
},
});