-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathParsedText.js
149 lines (133 loc) · 4.96 KB
/
ParsedText.js
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
import React from 'react';
import { Text, View, Pressable } from 'react-native';
import PropTypes from 'prop-types';
import TextExtraction from './lib/TextExtraction';
/**
* This is a list of the known patterns that are provided by this library
* @typedef {('url'|'phone'|'email')} KnownParsePattern
*/
/**
* @type {Object.<string, RegExp>}
* // The keys really should be KnownParsePattern -- but this is unsupported in jsdoc, sadly
*/
export const PATTERNS = {
/**
* Segments/Features:
* - http/https support https?
* - auto-detecting loose domains if preceded by `www.`
* - Localized & Long top-level domains \.(xn--)?[a-z0-9-]{2,20}\b
* - Allowed query parameters & values, it's two blocks of matchers
* ([-a-zA-Z0-9@:%_\+,.~#?&\/=]*[-a-zA-Z0-9@:%_\+~#?&\/=])*
* - First block is [-a-zA-Z0-9@:%_\+\[\],.~#?&\/=]* -- this matches parameter names & values (including commas, dots, opening & closing brackets)
* - The first block must be followed by a closing block [-a-zA-Z0-9@:%_\+\]~#?&\/=] -- this doesn't match commas, dots, and opening brackets
*/
url: /(https?:\/\/|www\.)[-a-zA-Z0-9@:%._\+~#=]{1,256}\.(xn--)?[a-z0-9-]{2,20}\b([-a-zA-Z0-9@:%_\+\[\],.~#?&\/=]*[-a-zA-Z0-9@:%_\+\]~#?&\/=])*/i,
phone: /[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,7}/,
email: /\S+@\S+\.\S+/,
};
/**
* This is for built-in-patterns already supported by this library
* Note: any additional keys/props are permitted, and will be passed along as props to the <Text> component!
* @typedef {Object} DefaultParseShape
* @property {KnownParsePattern} [type] key of the known pattern you'd like to configure
* @property {number} [nonExhaustiveModeMaxMatchCount] Enables "non-exhaustive mode", where you can limit how many matches are found. -- Must be a positive integer or Infinity matches are permitted
* @property {Function} [renderText] arbitrary function to rewrite the matched string into something else
* @property {Function} [onPress]
* @property {Function} [onLongPress]
*/
const defaultParseShape = PropTypes.shape({
...Text.propTypes,
type: PropTypes.oneOf(Object.keys(PATTERNS)).isRequired,
nonExhaustiveMaxMatchCount: PropTypes.number,
});
const customParseShape = PropTypes.shape({
...Text.propTypes,
pattern: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(RegExp)])
.isRequired,
nonExhaustiveMaxMatchCount: PropTypes.number,
});
/**
* The props added by this component
* @typedef {DefaultParseShape|import('./lib/TextExtraction').CustomParseShape} ParsedTextAddedProps
* @property {ParseShape[]} parse
* @property {import('react-native').TextProps} childrenProps -- the props set on each child Text component
*/
/** @typedef {ParsedTextAddedProps & import('react-native').TextProps} ParsedTextProps */
/** @type {import('react').ComponentClass<ParsedTextProps>} */
class ParsedText extends React.Component {
static displayName = 'ParsedText';
static propTypes = {
...Text.propTypes,
parse: PropTypes.arrayOf(
PropTypes.oneOfType([defaultParseShape, customParseShape]),
),
childrenProps: PropTypes.shape(Text.propTypes),
};
static defaultProps = {
parse: null,
childrenProps: {},
};
setNativeProps(nativeProps) {
this._root.setNativeProps(nativeProps);
}
/** @returns {import('./lib/TextExtraction').CustomParseShape[]} */
getPatterns() {
return this.props.parse.map((option) => {
const { type, ...patternOption } = option;
if (type) {
if (!PATTERNS[type]) {
throw new Error(`${option.type} is not a supported type`);
}
patternOption.pattern = PATTERNS[type];
}
return patternOption;
});
}
getParsedText() {
if (!this.props.parse) {
return this.props.children;
}
if (typeof this.props.children !== 'string') {
return this.props.children;
}
const textExtraction = new TextExtraction(
this.props.children,
this.getPatterns(),
);
return textExtraction.parse().map((props, index) => {
const { style: parentStyle } = this.props;
const { style, onPress = null, onLongPress = null, ...remainder } = props;
const isNormalText = typeof onPress !== 'function';
const ParentComponent = isNormalText ? View : Pressable;
return (
<ParentComponent
key={`parsedText-${index}`}
onPress={onPress}
onLongPress={onLongPress}
>
<Text
style={[parentStyle, style]}
{...this.props.childrenProps}
{...remainder}
/>
</ParentComponent>
);
});
}
render() {
// Discard custom props before passing remainder to Text
const { parse, childrenProps, style = {}, ...remainder } = {
...this.props,
};
return (
<View
ref={(ref) => (this._root = ref)}
style={[{ flexDirection: 'row', flexWrap: 'wrap' }, style]}
{...remainder}
>
{this.getParsedText()}
</View>
);
}
}
export default ParsedText;