forked from reggie3/react-native-webview-quilljs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebViewQuillViewer.js
187 lines (169 loc) · 4.77 KB
/
WebViewQuillViewer.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/********************************************
* WebViewQuillViewer.js
* A Delta viewer suitable for viewing output from a Quill.js
* editor. The Delta format is discussed here: https://quilljs.com/docs/delta/
* This component is useful for applications that must avoid using native code
*
*/
import React from 'react';
import { View, ActivityIndicator, StyleSheet, WebView } from 'react-native';
import PropTypes from 'prop-types';
import renderIf from 'render-if';
// path to the file that the webview will load
const INDEX_FILE = require(`./assets/assets/dist/reactQuillViewer-index.html`);
const MESSAGE_PREFIX = 'react-native-webview-quilljs';
export default class WebViewQuillViewer extends React.Component {
constructor() {
super();
this.webview = null;
this.state = {
webViewNotLoaded: true, // flag to show activity indicator
webViewFilesNotAvailable: true
};
}
webViewDownloadStatusCallBack = message => {
console.log(message);
};
createWebViewRef = webview => {
this.webview = webview;
};
handleMessage = event => {
let msgData;
try {
msgData = JSON.parse(event.nativeEvent.data);
if (msgData.hasOwnProperty('prefix') && msgData.prefix === MESSAGE_PREFIX) {
// console.log(`WebViewQuillEditor: received message ${msgData.type}`);
this.sendMessage('MESSAGE_ACKNOWLEDGED');
// console.log(`WebViewQuillEditor: sent MESSAGE_ACKNOWLEDGED`);
switch (msgData.type) {
case 'VIEWER_LOADED':
this.viewerLoaded();
break;
default:
console.warn(
`WebViewQuillViewer Error: Unhandled message type received "${msgData.type}"`
);
}
}
} catch (err) {
console.warn(err);
return;
}
};
onWebViewLoaded = () => {
this.setState({ webViewNotLoaded: false });
this.sendMessage('LOAD_VIEWER', {
theme: this.props.theme
});
if (this.props.hasOwnProperty('backgroundColor')) {
this.sendMessage('SET_BACKGROUND_COLOR', {
backgroundColor: this.props.backgroundColor
});
}
};
viewerLoaded = () => {
// send the content to the editor if we have it
if (this.props.hasOwnProperty('contentToDisplay')) {
this.sendMessage('SET_CONTENTS', {
ops: this.props.contentToDisplay
});
}
if (this.props.hasOwnProperty('htmlContentToDisplay')) {
this.sendMessage('SET_HTML_CONTENTS', {
html: this.props.htmlContentToDisplay
});
}
};
sendContentToViewer = delta => {
this.sendMessage('SET_CONTENTS', {
ops: delta.ops
});
};
sendMessage = (type, payload) => {
// only send message when webview is loaded
if (this.webview) {
console.log(`WebViewQuillViewer: sending message ${type}`);
this.webview.postMessage(
JSON.stringify({
prefix: MESSAGE_PREFIX,
type,
payload
}),
'*'
);
}
};
showLoadingIndicator = () => {
return (
<View style={styles.activityOverlayStyle}>
<ActivityIndicator size="large" animating={this.state.webViewNotLoaded} color="#4CB8C4" />
</View>
);
};
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') }
]);
};
render = () => {
return (
<View
style={{
flex: 1
}}
>
<WebView
style={{ ...StyleSheet.absoluteFillObject }}
ref={this.createWebViewRef}
source={INDEX_FILE}
onLoadEnd={this.onWebViewLoaded}
onMessage={this.handleMessage}
startInLoadingState={true}
renderLoading={this.showLoadingIndicator}
renderError={this.renderError}
javaScriptEnabled={true}
onError={this.onError}
scalesPageToFit={false}
mixedContentMode={'always'}
/>
</View>
);
};
}
WebViewQuillViewer.propTypes = {
contentToDisplay: PropTypes.object,
backgroundColor: PropTypes.string
};
// Specifies the default values for props:
WebViewQuillViewer.defaultProps = {
theme: 'bubble'
};
const styles = StyleSheet.create({
activityOverlayStyle: {
...StyleSheet.absoluteFillObject,
marginHorizontal: 20,
marginVertical: 60,
display: 'flex',
justifyContent: 'center',
alignContent: 'center',
borderRadius: 5
},
activityIndicatorContainer: {
backgroundColor: 'white',
padding: 10,
borderRadius: 50,
alignSelf: 'center',
shadowColor: '#000000',
shadowOffset: {
width: 0,
height: 3
},
shadowRadius: 5,
shadowOpacity: 1.0
}
});