Editor.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import * as codemirror from 'codemirror';
  4. import { UnControlled as ReactCodeMirror } from 'react-codemirror2';
  5. require('codemirror/lib/codemirror.css');
  6. require('codemirror/addon/display/autorefresh');
  7. require('codemirror/addon/edit/matchbrackets');
  8. require('codemirror/addon/edit/matchtags');
  9. require('codemirror/addon/edit/closetag');
  10. require('codemirror/addon/edit/continuelist');
  11. require('codemirror/addon/hint/show-hint');
  12. require('codemirror/addon/hint/show-hint.css');
  13. require('codemirror/addon/search/searchcursor');
  14. require('codemirror/addon/search/match-highlighter');
  15. require('codemirror/addon/scroll/annotatescrollbar');
  16. require('codemirror/mode/gfm/gfm');
  17. require('codemirror/theme/eclipse.css');
  18. import Dropzone from 'react-dropzone';
  19. import pasteHelper from './PasteHelper';
  20. import emojiAutoCompleteHelper from './EmojiAutoCompleteHelper';
  21. export default class Editor extends React.Component {
  22. constructor(props) {
  23. super(props);
  24. // https://regex101.com/r/7BN2fR/2
  25. this.indentAndMarkPattern = /^([ \t]*)(?:>|\-|\+|\*|\d+\.) /;
  26. this.state = {
  27. value: this.props.value,
  28. dropzoneActive: false,
  29. isUploading: false,
  30. };
  31. this.getCodeMirror = this.getCodeMirror.bind(this);
  32. this.setCaretLine = this.setCaretLine.bind(this);
  33. this.forceToFocus = this.forceToFocus.bind(this);
  34. this.dispatchSave = this.dispatchSave.bind(this);
  35. this.onDragEnterForCM = this.onDragEnterForCM.bind(this);
  36. this.onDragLeave = this.onDragLeave.bind(this);
  37. this.onDrop = this.onDrop.bind(this);
  38. this.renderOverlay = this.renderOverlay.bind(this);
  39. }
  40. componentDidMount() {
  41. // initialize caret line
  42. this.setCaretLine(0);
  43. // set save handler
  44. codemirror.commands.save = this.dispatchSave;
  45. }
  46. getCodeMirror() {
  47. return this.refs.cm.editor;
  48. }
  49. forceToFocus() {
  50. const editor = this.getCodeMirror();
  51. // use setInterval with reluctance -- 2018.01.11 Yuki Takei
  52. const intervalId = setInterval(() => {
  53. this.getCodeMirror().focus();
  54. if (editor.hasFocus()) {
  55. clearInterval(intervalId);
  56. }
  57. }, 100);
  58. }
  59. /**
  60. * set caret position of codemirror
  61. * @param {string} number
  62. */
  63. setCaretLine(line) {
  64. const editor = this.getCodeMirror();
  65. editor.setCursor({line: line-1}); // leave 'ch' field as null/undefined to indicate the end of line
  66. }
  67. /**
  68. * remove overlay and set isUploading to false
  69. */
  70. terminateUploadingState() {
  71. this.setState({
  72. dropzoneActive: false,
  73. isUploading: false,
  74. });
  75. }
  76. /**
  77. * insert text
  78. * @param {string} text
  79. */
  80. insertText(text) {
  81. const editor = this.getCodeMirror();
  82. editor.getDoc().replaceSelection(text);
  83. }
  84. /**
  85. * dispatch onSave event
  86. */
  87. dispatchSave() {
  88. if (this.props.onSave != null) {
  89. this.props.onSave();
  90. }
  91. }
  92. /**
  93. * dispatch onUpload event
  94. */
  95. dispatchUpload(files) {
  96. if (this.props.onUpload != null) {
  97. this.props.onUpload(files);
  98. }
  99. }
  100. onDragEnterForCM(editor, 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. onDragLeave() {
  109. this.setState({ dropzoneActive: false });
  110. }
  111. onDrop(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. renderOverlay() {
  122. const overlayStyle = {
  123. position: 'absolute',
  124. zIndex: 1060, // FIXME: required because .content-main.on-edit has 'z-index:1050'
  125. top: 0,
  126. right: 0,
  127. bottom: 0,
  128. left: 0,
  129. };
  130. return (
  131. <div style={overlayStyle} className="dropzone-overlay">
  132. {this.state.isUploading &&
  133. <span className="dropzone-overlay-content">
  134. <i className="fa fa-spinner fa-pulse fa-fw"></i>
  135. <span className="sr-only">Uploading...</span>
  136. </span>
  137. }
  138. {!this.state.isUploading && <span className="dropzone-overlay-content"></span>}
  139. </div>
  140. );
  141. }
  142. render() {
  143. const flexContainer = {
  144. height: '100%',
  145. display: 'flex',
  146. flexDirection: 'column',
  147. }
  148. const expandHeight = {
  149. height: '100%',
  150. }
  151. let accept = 'null'; // reject all
  152. let className = 'dropzone';
  153. if (!this.props.isUploadable) {
  154. className += ' dropzone-unuploadable';
  155. }
  156. else {
  157. accept = 'image/*'
  158. className += ' dropzone-uploadable';
  159. if (this.props.isUploadableFile) {
  160. className += ' dropzone-uploadablefile';
  161. accept = ''; // allow all
  162. }
  163. }
  164. // uploading
  165. if (this.state.isUploading) {
  166. className += ' dropzone-uploading';
  167. }
  168. return (
  169. <div style={flexContainer}>
  170. <Dropzone
  171. ref="dropzone"
  172. disableClick
  173. disablePreview={true}
  174. style={expandHeight}
  175. accept={accept}
  176. className={className}
  177. acceptClassName="dropzone-accepted"
  178. rejectClassName="dropzone-rejected"
  179. multiple={false}
  180. onDragLeave={this.onDragLeave}
  181. onDrop={this.onDrop}
  182. >
  183. { this.state.dropzoneActive && this.renderOverlay() }
  184. <ReactCodeMirror
  185. ref="cm"
  186. editorDidMount={(editor) => {
  187. // add event handlers
  188. editor.on('paste', pasteHelper.pasteHandler);
  189. }}
  190. value={this.state.value}
  191. options={{
  192. mode: 'gfm',
  193. theme: 'eclipse',
  194. lineNumbers: true,
  195. tabSize: 4,
  196. indentUnit: 4,
  197. lineWrapping: true,
  198. autoRefresh: true,
  199. autoCloseTags: true,
  200. matchBrackets: true,
  201. matchTags: {bothTags: true},
  202. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  203. highlightSelectionMatches: {annotateScrollbar: true},
  204. // markdown mode options
  205. highlightFormatting: true,
  206. // continuelist, indentlist
  207. extraKeys: {
  208. "Enter": "newlineAndIndentContinueMarkdownList",
  209. "Tab": "indentMore",
  210. "Shift-Tab": "indentLess",
  211. }
  212. }}
  213. onScroll={(editor, data) => {
  214. if (this.props.onScroll != null) {
  215. this.props.onScroll(data);
  216. }
  217. }}
  218. onChange={(editor, data, value) => {
  219. if (this.props.onChange != null) {
  220. this.props.onChange(value);
  221. }
  222. // Emoji AutoComplete
  223. emojiAutoCompleteHelper.showHint(editor);
  224. }}
  225. onDragEnter={this.onDragEnterForCM}
  226. />
  227. </Dropzone>
  228. <button type="button" className="btn btn-default btn-block btn-open-dropzone" onClick={() => {this.refs.dropzone.open()}}>
  229. <i className="fa fa-paperclip" aria-hidden="true"></i>&nbsp;
  230. Attach files by dragging &amp; dropping,&nbsp;
  231. <span className="btn-link">selecting them</span>,&nbsp;
  232. or (TBD) pasting from the clipboard.
  233. </button>
  234. </div>
  235. )
  236. }
  237. }
  238. Editor.propTypes = {
  239. value: PropTypes.string,
  240. isUploadable: PropTypes.bool,
  241. isUploadableFile: PropTypes.bool,
  242. onChange: PropTypes.func,
  243. onScroll: PropTypes.func,
  244. onSave: PropTypes.func,
  245. onUpload: PropTypes.func,
  246. };