|
@@ -19,6 +19,7 @@ export default class AutoHeightWebView extends PureComponent {
|
19
|
19
|
source: WebView.propTypes.source,
|
20
|
20
|
onHeightUpdated: PropTypes.func,
|
21
|
21
|
customScript: PropTypes.string,
|
|
22
|
+ customStyle: PropTypes.string,
|
22
|
23
|
enableAnimation: PropTypes.bool,
|
23
|
24
|
// if set to true may cause some layout issues (smaller font size)
|
24
|
25
|
scalesPageToFit: PropTypes.bool,
|
|
@@ -48,7 +49,13 @@ export default class AutoHeightWebView extends PureComponent {
|
48
|
49
|
if (this.props.enableAnimation) {
|
49
|
50
|
this.opacityAnimatedValue = new Animated.Value(0);
|
50
|
51
|
}
|
51
|
|
- const initialScript = props.files ? this.appendFilesToHead(props.files, props.hasIframe ? IframeBaseScript : BaseScript) : props.hasIframe ? IframeBaseScript : BaseScript;
|
|
52
|
+ let initialScript = props.hasIframe ? IframeBaseScript : BaseScript;
|
|
53
|
+ initialScript = props.files
|
|
54
|
+ ? this.appendFilesToHead(props.files, BaseScript)
|
|
55
|
+ : BaseScript;
|
|
56
|
+ initialScript = props.customStyle
|
|
57
|
+ ? this.appendStylesToHead(props.customStyle, initialScript)
|
|
58
|
+ : initialScript;
|
52
|
59
|
this.state = {
|
53
|
60
|
height: 0,
|
54
|
61
|
script: initialScript
|
|
@@ -58,8 +65,11 @@ export default class AutoHeightWebView extends PureComponent {
|
58
|
65
|
componentWillReceiveProps(nextProps) {
|
59
|
66
|
let currentScript = nextProps.hasIframe ? IframeBaseScript : BaseScript;
|
60
|
67
|
if (nextProps.files) {
|
61
|
|
- currentScript = this.appendFilesToHead(nextProps.files, nextProps.hasIframe ? IframeBaseScript : BaseScript);
|
|
68
|
+ currentScript = this.appendFilesToHead(nextProps.files, currentScript);
|
62
|
69
|
}
|
|
70
|
+ currentScript = nextProps.customStyle
|
|
71
|
+ ? this.appendStylesToHead(nextProps.customStyle, currentScript)
|
|
72
|
+ : currentScript;
|
63
|
73
|
this.setState({ script: currentScript });
|
64
|
74
|
}
|
65
|
75
|
|
|
@@ -80,6 +90,19 @@ export default class AutoHeightWebView extends PureComponent {
|
80
|
90
|
return script;
|
81
|
91
|
}
|
82
|
92
|
|
|
93
|
+ appendStylesToHead(styles, script) {
|
|
94
|
+ if (!styles) {
|
|
95
|
+ return script;
|
|
96
|
+ }
|
|
97
|
+ return `
|
|
98
|
+ var styleElement = document.createElement('style');
|
|
99
|
+ var styleText = document.createTextNode('${styles}');
|
|
100
|
+ styleElement.appendChild(styleText);
|
|
101
|
+ document.head.appendChild(styleElement);
|
|
102
|
+ ${script}
|
|
103
|
+ `
|
|
104
|
+ }
|
|
105
|
+
|
83
|
106
|
onHeightUpdated(height) {
|
84
|
107
|
if (this.props.onHeightUpdated) {
|
85
|
108
|
this.props.onHeightUpdated(height);
|