瀏覽代碼

Scroll enabled with zoomedin issues (#152)

* Clean up code styles

* Fix https://github.com/iou90/react-native-autoheight-webview/pull/148

* Fix https://github.com/iou90/react-native-autoheight-webview/pull/149

* Upgrade react-native-webview for demo

* Using scaling to fix https://github.com/iou90/react-native-autoheight-webview/pull/149

* Fix scaling, zoomedin undefined issue on Android

* Add usingScale to calculate scale only once per render

* Upgrade react-native-webview to 8.0.4 for demo
iou90 4 年之前
父節點
當前提交
ed93b06c76
No account linked to committer's email address

+ 1
- 1
autoHeightWebView/index.js 查看文件

46
         return;
46
         return;
47
       }
47
       }
48
       const { height, width, zoomedin } = data;
48
       const { height, width, zoomedin } = data;
49
-      !scrollEnabled && scrollEnabledWithZoomedin && setScrollable(zoomedin);
49
+      !scrollEnabled && scrollEnabledWithZoomedin && setScrollable(!!zoomedin);
50
       const { height: previousHeight, width: previousWidth } = size;
50
       const { height: previousHeight, width: previousWidth } = size;
51
       isSizeChanged({ height, previousHeight, width, previousWidth }) &&
51
       isSizeChanged({ height, previousHeight, width, previousWidth }) &&
52
         setSize({
52
         setSize({

+ 31
- 9
autoHeightWebView/utils.js 查看文件

1
 'use strict';
1
 'use strict';
2
 
2
 
3
-import { Dimensions, Platform } from 'react-native';
3
+import { Dimensions } from 'react-native';
4
 
4
 
5
 const domMutationObserveScript = `
5
 const domMutationObserveScript = `
6
   var MutationObserver =
6
   var MutationObserver =
14
 
14
 
15
 const updateSizeWithMessage = (element, scalesPageToFit) =>
15
 const updateSizeWithMessage = (element, scalesPageToFit) =>
16
   `
16
   `
17
+  var usingScale = 0;
18
+  var scaling = false;
19
+  var zoomedin = false;
17
   var lastHeight = 0;
20
   var lastHeight = 0;
18
   var heightTheSameTimes = 0;
21
   var heightTheSameTimes = 0;
19
   var maxHeightTheSameTimes = 5;
22
   var maxHeightTheSameTimes = 5;
21
   var forceRefreshTimeout;
24
   var forceRefreshTimeout;
22
   var checkPostMessageTimeout;
25
   var checkPostMessageTimeout;
23
 
26
 
24
-  function updateSize(event) {
27
+  function updateSize() {
28
+    if (zoomedin || scaling) {
29
+      return;
30
+    }
25
     if (
31
     if (
26
       !window.hasOwnProperty('ReactNativeWebView') || 
32
       !window.hasOwnProperty('ReactNativeWebView') || 
27
       !window.ReactNativeWebView.hasOwnProperty('postMessage')
33
       !window.ReactNativeWebView.hasOwnProperty('postMessage')
29
       checkPostMessageTimeout = setTimeout(updateSize, 200);
35
       checkPostMessageTimeout = setTimeout(updateSize, 200);
30
       return;
36
       return;
31
     }
37
     }
38
+
32
     clearTimeout(checkPostMessageTimeout);
39
     clearTimeout(checkPostMessageTimeout);
33
     height = ${element}.offsetHeight || document.documentElement.offsetHeight;
40
     height = ${element}.offsetHeight || document.documentElement.offsetHeight;
34
     width = ${element}.offsetWidth || document.documentElement.offsetWidth;
41
     width = ${element}.offsetWidth || document.documentElement.offsetWidth;
35
-    var scale = ${scalesPageToFit ? 'screen.width / window.innerWidth' : '1'};
36
-    window.ReactNativeWebView.postMessage(JSON.stringify({ width: Math.min(width, screen.width), height: height * scale }));
42
+
43
+    if(!usingScale && window.innerWidth) {
44
+      usingScale = ${scalesPageToFit ? 'screen.width / window.innerWidth' : '1'};
45
+    }
46
+
47
+    window.ReactNativeWebView.postMessage(JSON.stringify({ width: Math.min(width, screen.width), height: height * usingScale }));
37
 
48
 
38
     // Make additional height checks (required to fix issues wit twitter embeds)
49
     // Make additional height checks (required to fix issues wit twitter embeds)
39
     clearTimeout(forceRefreshTimeout);
50
     clearTimeout(forceRefreshTimeout);
51
+
40
     if (lastHeight !== height) {
52
     if (lastHeight !== height) {
41
       heightTheSameTimes = 1;
53
       heightTheSameTimes = 1;
42
     } else {
54
     } else {
67
 };
79
 };
68
 
80
 
69
 const detectZoomChanged = `
81
 const detectZoomChanged = `
70
-  var zoomedin = false;
71
   var latestTapStamp = 0;
82
   var latestTapStamp = 0;
72
-  var lastScale = false;
83
+  var lastScale = 1.0;
73
   var doubleTapDelay = 400;
84
   var doubleTapDelay = 400;
74
   function detectZoomChanged() {
85
   function detectZoomChanged() {
75
-    var tempZoomedin = (screen.width / window.innerWidth) > 1;
86
+    var tempZoomedin = (screen.width / window.innerWidth) > (usingScale || 1);
76
     tempZoomedin !== zoomedin && window.ReactNativeWebView.postMessage(JSON.stringify({ zoomedin: tempZoomedin }));
87
     tempZoomedin !== zoomedin && window.ReactNativeWebView.postMessage(JSON.stringify({ zoomedin: tempZoomedin }));
77
     zoomedin = tempZoomedin;
88
     zoomedin = tempZoomedin;
78
   }
89
   }
90
+  window.addEventListener('ontouchstart', event => {
91
+    if (event.touches.length === 2) {
92
+      scaling = true;
93
+    }
94
+  })
79
   window.addEventListener('touchend', event => {
95
   window.addEventListener('touchend', event => {
96
+    if(scaling) {
97
+      scaleing = false;
98
+    }
99
+
80
     var tempScale = event.scale; 
100
     var tempScale = event.scale; 
81
     tempScale !== lastScale && detectZoomChanged();
101
     tempScale !== lastScale && detectZoomChanged();
82
     lastScale = tempScale;
102
     lastScale = tempScale;
83
     var timeSince = new Date().getTime() - latestTapStamp;
103
     var timeSince = new Date().getTime() - latestTapStamp;
104
+
84
     // double tap   
105
     // double tap   
85
     if(timeSince < 600 && timeSince > 0) {
106
     if(timeSince < 600 && timeSince > 0) {
86
       zoomedinTimeOut = setTimeout(() => {
107
       zoomedinTimeOut = setTimeout(() => {
88
         detectZoomChanged();
109
         detectZoomChanged();
89
       }, doubleTapDelay);
110
       }, doubleTapDelay);
90
     }
111
     }
112
+
91
     latestTapStamp = new Date().getTime();
113
     latestTapStamp = new Date().getTime();
92
   });
114
   });
93
 `;
115
 `;
94
 
116
 
95
-const getBaseScript = ({ style, viewportContent, scalesPageToFit, scrollEnabledWithZoomedin }) =>
117
+const getBaseScript = ({ viewportContent, scalesPageToFit, scrollEnabledWithZoomedin }) =>
96
   `
118
   `
97
   ;
119
   ;
98
   if (!document.getElementById("rnahw-wrapper")) {
120
   if (!document.getElementById("rnahw-wrapper")) {
165
   scalesPageToFit,
187
   scalesPageToFit,
166
   scrollEnabledWithZoomedin
188
   scrollEnabledWithZoomedin
167
 }) => {
189
 }) => {
168
-  let script = getBaseScript({ style, viewportContent, scalesPageToFit, scrollEnabledWithZoomedin });
190
+  let script = getBaseScript({ viewportContent, scalesPageToFit, scrollEnabledWithZoomedin });
169
   script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
191
   script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
170
   script = appendStylesToHead({ style: customStyle, script });
192
   script = appendStylesToHead({ style: customStyle, script });
171
   customScript && (script = customScript + script);
193
   customScript && (script = customScript + script);

+ 2
- 2
demo/config.js 查看文件

2
 
2
 
3
 import newsletter from "./newsletter";
3
 import newsletter from "./newsletter";
4
 
4
 
5
-const autoHeightHtml0 = `<p style="font-weight: 400;font-style: normal;font-size: 21px;line-height: 1.58;letter-spacing: -.003em;"><a href="https://github.com/iou90/react-native-autoheight-webview">Tags</a> are great for describing the essence of your story in a single word or phrase, but stories are rarely about a single thing. <span style="background-color: transparent !important;background-image: linear-gradient(to bottom, rgba(146, 249, 190, 1), rgba(146, 249, 190, 1));">If I pen a story about moving across the country to start a new job in a car with my husband, two cats, a dog, and a tarantula, I wouldn't only tag the piece with "moving". I’d also use the <a href="http://x-squad.com">tags</a> "pets", "marriage", "career change", and "travel tips".</span></p>`;
5
+const autoHeightHtml0 = newsletter; //`<p style="font-weight: 400;font-style: normal;font-size: 21px;line-height: 1.58;letter-spacing: -.003em;"><a href="https://github.com/iou90/react-native-autoheight-webview">Tags</a> are great for describing the essence of your story in a single word or phrase, but stories are rarely about a single thing. <span style="background-color: transparent !important;background-image: linear-gradient(to bottom, rgba(146, 249, 190, 1), rgba(146, 249, 190, 1));">If I pen a story about moving across the country to start a new job in a car with my husband, two cats, a dog, and a tarantula, I wouldn't only tag the piece with "moving". I’d also use the <a href="http://x-squad.com">tags</a> "pets", "marriage", "career change", and "travel tips".</span></p>`;
6
 
6
 
7
-const autoHeightHtml1 = newsletter; //`Tags are great for describing the essence of your story in a single word or phrase, but stories are rarely about a single thing. If I pen a story about moving across the country to start a new job in a car with my husband, two cats, a dog, and a tarantula, I wouldn’t only tag the piece with "moving".`;
7
+const autoHeightHtml1 = `Tags are great for describing the essence of your story in a single word or phrase, but stories are rarely about a single thing. If I pen a story about moving across the country to start a new job in a car with my husband, two cats, a dog, and a tarantula, I wouldn’t only tag the piece with "moving".`;
8
 
8
 
9
 const style0 = `
9
 const style0 = `
10
     p {
10
     p {

+ 2
- 2
demo/ios/Podfile.lock 查看文件

182
     - React-cxxreact (= 0.61.5)
182
     - React-cxxreact (= 0.61.5)
183
     - React-jsi (= 0.61.5)
183
     - React-jsi (= 0.61.5)
184
   - React-jsinspector (0.61.5)
184
   - React-jsinspector (0.61.5)
185
-  - react-native-webview (8.0.2):
185
+  - react-native-webview (8.0.4):
186
     - React
186
     - React
187
   - React-RCTActionSheet (0.61.5):
187
   - React-RCTActionSheet (0.61.5):
188
     - React-Core/RCTActionSheetHeaders (= 0.61.5)
188
     - React-Core/RCTActionSheetHeaders (= 0.61.5)
326
   React-jsi: cb2cd74d7ccf4cffb071a46833613edc79cdf8f7
326
   React-jsi: cb2cd74d7ccf4cffb071a46833613edc79cdf8f7
327
   React-jsiexecutor: d5525f9ed5f782fdbacb64b9b01a43a9323d2386
327
   React-jsiexecutor: d5525f9ed5f782fdbacb64b9b01a43a9323d2386
328
   React-jsinspector: fa0ecc501688c3c4c34f28834a76302233e29dc0
328
   React-jsinspector: fa0ecc501688c3c4c34f28834a76302233e29dc0
329
-  react-native-webview: 99bdfd6c189772b0f15494f728430c23b18b93e4
329
+  react-native-webview: 3f5aa91c3cb083ea4762e006b9653291a96a777a
330
   React-RCTActionSheet: 600b4d10e3aea0913b5a92256d2719c0cdd26d76
330
   React-RCTActionSheet: 600b4d10e3aea0913b5a92256d2719c0cdd26d76
331
   React-RCTAnimation: 791a87558389c80908ed06cc5dfc5e7920dfa360
331
   React-RCTAnimation: 791a87558389c80908ed06cc5dfc5e7920dfa360
332
   React-RCTBlob: d89293cc0236d9cb0933d85e430b0bbe81ad1d72
332
   React-RCTBlob: d89293cc0236d9cb0933d85e430b0bbe81ad1d72

+ 0
- 8
demo/ios/demo.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist 查看文件

1
-<?xml version="1.0" encoding="UTF-8"?>
2
-<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
-<plist version="1.0">
4
-<dict>
5
-	<key>IDEDidComputeMac32BitWarning</key>
6
-	<true/>
7
-</dict>
8
-</plist>

+ 1
- 1
demo/package.json 查看文件

12
     "react": "16.9.0",
12
     "react": "16.9.0",
13
     "react-native": "0.61.5",
13
     "react-native": "0.61.5",
14
     "react-native-autoheight-webview": "../",
14
     "react-native-autoheight-webview": "../",
15
-    "react-native-webview": "^8.0.2"
15
+    "react-native-webview": "^8.0.4"
16
   },
16
   },
17
   "devDependencies": {
17
   "devDependencies": {
18
     "@babel/core": "^7.5.0",
18
     "@babel/core": "^7.5.0",

+ 4
- 4
demo/yarn.lock 查看文件

4973
   dependencies:
4973
   dependencies:
4974
     prop-types "^15.7.2"
4974
     prop-types "^15.7.2"
4975
 
4975
 
4976
-react-native-webview@^8.0.2:
4977
-  version "8.0.2"
4978
-  resolved "https://registry.yarnpkg.com/react-native-webview/-/react-native-webview-8.0.2.tgz#1afac4cd859ab24141490e105d1a27e18e3984a9"
4979
-  integrity sha512-Higfj0CCPM9N76S3rQsKHKS8Rw5QA7h7UENrREmMarEN/2saZJ+ko0aIVYOlEJ4P9ZhhRLNH/3mWvzmT4spncw==
4976
+react-native-webview@^8.0.4:
4977
+  version "8.0.4"
4978
+  resolved "https://registry.yarnpkg.com/react-native-webview/-/react-native-webview-8.0.4.tgz#4951df32bd54f6fa16bc0eda5e6e6762d8b83ba8"
4979
+  integrity sha512-XEatna1wwF43Rg500HaDIC/MS2IAob/PvjkTePCevs9wQz90SwERTjGZNuM2WxH+JwrZrxoilGBOn34vQTTJkw==
4980
   dependencies:
4980
   dependencies:
4981
     escape-string-regexp "2.0.0"
4981
     escape-string-regexp "2.0.0"
4982
     invariant "2.2.4"
4982
     invariant "2.2.4"