Drawio.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { debounce } from 'throttle-debounce';
  4. import { withTranslation } from 'react-i18next';
  5. import AppContainer from '../services/AppContainer';
  6. import { withUnstatedContainers } from './UnstatedUtils';
  7. import NotAvailableForGuest from './NotAvailableForGuest';
  8. class Drawio extends React.Component {
  9. constructor(props) {
  10. super(props);
  11. this.drawioContainer = React.createRef();
  12. this.style = {
  13. borderRadius: 3,
  14. border: '1px solid #d7d7d7',
  15. margin: '20px 0',
  16. };
  17. this.isPreview = this.props.isPreview;
  18. this.drawioContent = this.props.drawioContent;
  19. this.onEdit = this.onEdit.bind(this);
  20. // create debounced method for rendering Drawio
  21. this.renderDrawioWithDebounce = debounce(200, this.renderDrawio);
  22. }
  23. onEdit() {
  24. const { appContainer, rangeLineNumberOfMarkdown } = this.props;
  25. const { beginLineNumber, endLineNumber } = rangeLineNumberOfMarkdown;
  26. appContainer.launchDrawioModal('page', beginLineNumber, endLineNumber);
  27. }
  28. componentDidMount() {
  29. const DrawioViewer = window.GraphViewer;
  30. if (DrawioViewer != null) {
  31. this.renderDrawio();
  32. }
  33. else {
  34. this.renderDrawioWithDebounce();
  35. }
  36. }
  37. renderDrawio() {
  38. const DrawioViewer = window.GraphViewer;
  39. if (DrawioViewer != null) {
  40. const mxgraphs = this.drawioContainer.getElementsByClassName('mxgraph');
  41. if (mxgraphs.length > 0) {
  42. // GROWI では、mxgraph element は最初のものをレンダリングする前提とする
  43. const div = mxgraphs[0];
  44. if (div != null) {
  45. div.innerHTML = '';
  46. DrawioViewer.createViewerForElement(div);
  47. }
  48. }
  49. }
  50. else {
  51. this.renderDrawioWithDebounce();
  52. }
  53. }
  54. renderContents() {
  55. return this.drawioContent;
  56. }
  57. render() {
  58. return (
  59. <div className="editable-with-drawio position-relative">
  60. { !this.isPreview && (
  61. <NotAvailableForGuest>
  62. <button type="button" className="drawio-iframe-trigger position-absolute btn btn-outline-secondary" onClick={this.onEdit}>
  63. <i className="icon-note mr-1"></i>{this.props.t('Edit')}
  64. </button>
  65. </NotAvailableForGuest>
  66. ) }
  67. <div
  68. className="drawio"
  69. style={this.style}
  70. ref={(c) => { this.drawioContainer = c }}
  71. // eslint-disable-next-line react/no-danger
  72. dangerouslySetInnerHTML={{ __html: this.renderContents() }}
  73. >
  74. </div>
  75. </div>
  76. );
  77. }
  78. }
  79. Drawio.propTypes = {
  80. t: PropTypes.func.isRequired, // i18next
  81. appContainer: PropTypes.object.isRequired,
  82. drawioContent: PropTypes.any.isRequired,
  83. isPreview: PropTypes.bool,
  84. rangeLineNumberOfMarkdown: PropTypes.object.isRequired,
  85. };
  86. export default withTranslation()(withUnstatedContainers(Drawio, [AppContainer]));