소스 검색

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 8 년 전
부모
커밋
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
 
18
 
19
 import ImmutableComponent from 'react-immutable-component';
19
 import ImmutableComponent from 'react-immutable-component';
20
 
20
 
21
+import Immutable from 'immutable';
22
+
21
 const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, { nativeOnly: { messagingEnabled: PropTypes.bool } });
23
 const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, { nativeOnly: { messagingEnabled: PropTypes.bool } });
22
 
24
 
23
 export default class AutoHeightWebView extends ImmutableComponent {
25
 export default class AutoHeightWebView extends ImmutableComponent {
47
     }
49
     }
48
 
50
 
49
     componentWillReceiveProps(nextProps) {
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
             return;
54
             return;
54
         }
55
         }
55
         else {
56
         else {
149
     }
150
     }
150
 
151
 
151
     render() {
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
         return (
159
         return (
157
             <View style={[{
160
             <View style={[{
158
                 width: ScreenWidth,
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
                         <RCTAutoHeightWebView
167
                         <RCTAutoHeightWebView
164
                             ref={webview => this.webview = webview}
168
                             ref={webview => this.webview = webview}
165
-                            style={{ flex: 1 }}
169
+                            style={{
170
+                                flex: 1,
171
+                                backgroundColor: 'transparent'
172
+                            }}
166
                             javaScriptEnabled={true}
173
                             javaScriptEnabled={true}
167
-                            injectedJavaScript={this.state.script + this.props.customScript}
174
+                            injectedJavaScript={script + customScript}
168
                             scrollEnabled={false}
175
                             scrollEnabled={false}
169
-                            source={source}
176
+                            source={webViewSource}
170
                             // below kitkat
177
                             // below kitkat
171
                             onChange={this.onMessage}
178
                             onChange={this.onMessage}
172
                             onMessage={this.onMessage}
179
                             onMessage={this.onMessage}
178
 }
185
 }
179
 
186
 
180
 AutoHeightWebView.propTypes = {
187
 AutoHeightWebView.propTypes = {
181
-    ...WebView.propTypes,
182
-    html: PropTypes.string,
188
+    source: WebView.propTypes.source,
183
     onHeightUpdated: PropTypes.func,
189
     onHeightUpdated: PropTypes.func,
184
     customScript: PropTypes.string,
190
     customScript: PropTypes.string,
185
     // offset rn webview margin
191
     // offset rn webview margin
186
     heightOffset: PropTypes.number,
192
     heightOffset: PropTypes.number,
187
     // baseUrl not work in android 4.3 or below version
193
     // baseUrl not work in android 4.3 or below version
188
     enableBaseUrl: PropTypes.bool,
194
     enableBaseUrl: PropTypes.bool,
195
+    style: View.propTypes.style,
189
     // works if set enableBaseUrl to true; add web/files... to android/app/src/assets/
196
     // works if set enableBaseUrl to true; add web/files... to android/app/src/assets/
190
     files: PropTypes.arrayOf(PropTypes.shape({
197
     files: PropTypes.arrayOf(PropTypes.shape({
191
         href: PropTypes.string,
198
         href: PropTypes.string,

+ 13
- 9
autoHeightWebView/index.ios.js 파일 보기

60
     }
60
     }
61
 
61
 
62
     render() {
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
         return (
66
         return (
64
             <View style={[{
67
             <View style={[{
65
                 width: ScreenWidth,
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
                 <WebView
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
                     onNavigationStateChange={this.handleNavigationStateChange} />
80
                     onNavigationStateChange={this.handleNavigationStateChange} />
77
             </View>
81
             </View>
78
         );
82
         );
80
 }
84
 }
81
 
85
 
82
 AutoHeightWebView.propTypes = {
86
 AutoHeightWebView.propTypes = {
83
-    html: PropTypes.string,
87
+    source: WebView.propTypes.source,
84
     onHeightUpdated: PropTypes.func,
88
     onHeightUpdated: PropTypes.func,
85
     customScript: PropTypes.string,
89
     customScript: PropTypes.string,
86
     // offset rn webview margin
90
     // offset rn webview margin

+ 7
- 7
demo/explorer.js 파일 보기

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>`;
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
         this.script0 = '';
23
         this.script0 = '';
24
         this.script1 = `document.body.style.background = 'lightyellow';`;
24
         this.script1 = `document.body.style.background = 'lightyellow';`;
25
-        this.changeHtml = this.changeHtml.bind(this);
25
+        this.changeSource = this.changeSource.bind(this);
26
         this.changeScript = this.changeScript.bind(this);
26
         this.changeScript = this.changeScript.bind(this);
27
         this.state = {
27
         this.state = {
28
             html: this.html0,
28
             html: this.html0,
30
         };
30
         };
31
     }
31
     }
32
 
32
 
33
-    changeHtml() {
33
+    changeSource() {
34
         this.setState(prevState => ({
34
         this.setState(prevState => ({
35
             html: prevState.html === this.html0 ? this.html1 : this.html0
35
             html: prevState.html === this.html0 ? this.html1 : this.html0
36
         }));
36
         }));
37
     }
37
     }
38
 
38
 
39
     changeScript() {
39
     changeScript() {
40
-        this.changeHtml();
40
+        this.changeSource();
41
         this.setState(prevState => ({
41
         this.setState(prevState => ({
42
             script: prevState.script === this.script0 ? this.script1 : this.script0
42
             script: prevState.script === this.script0 ? this.script1 : this.script0
43
         }));
43
         }));
54
                     alignItems: 'center'
54
                     alignItems: 'center'
55
                 }}>
55
                 }}>
56
                 <AutoHeightWebView
56
                 <AutoHeightWebView
57
-                    html={this.state.html}
57
+                    source={{ html: this.state.html }}
58
                     customScript={this.state.script} />
58
                     customScript={this.state.script} />
59
                 <TouchableOpacity
59
                 <TouchableOpacity
60
-                    onPress={this.changeHtml}
60
+                    onPress={this.changeSource}
61
                     style={Styles.button}>
61
                     style={Styles.button}>
62
-                    <Text>change html</Text>
62
+                    <Text>change source</Text>
63
                 </TouchableOpacity>
63
                 </TouchableOpacity>
64
                 <TouchableOpacity
64
                 <TouchableOpacity
65
                     onPress={this.changeScript}
65
                     onPress={this.changeScript}
66
                     style={Styles.button}>
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
                 </TouchableOpacity>
68
                 </TouchableOpacity>
69
             </ScrollView>
69
             </ScrollView>
70
         );
70
         );

+ 3814
- 0
demo/yarn.lock
파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
파일 보기


+ 2
- 1
package.json 파일 보기

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

+ 11
- 0
yarn.lock 파일 보기

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"