This repository was archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessageText.js
More file actions
151 lines (151 loc) · 5.08 KB
/
MessageText.js
File metadata and controls
151 lines (151 loc) · 5.08 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
import PropTypes from 'prop-types';
import React from 'react';
import { Linking, StyleSheet, View, ViewPropTypes, } from 'react-native';
// @ts-ignore
import ParsedText from 'react-native-parsed-text';
import Communications from 'react-native-communications';
const WWW_URL_PATTERN = /^www\./i;
const textStyle = {
fontSize: 14,
lineHeight: 20,
marginTop: 5,
marginBottom: 5,
marginLeft: 10,
marginRight: 10,
};
const styles = {
left: StyleSheet.create({
container: {},
text: {
color: 'black',
...textStyle,
},
link: {
color: 'black',
textDecorationLine: 'underline',
},
}),
right: StyleSheet.create({
container: {},
text: {
color: 'white',
...textStyle,
},
link: {
color: 'white',
textDecorationLine: 'underline',
},
}),
};
const DEFAULT_OPTION_TITLES = ['Call', 'Text', 'Cancel'];
export default class MessageText extends React.Component {
constructor() {
super(...arguments);
this.onUrlPress = (url) => {
// When someone sends a message that includes a website address beginning with "www." (omitting the scheme),
// react-native-parsed-text recognizes it as a valid url, but Linking fails to open due to the missing scheme.
if (WWW_URL_PATTERN.test(url)) {
this.onUrlPress(`http://${url}`);
}
else {
Linking.canOpenURL(url).then(supported => {
if (!supported) {
console.error('No handler for URL:', url);
}
else {
Linking.openURL(url);
}
});
}
};
this.onPhonePress = (phone) => {
const { optionTitles } = this.props;
const options = optionTitles && optionTitles.length > 0
? optionTitles.slice(0, 3)
: DEFAULT_OPTION_TITLES;
const cancelButtonIndex = options.length - 1;
this.context.actionSheet().showActionSheetWithOptions({
options,
cancelButtonIndex,
}, (buttonIndex) => {
switch (buttonIndex) {
case 0:
Communications.phonecall(phone, true);
break;
case 1:
Communications.text(phone);
break;
default:
break;
}
});
};
this.onEmailPress = (email) => Communications.email([email], null, null, null, null);
}
shouldComponentUpdate(nextProps) {
return (!!this.props.currentMessage &&
!!nextProps.currentMessage &&
this.props.currentMessage.text !== nextProps.currentMessage.text);
}
render() {
const linkStyle = [
styles[this.props.position].link,
this.props.linkStyle && this.props.linkStyle[this.props.position],
];
return (<View style={[
styles[this.props.position].container,
this.props.containerStyle &&
this.props.containerStyle[this.props.position],
]}>
<ParsedText style={[
styles[this.props.position].text,
this.props.textStyle && this.props.textStyle[this.props.position],
this.props.customTextStyle,
]} parse={[
...this.props.parsePatterns(linkStyle),
{ type: 'url', style: linkStyle, onPress: this.onUrlPress },
{ type: 'phone', style: linkStyle, onPress: this.onPhonePress },
{ type: 'email', style: linkStyle, onPress: this.onEmailPress },
]} childrenProps={{ ...this.props.textProps }}>
{this.props.currentMessage.text}
</ParsedText>
</View>);
}
}
MessageText.contextTypes = {
actionSheet: PropTypes.func,
};
MessageText.defaultProps = {
position: 'left',
optionTitles: DEFAULT_OPTION_TITLES,
currentMessage: {
text: '',
},
containerStyle: {},
textStyle: {},
linkStyle: {},
customTextStyle: {},
textProps: {},
parsePatterns: () => [],
};
MessageText.propTypes = {
position: PropTypes.oneOf(['left', 'right']),
optionTitles: PropTypes.arrayOf(PropTypes.string),
currentMessage: PropTypes.object,
containerStyle: PropTypes.shape({
left: ViewPropTypes.style,
right: ViewPropTypes.style,
}),
textStyle: PropTypes.shape({
left: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
right: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
}),
linkStyle: PropTypes.shape({
left: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
right: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
}),
parsePatterns: PropTypes.func,
textProps: PropTypes.object,
customTextStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
};
//# sourceMappingURL=MessageText.js.map