Преглед изворни кода

change html property to source (now can use uri as webview source); make background color transparent by default; update version to 0.1.2

iou90 пре 7 година
родитељ
комит
c4879bbf1b
6 измењених фајлова са 3869 додато и 32 уклоњено
  1. 22
    15
      autoHeightWebView/index.android.js
  2. 13
    9
      autoHeightWebView/index.ios.js
  3. 7
    7
      demo/explorer.js
  4. 3814
    0
      demo/yarn.lock
  5. 2
    1
      package.json
  6. 11
    0
      yarn.lock

+ 22
- 15
autoHeightWebView/index.android.js Прегледај датотеку

@@ -18,6 +18,8 @@ import {
18 18
 
19 19
 import ImmutableComponent from 'react-immutable-component';
20 20
 
21
+import Immutable from 'immutable';
22
+
21 23
 const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, { nativeOnly: { messagingEnabled: PropTypes.bool } });
22 24
 
23 25
 export default class AutoHeightWebView extends ImmutableComponent {
@@ -47,9 +49,8 @@ export default class AutoHeightWebView extends ImmutableComponent {
47 49
     }
48 50
 
49 51
     componentWillReceiveProps(nextProps) {
50
-        // injectedJavaScript only works when webview reload (html changed)
51
-        if (nextProps.html === this.props.html) {
52
-            this.htmlHasChanged = false;
52
+        // injectedJavaScript only works when webview reload (source changed)
53
+        if (Immutable.is(Immutable.fromJS(this.props.source), Immutable.fromJS(nextProps.source))) {
53 54
             return;
54 55
         }
55 56
         else {
@@ -149,24 +150,30 @@ export default class AutoHeightWebView extends ImmutableComponent {
149 150
     }
150 151
 
151 152
     render() {
152
-        const source = this.props.enableBaseUrl ? {
153
-            html: this.props.html,
154
-            baseUrl: 'file:///android_asset/web/'
155
-        } : { html: this.props.html };
153
+        const { height, script, isChangingSource } = this.state;
154
+        const { source, heightOffset, customScript, style, enableBaseUrl } = this.props;
155
+        let webViewSource = source;
156
+        if (enableBaseUrl) {
157
+            webViewSource = Object.assign({}, source, { baseUrl: 'file:///android_asset/web/' });
158
+        }
156 159
         return (
157 160
             <View style={[{
158 161
                 width: ScreenWidth,
159
-                height: this.state.height + this.state.heightOffset
160
-            }, this.props.style]}>
162
+                height: height + heightOffset,
163
+                backgroundColor: 'transparent'
164
+            }, style]}>
161 165
                 {
162
-                    this.state.isChangingSource ? null :
166
+                    isChangingSource ? null :
163 167
                         <RCTAutoHeightWebView
164 168
                             ref={webview => this.webview = webview}
165
-                            style={{ flex: 1 }}
169
+                            style={{
170
+                                flex: 1,
171
+                                backgroundColor: 'transparent'
172
+                            }}
166 173
                             javaScriptEnabled={true}
167
-                            injectedJavaScript={this.state.script + this.props.customScript}
174
+                            injectedJavaScript={script + customScript}
168 175
                             scrollEnabled={false}
169
-                            source={source}
176
+                            source={webViewSource}
170 177
                             // below kitkat
171 178
                             onChange={this.onMessage}
172 179
                             onMessage={this.onMessage}
@@ -178,14 +185,14 @@ export default class AutoHeightWebView extends ImmutableComponent {
178 185
 }
179 186
 
180 187
 AutoHeightWebView.propTypes = {
181
-    ...WebView.propTypes,
182
-    html: PropTypes.string,
188
+    source: WebView.propTypes.source,
183 189
     onHeightUpdated: PropTypes.func,
184 190
     customScript: PropTypes.string,
185 191
     // offset rn webview margin
186 192
     heightOffset: PropTypes.number,
187 193
     // baseUrl not work in android 4.3 or below version
188 194
     enableBaseUrl: PropTypes.bool,
195
+    style: View.propTypes.style,
189 196
     // works if set enableBaseUrl to true; add web/files... to android/app/src/assets/
190 197
     files: PropTypes.arrayOf(PropTypes.shape({
191 198
         href: PropTypes.string,

+ 13
- 9
autoHeightWebView/index.ios.js Прегледај датотеку

@@ -60,19 +60,23 @@ export default class AutoHeightWebView extends ImmutableComponent {
60 60
     }
61 61
 
62 62
     render() {
63
+        const { height, script } = this.state;
64
+        const { source, heightOffset, customScript, style } = this.props;
65
+        const webViewSource = Object.assign({}, source, { baseUrl: 'web/' });
63 66
         return (
64 67
             <View style={[{
65 68
                 width: ScreenWidth,
66
-                height: this.state.height + this.props.heightOffset,
67
-            }, this.props.style]}>
69
+                height: height + heightOffset,
70
+                backgroundColor: 'transparent'
71
+            }, style]}>
68 72
                 <WebView
69
-                    style={{ flex: 1 }}
70
-                    injectedJavaScript={this.state.script + this.props.customScript}
71
-                    scrollEnabled={false}
72
-                    source={{
73
-                        html: this.props.html,
74
-                        baseUrl: 'web/'
73
+                    style={{
74
+                        flex: 1,
75
+                        backgroundColor: 'transparent'
75 76
                     }}
77
+                    injectedJavaScript={script + customScript}
78
+                    scrollEnabled={false}
79
+                    source={webViewSource}
76 80
                     onNavigationStateChange={this.handleNavigationStateChange} />
77 81
             </View>
78 82
         );
@@ -80,7 +84,7 @@ export default class AutoHeightWebView extends ImmutableComponent {
80 84
 }
81 85
 
82 86
 AutoHeightWebView.propTypes = {
83
-    html: PropTypes.string,
87
+    source: WebView.propTypes.source,
84 88
     onHeightUpdated: PropTypes.func,
85 89
     customScript: PropTypes.string,
86 90
     // offset rn webview margin

+ 7
- 7
demo/explorer.js Прегледај датотеку

@@ -22,7 +22,7 @@ export default class Explorer extends Component {
22 22
         this.html1 = `<p>Tags are great for describing the essence of your story in a single word or phrase, but stories are rarely about a single thing. <span>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 tags “pets”, “marriage”, “career change”, and “travel tips”.</span></p>`;
23 23
         this.script0 = '';
24 24
         this.script1 = `document.body.style.background = 'lightyellow';`;
25
-        this.changeHtml = this.changeHtml.bind(this);
25
+        this.changeSource = this.changeSource.bind(this);
26 26
         this.changeScript = this.changeScript.bind(this);
27 27
         this.state = {
28 28
             html: this.html0,
@@ -30,14 +30,14 @@ export default class Explorer extends Component {
30 30
         };
31 31
     }
32 32
 
33
-    changeHtml() {
33
+    changeSource() {
34 34
         this.setState(prevState => ({
35 35
             html: prevState.html === this.html0 ? this.html1 : this.html0
36 36
         }));
37 37
     }
38 38
 
39 39
     changeScript() {
40
-        this.changeHtml();
40
+        this.changeSource();
41 41
         this.setState(prevState => ({
42 42
             script: prevState.script === this.script0 ? this.script1 : this.script0
43 43
         }));
@@ -54,17 +54,17 @@ export default class Explorer extends Component {
54 54
                     alignItems: 'center'
55 55
                 }}>
56 56
                 <AutoHeightWebView
57
-                    html={this.state.html}
57
+                    source={{ html: this.state.html }}
58 58
                     customScript={this.state.script} />
59 59
                 <TouchableOpacity
60
-                    onPress={this.changeHtml}
60
+                    onPress={this.changeSource}
61 61
                     style={Styles.button}>
62
-                    <Text>change html</Text>
62
+                    <Text>change source</Text>
63 63
                 </TouchableOpacity>
64 64
                 <TouchableOpacity
65 65
                     onPress={this.changeScript}
66 66
                     style={Styles.button}>
67
-                    <Text>change script (have to change html to reload)</Text>
67
+                    <Text>change script (have to change source to reload on android)</Text>
68 68
                 </TouchableOpacity>
69 69
             </ScrollView>
70 70
         );

+ 3814
- 0
demo/yarn.lock
Разлика између датотеке није приказан због своје велике величине
Прегледај датотеку


+ 2
- 1
package.json Прегледај датотеку

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "react-native-autoheight-webview",
3
-  "version": "0.1.1",
3
+  "version": "0.1.2",
4 4
   "description": "An auto height webview for React Native",
5 5
   "main": "autoHeightWebView",
6 6
   "files": [
@@ -27,6 +27,7 @@
27 27
   },
28 28
   "homepage": "https://github.com/iou90/react-native-autoheight-webview#readme",
29 29
   "dependencies": {
30
+    "immutable": "^3.8.1",
30 31
     "react-immutable-component": "0.0.1"
31 32
   }
32 33
 }

+ 11
- 0
yarn.lock Прегледај датотеку

@@ -0,0 +1,11 @@
1
+# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2
+# yarn lockfile v1
3
+
4
+
5
+immutable@^3.8.1:
6
+  version "3.8.1"
7
+  resolved "http://registry.npm.taobao.org/immutable/download/immutable-3.8.1.tgz#200807f11ab0f72710ea485542de088075f68cd2"
8
+
9
+react-immutable-component@0.0.1:
10
+  version "0.0.1"
11
+  resolved "http://registry.npm.taobao.org/react-immutable-component/download/react-immutable-component-0.0.1.tgz#d39d994e3f3c896a4bf96395accaa63149d57505"