-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
75 lines (65 loc) · 1.8 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
import React from 'react';
import { View } from 'react-native';
import { WebView } from 'react-native-webview';
const defaultOptions = {
messageStyle: 'none',
extensions: ['tex2jax.js'],
jax: ['input/TeX', 'output/HTML-CSS'],
tex2jax: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']],
processEscapes: true,
},
TeX: {
extensions: ['AMSmath.js', 'AMSsymbols.js', 'noErrors.js', 'noUndefined.js']
}
};
class MathJax extends React.Component {
constructor(props) {
super(props);
this.state = {
height: 1
};
}
handleMessage(message) {
this.setState({
height: Number(message.nativeEvent.data)
});
}
wrapMathjax(content) {
const options = JSON.stringify(
Object.assign({}, defaultOptions, this.props.mathJaxOptions)
);
return `
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script type="text/x-mathjax-config">
MathJax.Hub.Config(${options});
MathJax.Hub.Queue(function() {
var height = document.documentElement.scrollHeight;
window.ReactNativeWebView.postMessage(String(height));
document.getElementById("formula").style.visibility = '';
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<div id="formula" style="visibility: hidden;">
${content}
</div>
`;
}
render() {
const html = this.wrapMathjax(this.props.html);
// Create new props without `props.html` field. Since it's deprecated.
const props = Object.assign({}, this.props, { html: undefined });
return (
<View style={{ height: this.state.height, ...props.style }}>
<WebView
scrollEnabled={false}
onMessage={this.handleMessage.bind(this)}
source={{ html }}
{...props}
/>
</View>
);
}
}
export default MathJax;