Editor.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. dropzoneAccept: '',
  29. dropzoneActive: 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.onDragEnter = this.onDragEnter.bind(this);
  36. this.onDragLeave = this.onDragLeave.bind(this);
  37. this.onDrop = this.onDrop.bind(this);
  38. this.renderOverlayMessage = this.renderOverlayMessage.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. * insert text
  69. * @param {string} text
  70. */
  71. insertText(text) {
  72. const editor = this.getCodeMirror();
  73. editor.getDoc().replaceSelection(text);
  74. }
  75. /**
  76. * dispatch onSave event
  77. */
  78. dispatchSave() {
  79. if (this.props.onSave != null) {
  80. this.props.onSave();
  81. }
  82. }
  83. /**
  84. * dispatch onUpload event
  85. */
  86. dispatchUpload(files) {
  87. if (this.props.onUpload != null) {
  88. this.props.onUpload(files);
  89. }
  90. }
  91. onDragEnter(event) {
  92. console.log('dragenter', event);
  93. this.setState({
  94. dropzoneActive: true
  95. });
  96. }
  97. onDragLeave() {
  98. this.setState({
  99. dropzoneActive: false
  100. });
  101. }
  102. onDrop(files) {
  103. if (!this.props.isUploadable) {
  104. return;
  105. }
  106. this.setState({
  107. dropzoneActive: false
  108. });
  109. // TODO abort multi files
  110. this.dispatchUpload(files[0]);
  111. }
  112. renderOverlayMessage() {
  113. const overlayStyle = {
  114. position: 'absolute',
  115. zIndex: 1060, // FIXME: required because .content-main.on-edit has 'z-index:1050'
  116. top: 0,
  117. right: 0,
  118. bottom: 0,
  119. left: 0,
  120. };
  121. let overlayClassName;
  122. let message;
  123. if (this.props.isUploadable) {
  124. overlayClassName = 'dropzone-overlay dropzone-overlay-activated';
  125. message = (
  126. <span>
  127. <i className="fa fa-fw fa-upload" aria-hidden="true"></i>
  128. Upload to drop here
  129. </span>
  130. );
  131. }
  132. else {
  133. overlayClassName = 'dropzone-overlay dropzone-overlay-disabled';
  134. message = (
  135. <span>
  136. <i className="fa fa-fw fa-exclamation-triangle" aria-hidden="true"></i>
  137. Uploading is disabled
  138. </span>
  139. );
  140. }
  141. return (
  142. <div style={overlayStyle} className={overlayClassName}>
  143. {message}
  144. </div>
  145. );
  146. }
  147. render() {
  148. const dropzoneStyle = {
  149. height: '100%'
  150. }
  151. return (
  152. <Dropzone
  153. disableClick
  154. style={dropzoneStyle}
  155. accept={this.state.accept}
  156. onDragEnter={this.onDragEnter}
  157. onDragLeave={this.onDragLeave}
  158. onDrop={this.onDrop}
  159. >
  160. { this.state.dropzoneActive && this.renderOverlayMessage() }
  161. <ReactCodeMirror
  162. ref="cm"
  163. editorDidMount={(editor) => {
  164. // add event handlers
  165. editor.on('paste', pasteHelper.pasteHandler);
  166. }}
  167. value={this.state.value}
  168. options={{
  169. mode: 'gfm',
  170. theme: 'eclipse',
  171. lineNumbers: true,
  172. tabSize: 4,
  173. indentUnit: 4,
  174. lineWrapping: true,
  175. autoRefresh: true,
  176. autoCloseTags: true,
  177. matchBrackets: true,
  178. matchTags: {bothTags: true},
  179. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  180. highlightSelectionMatches: {annotateScrollbar: true},
  181. // markdown mode options
  182. highlightFormatting: true,
  183. // continuelist, indentlist
  184. extraKeys: {
  185. "Enter": "newlineAndIndentContinueMarkdownList",
  186. "Tab": "indentMore",
  187. "Shift-Tab": "indentLess",
  188. }
  189. }}
  190. onScroll={(editor, data) => {
  191. if (this.props.onScroll != null) {
  192. this.props.onScroll(data);
  193. }
  194. }}
  195. onChange={(editor, data, value) => {
  196. if (this.props.onChange != null) {
  197. this.props.onChange(value);
  198. }
  199. // Emoji AutoComplete
  200. emojiAutoCompleteHelper.showHint(editor);
  201. }}
  202. // onDragEnter={this.onDragEnter}
  203. />
  204. </Dropzone>
  205. )
  206. }
  207. }
  208. Editor.propTypes = {
  209. value: PropTypes.string,
  210. isUploadable: PropTypes.bool,
  211. onChange: PropTypes.func,
  212. onScroll: PropTypes.func,
  213. onSave: PropTypes.func,
  214. onUpload: PropTypes.func,
  215. };