2
0

Drawio.jsx 2.2 KB

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