-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathHyperlink.js
More file actions
171 lines (148 loc) · 4.79 KB
/
Hyperlink.js
File metadata and controls
171 lines (148 loc) · 4.79 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
/**
* @providesModule Hyperlink
**/
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {
View,
Text,
Linking,
Platform
} from 'react-native'
import mdurl from 'mdurl'
const linkify = require('linkify-it')()
const textPropTypes = Text.propTypes || {}
const { OS } = Platform
class Hyperlink extends Component {
constructor(props) {
super(props)
this.state = { linkifyIt: props.linkify || linkify }
}
render() {
const { ...wrapperProps } = this.props
delete wrapperProps.onPress
delete wrapperProps.linkDefault
delete wrapperProps.onLongPress
delete wrapperProps.linkStyle
delete wrapperProps.wrapperComponent
const Wrapper = this.props.wrapperComponent || View;
return (
<Wrapper { ...wrapperProps } style={ this.props.style }>
{ !this.props.onPress && !this.props.onLongPress && !this.props.linkStyle
? this.props.children
: this.parse(this).props.children }
</Wrapper>
)
}
isTextNested(component) {
if (!React.isValidElement(component))
throw new Error('Invalid component')
let { type: { displayName } = {} } = component
if (displayName !== 'Text')
throw new Error('Not a Text component')
return typeof component.props.children !== 'string'
}
linkify = component => {
if (
!this.state.linkifyIt.pretest(component.props.children)
|| !this.state.linkifyIt.test(component.props.children)
)
return component
let elements = []
let _lastIndex = 0
const componentProps = {
...component.props,
ref: undefined,
key: undefined,
}
try {
this.state.linkifyIt.match(component.props.children).forEach(({ index, lastIndex, text, url }) => {
let nonLinkedText = component.props.children.substring(_lastIndex, index)
nonLinkedText && elements.push(nonLinkedText)
_lastIndex = lastIndex
if (this.props.linkText)
text = typeof this.props.linkText === 'function'
? this.props.linkText(url)
: this.props.linkText
const clickHandlerProps = {}
if (OS !== 'web') {
clickHandlerProps.onLongPress = this.props.onLongPress
? () => this.props.onLongPress(url, text)
: undefined
}
clickHandlerProps.onPress = this.props.onPress
? () => this.props.onPress(url, text)
: undefined
elements.push(
<Text
{ ...componentProps }
{ ...clickHandlerProps }
key={ url + index }
style={ [ component.props.style, this.props.linkStyle ] }
{ ...this.props.injectViewProps(url) }
>
{ text }
</Text>
)
})
elements.push(component.props.children.substring(_lastIndex, component.props.children.length))
return React.cloneElement(component, componentProps, elements)
} catch (err) {
return component
}
}
parse = component => {
let { props: { children } = {} } = component || {}
if (!children)
return component
const componentProps = {
...component.props,
ref: undefined,
key: undefined,
}
return React.cloneElement(component, componentProps, React.Children.map(children, child => {
let { type : { displayName } = {} } = child || {}
if (typeof child === 'string' && this.state.linkifyIt.pretest(child))
return this.linkify(<Text { ...componentProps } style={ component.props.style }>{ child }</Text>)
if (displayName === 'Text' && !this.isTextNested(child))
return this.linkify(child)
return this.parse(child)
}))
}
}
Hyperlink.propTypes = {
linkDefault: PropTypes.bool,
linkify: PropTypes.object,
linkStyle: textPropTypes.style,
linkText: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
]),
onPress: PropTypes.func,
onLongPress: PropTypes.func,
injectViewProps: PropTypes.func,
wrapperComponent: PropTypes.elementType,
}
Hyperlink.defaultProps = { linkify, injectViewProps: i => ({}) }
Hyperlink.getDerivedStateFromProps = (nextProps, prevState) => (nextProps.linkify !== prevState.linkifyIt)
? { linkifyIt: nextProps.linkify }
: null
export default class extends Component {
constructor (props) {
super(props)
this.handleLink = this.handleLink.bind(this)
}
handleLink (url) {
const urlObject = mdurl.parse(url)
urlObject.protocol = urlObject.protocol.toLowerCase()
const normalizedURL = mdurl.format(urlObject)
Linking.canOpenURL(normalizedURL)
.then(supported => supported && Linking.openURL(normalizedURL))
}
render () {
const onPress = this.handleLink || this.props.onPress
if (this.props.linkDefault)
return <Hyperlink { ...this.props } onPress={ onPress }/>
return <Hyperlink { ...this.props } />
}
}