RevisionBody.jsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { debounce } from 'throttle-debounce';
  4. export default class RevisionBody extends React.PureComponent {
  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. // Workaround MathJax Rendering (Errors still occur, but MathJax can be rendered)
  31. //
  32. // Reason:
  33. // Addition of draw.io Integration causes initialization conflict between MathJax of draw.io and MathJax of GROWI.
  34. // So, before MathJax is initialized, execute renderMathJaxWithDebounce again.
  35. // Avoiding initialization of MathJax of draw.io solves the problem.
  36. // refs: https://github.com/jgraph/drawio/pull/831
  37. if (MathJax != null && this.element != null) {
  38. MathJax.typesetPromise([this.element]);
  39. }
  40. else {
  41. this.renderMathJaxWithDebounce();
  42. }
  43. }
  44. generateInnerHtml(html) {
  45. return { __html: html };
  46. }
  47. render() {
  48. const additionalClassName = this.props.additionalClassName || '';
  49. return (
  50. <div
  51. ref={this.props.inputRef}
  52. id="wiki"
  53. className={`wiki ${additionalClassName}`}
  54. // eslint-disable-next-line react/no-danger
  55. dangerouslySetInnerHTML={this.generateInnerHtml(this.props.html)}
  56. />
  57. );
  58. }
  59. }
  60. RevisionBody.propTypes = {
  61. html: PropTypes.string,
  62. inputRef: PropTypes.shape({ current: PropTypes.instanceOf(Element) }),
  63. isMathJaxEnabled: PropTypes.bool,
  64. renderMathJaxOnInit: PropTypes.bool,
  65. renderMathJaxInRealtime: PropTypes.bool,
  66. additionalClassName: PropTypes.string,
  67. };