PageEditorByHackmd.jsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import SplitButton from 'react-bootstrap/es/SplitButton';
  4. import MenuItem from 'react-bootstrap/es/MenuItem';
  5. import * as toastr from 'toastr';
  6. import HackmdEditor from './PageEditorByHackmd/HackmdEditor';
  7. export default class PageEditorByHackmd extends React.PureComponent {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. markdown: this.props.markdown,
  12. isInitialized: false,
  13. isInitializing: false,
  14. initialRevisionId: this.props.revisionId,
  15. revisionId: this.props.revisionId,
  16. revisionIdHackmdSynced: this.props.revisionIdHackmdSynced,
  17. pageIdOnHackmd: this.props.pageIdOnHackmd,
  18. hasDraftOnHackmd: this.props.hasDraftOnHackmd,
  19. };
  20. this.getHackmdUri = this.getHackmdUri.bind(this);
  21. this.startToEdit = this.startToEdit.bind(this);
  22. this.resumeToEdit = this.resumeToEdit.bind(this);
  23. this.hackmdEditorChangeHandler = this.hackmdEditorChangeHandler.bind(this);
  24. this.apiErrorHandler = this.apiErrorHandler.bind(this);
  25. }
  26. componentWillMount() {
  27. }
  28. /**
  29. * return markdown document of HackMD
  30. * @return {Promise<string>}
  31. */
  32. getMarkdown() {
  33. if (!this.state.isInitialized) {
  34. return Promise.reject(new Error('HackmdEditor component has not initialized'));
  35. }
  36. return this.refs.hackmdEditor.getValue()
  37. .then(document => {
  38. this.setState({ markdown: document });
  39. return document;
  40. });
  41. }
  42. setMarkdown(markdown, updateEditorValue = true) {
  43. this.setState({ markdown });
  44. if (this.state.isInitialized && updateEditorValue) {
  45. this.refs.hackmdEditor.setValue(markdown);
  46. }
  47. }
  48. /**
  49. * update revisionId of state
  50. * @param {string} revisionId
  51. * @param {string} revisionIdHackmdSynced
  52. */
  53. setRevisionId(revisionId, revisionIdHackmdSynced) {
  54. this.setState({ revisionId, revisionIdHackmdSynced });
  55. }
  56. /**
  57. * update hasDraftOnHackmd of state
  58. * @param {bool} hasDraftOnHackmd
  59. */
  60. setHasDraftOnHackmd(hasDraftOnHackmd) {
  61. this.setState({ hasDraftOnHackmd });
  62. }
  63. getHackmdUri() {
  64. const envVars = this.props.crowi.config.env;
  65. return envVars.HACKMD_URI;
  66. }
  67. /**
  68. * Start integration with HackMD
  69. */
  70. startToEdit() {
  71. const hackmdUri = this.getHackmdUri();
  72. if (hackmdUri == null) {
  73. // do nothing
  74. return;
  75. }
  76. this.setState({
  77. isInitialized: false,
  78. isInitializing: true,
  79. });
  80. const params = {
  81. pageId: this.props.pageId,
  82. };
  83. this.props.crowi.apiPost('/hackmd.integrate', params)
  84. .then(res => {
  85. if (!res.ok) {
  86. throw new Error(res.error);
  87. }
  88. this.setState({
  89. isInitialized: true,
  90. pageIdOnHackmd: res.pageIdOnHackmd,
  91. revisionIdHackmdSynced: res.revisionIdHackmdSynced,
  92. });
  93. })
  94. .catch(this.apiErrorHandler)
  95. .then(() => {
  96. this.setState({isInitializing: false});
  97. });
  98. }
  99. /**
  100. * Start to edit w/o any api request
  101. */
  102. resumeToEdit() {
  103. this.setState({isInitialized: true});
  104. }
  105. /**
  106. * Reset draft
  107. */
  108. discardChanges() {
  109. this.setState({hasDraftOnHackmd: false});
  110. }
  111. /**
  112. * onChange event of HackmdEditor handler
  113. */
  114. hackmdEditorChangeHandler(body) {
  115. const hackmdUri = this.getHackmdUri();
  116. if (hackmdUri == null) {
  117. // do nothing
  118. return;
  119. }
  120. // do nothing if contents are same
  121. if (this.props.markdown === body) {
  122. return;
  123. }
  124. const params = {
  125. pageId: this.props.pageId,
  126. };
  127. this.props.crowi.apiPost('/hackmd.saveOnHackmd', params)
  128. .then(res => {
  129. // do nothing
  130. })
  131. .catch(err => {
  132. // do nothing
  133. });
  134. }
  135. apiErrorHandler(error) {
  136. toastr.error(error.message, 'Error occured', {
  137. closeButton: true,
  138. progressBar: true,
  139. newestOnTop: false,
  140. showDuration: '100',
  141. hideDuration: '100',
  142. timeOut: '3000',
  143. });
  144. }
  145. render() {
  146. const hackmdUri = this.getHackmdUri();
  147. const isPageExistsOnHackmd = (this.state.pageIdOnHackmd != null);
  148. const isResume = isPageExistsOnHackmd && this.state.hasDraftOnHackmd;
  149. if (this.state.isInitialized) {
  150. return (
  151. <HackmdEditor
  152. ref='hackmdEditor'
  153. hackmdUri={hackmdUri}
  154. pageIdOnHackmd={this.state.pageIdOnHackmd}
  155. initializationMarkdown={isResume ? null : this.state.markdown}
  156. onChange={this.hackmdEditorChangeHandler}
  157. onSaveWithShortcut={(document) => {
  158. this.props.onSaveWithShortcut(document);
  159. }}
  160. >
  161. </HackmdEditor>
  162. );
  163. }
  164. const isRevisionOutdated = this.state.initialRevisionId !== this.state.revisionId;
  165. const isHackmdDocumentOutdated = this.state.revisionId !== this.state.revisionIdHackmdSynced;
  166. let content = undefined;
  167. /*
  168. * HackMD is not setup
  169. */
  170. if (hackmdUri == null) {
  171. content = (
  172. <div>
  173. <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is not set up.</p>
  174. </div>
  175. );
  176. }
  177. /*
  178. * Resume to edit or discard changes
  179. */
  180. else if (isResume) {
  181. const revisionIdHackmdSynced = this.state.revisionIdHackmdSynced;
  182. const title = (
  183. <React.Fragment>
  184. <span className="btn-label"><i className="icon-control-end"></i></span>
  185. Resume to edit with HackMD
  186. </React.Fragment>);
  187. content = (
  188. <div>
  189. <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is READY!</p>
  190. <div className="text-center hackmd-resume-button-container mb-3">
  191. <SplitButton id='split-button-resume-hackmd' title={title} bsStyle="success" bsSize="large" disabled={isRevisionOutdated}
  192. className="btn-resume waves-effect waves-light" onClick={() => this.resumeToEdit()}>
  193. <MenuItem className="text-center" onClick={() => this.discardChanges()}>
  194. <i className="icon-control-rewind"></i> Discard changes
  195. </MenuItem>
  196. </SplitButton>
  197. </div>
  198. <p className="text-center">Click to edit from the previous continuation<br />or <span className="text-danger">Discard changes</span> from pull down.</p>
  199. { isHackmdDocumentOutdated &&
  200. <div className="panel panel-warning mt-5">
  201. <div className="panel-heading"><i className="icon-fw icon-info"></i> DRAFT MAY BE OUTDATED</div>
  202. <div className="panel-body text-center">
  203. The current draft on HackMD is based on&nbsp;
  204. <a href={`?revision=${revisionIdHackmdSynced}`}><span className="label label-default">{revisionIdHackmdSynced.substr(-8)}</span></a>.<br />
  205. <span className="text-danger">Discard it</span> to start to edit with current revision.
  206. </div>
  207. </div>
  208. }
  209. </div>
  210. );
  211. }
  212. /*
  213. * Start to edit
  214. */
  215. else {
  216. content = (
  217. <div>
  218. <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is READY!</p>
  219. <div className="text-center hackmd-start-button-container mb-3">
  220. <button className="btn btn-info btn-lg waves-effect waves-light" type="button" disabled={isRevisionOutdated || this.state.isInitializing}
  221. onClick={() => this.startToEdit()}>
  222. <span className="btn-label"><i className="icon-paper-plane"></i></span>
  223. Start to edit with HackMD
  224. </button>
  225. </div>
  226. <p className="text-center">Click to clone page content and start to edit.</p>
  227. </div>
  228. );
  229. }
  230. return (
  231. <div className="hackmd-preinit d-flex justify-content-center align-items-center">
  232. {content}
  233. </div>
  234. );
  235. }
  236. }
  237. PageEditorByHackmd.propTypes = {
  238. crowi: PropTypes.object.isRequired,
  239. markdown: PropTypes.string.isRequired,
  240. onSaveWithShortcut: PropTypes.func.isRequired,
  241. pageId: PropTypes.string,
  242. revisionId: PropTypes.string,
  243. pageIdOnHackmd: PropTypes.string,
  244. revisionIdHackmdSynced: PropTypes.string,
  245. hasDraftOnHackmd: PropTypes.bool,
  246. };