Drawio.jsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.launchDrawioIFrame('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. DrawioViewer.processElements();
  28. }
  29. }
  30. renderContents() {
  31. return this.drawioContent;
  32. }
  33. render() {
  34. return (
  35. <div className="editable-with-drawio position-relative">
  36. { !this.isPreview
  37. && (
  38. <button type="button" className="drawio-iframe-trigger position-absolute btn" onClick={this.onEdit}>
  39. <i className="icon-note mr-1"></i>{this.props.t('Edit')}
  40. </button>
  41. )
  42. }
  43. <div
  44. className="drawio"
  45. style={this.style}
  46. ref={(c) => { this.drawioContainer = c }}
  47. onScroll={(event) => {
  48. event.preventDefault();
  49. }}
  50. dangerouslySetInnerHTML={{ __html: this.renderContents() }}
  51. >
  52. </div>
  53. </div>
  54. );
  55. }
  56. }
  57. Drawio.propTypes = {
  58. t: PropTypes.func.isRequired, // i18next
  59. appContainer: PropTypes.object.isRequired,
  60. drawioContent: PropTypes.any.isRequired,
  61. isPreview: PropTypes.bool,
  62. rangeLineNumberOfMarkdown: PropTypes.object.isRequired,
  63. };
  64. export default withTranslation()(Drawio);