PageEditorByHackmd.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import * as toastr from 'toastr';
  4. import HackmdEditor from './PageEditorByHackmd/HackmdEditor';
  5. export default class PageEditorByHackmd extends React.PureComponent {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. isInitializing: false,
  10. pageIdOnHackmd: this.props.pageIdOnHackmd,
  11. };
  12. this.getHackmdUri = this.getHackmdUri.bind(this);
  13. this.startIntegrationWithHackmd = this.startIntegrationWithHackmd.bind(this);
  14. this.apiErrorHandler = this.apiErrorHandler.bind(this);
  15. }
  16. componentWillMount() {
  17. }
  18. getHackmdUri() {
  19. const envVars = this.props.crowi.config.env;
  20. return envVars.HACKMD_URI;
  21. }
  22. syncToLatestRevision() {
  23. }
  24. /**
  25. * Start integration with HackMD
  26. */
  27. startIntegrationWithHackmd() {
  28. const hackmdUri = this.getHackmdUri();
  29. if (hackmdUri == null) {
  30. // do nothing
  31. return;
  32. }
  33. this.setState({isInitializing: true});
  34. const params = {
  35. pageId: this.props.pageId,
  36. };
  37. this.props.crowi.apiPost('/hackmd/integrate', params)
  38. .then(res => {
  39. if (!res.ok) {
  40. throw new Error(res.error);
  41. }
  42. this.setState({pageIdOnHackmd: res.pageIdOnHackmd});
  43. })
  44. .catch(this.apiErrorHandler)
  45. .then(() => {
  46. this.setState({isInitializing: false});
  47. });
  48. }
  49. apiErrorHandler(error) {
  50. toastr.error(error.message, 'Error occured', {
  51. closeButton: true,
  52. progressBar: true,
  53. newestOnTop: false,
  54. showDuration: '100',
  55. hideDuration: '100',
  56. timeOut: '3000',
  57. });
  58. }
  59. render() {
  60. const hackmdUri = this.getHackmdUri();
  61. if (hackmdUri == null || this.state.pageIdOnHackmd == null) {
  62. return (
  63. <div className="hackmd-nopage d-flex justify-content-center align-items-center">
  64. <div>
  65. <p className="text-center">
  66. <button className="btn btn-success btn-lg waves-effect waves-light" type="button"
  67. onClick={() => this.startIntegrationWithHackmd()} disabled={this.state.isInitializing}>
  68. <span className="btn-label"><i className="fa fa-file-text-o"></i></span>
  69. Start to edit with HackMD
  70. </button>
  71. </p>
  72. <p className="text-center">Clone this page and start to edit with multiple peoples.</p>
  73. </div>
  74. </div>
  75. );
  76. }
  77. return (
  78. <HackmdEditor
  79. markdown={this.props.markdown}
  80. hackmdUri={hackmdUri}
  81. pageIdOnHackmd={this.state.pageIdOnHackmd}
  82. >
  83. </HackmdEditor>
  84. );
  85. }
  86. }
  87. PageEditorByHackmd.propTypes = {
  88. crowi: PropTypes.object.isRequired,
  89. markdown: PropTypes.string.isRequired,
  90. pageId: PropTypes.string,
  91. revisionId: PropTypes.string,
  92. revisionIdHackmdSynced: PropTypes.string,
  93. pageIdOnHackmd: PropTypes.string,
  94. };