RevisionBody.jsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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) {
  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={(elm) => {
  52. this.element = elm;
  53. if (this.props.inputRef != null) {
  54. this.props.inputRef(elm);
  55. }
  56. }}
  57. className={`wiki ${additionalClassName}`}
  58. // eslint-disable-next-line react/no-danger
  59. dangerouslySetInnerHTML={this.generateInnerHtml(this.props.html)}
  60. />
  61. );
  62. }
  63. }
  64. RevisionBody.propTypes = {
  65. html: PropTypes.string,
  66. inputRef: PropTypes.func, // for getting div element
  67. isMathJaxEnabled: PropTypes.bool,
  68. renderMathJaxOnInit: PropTypes.bool,
  69. renderMathJaxInRealtime: PropTypes.bool,
  70. additionalClassName: PropTypes.string,
  71. };