2
0

Editor.jsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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(isDragAccept, isDragReject) {
  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. if (isDragAccept) {
  158. className += ' dropzone-accepted';
  159. }
  160. if (isDragReject) {
  161. className += ' dropzone-rejected';
  162. }
  163. return className;
  164. }
  165. renderDropzoneOverlay() {
  166. return (
  167. <div className="overlay overlay-dropzone-active">
  168. {this.state.isUploading
  169. && (
  170. <span className="overlay-content">
  171. <div className="speeding-wheel d-inline-block"></div>
  172. <span className="sr-only">Uploading...</span>
  173. </span>
  174. )
  175. }
  176. {!this.state.isUploading && <span className="overlay-content"></span>}
  177. </div>
  178. );
  179. }
  180. renderNavbar() {
  181. return (
  182. <div className="m-0 navbar navbar-default navbar-editor" style={{ minHeight: 'unset' }}>
  183. <ul className="pl-2 nav nav-navbar">
  184. { this.getNavbarItems() != null && this.getNavbarItems().map((item, idx) => {
  185. // eslint-disable-next-line react/no-array-index-key
  186. return <li key={`navbarItem-${idx}`}>{item}</li>;
  187. }) }
  188. </ul>
  189. </div>
  190. );
  191. }
  192. getNavbarItems() {
  193. // set navbar items(react elements) here that are common in CodeMirrorEditor or TextAreaEditor
  194. const navbarItems = [];
  195. // concat common items and items specific to CodeMirrorEditor or TextAreaEditor
  196. return navbarItems.concat(this.getEditorSubstance().getNavbarItems());
  197. }
  198. render() {
  199. const flexContainer = {
  200. height: '100%',
  201. display: 'flex',
  202. flexDirection: 'column',
  203. };
  204. const isMobile = this.props.isMobile;
  205. return (
  206. <div style={flexContainer} className="editor-container">
  207. <Dropzone
  208. ref={(c) => { this.dropzone = c }}
  209. disableClick
  210. accept={this.getAcceptableType()}
  211. multiple={false}
  212. onDragLeave={this.dragLeaveHandler}
  213. onDrop={this.dropHandler}
  214. >
  215. {({getRootProps, getInputProps, isDragAccept, isDragReject}) => {
  216. const { ref } = getInputProps();
  217. if (isMobile) {
  218. this.taEditor = ref.current;
  219. } else {
  220. this.cmEditor = ref.current;
  221. }
  222. return (
  223. <div className={this.getDropzoneClassName(isDragAccept, isDragReject)} {...getRootProps()}>
  224. { this.state.dropzoneActive && this.renderDropzoneOverlay() }
  225. { this.state.isComponentDidMount && this.renderNavbar() }
  226. {/* for PC */}
  227. { !isMobile && (
  228. <CodeMirrorEditor
  229. onPasteFiles={this.pasteFilesHandler}
  230. onDragEnter={this.dragEnterHandler}
  231. {...this.props}
  232. {...getInputProps()}
  233. />
  234. )
  235. }
  236. {/* for mobile */}
  237. { isMobile && (
  238. <TextAreaEditor
  239. onPasteFiles={this.pasteFilesHandler}
  240. onDragEnter={this.dragEnterHandler}
  241. {...this.props}
  242. {...getInputProps()}
  243. />
  244. )
  245. }
  246. </div>
  247. )
  248. }}
  249. </Dropzone>
  250. { this.props.isUploadable
  251. && (
  252. <button
  253. type="button"
  254. className="btn btn-default btn-block btn-open-dropzone"
  255. onClick={() => { this.dropzone.open() }}
  256. >
  257. <i className="icon-paper-clip" aria-hidden="true"></i>&nbsp;
  258. Attach files
  259. <span className="desc-long">
  260. &nbsp;by dragging &amp; dropping,&nbsp;
  261. <span className="btn-link">selecting them</span>,&nbsp;
  262. or pasting from the clipboard.
  263. </span>
  264. </button>
  265. )
  266. }
  267. </div>
  268. );
  269. }
  270. }
  271. Editor.propTypes = Object.assign({
  272. noCdn: PropTypes.bool,
  273. isMobile: PropTypes.bool,
  274. isUploadable: PropTypes.bool,
  275. isUploadableFile: PropTypes.bool,
  276. emojiStrategy: PropTypes.object,
  277. onChange: PropTypes.func,
  278. onUpload: PropTypes.func,
  279. }, AbstractEditor.propTypes);