forked from chenshuai2144/react-native-webview-crosswalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.android.js
130 lines (121 loc) · 3.86 KB
/
index.android.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
import React from 'react';
import PropTypes from 'prop-types';
import ReactNative, { requireNativeComponent, View, ViewPropTypes } from 'react-native';
var createReactClass = require('create-react-class');
var {
NativeModules: { UIManager, CrosswalkWebViewManager: { JSNavigationScheme } }
} = ReactNative;
var resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource');
var WEBVIEW_REF = 'crosswalkWebView';
class CrosswalkWebView extends React.PureComponent {
constructor(props) {
super(props);
this.statics = JSNavigationScheme;
this.onProgress = this.onProgress.bind(this);
this.onNavigationStateChange = this.onNavigationStateChange.bind(this);
this.onProgress = this.onProgress.bind(this);
this.onError = this.onError.bind(this);
this.onMessage = this.onMessage.bind(this);
}
render() {
var source = this.props.source || {};
if (this.props.url) {
source.uri = this.props.url;
}
var nativeProps = Object.assign({}, this.props, {
messagingEnabled: typeof this.props.onMessage === 'function',
onCrosswalkWebViewNavigationStateChange: this.onNavigationStateChange,
onCrosswalkWebViewError: this.onError,
onCrosswalkWebViewProgress: this.onProgress
});
return (
<NativeCrosswalkWebView
{ ...nativeProps }
ref={WEBVIEW_REF}
source={resolveAssetSource(source)}
/>
);
}
getWebViewHandle() {
return ReactNative.findNodeHandle(this.refs[WEBVIEW_REF]);
}
onNavigationStateChange(event) {
var { onNavigationStateChange } = this.props;
if (onNavigationStateChange) {
onNavigationStateChange(event.nativeEvent);
}
}
onError(event) {
var { onError } = this.props;
if (onError) {
onError(event.nativeEvent);
}
}
onProgress(event) {
var { onProgress } = this.props;
if (onProgress) {
onProgress(event.nativeEvent.progress / 100);
}
}
goBack() {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
UIManager.CrosswalkWebView.Commands.goBack,
null
);
}
goForward() {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
UIManager.CrosswalkWebView.Commands.goForward,
null
);
}
reload() {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
UIManager.CrosswalkWebView.Commands.reload,
null
);
}
postMessage(data) {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
UIManager.CrosswalkWebView.Commands.postMessage,
[String(data)]
);
}
onMessage(event) {
var { onMessage } = this.props;
onMessage && onMessage(event);
}
};
CrosswalkWebView.propTypes = {
injectedJavaScript: PropTypes.string,
localhost: PropTypes.bool.isRequired,
onError: PropTypes.func,
onMessage: PropTypes.func,
onNavigationStateChange: PropTypes.func,
onProgress: PropTypes.func,
source: PropTypes.oneOfType([
PropTypes.shape({
uri: PropTypes.string, // URI to load in WebView
}),
PropTypes.shape({
html: PropTypes.string, // static HTML to load in WebView
}),
PropTypes.number, // used internally by React packager
]),
url: PropTypes.string,
...ViewPropTypes
}
CrosswalkWebView.defaultProps = {
localhost: false
}
var NativeCrosswalkWebView = requireNativeComponent('CrosswalkWebView', CrosswalkWebView, {
nativeOnly: {
messagingEnabled: PropTypes.bool,
},
});
export default CrosswalkWebView;