Editor.jsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. for (let i = 0; i < items.length; i++) {
  94. try {
  95. const file = items[i].getAsFile();
  96. // check type and size
  97. if (pasteHelper.fileAccepted(file, dropzone.props.accept) &&
  98. pasteHelper.fileMatchSize(file, dropzone.props.maxSize, dropzone.props.minSize)) {
  99. this.dispatchUpload(file);
  100. this.setState({ isUploading: true });
  101. }
  102. }
  103. catch (e) {
  104. this.logger.error(e);
  105. }
  106. }
  107. }
  108. dragEnterHandler(event) {
  109. const dataTransfer = event.dataTransfer;
  110. // do nothing if contents is not files
  111. if (!dataTransfer.types.includes('Files')) {
  112. return;
  113. }
  114. this.setState({ dropzoneActive: true });
  115. }
  116. dragLeaveHandler() {
  117. this.setState({ dropzoneActive: false });
  118. }
  119. dropHandler(accepted, rejected) {
  120. // rejected
  121. if (accepted.length != 1) { // length should be 0 or 1 because `multiple={false}` is set
  122. this.setState({ dropzoneActive: false });
  123. return;
  124. }
  125. const file = accepted[0];
  126. this.dispatchUpload(file);
  127. this.setState({ isUploading: true });
  128. }
  129. getDropzoneAccept() {
  130. let accept = 'null'; // reject all
  131. if (this.props.isUploadable) {
  132. if (!this.props.isUploadableFile) {
  133. accept = 'image/*'; // image only
  134. }
  135. else {
  136. accept = ''; // allow all
  137. }
  138. }
  139. return accept;
  140. }
  141. getDropzoneClassName() {
  142. let className = 'dropzone';
  143. if (!this.props.isUploadable) {
  144. className += ' dropzone-unuploadable';
  145. }
  146. else {
  147. className += ' dropzone-uploadable';
  148. if (this.props.isUploadableFile) {
  149. className += ' dropzone-uploadablefile';
  150. }
  151. }
  152. // uploading
  153. if (this.state.isUploading) {
  154. className += ' dropzone-uploading';
  155. }
  156. return className;
  157. }
  158. renderDropzoneOverlay() {
  159. return (
  160. <div className="overlay overlay-dropzone-active">
  161. {this.state.isUploading &&
  162. <span className="overlay-content">
  163. <div className="speeding-wheel d-inline-block"></div>
  164. <span className="sr-only">Uploading...</span>
  165. </span>
  166. }
  167. {!this.state.isUploading && <span className="overlay-content"></span>}
  168. </div>
  169. );
  170. }
  171. renderNavbar() {
  172. return (
  173. <div className="m-0 navbar navbar-default navbar-editor" style={{ minHeight: 'unset' }}>
  174. <ul className="pr-4 nav nav-navbar navbar-right">
  175. { this.getNavbarItems() != null && this.getNavbarItems().map((item, idx) => {
  176. return <li key={idx}>{item}</li>;
  177. }) }
  178. </ul>
  179. </div>
  180. );
  181. }
  182. getNavbarItems() {
  183. // set navbar items(react elements) here that are common in CodeMirrorEditor or TextAreaEditor
  184. const navbarItems = [];
  185. // concat common items and items specific to CodeMirrorEditor or TextAreaEditor
  186. return navbarItems.concat(this.getEditorSubstance().getNavbarItems());
  187. }
  188. render() {
  189. const flexContainer = {
  190. height: '100%',
  191. display: 'flex',
  192. flexDirection: 'column',
  193. };
  194. const isMobile = this.props.isMobile;
  195. return (
  196. <div style={flexContainer} className="editor-container">
  197. <Dropzone
  198. ref="dropzone"
  199. disableClick
  200. accept={this.getDropzoneAccept()}
  201. className={this.getDropzoneClassName()}
  202. acceptClassName="dropzone-accepted"
  203. rejectClassName="dropzone-rejected"
  204. multiple={false}
  205. onDragLeave={this.dragLeaveHandler}
  206. onDrop={this.dropHandler}
  207. >
  208. { this.state.dropzoneActive && this.renderDropzoneOverlay() }
  209. { this.state.isComponentDidMount && this.renderNavbar() }
  210. {/* for PC */}
  211. { !isMobile &&
  212. <CodeMirrorEditor
  213. ref="cmEditor"
  214. onPasteFiles={this.pasteFilesHandler}
  215. onDragEnter={this.dragEnterHandler}
  216. {...this.props}
  217. />
  218. }
  219. {/* for mobile */}
  220. { isMobile &&
  221. <TextAreaEditor
  222. ref="taEditor"
  223. onPasteFiles={this.pasteFilesHandler}
  224. onDragEnter={this.dragEnterHandler}
  225. {...this.props}
  226. />
  227. }
  228. </Dropzone>
  229. <button type="button" className="btn btn-default btn-block btn-open-dropzone"
  230. onClick={() => {this.refs.dropzone.open()}}>
  231. <i className="icon-paper-clip" aria-hidden="true"></i>&nbsp;
  232. Attach files
  233. <span className="desc-long">
  234. &nbsp;by dragging &amp; dropping,&nbsp;
  235. <span className="btn-link">selecting them</span>,&nbsp;
  236. or pasting from the clipboard.
  237. </span>
  238. </button>
  239. </div>
  240. );
  241. }
  242. }
  243. Editor.propTypes = Object.assign({
  244. noCdn: PropTypes.bool,
  245. isMobile: PropTypes.bool,
  246. isUploadable: PropTypes.bool,
  247. isUploadableFile: PropTypes.bool,
  248. emojiStrategy: PropTypes.object,
  249. onChange: PropTypes.func,
  250. onUpload: PropTypes.func,
  251. }, AbstractEditor.propTypes);