Browse Source

remove makeScalePageToFit script when scalesPageToFit prop was true; apply scale on size when scalesPageToFit prop was true to fix https://github.com/iou90/react-native-autoheight-webview/pull/133

iou90 5 years ago
parent
commit
8ce8ce6ec0
2 changed files with 31 additions and 21 deletions
  1. 0
    2
      autoHeightWebView/index.js
  2. 31
    19
      autoHeightWebView/utils.js

+ 0
- 2
autoHeightWebView/index.js View File

120
 
120
 
121
 Platform.OS === 'android' &&
121
 Platform.OS === 'android' &&
122
   Object.assign(defaultProps, {
122
   Object.assign(defaultProps, {
123
-    zoomable: false,
124
-    // if set to true may cause some layout issues (width of container will be larger than width of screen) on android
125
     scalesPageToFit: false
123
     scalesPageToFit: false
126
   });
124
   });
127
 
125
 

+ 31
- 19
autoHeightWebView/utils.js View File

3
 import { Dimensions, Platform } from 'react-native';
3
 import { Dimensions, Platform } from 'react-native';
4
 
4
 
5
 const domMutationObserveScript = `
5
 const domMutationObserveScript = `
6
-var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
7
-var observer = new MutationObserver(updateSize);
8
-observer.observe(document, {
6
+  var MutationObserver =
7
+    window.MutationObserver || window.WebKitMutationObserver;
8
+  var observer = new MutationObserver(updateSize);
9
+  observer.observe(document, {
9
     subtree: true,
10
     subtree: true,
10
     attributes: true
11
     attributes: true
11
-});
12
+  });
12
 `;
13
 `;
13
 
14
 
14
-const updateSizeWithMessage = element =>
15
+const updateSizeWithMessage = (element, scalesPageToFit) =>
15
   `
16
   `
17
+  var scale = ${scalesPageToFit ? 'screen.width / window.innerWidth' : '1'};
16
   var lastHeight = 0;
18
   var lastHeight = 0;
17
   var heightTheSameTimes = 0;
19
   var heightTheSameTimes = 0;
18
   var maxHeightTheSameTimes = 5;
20
   var maxHeightTheSameTimes = 5;
29
     }
31
     }
30
     height = ${element}.offsetHeight || document.documentElement.offsetHeight;
32
     height = ${element}.offsetHeight || document.documentElement.offsetHeight;
31
     width = ${element}.offsetWidth || document.documentElement.offsetWidth;
33
     width = ${element}.offsetWidth || document.documentElement.offsetWidth;
32
-    window.ReactNativeWebView.postMessage(JSON.stringify({ width: width, height: height }));
34
+    var needScale = width > screen.width;
35
+    window.ReactNativeWebView.postMessage(JSON.stringify({ width: needScale ? width * scale : width, height: needScale ? height * scale : height }));
33
 
36
 
34
     // Make additional height checks (required to fix issues wit twitter embeds)
37
     // Make additional height checks (required to fix issues wit twitter embeds)
35
     clearTimeout(forceRefreshTimeout);
38
     clearTimeout(forceRefreshTimeout);
51
   `;
54
   `;
52
 
55
 
53
 // add viewport setting to meta
56
 // add viewport setting to meta
54
-const makeScalePageToFit = zoomable => `
55
-var meta = document.createElement('meta'); 
56
-meta.setAttribute('name', 'viewport'); 
57
-meta.setAttribute('content', 'width=device-width, user-scalable=${
58
-  zoomable ? 'yes' : 'no'
59
-}'); document.getElementsByTagName('head')[0].appendChild(meta);
57
+const makeScalePageToFit = (zoomable, scalesPageToFit) =>
58
+  scalesPageToFit || Platform.OS === 'android'
59
+    ? ''
60
+    : `
61
+  var meta = document.createElement("meta");
62
+  meta.setAttribute("name", "viewport");
63
+  meta.setAttribute("content", "width=device-width, user-scalable=${zoomable ? 'yes' : 'no'}");
64
+  document.getElementsByTagName("head")[0].appendChild(meta);
60
 `;
65
 `;
61
 
66
 
62
-const getBaseScript = ({ style, zoomable }) =>
67
+const getBaseScript = ({ style, zoomable, scalesPageToFit }) =>
63
   `
68
   `
64
   ;
69
   ;
65
   if (!document.getElementById("rnahw-wrapper")) {
70
   if (!document.getElementById("rnahw-wrapper")) {
70
     }
75
     }
71
     document.body.appendChild(wrapper);
76
     document.body.appendChild(wrapper);
72
   }
77
   }
73
-  ${updateSizeWithMessage('wrapper')}
78
+  ${updateSizeWithMessage('wrapper', scalesPageToFit)}
74
   window.addEventListener('load', updateSize);
79
   window.addEventListener('load', updateSize);
75
   window.addEventListener('resize', updateSize);
80
   window.addEventListener('resize', updateSize);
76
   ${domMutationObserveScript}
81
   ${domMutationObserveScript}
77
-  ${makeScalePageToFit(zoomable)}
82
+  ${makeScalePageToFit(zoomable, scalesPageToFit)}
78
   updateSize();
83
   updateSize();
79
   `;
84
   `;
80
 
85
 
122
 </script>
127
 </script>
123
 `;
128
 `;
124
 
129
 
125
-const getScript = ({ files, customStyle, customScript, style, zoomable }) => {
126
-  let script = getBaseScript({ style, zoomable });
130
+const getScript = ({ files, customStyle, customScript, style, zoomable, scalesPageToFit }) => {
131
+  let script = getBaseScript({ style, zoomable, scalesPageToFit });
127
   script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
132
   script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
128
   script = appendStylesToHead({ style: customStyle, script });
133
   script = appendStylesToHead({ style: customStyle, script });
129
   customScript && (script = customScript + script);
134
   customScript && (script = customScript + script);
146
   const script = getScript(props);
151
   const script = getScript(props);
147
   const { html, baseUrl } = source;
152
   const { html, baseUrl } = source;
148
   if (html) {
153
   if (html) {
149
-    return { currentSource: { baseUrl, html: getInjectedSource({ html, script }) } };
154
+    return {
155
+      currentSource: { baseUrl, html: getInjectedSource({ html, script }) }
156
+    };
150
   } else {
157
   } else {
151
     return {
158
     return {
152
       currentSource: source,
159
       currentSource: source,
162
   for (const prop in nextProps) {
169
   for (const prop in nextProps) {
163
     if (nextProps[prop] !== prevProps[prop]) {
170
     if (nextProps[prop] !== prevProps[prop]) {
164
       if (typeof nextProps[prop] === 'object' && typeof prevProps[prop] === 'object') {
171
       if (typeof nextProps[prop] === 'object' && typeof prevProps[prop] === 'object') {
165
-        if (shouldUpdate({ prevProps: prevProps[prop], nextProps: nextProps[prop] })) {
172
+        if (
173
+          shouldUpdate({
174
+            prevProps: prevProps[prop],
175
+            nextProps: nextProps[prop]
176
+          })
177
+        ) {
166
           return true;
178
           return true;
167
         }
179
         }
168
       } else {
180
       } else {