RevisionBody.js 1.7 KB

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