Skip to content

Commit

Permalink
Merge pull request #13 from mrded/patch-2
Browse files Browse the repository at this point in the history
Replace react-native-autoheight-webview with WebView
  • Loading branch information
calvinkei authored Sep 24, 2018
2 parents a0e233c + 9f4e042 commit ee4d439
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5,403 deletions.
47 changes: 36 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import _ from 'lodash';
import AutoHeightWebView from 'react-native-autoheight-webview';

import { WebView } from 'react-native';

const defaultOptions = {
messageStyle: 'none',
Expand All @@ -17,31 +17,56 @@ const defaultOptions = {
};

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(
_.merge(defaultOptions, this.props.mathJaxOptions)
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() {
document.getElementById("formula").style.visibility = '';
});
var height = document.documentElement.scrollHeight;
window.postMessage(String(height));
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<div id="formula" style="visibility: hidden;">
${content}
</div>
${content}
`;
}
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 (
<AutoHeightWebView
source={{ html: this.wrapMathjax(this.props.html) }}
{...this.props}
<WebView
scrollEnabled={false}
onMessage={ this.handleMessage.bind(this) }
source={{ html: html }}
style={{ height: this.state.height, ...props.style }}

{...props}
/>
);
}
Expand Down
Loading

0 comments on commit ee4d439

Please sign in to comment.