Editor.jsx 7.5 KB

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