Editor.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { UnControlled as CodeMirror } from 'react-codemirror2';
  4. require('codemirror/lib/codemirror.css');
  5. require('codemirror/addon/display/autorefresh');
  6. require('codemirror/addon/edit/matchbrackets');
  7. require('codemirror/addon/edit/matchtags');
  8. require('codemirror/addon/edit/closetag');
  9. require('codemirror/addon/edit/continuelist');
  10. require('codemirror/addon/search/match-highlighter');
  11. require('codemirror/addon/scroll/annotatescrollbar');
  12. require('codemirror/mode/gfm/gfm');
  13. require('codemirror/theme/eclipse.css');
  14. require('../../../../local_modules/codemirror-markdown-list-autoindentlist');
  15. export default class Editor extends React.Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. value: this.props.value,
  20. };
  21. this.getCodeMirror = this.getCodeMirror.bind(this);
  22. this.setCaretLine = this.setCaretLine.bind(this);
  23. this.forceToFocus = this.forceToFocus.bind(this);
  24. }
  25. componentDidMount() {
  26. // initialize caret line
  27. this.setCaretLine(0);
  28. }
  29. getCodeMirror() {
  30. return this.refs.cm.editor;
  31. }
  32. forceToFocus() {
  33. const editor = this.getCodeMirror();
  34. // use setInterval with reluctance -- 2018.01.11 Yuki Takei
  35. const intervalId = setInterval(() => {
  36. this.getCodeMirror().focus();
  37. if (editor.hasFocus()) {
  38. clearInterval(intervalId);
  39. }
  40. }, 100);
  41. }
  42. /**
  43. * set caret position of codemirror
  44. * @param {string} number
  45. */
  46. setCaretLine(line) {
  47. const editor = this.getCodeMirror();
  48. editor.setCursor({line: line-1}); // leave 'ch' field as null/undefined to indicate the end of line
  49. }
  50. render() {
  51. return (
  52. <CodeMirror
  53. ref="cm"
  54. value={this.state.value}
  55. options={{
  56. mode: 'gfm',
  57. theme: 'eclipse',
  58. lineNumbers: true,
  59. tabSize: 4,
  60. indentUnit: 4,
  61. lineWrapping: true,
  62. autoRefresh: true,
  63. autoCloseTags: true,
  64. matchBrackets: true,
  65. matchTags: {bothTags: true},
  66. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  67. highlightSelectionMatches: {annotateScrollbar: true},
  68. // markdown mode options
  69. highlightFormatting: true,
  70. // continuelist, indentlist
  71. extraKeys: {
  72. "Enter": "newlineAndIndentContinueMarkdownList",
  73. "Tab": "autoIndentMarkdownList",
  74. "Shift-Tab": "autoUnindentMarkdownList"
  75. }
  76. }}
  77. onScroll={(editor, data) => {
  78. if (this.props.onScroll != null) {
  79. this.props.onScroll(editor, data);
  80. }
  81. }}
  82. onChange={(editor, data, value) => {
  83. if (this.props.onChange != null) {
  84. this.props.onChange(value);
  85. }
  86. }}
  87. />
  88. )
  89. }
  90. }
  91. Editor.propTypes = {
  92. value: PropTypes.string,
  93. onChange: PropTypes.func,
  94. onScroll: PropTypes.func,
  95. };