Editor.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. dropzoneActive: false,
  13. isUploading: false,
  14. };
  15. this.getEditorSubstance = this.getEditorSubstance.bind(this);
  16. this.pasteFilesHandler = this.pasteFilesHandler.bind(this);
  17. this.dragEnterHandler = this.dragEnterHandler.bind(this);
  18. this.dragLeaveHandler = this.dragLeaveHandler.bind(this);
  19. this.dropHandler = this.dropHandler.bind(this);
  20. this.getDropzoneAccept = this.getDropzoneAccept.bind(this);
  21. this.getDropzoneClassName = this.getDropzoneClassName.bind(this);
  22. this.renderDropzoneOverlay = this.renderDropzoneOverlay.bind(this);
  23. }
  24. componentDidMount() {
  25. // initialize caret line
  26. this.setCaretLine(0);
  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. setCaretLine(line) {
  43. this.getEditorSubstance().setCaretLine(line);
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. setScrollTopByLine(line) {
  49. this.getEditorSubstance().setScrollTopByLine(line);
  50. }
  51. /**
  52. * @inheritDoc
  53. */
  54. insertText(text) {
  55. this.getEditorSubstance().insertText(text);
  56. }
  57. /**
  58. * remove overlay and set isUploading to false
  59. */
  60. terminateUploadingState() {
  61. this.setState({
  62. dropzoneActive: false,
  63. isUploading: false,
  64. });
  65. }
  66. /**
  67. * dispatch onUpload event
  68. */
  69. dispatchUpload(files) {
  70. if (this.props.onUpload != null) {
  71. this.props.onUpload(files);
  72. }
  73. }
  74. pasteFilesHandler(event) {
  75. const dropzone = this.refs.dropzone;
  76. const items = event.clipboardData.items || event.clipboardData.files || [];
  77. // abort if length is not 1
  78. if (items.length != 1) {
  79. return;
  80. }
  81. const file = items[0].getAsFile();
  82. // check type and size
  83. if (pasteHelper.fileAccepted(file, dropzone.props.accept) &&
  84. pasteHelper.fileMatchSize(file, dropzone.props.maxSize, dropzone.props.minSize)) {
  85. this.dispatchUpload(file);
  86. this.setState({ isUploading: true });
  87. }
  88. }
  89. dragEnterHandler(event) {
  90. const dataTransfer = event.dataTransfer;
  91. // do nothing if contents is not files
  92. if (!dataTransfer.types.includes('Files')) {
  93. return;
  94. }
  95. this.setState({ dropzoneActive: true });
  96. }
  97. dragLeaveHandler() {
  98. this.setState({ dropzoneActive: false });
  99. }
  100. dropHandler(accepted, rejected) {
  101. // rejected
  102. if (accepted.length != 1) { // length should be 0 or 1 because `multiple={false}` is set
  103. this.setState({ dropzoneActive: false });
  104. return;
  105. }
  106. const file = accepted[0];
  107. this.dispatchUpload(file);
  108. this.setState({ isUploading: true });
  109. }
  110. getDropzoneAccept() {
  111. let accept = 'null'; // reject all
  112. if (this.props.isUploadable) {
  113. if (!this.props.isUploadableFile) {
  114. accept = 'image/*'; // image only
  115. }
  116. else {
  117. accept = ''; // allow all
  118. }
  119. }
  120. return accept;
  121. }
  122. getDropzoneClassName() {
  123. let className = 'dropzone';
  124. if (!this.props.isUploadable) {
  125. className += ' dropzone-unuploadable';
  126. }
  127. else {
  128. className += ' dropzone-uploadable';
  129. if (this.props.isUploadableFile) {
  130. className += ' dropzone-uploadablefile';
  131. }
  132. }
  133. // uploading
  134. if (this.state.isUploading) {
  135. className += ' dropzone-uploading';
  136. }
  137. return className;
  138. }
  139. getOverlayStyle() {
  140. return {
  141. position: 'absolute',
  142. zIndex: 4, // forward than .CodeMirror-gutters
  143. top: 0,
  144. right: 0,
  145. bottom: 0,
  146. left: 0,
  147. };
  148. }
  149. renderDropzoneOverlay() {
  150. const overlayStyle = this.getOverlayStyle();
  151. return (
  152. <div style={overlayStyle} className="overlay">
  153. {this.state.isUploading &&
  154. <span className="overlay-content">
  155. <div className="speeding-wheel d-inline-block"></div>
  156. <span className="sr-only">Uploading...</span>
  157. </span>
  158. }
  159. {!this.state.isUploading && <span className="overlay-content"></span>}
  160. </div>
  161. );
  162. }
  163. render() {
  164. const flexContainer = {
  165. height: '100%',
  166. display: 'flex',
  167. flexDirection: 'column',
  168. };
  169. const isMobile = this.props.isMobile;
  170. return <React.Fragment>
  171. <div style={flexContainer}>
  172. <Dropzone
  173. ref="dropzone"
  174. disableClick
  175. disablePreview={true}
  176. accept={this.getDropzoneAccept()}
  177. className={this.getDropzoneClassName()}
  178. acceptClassName="dropzone-accepted"
  179. rejectClassName="dropzone-rejected"
  180. multiple={false}
  181. onDragLeave={this.dragLeaveHandler}
  182. onDrop={this.dropHandler}
  183. >
  184. { this.state.dropzoneActive && this.renderDropzoneOverlay() }
  185. {/* for PC */}
  186. { !isMobile &&
  187. <CodeMirrorEditor
  188. ref="cmEditor"
  189. onPasteFiles={this.pasteFilesHandler}
  190. onDragEnter={this.dragEnterHandler}
  191. {...this.props}
  192. />
  193. }
  194. {/* for mobile */}
  195. { isMobile &&
  196. <TextAreaEditor
  197. ref="taEditor"
  198. onPasteFiles={this.pasteFilesHandler}
  199. onDragEnter={this.dragEnterHandler}
  200. {...this.props}
  201. />
  202. }
  203. </Dropzone>
  204. <button type="button" className="btn btn-default btn-block btn-open-dropzone"
  205. onClick={() => {this.refs.dropzone.open()}}>
  206. <i className="icon-paper-clip" aria-hidden="true"></i>&nbsp;
  207. Attach files by dragging &amp; dropping,&nbsp;
  208. <span className="btn-link">selecting them</span>,&nbsp;
  209. or pasting from the clipboard.
  210. </button>
  211. </div>
  212. </React.Fragment>;
  213. }
  214. }
  215. Editor.propTypes = Object.assign({
  216. isMobile: PropTypes.bool,
  217. isUploadable: PropTypes.bool,
  218. isUploadableFile: PropTypes.bool,
  219. emojiStrategy: PropTypes.object,
  220. onChange: PropTypes.func,
  221. onUpload: PropTypes.func,
  222. }, AbstractEditor.propTypes);