Editor.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * insert text
  68. * @param {string} text
  69. */
  70. insertText(text) {
  71. const editor = this.getCodeMirror();
  72. editor.getDoc().replaceSelection(text);
  73. }
  74. /**
  75. * dispatch onSave event
  76. */
  77. dispatchSave() {
  78. if (this.props.onSave != null) {
  79. this.props.onSave();
  80. }
  81. }
  82. /**
  83. * dispatch onUpload event
  84. */
  85. dispatchUpload(files) {
  86. if (this.props.onUpload != null) {
  87. this.props.onUpload(files);
  88. }
  89. }
  90. onDragEnter() {
  91. this.setState({
  92. dropzoneActive: true
  93. });
  94. }
  95. onDragLeave() {
  96. this.setState({
  97. dropzoneActive: false
  98. });
  99. }
  100. onDrop(files) {
  101. this.setState({
  102. dropzoneActive: false
  103. });
  104. // TODO abort multi files
  105. this.dispatchUpload(files[0]);
  106. }
  107. render() {
  108. const dropzoneStyle = {
  109. height: '100%'
  110. }
  111. const overlayStyle = {
  112. position: 'absolute',
  113. zIndex: 1060,
  114. top: 0,
  115. right: 0,
  116. bottom: 0,
  117. left: 0,
  118. padding: '2.5em 0',
  119. background: 'rgba(0,0,0,0.5)',
  120. textAlign: 'center',
  121. color: '#fff'
  122. };
  123. return (
  124. <Dropzone
  125. disableClick
  126. style={dropzoneStyle}
  127. accept={this.state.accept}
  128. onDragLeave={this.onDragLeave}
  129. onDrop={this.onDrop}
  130. >
  131. { this.state.dropzoneActive && <div style={overlayStyle}>Drop files...</div> }
  132. <ReactCodeMirror
  133. ref="cm"
  134. editorDidMount={(editor) => {
  135. // add paste event handler
  136. editor.on('paste', pasteHelper.pasteHandler);
  137. }}
  138. value={this.state.value}
  139. options={{
  140. mode: 'gfm',
  141. theme: 'eclipse',
  142. lineNumbers: true,
  143. tabSize: 4,
  144. indentUnit: 4,
  145. lineWrapping: true,
  146. autoRefresh: true,
  147. autoCloseTags: true,
  148. matchBrackets: true,
  149. matchTags: {bothTags: true},
  150. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  151. highlightSelectionMatches: {annotateScrollbar: true},
  152. // markdown mode options
  153. highlightFormatting: true,
  154. // continuelist, indentlist
  155. extraKeys: {
  156. "Enter": "newlineAndIndentContinueMarkdownList",
  157. "Tab": "indentMore",
  158. "Shift-Tab": "indentLess",
  159. }
  160. }}
  161. onScroll={(editor, data) => {
  162. if (this.props.onScroll != null) {
  163. this.props.onScroll(data);
  164. }
  165. }}
  166. onChange={(editor, data, value) => {
  167. if (this.props.onChange != null) {
  168. this.props.onChange(value);
  169. }
  170. // Emoji AutoComplete
  171. emojiAutoCompleteHelper.showHint(editor);
  172. }}
  173. onDragEnter={this.onDragEnter}
  174. />
  175. </Dropzone>
  176. )
  177. }
  178. }
  179. Editor.propTypes = {
  180. value: PropTypes.string,
  181. onChange: PropTypes.func,
  182. onScroll: PropTypes.func,
  183. onSave: PropTypes.func,
  184. onUpload: PropTypes.func,
  185. };