PageEditor.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import * as toastr from 'toastr';
  4. import {debounce} from 'throttle-debounce';
  5. import Editor from './PageEditor/Editor';
  6. import Preview from './PageEditor/Preview';
  7. export default class PageEditor extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. revisionId: this.props.revisionId,
  12. markdown: this.props.markdown,
  13. };
  14. this.setCaretLine = this.setCaretLine.bind(this);
  15. this.focusToEditor = this.focusToEditor.bind(this);
  16. this.onMarkdownChanged = this.onMarkdownChanged.bind(this);
  17. this.onSave = this.onSave.bind(this);
  18. this.onUpload = this.onUpload.bind(this);
  19. this.onEditorScroll = this.onEditorScroll.bind(this);
  20. this.getMaxScrollTop = this.getMaxScrollTop.bind(this);
  21. this.getScrollTop = this.getScrollTop.bind(this);
  22. this.saveDraft = this.saveDraft.bind(this);
  23. this.clearDraft = this.clearDraft.bind(this);
  24. // create debounced function
  25. this.saveDraftWithDebounce = debounce(300, this.saveDraft);
  26. }
  27. componentWillMount() {
  28. // restore draft
  29. this.restoreDraft();
  30. // initial preview
  31. this.renderPreview();
  32. }
  33. focusToEditor() {
  34. this.refs.editor.forceToFocus();
  35. }
  36. /**
  37. * set caret position of editor
  38. * @param {number} line
  39. */
  40. setCaretLine(line) {
  41. this.refs.editor.setCaretLine(line);
  42. }
  43. /**
  44. * the change event handler for `markdown` state
  45. * @param {string} value
  46. */
  47. onMarkdownChanged(value) {
  48. this.setState({
  49. markdown: value,
  50. });
  51. this.renderPreview();
  52. this.saveDraftWithDebounce()
  53. }
  54. /**
  55. * the save event handler
  56. */
  57. onSave() {
  58. let endpoint;
  59. let data;
  60. // update
  61. if (this.props.pageId != null) {
  62. endpoint = '/pages.update';
  63. data = {
  64. page_id: this.props.pageId,
  65. revision_id: this.state.revisionId,
  66. body: this.state.markdown,
  67. };
  68. }
  69. // create
  70. else {
  71. endpoint = '/pages.create';
  72. data = {
  73. path: this.props.pagePath,
  74. body: this.state.markdown,
  75. };
  76. }
  77. this.props.crowi.apiPost(endpoint, data)
  78. .then((res) => {
  79. const page = res.page;
  80. toastr.success(undefined, 'Saved successfully', {
  81. closeButton: true,
  82. progressBar: true,
  83. newestOnTop: false,
  84. showDuration: "100",
  85. hideDuration: "100",
  86. timeOut: "1200",
  87. extendedTimeOut: "150",
  88. });
  89. // update states
  90. this.setState({
  91. revisionId: page.revision._id,
  92. markdown: page.revision.body
  93. })
  94. // clear draft
  95. this.clearDraft();
  96. // dispatch onSaveSuccess
  97. this.dispatchSaveSuccess(page);
  98. })
  99. .catch((error) => {
  100. console.error(error);
  101. toastr.error(error.message, 'Error occured on saveing', {
  102. closeButton: true,
  103. progressBar: true,
  104. newestOnTop: false,
  105. showDuration: "100",
  106. hideDuration: "100",
  107. timeOut: "3000",
  108. });
  109. });
  110. }
  111. /**
  112. * the upload event handler
  113. * @param {any} files
  114. */
  115. onUpload(files) {
  116. console.log(files);
  117. }
  118. /**
  119. * the scroll event handler from codemirror
  120. * @param {any} data {left, top, width, height, clientWidth, clientHeight} object that represents the current scroll position, the size of the scrollable area, and the size of the visible area (minus scrollbars).
  121. * see https://codemirror.net/doc/manual.html#events
  122. */
  123. onEditorScroll(data) {
  124. const rate = data.top / (data.height - data.clientHeight)
  125. const top = this.getScrollTop(this.previewElement, rate);
  126. this.previewElement.scrollTop = top;
  127. }
  128. /**
  129. * transplanted from crowi-form.js -- 2018.01.21 Yuki Takei
  130. * @param {*} dom
  131. */
  132. getMaxScrollTop(dom) {
  133. var rect = dom.getBoundingClientRect();
  134. return dom.scrollHeight - rect.height;
  135. };
  136. /**
  137. * transplanted from crowi-form.js -- 2018.01.21 Yuki Takei
  138. * @param {*} dom
  139. */
  140. getScrollTop(dom, rate) {
  141. var maxScrollTop = this.getMaxScrollTop(dom);
  142. var top = maxScrollTop * rate;
  143. return top;
  144. };
  145. restoreDraft() {
  146. // restore draft when the first time to edit
  147. const draft = this.props.crowi.findDraft(this.props.pagePath);
  148. if (!this.props.revisionId && draft != null) {
  149. this.setState({markdown: draft});
  150. }
  151. }
  152. saveDraft() {
  153. // only when the first time to edit
  154. if (!this.state.revisionId) {
  155. this.props.crowi.saveDraft(this.props.pagePath, this.state.markdown);
  156. }
  157. }
  158. clearDraft() {
  159. this.props.crowi.clearDraft(this.props.pagePath);
  160. }
  161. /**
  162. * dispatch onSaveSuccess event
  163. */
  164. dispatchSaveSuccess(page) {
  165. if (this.props.onSaveSuccess != null) {
  166. this.props.onSaveSuccess(page);
  167. }
  168. }
  169. renderPreview() {
  170. const config = this.props.crowi.config;
  171. // generate options obj
  172. const rendererOptions = {
  173. // see: https://www.npmjs.com/package/marked
  174. marked: {
  175. breaks: config.isEnabledLineBreaks,
  176. }
  177. };
  178. // render html
  179. var context = {
  180. markdown: this.state.markdown,
  181. dom: this.previewElement,
  182. currentPagePath: decodeURIComponent(location.pathname)
  183. };
  184. this.props.crowi.interceptorManager.process('preRenderPreview', context)
  185. .then(() => crowi.interceptorManager.process('prePreProcess', context))
  186. .then(() => {
  187. context.markdown = crowiRenderer.preProcess(context.markdown, context.dom);
  188. })
  189. .then(() => crowi.interceptorManager.process('postPreProcess', context))
  190. .then(() => {
  191. var parsedHTML = crowiRenderer.render(context.markdown, context.dom, rendererOptions);
  192. context['parsedHTML'] = parsedHTML;
  193. })
  194. .then(() => crowi.interceptorManager.process('postRenderPreview', context))
  195. .then(() => crowi.interceptorManager.process('preRenderPreviewHtml', context))
  196. .then(() => {
  197. this.setState({html: context.parsedHTML});
  198. // set html to the hidden input (for submitting to save)
  199. $('#form-body').val(this.state.markdown);
  200. })
  201. // process interceptors for post rendering
  202. .then(() => crowi.interceptorManager.process('postRenderPreviewHtml', context));
  203. }
  204. render() {
  205. return (
  206. <div className="row">
  207. <div className="col-md-6 col-sm-12 page-editor-editor-container">
  208. <Editor ref="editor" value={this.state.markdown}
  209. onScroll={this.onEditorScroll}
  210. onChange={this.onMarkdownChanged}
  211. onSave={this.onSave}
  212. onUpload={this.onUpload}
  213. />
  214. </div>
  215. <div className="col-md-6 hidden-sm hidden-xs page-editor-preview-container">
  216. <Preview html={this.state.html} inputRef={el => this.previewElement = el} />
  217. </div>
  218. </div>
  219. )
  220. }
  221. }
  222. PageEditor.propTypes = {
  223. crowi: PropTypes.object.isRequired,
  224. markdown: PropTypes.string.isRequired,
  225. pageId: PropTypes.string,
  226. revisionId: PropTypes.string,
  227. pagePath: PropTypes.string,
  228. onSaveSuccess: PropTypes.func,
  229. };