Drawio.jsx 2.4 KB

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