Drawio.jsx 2.3 KB

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