RevisionBody.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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}`} dangerouslySetInnerHTML={this.generateInnerHtml(this.props.html)}>
  46. </div>
  47. );
  48. }
  49. }
  50. RevisionBody.propTypes = {
  51. html: PropTypes.string,
  52. inputRef: PropTypes.func, // for getting div element
  53. isMathJaxEnabled: PropTypes.bool,
  54. renderMathJaxOnInit: PropTypes.bool,
  55. renderMathJaxInRealtime: PropTypes.bool,
  56. additionalClassName: PropTypes.string,
  57. };