RevisionBody.jsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // create debounced method for rendering draw.io
  10. this.renderDrawioWithDebounce = debounce(200, this.renderDrawio);
  11. }
  12. componentDidMount() {
  13. const MathJax = window.MathJax;
  14. if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
  15. this.renderMathJaxWithDebounce();
  16. }
  17. const DrawioViewer = window.GraphViewer;
  18. if (DrawioViewer != null) {
  19. this.renderDrawioWithDebounce();
  20. }
  21. }
  22. componentDidUpdate() {
  23. const MathJax = window.MathJax;
  24. if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxInRealtime) {
  25. this.renderMathJaxWithDebounce();
  26. }
  27. const DrawioViewer = window.GraphViewer;
  28. if (DrawioViewer != null) {
  29. this.renderDrawioWithDebounce();
  30. }
  31. }
  32. componentWillReceiveProps(nextProps) {
  33. const MathJax = window.MathJax;
  34. if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
  35. this.renderMathJaxWithDebounce();
  36. }
  37. const DrawioViewer = window.GraphViewer;
  38. if (DrawioViewer != null) {
  39. this.renderDrawioWithDebounce();
  40. }
  41. }
  42. renderMathJax() {
  43. const MathJax = window.MathJax;
  44. MathJax.Hub.Queue(['Typeset', MathJax.Hub, this.element]);
  45. }
  46. renderDrawio() {
  47. const DrawioViewer = window.GraphViewer;
  48. DrawioViewer.processElements();
  49. }
  50. generateInnerHtml(html) {
  51. return { __html: html };
  52. }
  53. render() {
  54. const additionalClassName = this.props.additionalClassName || '';
  55. return (
  56. <div
  57. ref={(elm) => {
  58. this.element = elm;
  59. if (this.props.inputRef != null) {
  60. this.props.inputRef(elm);
  61. }
  62. }}
  63. className={`wiki ${additionalClassName}`}
  64. // eslint-disable-next-line react/no-danger
  65. dangerouslySetInnerHTML={this.generateInnerHtml(this.props.html)}
  66. />
  67. );
  68. }
  69. }
  70. RevisionBody.propTypes = {
  71. html: PropTypes.string,
  72. inputRef: PropTypes.func, // for getting div element
  73. isMathJaxEnabled: PropTypes.bool,
  74. renderMathJaxOnInit: PropTypes.bool,
  75. renderMathJaxInRealtime: PropTypes.bool,
  76. additionalClassName: PropTypes.string,
  77. };