Editor.jsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import AbstractEditor from './AbstractEditor';
  4. import CodeMirrorEditor from './CodeMirrorEditor';
  5. import TextAreaEditor from './TextAreaEditor';
  6. import Dropzone from 'react-dropzone';
  7. import pasteHelper from './PasteHelper';
  8. export default class Editor extends AbstractEditor {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. isComponentDidMount: false,
  13. dropzoneActive: false,
  14. isUploading: false,
  15. };
  16. this.getEditorSubstance = this.getEditorSubstance.bind(this);
  17. this.pasteFilesHandler = this.pasteFilesHandler.bind(this);
  18. this.dragEnterHandler = this.dragEnterHandler.bind(this);
  19. this.dragLeaveHandler = this.dragLeaveHandler.bind(this);
  20. this.dropHandler = this.dropHandler.bind(this);
  21. this.getDropzoneAccept = this.getDropzoneAccept.bind(this);
  22. this.getDropzoneClassName = this.getDropzoneClassName.bind(this);
  23. this.renderDropzoneOverlay = this.renderDropzoneOverlay.bind(this);
  24. }
  25. componentDidMount() {
  26. this.setState({ isComponentDidMount: true });
  27. }
  28. getEditorSubstance() {
  29. return this.props.isMobile
  30. ? this.refs.taEditor
  31. : this.refs.cmEditor;
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. forceToFocus() {
  37. this.getEditorSubstance().forceToFocus();
  38. }
  39. /**
  40. * @inheritDoc
  41. */
  42. setValue(newValue) {
  43. this.getEditorSubstance().setValue(newValue);
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. setGfmMode(bool) {
  49. this.getEditorSubstance().setGfmMode(bool);
  50. }
  51. /**
  52. * @inheritDoc
  53. */
  54. setCaretLine(line) {
  55. this.getEditorSubstance().setCaretLine(line);
  56. }
  57. /**
  58. * @inheritDoc
  59. */
  60. setScrollTopByLine(line) {
  61. this.getEditorSubstance().setScrollTopByLine(line);
  62. }
  63. /**
  64. * @inheritDoc
  65. */
  66. insertText(text) {
  67. this.getEditorSubstance().insertText(text);
  68. }
  69. /**
  70. * remove overlay and set isUploading to false
  71. */
  72. terminateUploadingState() {
  73. this.setState({
  74. dropzoneActive: false,
  75. isUploading: false,
  76. });
  77. }
  78. /**
  79. * dispatch onUpload event
  80. */
  81. dispatchUpload(files) {
  82. if (this.props.onUpload != null) {
  83. this.props.onUpload(files);
  84. }
  85. }
  86. pasteFilesHandler(event) {
  87. const dropzone = this.refs.dropzone;
  88. const items = event.clipboardData.items || event.clipboardData.files || [];
  89. // abort if length is not 1
  90. if (items.length != 1) {
  91. return;
  92. }
  93. const file = items[0].getAsFile();
  94. // check type and size
  95. if (pasteHelper.fileAccepted(file, dropzone.props.accept) &&
  96. pasteHelper.fileMatchSize(file, dropzone.props.maxSize, dropzone.props.minSize)) {
  97. this.dispatchUpload(file);
  98. this.setState({ isUploading: true });
  99. }
  100. }
  101. dragEnterHandler(event) {
  102. const dataTransfer = event.dataTransfer;
  103. // do nothing if contents is not files
  104. if (!dataTransfer.types.includes('Files')) {
  105. return;
  106. }
  107. this.setState({ dropzoneActive: true });
  108. }
  109. dragLeaveHandler() {
  110. this.setState({ dropzoneActive: false });
  111. }
  112. dropHandler(accepted, rejected) {
  113. // rejected
  114. if (accepted.length != 1) { // length should be 0 or 1 because `multiple={false}` is set
  115. this.setState({ dropzoneActive: false });
  116. return;
  117. }
  118. const file = accepted[0];
  119. this.dispatchUpload(file);
  120. this.setState({ isUploading: true });
  121. }
  122. getDropzoneAccept() {
  123. let accept = 'null'; // reject all
  124. if (this.props.isUploadable) {
  125. if (!this.props.isUploadableFile) {
  126. accept = 'image/*'; // image only
  127. }
  128. else {
  129. accept = ''; // allow all
  130. }
  131. }
  132. return accept;
  133. }
  134. getDropzoneClassName() {
  135. let className = 'dropzone';
  136. if (!this.props.isUploadable) {
  137. className += ' dropzone-unuploadable';
  138. }
  139. else {
  140. className += ' dropzone-uploadable';
  141. if (this.props.isUploadableFile) {
  142. className += ' dropzone-uploadablefile';
  143. }
  144. }
  145. // uploading
  146. if (this.state.isUploading) {
  147. className += ' dropzone-uploading';
  148. }
  149. return className;
  150. }
  151. renderDropzoneOverlay() {
  152. return (
  153. <div className="overlay overlay-dropzone-active">
  154. {this.state.isUploading &&
  155. <span className="overlay-content">
  156. <div className="speeding-wheel d-inline-block"></div>
  157. <span className="sr-only">Uploading...</span>
  158. </span>
  159. }
  160. {!this.state.isUploading && <span className="overlay-content"></span>}
  161. </div>
  162. );
  163. }
  164. renderNavbar() {
  165. return (
  166. <div className="m-0 navbar navbar-default navbar-editor" style={{ minHeight: 'unset' }}>
  167. <ul className="pr-4 nav nav-navbar navbar-right">
  168. { this.getNavbarItems() != null && this.getNavbarItems().map((item, idx) => {
  169. return <li key={idx}>{item}</li>;
  170. }) }
  171. </ul>
  172. </div>
  173. );
  174. }
  175. getNavbarItems() {
  176. // set navbar items(react elements) here that are common in CodeMirrorEditor or TextAreaEditor
  177. const navbarItems = [];
  178. // concat common items and items specific to CodeMirrorEditor or TextAreaEditor
  179. return navbarItems.concat(this.getEditorSubstance().getNavbarItems());
  180. }
  181. render() {
  182. const flexContainer = {
  183. height: '100%',
  184. display: 'flex',
  185. flexDirection: 'column',
  186. };
  187. const isMobile = this.props.isMobile;
  188. return (
  189. <div style={flexContainer} className="editor-container">
  190. <Dropzone
  191. ref="dropzone"
  192. disableClick
  193. disablePreview={true}
  194. accept={this.getDropzoneAccept()}
  195. className={this.getDropzoneClassName()}
  196. acceptClassName="dropzone-accepted"
  197. rejectClassName="dropzone-rejected"
  198. multiple={false}
  199. onDragLeave={this.dragLeaveHandler}
  200. onDrop={this.dropHandler}
  201. >
  202. { this.state.dropzoneActive && this.renderDropzoneOverlay() }
  203. { this.state.isComponentDidMount && this.renderNavbar() }
  204. {/* for PC */}
  205. { !isMobile &&
  206. <CodeMirrorEditor
  207. ref="cmEditor"
  208. onPasteFiles={this.pasteFilesHandler}
  209. onDragEnter={this.dragEnterHandler}
  210. {...this.props}
  211. />
  212. }
  213. {/* for mobile */}
  214. { isMobile &&
  215. <TextAreaEditor
  216. ref="taEditor"
  217. onPasteFiles={this.pasteFilesHandler}
  218. onDragEnter={this.dragEnterHandler}
  219. {...this.props}
  220. />
  221. }
  222. </Dropzone>
  223. <button type="button" className="btn btn-default btn-block btn-open-dropzone"
  224. onClick={() => {this.refs.dropzone.open()}}>
  225. <i className="icon-paper-clip" aria-hidden="true"></i>&nbsp;
  226. Attach files
  227. <span className="desc-long">
  228. &nbsp;by dragging &amp; dropping,&nbsp;
  229. <span className="btn-link">selecting them</span>,&nbsp;
  230. or pasting from the clipboard.
  231. </span>
  232. </button>
  233. </div>
  234. );
  235. }
  236. }
  237. Editor.propTypes = Object.assign({
  238. isMobile: PropTypes.bool,
  239. isUploadable: PropTypes.bool,
  240. isUploadableFile: PropTypes.bool,
  241. emojiStrategy: PropTypes.object,
  242. onChange: PropTypes.func,
  243. onUpload: PropTypes.func,
  244. }, AbstractEditor.propTypes);