ソースを参照

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 年 前
コミット
8ce8ce6ec0
共有2 個のファイルを変更した31 個の追加21 個の削除を含む
  1. 0
    2
      autoHeightWebView/index.js
  2. 31
    19
      autoHeightWebView/utils.js

+ 0
- 2
autoHeightWebView/index.js ファイルの表示

@@ -120,8 +120,6 @@ let defaultProps = {
120 120
 
121 121
 Platform.OS === 'android' &&
122 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 123
     scalesPageToFit: false
126 124
   });
127 125
 

+ 31
- 19
autoHeightWebView/utils.js ファイルの表示

@@ -3,16 +3,18 @@
3 3
 import { Dimensions, Platform } from 'react-native';
4 4
 
5 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 10
     subtree: true,
10 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 18
   var lastHeight = 0;
17 19
   var heightTheSameTimes = 0;
18 20
   var maxHeightTheSameTimes = 5;
@@ -29,7 +31,8 @@ const updateSizeWithMessage = element =>
29 31
     }
30 32
     height = ${element}.offsetHeight || document.documentElement.offsetHeight;
31 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 37
     // Make additional height checks (required to fix issues wit twitter embeds)
35 38
     clearTimeout(forceRefreshTimeout);
@@ -51,15 +54,17 @@ const updateSizeWithMessage = element =>
51 54
   `;
52 55
 
53 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 70
   if (!document.getElementById("rnahw-wrapper")) {
@@ -70,11 +75,11 @@ const getBaseScript = ({ style, zoomable }) =>
70 75
     }
71 76
     document.body.appendChild(wrapper);
72 77
   }
73
-  ${updateSizeWithMessage('wrapper')}
78
+  ${updateSizeWithMessage('wrapper', scalesPageToFit)}
74 79
   window.addEventListener('load', updateSize);
75 80
   window.addEventListener('resize', updateSize);
76 81
   ${domMutationObserveScript}
77
-  ${makeScalePageToFit(zoomable)}
82
+  ${makeScalePageToFit(zoomable, scalesPageToFit)}
78 83
   updateSize();
79 84
   `;
80 85
 
@@ -122,8 +127,8 @@ ${html}
122 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 132
   script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
128 133
   script = appendStylesToHead({ style: customStyle, script });
129 134
   customScript && (script = customScript + script);
@@ -146,7 +151,9 @@ export const reduceData = props => {
146 151
   const script = getScript(props);
147 152
   const { html, baseUrl } = source;
148 153
   if (html) {
149
-    return { currentSource: { baseUrl, html: getInjectedSource({ html, script }) } };
154
+    return {
155
+      currentSource: { baseUrl, html: getInjectedSource({ html, script }) }
156
+    };
150 157
   } else {
151 158
     return {
152 159
       currentSource: source,
@@ -162,7 +169,12 @@ export const shouldUpdate = ({ prevProps, nextProps }) => {
162 169
   for (const prop in nextProps) {
163 170
     if (nextProps[prop] !== prevProps[prop]) {
164 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 178
           return true;
167 179
         }
168 180
       } else {