-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
121 lines (108 loc) · 3.03 KB
/
index.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
import React from 'react';
import { ActivityIndicator, Alert, StyleSheet, View } from 'react-native';
import { WebView } from 'react-native-webview';
const webapp = require('./index.html');
// fix https://github.com/facebook/react-native/issues/10865
const patchPostMessageJsCode = `(${String(function() {
var originalPostMessage = window.postMessage;
var patchedPostMessage = function(message, targetOrigin, transfer) {
originalPostMessage(message, targetOrigin, transfer);
};
patchedPostMessage.toString = function() {
return String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage');
};
window.postMessage = patchedPostMessage;
})})();`;
class CKEditor extends React.Component {
state = {
webViewNotLoaded: true
};
onError = error => {
Alert.alert('WebView onError', error, [
{ text: 'OK', onPress: () => console.log('OK Pressed') }
]);
};
renderError = error => {
Alert.alert('WebView renderError', error, [
{ text: 'OK', onPress: () => console.log('OK Pressed') }
]);
};
createWebViewRef = webview => {
this.webview = webview;
};
postMessage = payload => {
// only send message when webview is loaded
if (this.webview) {
console.log(`WebViewEditor: sending message ${payload}`);
this.webview.postMessage(payload);
}
};
handleMessage = event => {
console.log('event', event);
let msgData;
try {
msgData = event.nativeEvent.data;
console.log('msgData', msgData);
this.props.onChange(msgData);
} catch (err) {
console.warn(err);
return;
}
};
onWebViewLoaded = () => {
console.log('Webview loaded');
this.setState({ webViewNotLoaded: false });
this.postMessage(this.props.content);
};
showLoadingIndicator = () => {
return (
<View style={styles.activityOverlayStyle}>
<View style={styles.activityIndicatorContainer}>
<ActivityIndicator size="large" animating={this.state.webViewNotLoaded} color="green" />
</View>
</View>
);
};
render() {
return (
<WebView
ref={this.createWebViewRef}
injectedJavaScript={patchPostMessageJsCode}
style={{ marginTop: 20 }}
scrollEnabled={false}
source={webapp}
scalesPageToFit={false}
onError={this.onError}
renderError={this.renderError}
javaScriptEnabled
onLoadEnd={this.onWebViewLoaded}
onMessage={this.handleMessage}
renderLoading={this.showLoadingIndicator}
mixedContentMode="always"
/>
);
}
}
const styles = StyleSheet.create({
activityOverlayStyle: {
...StyleSheet.absoluteFillObject,
display: 'flex',
justifyContent: 'center',
alignContent: 'center',
borderRadius: 0
},
activityIndicatorContainer: {
backgroundColor: 'white',
padding: 10,
borderRadius: 50,
alignSelf: 'center',
shadowColor: '#000000',
shadowOffset: {
width: 0,
height: 3
},
shadowRadius: 5,
shadowOpacity: 1.0
}
});
export default CKEditor;