Drawio.jsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if (window.crowi != null) {
  19. window.crowi.launchDrawioModal('page',
  20. this.props.rangeLineNumberOfMarkdown.beginLineNumber,
  21. this.props.rangeLineNumberOfMarkdown.endLineNumber);
  22. }
  23. }
  24. componentDidMount() {
  25. const DrawioViewer = window.GraphViewer;
  26. if (DrawioViewer != null) {
  27. const mxgraphs = this.drawioContainer.getElementsByClassName('mxgraph');
  28. if (mxgraphs.length > 0) {
  29. // GROWI では、mxgraph element は最初のものをレンダリングする前提とする
  30. const div = mxgraphs[0];
  31. if (div != null) {
  32. div.innerHTML = '';
  33. DrawioViewer.createViewerForElement(div);
  34. }
  35. }
  36. }
  37. }
  38. renderContents() {
  39. return this.drawioContent;
  40. }
  41. render() {
  42. return (
  43. <div className="editable-with-drawio position-relative">
  44. { !this.isPreview
  45. && (
  46. <button type="button" className="drawio-iframe-trigger position-absolute btn btn-outline-secondary" onClick={this.onEdit}>
  47. <i className="icon-note mr-1"></i>{this.props.t('Edit')}
  48. </button>
  49. )
  50. }
  51. <div
  52. className="drawio"
  53. style={this.style}
  54. ref={(c) => { this.drawioContainer = c }}
  55. // eslint-disable-next-line react/no-danger
  56. dangerouslySetInnerHTML={{ __html: this.renderContents() }}
  57. >
  58. </div>
  59. </div>
  60. );
  61. }
  62. }
  63. Drawio.propTypes = {
  64. t: PropTypes.func.isRequired, // i18next
  65. appContainer: PropTypes.object.isRequired,
  66. drawioContent: PropTypes.any.isRequired,
  67. isPreview: PropTypes.bool,
  68. rangeLineNumberOfMarkdown: PropTypes.object.isRequired,
  69. };
  70. export default withTranslation()(Drawio);