Editor.js 7.1 KB

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