2017-11-03 02:09:34 +02:00
|
|
|
const React = require('react'); const Component = React.Component;
|
2017-11-19 17:19:36 +02:00
|
|
|
const { Platform, WebView, View, Linking } = require('react-native');
|
2017-11-03 02:09:34 +02:00
|
|
|
const { globalStyle } = require('lib/components/global-style.js');
|
|
|
|
const { Resource } = require('lib/models/resource.js');
|
|
|
|
const { reg } = require('lib/registry.js');
|
2017-11-07 20:39:11 +02:00
|
|
|
const MdToHtml = require('lib/MdToHtml.js');
|
2017-07-30 21:51:18 +02:00
|
|
|
|
|
|
|
class NoteBodyViewer extends Component {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
resources: {},
|
2017-08-21 22:46:31 +02:00
|
|
|
webViewLoaded: false,
|
2017-07-30 21:51:18 +02:00
|
|
|
}
|
2017-08-21 22:46:31 +02:00
|
|
|
|
|
|
|
this.isMounted_ = false;
|
2017-07-30 21:51:18 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 22:46:31 +02:00
|
|
|
componentWillMount() {
|
2017-11-12 18:33:34 +02:00
|
|
|
this.mdToHtml_ = new MdToHtml({ supportsResourceLinks: false });
|
2017-08-21 22:46:31 +02:00
|
|
|
this.isMounted_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2017-11-05 17:35:38 +02:00
|
|
|
this.mdToHtml_ = null;
|
2017-08-21 22:46:31 +02:00
|
|
|
this.isMounted_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onLoadEnd() {
|
|
|
|
if (this.state.webViewLoaded) return;
|
|
|
|
|
|
|
|
// Need to display after a delay to avoid a white flash before
|
|
|
|
// the content is displayed.
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!this.isMounted_) return;
|
|
|
|
this.setState({ webViewLoaded: true });
|
2017-09-10 18:57:06 +02:00
|
|
|
}, 100);
|
2017-08-21 22:46:31 +02:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:51:18 +02:00
|
|
|
render() {
|
|
|
|
const note = this.props.note;
|
|
|
|
const style = this.props.style;
|
|
|
|
const onCheckboxChange = this.props.onCheckboxChange;
|
2017-11-05 17:35:38 +02:00
|
|
|
|
|
|
|
const mdOptions = {
|
2017-11-06 20:05:12 +02:00
|
|
|
onResourceLoaded: () => {
|
|
|
|
this.forceUpdate();
|
2017-11-05 17:35:38 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const html = this.mdToHtml_.render(note ? note.body : '', this.props.webViewStyle, mdOptions);
|
2017-08-21 22:46:31 +02:00
|
|
|
|
|
|
|
let webViewStyle = {}
|
2017-11-19 17:19:36 +02:00
|
|
|
// On iOS, the onLoadEnd() event is never fired so always
|
|
|
|
// display the webview (don't do the little trick
|
|
|
|
// to avoid the white flash).
|
|
|
|
if (Platform.OS !== 'ios') {
|
|
|
|
webViewStyle.opacity = this.state.webViewLoaded ? 1 : 0.01;
|
|
|
|
}
|
2017-07-30 21:51:18 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={style}>
|
|
|
|
<WebView
|
2017-11-19 17:19:36 +02:00
|
|
|
scalesPageToFit={false}
|
2017-08-21 22:46:31 +02:00
|
|
|
style={webViewStyle}
|
|
|
|
source={{ html: html }}
|
|
|
|
onLoadEnd={() => this.onLoadEnd()}
|
2017-10-30 20:27:51 +02:00
|
|
|
onError={(e) => reg.logger().error('WebView error', e) }
|
2017-07-30 21:51:18 +02:00
|
|
|
onMessage={(event) => {
|
|
|
|
let msg = event.nativeEvent.data;
|
|
|
|
|
|
|
|
if (msg.indexOf('checkboxclick:') === 0) {
|
2017-11-05 17:35:38 +02:00
|
|
|
const newBody = this.mdToHtml_.handleCheckboxClick(msg, note.body);
|
2017-07-30 21:51:18 +02:00
|
|
|
if (onCheckboxChange) onCheckboxChange(newBody);
|
|
|
|
} else if (msg.indexOf('bodyscroll:') === 0) {
|
2017-11-05 17:35:38 +02:00
|
|
|
//msg = msg.split(':');
|
|
|
|
//this.bodyScrollTop_ = Number(msg[1]);
|
2017-07-30 21:51:18 +02:00
|
|
|
} else {
|
|
|
|
Linking.openURL(msg);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-03 02:13:17 +02:00
|
|
|
module.exports = { NoteBodyViewer };
|