ソースを参照

Add scrollableWhenZoomin prop

iou90 4 年 前
コミット
dc7e18ba15
共有2 個のファイルを変更した13 個の追加10 個の削除を含む
  1. 9
    7
      autoHeightWebView/index.js
  2. 4
    3
      autoHeightWebView/utils.js

+ 9
- 7
autoHeightWebView/index.js ファイルの表示

@@ -12,7 +12,7 @@ import { reduceData, getWidth, isSizeChanged, shouldUpdate } from './utils';
12 12
 
13 13
 const AutoHeightWebView = React.memo(
14 14
   forwardRef((props, ref) => {
15
-    const { style, onMessage, onSizeUpdated, source } = props;
15
+    const { style, onMessage, onSizeUpdated, scrollableWhenZoomin, scrollEnabled, source } = props;
16 16
 
17 17
     if (!source) {
18 18
       return null;
@@ -31,6 +31,7 @@ const AutoHeightWebView = React.memo(
31 31
       height: style && style.height ? style.height : 0,
32 32
       width: getWidth(style)
33 33
     });
34
+    const [scrollable, setScrollable] = useState(false);
34 35
     const handleMessage = event => {
35 36
       onMessage && onMessage(event);
36 37
       if (!event.nativeEvent) {
@@ -44,7 +45,8 @@ const AutoHeightWebView = React.memo(
44 45
         console.error(error);
45 46
         return;
46 47
       }
47
-      const { height, width } = data;
48
+      const { height, width, zoomin } = data;
49
+      !scrollEnabled && scrollableWhenZoomin && setScrollable(zoomin);
48 50
       const { height: previousHeight, width: previousWidth } = size;
49 51
       isSizeChanged({ height, previousHeight, width, previousWidth }) &&
50 52
         setSize({
@@ -53,6 +55,8 @@ const AutoHeightWebView = React.memo(
53 55
         });
54 56
     };
55 57
 
58
+    const currentScrollEnabled = scrollEnabled === false && scrollableWhenZoomin ? scrollable : scrollEnabled;
59
+
56 60
     const { currentSource, script } = reduceData(props);
57 61
 
58 62
     const { width, height } = size;
@@ -81,6 +85,7 @@ const AutoHeightWebView = React.memo(
81 85
         ]}
82 86
         injectedJavaScript={script}
83 87
         source={currentSource}
88
+        scrollEnabled={currentScrollEnabled}
84 89
       />
85 90
     );
86 91
   }),
@@ -89,8 +94,6 @@ const AutoHeightWebView = React.memo(
89 94
 
90 95
 AutoHeightWebView.propTypes = {
91 96
   onSizeUpdated: PropTypes.func,
92
-  // add files to android/app/src/main/assets/ (depends on baseUrl) on android
93
-  // add files to web/ (depends on baseUrl) on iOS
94 97
   files: PropTypes.arrayOf(
95 98
     PropTypes.shape({
96 99
       href: PropTypes.string,
@@ -102,12 +105,11 @@ AutoHeightWebView.propTypes = {
102 105
   customScript: PropTypes.string,
103 106
   customStyle: PropTypes.string,
104 107
   zoomable: PropTypes.bool,
108
+  scrollableWhenZoomin: PropTypes.bool,
105 109
   // webview props
106 110
   originWhitelist: PropTypes.arrayOf(PropTypes.string),
107 111
   onMessage: PropTypes.func,
108
-  // baseUrl now contained by source
109
-  // 'web/' by default on iOS
110
-  // 'file:///android_asset/' by default on Android
112
+  scalesPageToFit: PropTypes.bool,
111 113
   source: PropTypes.object
112 114
 };
113 115
 

+ 4
- 3
autoHeightWebView/utils.js ファイルの表示

@@ -91,7 +91,7 @@ const detectZoomChanged = `
91 91
   });
92 92
 `
93 93
 
94
-const getBaseScript = ({ style, zoomable, scalesPageToFit }) =>
94
+const getBaseScript = ({ style, zoomable, scalesPageToFit, scrollableWhenZoomin }) =>
95 95
   `
96 96
   ;
97 97
   if (!document.getElementById("rnahw-wrapper")) {
@@ -107,6 +107,7 @@ const getBaseScript = ({ style, zoomable, scalesPageToFit }) =>
107 107
   window.addEventListener('resize', updateSize);
108 108
   ${domMutationObserveScript}
109 109
   ${makeScalePageToFit(zoomable, scalesPageToFit)}
110
+  ${scrollableWhenZoomin ? detectZoomChanged : ''}
110 111
   updateSize();
111 112
   `;
112 113
 
@@ -154,8 +155,8 @@ const getInjectedSource = ({ html, script }) => `
154 155
   </script>
155 156
 `;
156 157
 
157
-const getScript = ({ files, customStyle, customScript, style, zoomable, scalesPageToFit }) => {
158
-  let script = getBaseScript({ style, zoomable, scalesPageToFit });
158
+const getScript = ({ files, customStyle, customScript, style, zoomable, scalesPageToFit, scrollableWhenZoomin }) => {
159
+  let script = getBaseScript({ style, zoomable, scalesPageToFit, scrollableWhenZoomin });
159 160
   script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
160 161
   script = appendStylesToHead({ style: customStyle, script });
161 162
   customScript && (script = customScript + script);