Editor.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. };
  30. this.getCodeMirror = this.getCodeMirror.bind(this);
  31. this.setCaretLine = this.setCaretLine.bind(this);
  32. this.forceToFocus = this.forceToFocus.bind(this);
  33. this.dispatchSave = this.dispatchSave.bind(this);
  34. this.onDragEnterForCM = this.onDragEnterForCM.bind(this);
  35. this.onDragLeave = this.onDragLeave.bind(this);
  36. this.onDrop = this.onDrop.bind(this);
  37. this.renderOverlayMessage = this.renderOverlayMessage.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. onDragEnterForCM(editor, event) {
  91. const dataTransfer = event.dataTransfer;
  92. // do nothing if contents is not files
  93. if (!dataTransfer.types.includes('Files')) {
  94. return;
  95. }
  96. this.setState({
  97. dropzoneActive: true
  98. });
  99. }
  100. onDragLeave() {
  101. this.setState({
  102. dropzoneActive: false
  103. });
  104. }
  105. onDrop(accepted, rejected) {
  106. // if (!this.props.isUploadable) {
  107. // return;
  108. // }
  109. // this.setState({
  110. // dropzoneActive: false
  111. // });
  112. // // TODO abort multi files
  113. // this.dispatchUpload(files[0]);
  114. }
  115. renderOverlayMessage() {
  116. const overlayStyle = {
  117. position: 'absolute',
  118. zIndex: 1060, // FIXME: required because .content-main.on-edit has 'z-index:1050'
  119. top: 0,
  120. right: 0,
  121. bottom: 0,
  122. left: 0,
  123. };
  124. return (
  125. <div style={overlayStyle} className="dropzone-overlay"></div>
  126. );
  127. }
  128. render() {
  129. const dropzoneStyle = {
  130. height: '100%'
  131. }
  132. let accept = null;
  133. let className = 'dropzone';
  134. if (this.props.isUploadable) {
  135. accept = 'image/*'
  136. className += ' dropzone-uploadable';
  137. if (this.props.isUploadableFile) {
  138. className += ' dropzone-uploadablefile';
  139. accept = '';
  140. }
  141. }
  142. return (
  143. <Dropzone
  144. disableClick
  145. disablePreview={true}
  146. style={dropzoneStyle}
  147. accept={accept}
  148. className={className}
  149. acceptClassName="dropzone-accepted"
  150. rejectClassName="dropzone-rejected"
  151. multiple={false}
  152. onDragLeave={this.onDragLeave}
  153. onDrop={this.onDrop}
  154. >
  155. { this.state.dropzoneActive && this.renderOverlayMessage() }
  156. <ReactCodeMirror
  157. ref="cm"
  158. editorDidMount={(editor) => {
  159. // add event handlers
  160. editor.on('paste', pasteHelper.pasteHandler);
  161. }}
  162. value={this.state.value}
  163. options={{
  164. mode: 'gfm',
  165. theme: 'eclipse',
  166. lineNumbers: true,
  167. tabSize: 4,
  168. indentUnit: 4,
  169. lineWrapping: true,
  170. autoRefresh: true,
  171. autoCloseTags: true,
  172. matchBrackets: true,
  173. matchTags: {bothTags: true},
  174. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  175. highlightSelectionMatches: {annotateScrollbar: true},
  176. // markdown mode options
  177. highlightFormatting: true,
  178. // continuelist, indentlist
  179. extraKeys: {
  180. "Enter": "newlineAndIndentContinueMarkdownList",
  181. "Tab": "indentMore",
  182. "Shift-Tab": "indentLess",
  183. }
  184. }}
  185. onScroll={(editor, data) => {
  186. if (this.props.onScroll != null) {
  187. this.props.onScroll(data);
  188. }
  189. }}
  190. onChange={(editor, data, value) => {
  191. if (this.props.onChange != null) {
  192. this.props.onChange(value);
  193. }
  194. // Emoji AutoComplete
  195. emojiAutoCompleteHelper.showHint(editor);
  196. }}
  197. onDragEnter={this.onDragEnterForCM}
  198. />
  199. </Dropzone>
  200. )
  201. }
  202. }
  203. Editor.propTypes = {
  204. value: PropTypes.string,
  205. isUploadable: PropTypes.bool,
  206. isUploadableFile: PropTypes.bool,
  207. onChange: PropTypes.func,
  208. onScroll: PropTypes.func,
  209. onSave: PropTypes.func,
  210. onUpload: PropTypes.func,
  211. };