RevisionBody.jsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. const additionalClassName = this.props.additionalClassName || '';
  37. return (
  38. <div
  39. ref={(elm) => {
  40. this.element = elm;
  41. if (this.props.inputRef != null) {
  42. this.props.inputRef(elm);
  43. }
  44. }}
  45. className={`wiki ${additionalClassName}`}
  46. dangerouslySetInnerHTML={this.generateInnerHtml(this.props.html)}
  47. />
  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. additionalClassName: PropTypes.string,
  58. };