Editor.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. }
  39. componentDidMount() {
  40. // initialize caret line
  41. this.setCaretLine(0);
  42. // set save handler
  43. codemirror.commands.save = this.dispatchSave;
  44. }
  45. getCodeMirror() {
  46. return this.refs.cm.editor;
  47. }
  48. forceToFocus() {
  49. const editor = this.getCodeMirror();
  50. // use setInterval with reluctance -- 2018.01.11 Yuki Takei
  51. const intervalId = setInterval(() => {
  52. this.getCodeMirror().focus();
  53. if (editor.hasFocus()) {
  54. clearInterval(intervalId);
  55. }
  56. }, 100);
  57. }
  58. /**
  59. * set caret position of codemirror
  60. * @param {string} number
  61. */
  62. setCaretLine(line) {
  63. const editor = this.getCodeMirror();
  64. editor.setCursor({line: line-1}); // leave 'ch' field as null/undefined to indicate the end of line
  65. }
  66. /**
  67. * dispatch onSave event
  68. */
  69. dispatchSave() {
  70. if (this.props.onSave != null) {
  71. this.props.onSave();
  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. onDragEnter() {
  83. this.setState({
  84. dropzoneActive: true
  85. });
  86. }
  87. onDragLeave() {
  88. this.setState({
  89. dropzoneActive: false
  90. });
  91. }
  92. onDrop(files) {
  93. this.setState({
  94. dropzoneActive: false
  95. });
  96. this.dispatchUpload(files);
  97. }
  98. render() {
  99. const dropzoneStyle = {
  100. height: '100%'
  101. }
  102. const overlayStyle = {
  103. position: 'absolute',
  104. zIndex: 1060,
  105. top: 0,
  106. right: 0,
  107. bottom: 0,
  108. left: 0,
  109. padding: '2.5em 0',
  110. background: 'rgba(0,0,0,0.5)',
  111. textAlign: 'center',
  112. color: '#fff'
  113. };
  114. return (
  115. <Dropzone
  116. disableClick
  117. style={dropzoneStyle}
  118. accept={this.state.accept}
  119. onDragLeave={this.onDragLeave}
  120. onDrop={this.onDrop}
  121. >
  122. { this.state.dropzoneActive && <div style={overlayStyle}>Drop files...</div> }
  123. <ReactCodeMirror
  124. ref="cm"
  125. editorDidMount={(editor) => {
  126. // add paste event handler
  127. editor.on('paste', pasteHelper.pasteHandler);
  128. }}
  129. value={this.state.value}
  130. options={{
  131. mode: 'gfm',
  132. theme: 'eclipse',
  133. lineNumbers: true,
  134. tabSize: 4,
  135. indentUnit: 4,
  136. lineWrapping: true,
  137. autoRefresh: true,
  138. autoCloseTags: true,
  139. matchBrackets: true,
  140. matchTags: {bothTags: true},
  141. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  142. highlightSelectionMatches: {annotateScrollbar: true},
  143. // markdown mode options
  144. highlightFormatting: true,
  145. // continuelist, indentlist
  146. extraKeys: {
  147. "Enter": "newlineAndIndentContinueMarkdownList",
  148. "Tab": "indentMore",
  149. "Shift-Tab": "indentLess",
  150. }
  151. }}
  152. onScroll={(editor, data) => {
  153. if (this.props.onScroll != null) {
  154. this.props.onScroll(data);
  155. }
  156. }}
  157. onChange={(editor, data, value) => {
  158. if (this.props.onChange != null) {
  159. this.props.onChange(value);
  160. }
  161. // Emoji AutoComplete
  162. emojiAutoCompleteHelper.showHint(editor);
  163. }}
  164. onDragEnter={this.onDragEnter}
  165. />
  166. </Dropzone>
  167. )
  168. }
  169. }
  170. Editor.propTypes = {
  171. value: PropTypes.string,
  172. onChange: PropTypes.func,
  173. onScroll: PropTypes.func,
  174. onSave: PropTypes.func,
  175. onUpload: PropTypes.func,
  176. };