Browse Source

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 years ago
parent
commit
ed93b06c76
No account linked to committer's email address

+ 1
- 1
autoHeightWebView/index.js View File

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

+ 31
- 9
autoHeightWebView/utils.js View File

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

+ 2
- 2
demo/config.js View File

@@ -2,9 +2,9 @@
2 2
 
3 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 9
 const style0 = `
10 10
     p {

+ 2
- 2
demo/ios/Podfile.lock View File

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

+ 0
- 8
demo/ios/demo.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist View File

@@ -1,8 +0,0 @@
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 View File

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

+ 4
- 4
demo/yarn.lock View File

@@ -4973,10 +4973,10 @@ react-native-autoheight-webview@../:
4973 4973
   dependencies:
4974 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 4980
   dependencies:
4981 4981
     escape-string-regexp "2.0.0"
4982 4982
     invariant "2.2.4"