2
0

PageEditorByHackmd.jsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. isInitialized: false,
  10. isInitializing: false,
  11. pageIdOnHackmd: this.props.pageIdOnHackmd,
  12. };
  13. this.getHackmdUri = this.getHackmdUri.bind(this);
  14. this.startToEdit = this.startToEdit.bind(this);
  15. this.resumeToEdit = this.resumeToEdit.bind(this);
  16. this.hackmdEditorChangeHandler = this.hackmdEditorChangeHandler.bind(this);
  17. this.apiErrorHandler = this.apiErrorHandler.bind(this);
  18. }
  19. componentWillMount() {
  20. }
  21. getHackmdUri() {
  22. const envVars = this.props.crowi.config.env;
  23. return envVars.HACKMD_URI;
  24. }
  25. /**
  26. * Start integration with HackMD
  27. */
  28. startToEdit() {
  29. const hackmdUri = this.getHackmdUri();
  30. if (hackmdUri == null) {
  31. // do nothing
  32. return;
  33. }
  34. this.setState({
  35. isInitialized: false,
  36. isInitializing: true,
  37. });
  38. const params = {
  39. pageId: this.props.pageId,
  40. };
  41. this.props.crowi.apiPost('/hackmd.integrate', params)
  42. .then(res => {
  43. if (!res.ok) {
  44. throw new Error(res.error);
  45. }
  46. this.setState({
  47. isInitialized: true,
  48. pageIdOnHackmd: res.pageIdOnHackmd,
  49. revisionIdHackmdSynced: res.revisionIdHackmdSynced,
  50. });
  51. })
  52. .catch(this.apiErrorHandler)
  53. .then(() => {
  54. this.setState({isInitializing: false});
  55. });
  56. }
  57. /**
  58. * Start to edit w/o any api request
  59. */
  60. resumeToEdit() {
  61. this.setState({isInitialized: true});
  62. }
  63. /**
  64. * onChange event of HackmdEditor handler
  65. */
  66. hackmdEditorChangeHandler() {
  67. const hackmdUri = this.getHackmdUri();
  68. if (hackmdUri == null) {
  69. // do nothing
  70. return;
  71. }
  72. const params = {
  73. pageId: this.props.pageId,
  74. };
  75. this.props.crowi.apiPost('/hackmd.saveOnHackmd', params)
  76. .then(res => {
  77. // do nothing
  78. })
  79. .catch(err => {
  80. // do nothing
  81. });
  82. }
  83. apiErrorHandler(error) {
  84. toastr.error(error.message, 'Error occured', {
  85. closeButton: true,
  86. progressBar: true,
  87. newestOnTop: false,
  88. showDuration: '100',
  89. hideDuration: '100',
  90. timeOut: '3000',
  91. });
  92. }
  93. render() {
  94. const hackmdUri = this.getHackmdUri();
  95. if (this.state.isInitialized) {
  96. return (
  97. <HackmdEditor
  98. markdown={this.props.markdown}
  99. hackmdUri={hackmdUri}
  100. pageIdOnHackmd={this.state.pageIdOnHackmd}
  101. onChange={this.hackmdEditorChangeHandler}
  102. >
  103. </HackmdEditor>
  104. );
  105. }
  106. let content = undefined;
  107. const isPageExistsOnHackmd = (this.state.pageIdOnHackmd != null);
  108. const isRevisionMatch = (this.props.revisionId === this.props.revisionIdHackmdSynced);
  109. // HackMD is not setup
  110. if (hackmdUri == null) {
  111. content = (
  112. <div>
  113. <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is not set up.</p>
  114. </div>
  115. );
  116. }
  117. // page is exists, revisions are match, hasDraftOnHackmd is true
  118. else if (isPageExistsOnHackmd && isRevisionMatch && this.props.hasDraftOnHackmd) {
  119. content = (
  120. <div>
  121. <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is READY!</p>
  122. <p className="text-center">
  123. <button className="btn btn-success btn-lg waves-effect waves-light" type="button"
  124. onClick={() => this.resumeToEdit()}>
  125. <span className="btn-label"><i className="icon-control-end"></i></span>
  126. Resume to edit with HackMD
  127. </button>
  128. </p>
  129. <p className="text-center">Click to edit from the previous continuation.</p>
  130. </div>
  131. );
  132. }
  133. else {
  134. content = (
  135. <div>
  136. <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is READY!</p>
  137. <p className="text-center">
  138. <button className="btn btn-info btn-lg waves-effect waves-light" type="button"
  139. onClick={() => this.startToEdit()} disabled={this.state.isInitializing}>
  140. <span className="btn-label"><i className="icon-paper-plane"></i></span>
  141. Start to edit with HackMD
  142. </button>
  143. </p>
  144. <p className="text-center">Click to clone page content and start to edit.</p>
  145. </div>
  146. );
  147. }
  148. return (
  149. <div className="hackmd-preinit d-flex justify-content-center align-items-center">
  150. {content}
  151. </div>
  152. );
  153. }
  154. }
  155. PageEditorByHackmd.propTypes = {
  156. crowi: PropTypes.object.isRequired,
  157. markdown: PropTypes.string.isRequired,
  158. pageId: PropTypes.string,
  159. revisionId: PropTypes.string,
  160. pageIdOnHackmd: PropTypes.string,
  161. revisionIdHackmdSynced: PropTypes.string,
  162. hasDraftOnHackmd: PropTypes.bool,
  163. };