Editor.js 7.1 KB

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