RevisionBody.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. const MathJax = window.MathJax;
  9. if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
  10. const intervalId = setInterval(() => {
  11. if (MathJax.isReady) {
  12. this.renderMathJax();
  13. clearInterval(intervalId);
  14. }
  15. }, 100);
  16. }
  17. }
  18. componentDidUpdate() {
  19. const MathJax = window.MathJax;
  20. if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxInRealtime) {
  21. this.renderMathJax();
  22. }
  23. }
  24. componentWillReceiveProps(nextProps) {
  25. const MathJax = window.MathJax;
  26. if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
  27. this.renderMathJax();
  28. }
  29. }
  30. renderMathJax() {
  31. const MathJax = window.MathJax;
  32. MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.element]);
  33. }
  34. generateInnerHtml(html) {
  35. return {__html: html};
  36. }
  37. render() {
  38. return (
  39. <div
  40. ref={(elm) => {
  41. this.element = elm;
  42. if (this.props.inputRef != null) {
  43. this.props.inputRef(elm);
  44. }
  45. }}
  46. className="wiki" dangerouslySetInnerHTML={this.generateInnerHtml(this.props.html)}>
  47. </div>
  48. )
  49. }
  50. }
  51. RevisionBody.propTypes = {
  52. html: PropTypes.string,
  53. inputRef: PropTypes.func, // for getting div element
  54. isMathJaxEnabled: PropTypes.bool,
  55. renderMathJaxOnInit: PropTypes.bool,
  56. renderMathJaxInRealtime: PropTypes.bool,
  57. };