Browse Source

Add autoWidth Adjusment

Couldn't test.... don't have Mac.
Did my best updating the code.
ShaMan123 6 years ago
parent
commit
976e789cbd
No account linked to committer's email address
1 changed files with 66 additions and 39 deletions
  1. 66
    39
      autoHeightWebView/index.ios.js

+ 66
- 39
autoHeightWebView/index.ios.js View File

6
 
6
 
7
 import PropTypes from 'prop-types';
7
 import PropTypes from 'prop-types';
8
 
8
 
9
-import { getScript, onHeightUpdated, domMutationObserveScript } from './common.js';
9
+import { getScript, onHeightUpdated, onWidthUpdated, onHeightWidthUpdated, domMutationObserveScript } from './common.js';
10
+
11
+const screenWidth = Dimensions.get('window').width;
10
 
12
 
11
 export default class AutoHeightWebView extends PureComponent {
13
 export default class AutoHeightWebView extends PureComponent {
12
   static propTypes = {
14
   static propTypes = {
13
     hasIframe: PropTypes.bool,
15
     hasIframe: PropTypes.bool,
14
     source: WebView.propTypes.source,
16
     source: WebView.propTypes.source,
15
-    onHeightUpdated: PropTypes.func,
17
+      onHeightUpdated: PropTypes.func,
18
+      onWidthUpdated: PropTypes.func,
19
+      onHeightWidthUpdated: PropTypes.func,
20
+      shouldResizeWidth: PropTypes.bool,
16
     customScript: PropTypes.string,
21
     customScript: PropTypes.string,
17
     customStyle: PropTypes.string,
22
     customStyle: PropTypes.string,
18
     enableAnimation: PropTypes.bool,
23
     enableAnimation: PropTypes.bool,
21
     // only works on enable animation
26
     // only works on enable animation
22
     animationDuration: PropTypes.number,
27
     animationDuration: PropTypes.number,
23
     // offset of rn webview margin
28
     // offset of rn webview margin
24
-    heightOffset: PropTypes.number,
29
+      heightOffset: PropTypes.number,
30
+      widthOffset: PropTypes.number,
25
     style: ViewPropTypes.style,
31
     style: ViewPropTypes.style,
26
     //  rn WebView callback
32
     //  rn WebView callback
27
     onError: PropTypes.func,
33
     onError: PropTypes.func,
43
     scalesPageToFit: false,
49
     scalesPageToFit: false,
44
     enableAnimation: true,
50
     enableAnimation: true,
45
     animationDuration: 555,
51
     animationDuration: 555,
46
-    heightOffset: 12
52
+      heightOffset: 12,
53
+      widthOffset: 12,
54
+      shouldResizeWidth: false
47
   };
55
   };
48
 
56
 
49
   constructor(props) {
57
   constructor(props) {
50
     super(props);
58
     super(props);
51
     props.enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
59
     props.enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
52
     this.state = {
60
     this.state = {
53
-      height: 0,
61
+        height: 0,
62
+        //heightOffset: 0, //?? I added this
63
+        width: screenWidth,
64
+        //widthOffset: 0,
54
       script: getScript(props, baseScript, iframeBaseScript)
65
       script: getScript(props, baseScript, iframeBaseScript)
55
     };
66
     };
56
   }
67
   }
59
     this.setState({ script: getScript(nextProps, baseScript, iframeBaseScript) });
70
     this.setState({ script: getScript(nextProps, baseScript, iframeBaseScript) });
60
   }
71
   }
61
 
72
 
62
-  handleNavigationStateChange = navState => {
63
-    const height = Number(navState.title);
64
-    const { enableAnimation, animationDuration } = this.props;
65
-    if (height && height !== this.state.height) {
66
-      enableAnimation && this.opacityAnimatedValue.setValue(0);
67
-      this.setState({ height }, () => {
68
-        enableAnimation
69
-          ? Animated.timing(this.opacityAnimatedValue, {
70
-              toValue: 1,
71
-              duration: animationDuration
72
-            }).start(() => onHeightUpdated(height, this.props))
73
-          : onHeightUpdated(height, this.props);
74
-      });
75
-    }
73
+    handleNavigationStateChange = navState => {
74
+        var [width, height] = navState.title.split(',');
75
+        width = Number(width);
76
+        height = Number(height);
77
+        const { enableAnimation, animationDuration } = this.props;
78
+        if (height && height !== this.state.height) {       // ??? add to logic ??? width && width !== this.state.width
79
+            enableAnimation && this.opacityAnimatedValue.setValue(0);
80
+            this.setState({ height, width }, () => {
81
+                enableAnimation
82
+                    ? Animated.timing(this.opacityAnimatedValue, {
83
+                        toValue: 1,
84
+                        duration: animationDuration
85
+                    }).start(() => onHeightWidthUpdated(height, width, this.props))
86
+                    : onHeightWidthUpdated(height, width, this.props);
87
+            });
88
+        }
76
   };
89
   };
77
 
90
 
78
   getWebView = webView => (this.webView = webView);
91
   getWebView = webView => (this.webView = webView);
82
   }
95
   }
83
 
96
 
84
   render() {
97
   render() {
85
-    const { height, script } = this.state;
98
+    const { height, width, script } = this.state;
86
     const {
99
     const {
87
       onError,
100
       onError,
88
       onLoad,
101
       onLoad,
93
       enableAnimation,
106
       enableAnimation,
94
       source,
107
       source,
95
       heightOffset,
108
       heightOffset,
109
+      widthOffset,
96
       customScript,
110
       customScript,
97
       style
111
       style
98
     } = this.props;
112
     } = this.props;
103
           styles.container,
117
           styles.container,
104
           {
118
           {
105
             opacity: enableAnimation ? this.opacityAnimatedValue : 1,
119
             opacity: enableAnimation ? this.opacityAnimatedValue : 1,
106
-            height: height + heightOffset
120
+              height: height + heightOffset,
121
+              width: width + widthOffset
107
           },
122
           },
108
           style
123
           style
109
         ]}
124
         ]}
127
   }
142
   }
128
 }
143
 }
129
 
144
 
130
-const screenWidth = Dimensions.get('window').width;
131
-
132
 const styles = StyleSheet.create({
145
 const styles = StyleSheet.create({
133
   container: {
146
   container: {
134
-    width: screenWidth,
135
     backgroundColor: 'transparent'
147
     backgroundColor: 'transparent'
136
   },
148
   },
137
   webView: {
149
   webView: {
141
 });
153
 });
142
 
154
 
143
 const commonScript = `
155
 const commonScript = `
144
-    updateHeight();
145
-    window.addEventListener('load', updateHeight);
146
-    window.addEventListener('resize', updateHeight);
156
+    updateSize();
157
+    window.addEventListener('load', updateSize);
158
+    window.addEventListener('resize', updateSize);
147
     `;
159
     `;
148
 
160
 
149
-const getHeight = `
161
+const _getter = `
150
     function getHeight(height) {
162
     function getHeight(height) {
151
       if(height < 1) {
163
       if(height < 1) {
152
         return document.body.offsetHeight;
164
         return document.body.offsetHeight;
153
       }
165
       }
154
       return height;
166
       return height;
155
     }
167
     }
168
+    function getWidth(width) {
169
+      if(width < 1) {
170
+        return document.body.clientWidth; // maybe should be .offsetWidth ??
171
+      }
172
+      return width;
173
+    }
156
     `;
174
     `;
157
-
158
 const baseScript = `
175
 const baseScript = `
159
     ;
176
     ;
160
-    ${getHeight}
177
+    ${_getter}
161
     (function () {
178
     (function () {
162
         var i = 0;
179
         var i = 0;
163
         var height = 0;
180
         var height = 0;
181
+        var width = ${screenWidth};
164
         var wrapper = document.createElement('div');
182
         var wrapper = document.createElement('div');
165
         wrapper.id = 'height-wrapper';
183
         wrapper.id = 'height-wrapper';
166
         while (document.body.firstChild instanceof Node) {
184
         while (document.body.firstChild instanceof Node) {
167
             wrapper.appendChild(document.body.firstChild);
185
             wrapper.appendChild(document.body.firstChild);
168
         }
186
         }
169
         document.body.appendChild(wrapper);
187
         document.body.appendChild(wrapper);
170
-        function updateHeight() {
171
-            if(document.body.offsetHeight !== height) {
172
-                height = getHeight(wrapper.clientHeight);
173
-                document.title = height;
188
+        function updateSize() {
189
+            var rect = document.body.firstElementChild.getBoundingClientRect().toJSON();
190
+            var newWidth = Math.round(rect.width);
191
+            var newHeight = Math.round(rect.height);
192
+            if(newHeight !== height) {
193
+                //height = getHeight(wrapper.clientHeight);
194
+                //width = getWidth(wrapper.clientWidth);
195
+                document.title = newWidth + ',' + newHeight;
174
                 window.location.hash = ++i;
196
                 window.location.hash = ++i;
175
             }
197
             }
176
         }
198
         }
181
 
203
 
182
 const iframeBaseScript = `
204
 const iframeBaseScript = `
183
     ;
205
     ;
184
-    ${getHeight}
206
+    ${_getter}
185
     (function () {
207
     (function () {
186
         var i = 0;
208
         var i = 0;
187
         var height = 0;
209
         var height = 0;
188
-        function updateHeight() {
189
-            if(document.body.offsetHeight !== height) {
190
-                height = getHeight(document.body.firstChild.clientHeight);
191
-                document.title = height;
210
+        var width = ${screenWidth};
211
+        function updateSize() {
212
+            var rect = document.body.firstElementChild.getBoundingClientRect().toJSON();
213
+            var newWidth = Math.round(rect.width);
214
+            var newHeight = Math.round(rect.height);
215
+            if(newHeight !== height) {
216
+                //height = getHeight(document.body.firstChild.clientHeight);
217
+                //width = getWidth(document.body.firstChild.clientHeight);
218
+                document.title = newWidth + ',' + newHeight;
192
                 window.location.hash = ++i;
219
                 window.location.hash = ++i;
193
             }
220
             }
194
         }
221
         }