Preview.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { PreviewOptions } from './OptionsSelector';
  4. export default class Preview extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. }
  8. componentDidMount() {
  9. if (this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
  10. this.renderMathJax();
  11. }
  12. }
  13. componentDidUpdate() {
  14. const opts = this.props.previewOptions;
  15. if (this.props.isMathJaxEnabled && opts != null && opts.renderMathJaxInRealtime) {
  16. this.renderMathJax();
  17. }
  18. }
  19. renderMathJax() {
  20. const MathJax = window.MathJax;
  21. if (MathJax != null) {
  22. MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.element]);
  23. }
  24. }
  25. generateInnerHtml(html) {
  26. return {__html: html};
  27. }
  28. render() {
  29. return (
  30. <div
  31. ref={(elm) => {
  32. this.element = elm;
  33. this.props.inputRef(elm);
  34. }}
  35. className="wiki page-editor-preview-body" dangerouslySetInnerHTML={this.generateInnerHtml(this.props.html)}>
  36. </div>
  37. )
  38. }
  39. }
  40. Preview.propTypes = {
  41. html: PropTypes.string,
  42. inputRef: PropTypes.func.isRequired, // for getting div element
  43. isMathJaxEnabled: PropTypes.bool,
  44. renderMathJaxOnInit: PropTypes.bool,
  45. previewOptions: PropTypes.instanceOf(PreviewOptions),
  46. };