-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathTextRenderer.tsx
More file actions
89 lines (77 loc) · 2.47 KB
/
Copy pathTextRenderer.tsx
File metadata and controls
89 lines (77 loc) · 2.47 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
import { useRef, useState } from 'react';
import type { TextProps, TextStyle } from 'react-native';
import {
EnrichedText,
type BlurEvent,
type EnrichedTextInstance,
type FocusEvent,
type OnImagePressEvent,
type OnLinkPressEvent,
type OnMentionPressEvent,
} from 'react-native-enriched-html';
import { WEB_DEFAULT_HTML_STYLE } from '../defaultHtmlStyle';
import { EnrichedTextActions } from './EnrichedTextActions';
const LINK_REGEX =
/^(?:enriched:\/\/\S+|(?:https?:\/\/)?(?:www\.)?swmansion\.com(?:\/\S*)?|https?:\/\/\S+)$/i;
const SANITIZATION_CONFIG = {
linkRegex: LINK_REGEX,
};
interface TextRendererProps {
htmlValue: string;
}
export function TextRenderer({ htmlValue }: TextRendererProps) {
const ref = useRef<EnrichedTextInstance>(null);
const [ellipsizeMode, setEllipsizeMode] =
useState<TextProps['ellipsizeMode']>('tail');
const [numberOfLines, setNumberOfLines] = useState(2);
const handleTextFocus = (e: FocusEvent) => {
console.log('[EnrichedText] onFocus', e.nativeEvent);
};
const handleTextBlur = (e: BlurEvent) => {
console.log('[EnrichedText] onBlur', e.nativeEvent);
};
const handleLinkPress = (e: OnLinkPressEvent) => {
console.log('[EnrichedText] link press event', e);
};
const handleMentionPress = (e: OnMentionPressEvent) => {
console.log('[EnrichedText] mention press event', e);
};
const handleImagePress = (e: OnImagePressEvent) => {
console.log('[EnrichedText] image press event', e);
};
return (
<div className="container enriched-text-container">
<h1 className="app-title">Enriched Text</h1>
<EnrichedText
ref={ref}
style={enrichedTextStyle}
htmlStyle={WEB_DEFAULT_HTML_STYLE}
onFocus={handleTextFocus}
onBlur={handleTextBlur}
onLinkPress={handleLinkPress}
onMentionPress={handleMentionPress}
numberOfLines={numberOfLines}
ellipsizeMode={ellipsizeMode}
sanitizationConfig={SANITIZATION_CONFIG}
onImagePress={handleImagePress}
>
{htmlValue}
</EnrichedText>
<EnrichedTextActions
ellipsizeMode={ellipsizeMode ?? 'tail'}
numberOfLines={numberOfLines}
onChangeEllipsizeMode={setEllipsizeMode}
onChangeNumberOfLines={setNumberOfLines}
/>
</div>
);
}
const enrichedTextStyle: TextStyle = {
backgroundColor: 'gainsboro',
width: '100%',
marginVertical: 12,
paddingVertical: 12,
paddingHorizontal: 14,
borderRadius: 8,
fontSize: 18,
};