RevisionBody.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. export default class RevisionBody extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. }
  7. componentDidMount() {
  8. if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
  9. const intervalId = setInterval(() => {
  10. if (MathJax.isReady) {
  11. this.renderMathJax();
  12. clearInterval(intervalId);
  13. }
  14. }, 100);
  15. }
  16. }
  17. componentDidUpdate() {
  18. if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxInRealtime) {
  19. this.renderMathJax();
  20. }
  21. }
  22. componentWillReceiveProps(nextProps) {
  23. if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
  24. this.renderMathJax();
  25. }
  26. }
  27. renderMathJax() {
  28. MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.element]);
  29. }
  30. generateInnerHtml(html) {
  31. return {__html: html};
  32. }
  33. render() {
  34. return (
  35. <div
  36. ref={(elm) => {
  37. this.element = elm;
  38. if (this.props.inputRef != null) {
  39. this.props.inputRef(elm);
  40. }
  41. }}
  42. className="wiki" dangerouslySetInnerHTML={this.generateInnerHtml(this.props.html)}>
  43. </div>
  44. )
  45. }
  46. }
  47. RevisionBody.propTypes = {
  48. html: PropTypes.string,
  49. inputRef: PropTypes.func, // for getting div element
  50. isMathJaxEnabled: PropTypes.bool,
  51. renderMathJaxOnInit: PropTypes.bool,
  52. renderMathJaxInRealtime: PropTypes.bool,
  53. };