PageEditorByHackmd.jsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. * reset initialized status
  50. */
  51. reset() {
  52. this.setState({ isInitialized: false });
  53. }
  54. /**
  55. * clear revision status (invoked when page is updated by myself)
  56. */
  57. clearRevisionStatus(updatedRevisionId, updatedRevisionIdHackmdSynced) {
  58. this.setState({
  59. initialRevisionId: updatedRevisionId,
  60. revisionId: updatedRevisionId,
  61. revisionIdHackmdSynced: updatedRevisionIdHackmdSynced,
  62. isDraftUpdatingInRealtime: false,
  63. });
  64. }
  65. /**
  66. * update revisionId of state
  67. * @param {string} revisionId
  68. * @param {string} revisionIdHackmdSynced
  69. */
  70. setRevisionId(revisionId, revisionIdHackmdSynced) {
  71. this.setState({ revisionId, revisionIdHackmdSynced });
  72. }
  73. getRevisionIdHackmdSynced() {
  74. return this.state.revisionIdHackmdSynced;
  75. }
  76. /**
  77. * update hasDraftOnHackmd of state
  78. * @param {bool} hasDraftOnHackmd
  79. */
  80. setHasDraftOnHackmd(hasDraftOnHackmd) {
  81. this.setState({ hasDraftOnHackmd });
  82. }
  83. getHackmdUri() {
  84. const envVars = this.props.crowi.config.env;
  85. return envVars.HACKMD_URI;
  86. }
  87. /**
  88. * Start integration with HackMD
  89. */
  90. startToEdit() {
  91. const hackmdUri = this.getHackmdUri();
  92. if (hackmdUri == null) {
  93. // do nothing
  94. return;
  95. }
  96. this.setState({
  97. isInitialized: false,
  98. isInitializing: true,
  99. });
  100. const params = {
  101. pageId: this.props.pageId,
  102. };
  103. this.props.crowi.apiPost('/hackmd.integrate', params)
  104. .then(res => {
  105. if (!res.ok) {
  106. throw new Error(res.error);
  107. }
  108. this.setState({
  109. isInitialized: true,
  110. pageIdOnHackmd: res.pageIdOnHackmd,
  111. revisionIdHackmdSynced: res.revisionIdHackmdSynced,
  112. });
  113. })
  114. .catch(this.apiErrorHandler)
  115. .then(() => {
  116. this.setState({isInitializing: false});
  117. });
  118. }
  119. /**
  120. * Start to edit w/o any api request
  121. */
  122. resumeToEdit() {
  123. this.setState({isInitialized: true});
  124. }
  125. /**
  126. * Reset draft
  127. */
  128. discardChanges() {
  129. this.setState({hasDraftOnHackmd: false});
  130. }
  131. /**
  132. * onChange event of HackmdEditor handler
  133. */
  134. hackmdEditorChangeHandler(body) {
  135. const hackmdUri = this.getHackmdUri();
  136. if (hackmdUri == null) {
  137. // do nothing
  138. return;
  139. }
  140. // do nothing if contents are same
  141. if (this.props.markdown === body) {
  142. return;
  143. }
  144. const params = {
  145. pageId: this.props.pageId,
  146. };
  147. this.props.crowi.apiPost('/hackmd.saveOnHackmd', params)
  148. .then(res => {
  149. // do nothing
  150. })
  151. .catch(err => {
  152. // do nothing
  153. });
  154. }
  155. apiErrorHandler(error) {
  156. toastr.error(error.message, 'Error occured', {
  157. closeButton: true,
  158. progressBar: true,
  159. newestOnTop: false,
  160. showDuration: '100',
  161. hideDuration: '100',
  162. timeOut: '3000',
  163. });
  164. }
  165. render() {
  166. const hackmdUri = this.getHackmdUri();
  167. const isPageExistsOnHackmd = (this.state.pageIdOnHackmd != null);
  168. const isResume = isPageExistsOnHackmd && this.state.hasDraftOnHackmd;
  169. if (this.state.isInitialized) {
  170. return (
  171. <HackmdEditor
  172. ref='hackmdEditor'
  173. hackmdUri={hackmdUri}
  174. pageIdOnHackmd={this.state.pageIdOnHackmd}
  175. initializationMarkdown={isResume ? null : this.state.markdown}
  176. onChange={this.hackmdEditorChangeHandler}
  177. onSaveWithShortcut={(document) => {
  178. this.props.onSaveWithShortcut(document);
  179. }}
  180. >
  181. </HackmdEditor>
  182. );
  183. }
  184. const isRevisionOutdated = this.state.initialRevisionId !== this.state.revisionId;
  185. const isHackmdDocumentOutdated = this.state.revisionId !== this.state.revisionIdHackmdSynced;
  186. let content = undefined;
  187. /*
  188. * HackMD is not setup
  189. */
  190. if (hackmdUri == null) {
  191. content = (
  192. <div>
  193. <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is not set up.</p>
  194. </div>
  195. );
  196. }
  197. /*
  198. * Resume to edit or discard changes
  199. */
  200. else if (isResume) {
  201. const revisionIdHackmdSynced = this.state.revisionIdHackmdSynced;
  202. const title = (
  203. <React.Fragment>
  204. <span className="btn-label"><i className="icon-control-end"></i></span>
  205. Resume to edit with HackMD
  206. </React.Fragment>);
  207. content = (
  208. <div>
  209. <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is READY!</p>
  210. <div className="text-center hackmd-resume-button-container mb-3">
  211. <SplitButton id='split-button-resume-hackmd' title={title} bsStyle="success" bsSize="large"
  212. className="btn-resume waves-effect waves-light" onClick={() => this.resumeToEdit()}>
  213. <MenuItem className="text-center" onClick={() => this.discardChanges()}>
  214. <i className="icon-control-rewind"></i> Discard changes
  215. </MenuItem>
  216. </SplitButton>
  217. </div>
  218. <p className="text-center">Click to edit from the previous continuation<br />or <span className="text-danger">Discard changes</span> from pull down.</p>
  219. { isHackmdDocumentOutdated &&
  220. <div className="panel panel-warning mt-5">
  221. <div className="panel-heading"><i className="icon-fw icon-info"></i> DRAFT MAY BE OUTDATED</div>
  222. <div className="panel-body text-center">
  223. The current draft on HackMD is based on&nbsp;
  224. <a href={`?revision=${revisionIdHackmdSynced}`}><span className="label label-default">{revisionIdHackmdSynced.substr(-8)}</span></a>.<br />
  225. <span className="text-danger">Discard it</span> to start to edit with current revision.
  226. </div>
  227. </div>
  228. }
  229. </div>
  230. );
  231. }
  232. /*
  233. * Start to edit
  234. */
  235. else {
  236. content = (
  237. <div>
  238. <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is READY!</p>
  239. <div className="text-center hackmd-start-button-container mb-3">
  240. <button className="btn btn-info btn-lg waves-effect waves-light" type="button" disabled={isRevisionOutdated || this.state.isInitializing}
  241. onClick={() => this.startToEdit()}>
  242. <span className="btn-label"><i className="icon-paper-plane"></i></span>
  243. Start to edit with HackMD
  244. </button>
  245. </div>
  246. <p className="text-center">Click to clone page content and start to edit.</p>
  247. </div>
  248. );
  249. }
  250. return (
  251. <div className="hackmd-preinit d-flex justify-content-center align-items-center">
  252. {content}
  253. </div>
  254. );
  255. }
  256. }
  257. PageEditorByHackmd.propTypes = {
  258. crowi: PropTypes.object.isRequired,
  259. markdown: PropTypes.string.isRequired,
  260. onSaveWithShortcut: PropTypes.func.isRequired,
  261. pageId: PropTypes.string,
  262. revisionId: PropTypes.string,
  263. pageIdOnHackmd: PropTypes.string,
  264. revisionIdHackmdSynced: PropTypes.string,
  265. hasDraftOnHackmd: PropTypes.bool,
  266. };