forked from Thomas101/react-native-fence-html
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHTMLRenderers.js
68 lines (65 loc) · 2.45 KB
/
HTMLRenderers.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
import React from 'react'
import { TouchableOpacity, Text, Image } from 'react-native'
import HTMLStyles from './HTMLStyles'
module.exports = {
/**
* Renders an anchor tag
* @param htmlAttribs: dict of html attributes
* @param children: the children to place within the element
* @param passProps: other props that are to be passed into the element
* @return a RN element that represents an anchor tag
*/
a: (htmlAttribs, children, passProps) => {
const style = []
.concat(
HTMLStyles.defaultStyles.a,
passProps.htmlStyles ? passProps.htmlStyles.a : undefined,
htmlAttribs.style ? HTMLStyles.cssStringToRNStyle(htmlAttribs.style, HTMLStyles.STYLESETS.TEXT) : undefined
).filter((s) => s !== undefined)
if (passProps.parentIsText) {
return (
<Text
{...passProps}
style={style}
onPress={(evt) => { passProps.onLinkPress ? passProps.onLinkPress(evt, htmlAttribs.href) : undefined }}>
{children}
</Text>
)
} else {
return (
<TouchableOpacity onPress={(evt) => { passProps.onLinkPress ? passProps.onLinkPress(evt, htmlAttribs.href) : undefined }}>
<Text {...passProps} style={style}>
{children}
</Text>
</TouchableOpacity>
)
}
},
/**
* Renders an image tag
* @param htmlAttribs: dict of html attributes
* @param children: the children to place within the element
* @param passProps: other props that are to be passed into the element
* @return a RN element that represents an image tag
*/
img: (htmlAttribs, children, passProps) => {
// Build our styles
const style = []
.concat(
HTMLStyles.defaultStyles.img,
passProps.htmlStyles ? passProps.htmlStyles.img : undefined,
htmlAttribs.style ? HTMLStyles.cssStringToRNStyle(htmlAttribs.style, HTMLStyles.STYLESETS.IMAGE) : undefined
).filter((s) => s !== undefined)
// Extract our width & height (if any. If not render some defaults)
let width, height
for (var i = style.length - 1; i >= 0; i--) {
if (!width && style[i].width) { width = style[i].width }
if (!height && style[i].height) { height = style[i].height }
if (width && height) { break }
}
width = width === undefined ? 100 : width
height = height === undefined ? 100 : height
// Done
return (<Image source={{uri: htmlAttribs.src, width: width, height: height}} style={style} {...passProps} />)
}
}