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.getAcceptableType = this.getAcceptableType.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.taEditor
  31. : this.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. /**
  87. * get acceptable(uploadable) file type
  88. */
  89. getAcceptableType() {
  90. let accept = 'null'; // reject all
  91. if (this.props.isUploadable) {
  92. if (!this.props.isUploadableFile) {
  93. accept = 'image/*'; // image only
  94. }
  95. else {
  96. accept = ''; // allow all
  97. }
  98. }
  99. return accept;
  100. }
  101. pasteFilesHandler(event) {
  102. const items = event.clipboardData.items || event.clipboardData.files || [];
  103. // abort if length is not 1
  104. if (items.length < 1) {
  105. return;
  106. }
  107. for (let i = 0; i < items.length; i++) {
  108. try {
  109. const file = items[i].getAsFile();
  110. // check file type (the same process as Dropzone)
  111. if (file != null && pasteHelper.isAcceptableType(file, this.getAcceptableType())) {
  112. this.dispatchUpload(file);
  113. this.setState({ isUploading: true });
  114. }
  115. }
  116. catch (e) {
  117. this.logger.error(e);
  118. }
  119. }
  120. }
  121. dragEnterHandler(event) {
  122. const dataTransfer = event.dataTransfer;
  123. // do nothing if contents is not files
  124. if (!dataTransfer.types.includes('Files')) {
  125. return;
  126. }
  127. this.setState({ dropzoneActive: true });
  128. }
  129. dragLeaveHandler() {
  130. this.setState({ dropzoneActive: false });
  131. }
  132. dropHandler(accepted, rejected) {
  133. // rejected
  134. if (accepted.length !== 1) { // length should be 0 or 1 because `multiple={false}` is set
  135. this.setState({ dropzoneActive: false });
  136. return;
  137. }
  138. const file = accepted[0];
  139. this.dispatchUpload(file);
  140. this.setState({ isUploading: true });
  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. // eslint-disable-next-line react/no-array-index-key
  180. return <li key={`navbarItem-${idx}`}>{item}</li>;
  181. }) }
  182. </ul>
  183. </div>
  184. );
  185. }
  186. getNavbarItems() {
  187. // set navbar items(react elements) here that are common in CodeMirrorEditor or TextAreaEditor
  188. const navbarItems = [];
  189. // concat common items and items specific to CodeMirrorEditor or TextAreaEditor
  190. return navbarItems.concat(this.getEditorSubstance().getNavbarItems());
  191. }
  192. render() {
  193. const flexContainer = {
  194. height: '100%',
  195. display: 'flex',
  196. flexDirection: 'column',
  197. };
  198. const isMobile = this.props.isMobile;
  199. return (
  200. <div style={flexContainer} className="editor-container">
  201. <Dropzone
  202. ref={(c) => { this.dropzone = c }}
  203. disableClick
  204. accept={this.getAcceptableType()}
  205. className={this.getDropzoneClassName()}
  206. acceptClassName="dropzone-accepted"
  207. rejectClassName="dropzone-rejected"
  208. multiple={false}
  209. onDragLeave={this.dragLeaveHandler}
  210. onDrop={this.dropHandler}
  211. >
  212. { this.state.dropzoneActive && this.renderDropzoneOverlay() }
  213. { this.state.isComponentDidMount && this.renderNavbar() }
  214. {/* for PC */}
  215. { !isMobile && (
  216. <CodeMirrorEditor
  217. ref={(c) => { this.cmEditor = c }}
  218. onPasteFiles={this.pasteFilesHandler}
  219. onDragEnter={this.dragEnterHandler}
  220. {...this.props}
  221. />
  222. )
  223. }
  224. {/* for mobile */}
  225. { isMobile && (
  226. <TextAreaEditor
  227. ref={(c) => { this.taEditor = c }}
  228. onPasteFiles={this.pasteFilesHandler}
  229. onDragEnter={this.dragEnterHandler}
  230. {...this.props}
  231. />
  232. )
  233. }
  234. </Dropzone>
  235. { this.props.isUploadable
  236. && (
  237. <button
  238. type="button"
  239. className="btn btn-default btn-block btn-open-dropzone"
  240. onClick={() => { this.dropzone.open() }}
  241. >
  242. <i className="icon-paper-clip" aria-hidden="true"></i>&nbsp;
  243. Attach files
  244. <span className="desc-long">
  245. &nbsp;by dragging &amp; dropping,&nbsp;
  246. <span className="btn-link">selecting them</span>,&nbsp;
  247. or pasting from the clipboard.
  248. </span>
  249. </button>
  250. )
  251. }
  252. </div>
  253. );
  254. }
  255. }
  256. Editor.propTypes = Object.assign({
  257. noCdn: PropTypes.bool,
  258. isMobile: PropTypes.bool,
  259. isUploadable: PropTypes.bool,
  260. isUploadableFile: PropTypes.bool,
  261. emojiStrategy: PropTypes.object,
  262. onChange: PropTypes.func,
  263. onUpload: PropTypes.func,
  264. }, AbstractEditor.propTypes);