Editor.js 6.5 KB

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