Editor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import emojione from 'emojione';
  4. import emojiStrategy from 'emojione/emoji_strategy.json';
  5. import * as codemirror from 'codemirror';
  6. import { UnControlled as ReactCodeMirror } from 'react-codemirror2';
  7. require('codemirror/lib/codemirror.css');
  8. require('codemirror/addon/display/autorefresh');
  9. require('codemirror/addon/edit/matchbrackets');
  10. require('codemirror/addon/edit/matchtags');
  11. require('codemirror/addon/edit/closetag');
  12. require('codemirror/addon/edit/continuelist');
  13. require('codemirror/addon/hint/show-hint');
  14. require('codemirror/addon/hint/show-hint.css');
  15. require('codemirror/addon/search/searchcursor');
  16. require('codemirror/addon/search/match-highlighter');
  17. require('codemirror/addon/scroll/annotatescrollbar');
  18. require('codemirror/mode/gfm/gfm');
  19. require('codemirror/theme/eclipse.css');
  20. import pasteHelper from './PasteHelper';
  21. import emojiAutoCompleteHelper from './EmojiAutoCompleteHelper';
  22. export default class Editor extends React.Component {
  23. constructor(props) {
  24. super(props);
  25. // https://regex101.com/r/7BN2fR/2
  26. this.indentAndMarkPattern = /^([ \t]*)(?:>|\-|\+|\*|\d+\.) /;
  27. this.state = {
  28. value: this.props.value,
  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. }
  35. componentDidMount() {
  36. // initialize caret line
  37. this.setCaretLine(0);
  38. // set save handler
  39. codemirror.commands.save = this.dispatchSave;
  40. }
  41. getCodeMirror() {
  42. return this.refs.cm.editor;
  43. }
  44. forceToFocus() {
  45. const editor = this.getCodeMirror();
  46. // use setInterval with reluctance -- 2018.01.11 Yuki Takei
  47. const intervalId = setInterval(() => {
  48. this.getCodeMirror().focus();
  49. if (editor.hasFocus()) {
  50. clearInterval(intervalId);
  51. }
  52. }, 100);
  53. }
  54. /**
  55. * set caret position of codemirror
  56. * @param {string} number
  57. */
  58. setCaretLine(line) {
  59. const editor = this.getCodeMirror();
  60. editor.setCursor({line: line-1}); // leave 'ch' field as null/undefined to indicate the end of line
  61. }
  62. /**
  63. * dispatch onSave event
  64. */
  65. dispatchSave() {
  66. if (this.props.onSave != null) {
  67. this.props.onSave();
  68. }
  69. }
  70. render() {
  71. return (
  72. <ReactCodeMirror
  73. ref="cm"
  74. editorDidMount={(editor) => {
  75. editor.on('paste', pasteHelper.pasteHandler);
  76. }}
  77. value={this.state.value}
  78. options={{
  79. mode: 'gfm',
  80. theme: 'eclipse',
  81. lineNumbers: true,
  82. tabSize: 4,
  83. indentUnit: 4,
  84. lineWrapping: true,
  85. autoRefresh: true,
  86. autoCloseTags: true,
  87. matchBrackets: true,
  88. matchTags: {bothTags: true},
  89. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  90. highlightSelectionMatches: {annotateScrollbar: true},
  91. // markdown mode options
  92. highlightFormatting: true,
  93. // continuelist, indentlist
  94. extraKeys: {
  95. "Enter": "newlineAndIndentContinueMarkdownList",
  96. "Tab": "indentMore",
  97. "Shift-Tab": "indentLess",
  98. }
  99. }}
  100. onScroll={(editor, data) => {
  101. if (this.props.onScroll != null) {
  102. this.props.onScroll(data);
  103. }
  104. }}
  105. onChange={(editor, data, value) => {
  106. if (this.props.onChange != null) {
  107. this.props.onChange(value);
  108. }
  109. // Emoji AutoComplete
  110. emojiAutoCompleteHelper.showHint(editor);
  111. }}
  112. />
  113. )
  114. }
  115. }
  116. Editor.propTypes = {
  117. value: PropTypes.string,
  118. onChange: PropTypes.func,
  119. onScroll: PropTypes.func,
  120. onSave: PropTypes.func,
  121. };