Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace react-native-autoheight-webview with WebView #13

Merged
merged 5 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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