forked from jordaaash/react-native-webview-crosswalk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.android.js
132 lines (112 loc) · 3.59 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
131
132
'use strict';
import React, { PureComponent } from 'react';
import ReactNative, { requireNativeComponent, View } from 'react-native';
import PropTypes from 'prop-types';
var {
NativeModules: { UIManager, CrosswalkWebViewManager: { JSNavigationScheme } }
} = ReactNative;
var resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource');
var WEBVIEW_REF = 'crosswalkWebView';
class WebkitView extends PureComponent{
static JSNavigationScheme;
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);
}
}
WebkitView.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,
...View.propTypes
}
WebkitView.defaultProps = {
localhost: false
}
var NativeCrosswalkWebView = requireNativeComponent('CrosswalkWebView', WebkitView, {
nativeOnly: {
messagingEnabled: PropTypes.bool,
},
});
export default WebkitView;