Editor.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/search/match-highlighter');
  12. require('codemirror/addon/scroll/annotatescrollbar');
  13. require('codemirror/mode/gfm/gfm');
  14. require('codemirror/theme/eclipse.css');
  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. this.dispatchSave = this.dispatchSave.bind(this);
  25. }
  26. componentDidMount() {
  27. // initialize caret line
  28. this.setCaretLine(0);
  29. // set save handler
  30. codemirror.commands.save = this.dispatchSave;
  31. }
  32. getCodeMirror() {
  33. return this.refs.cm.editor;
  34. }
  35. forceToFocus() {
  36. const editor = this.getCodeMirror();
  37. // use setInterval with reluctance -- 2018.01.11 Yuki Takei
  38. const intervalId = setInterval(() => {
  39. this.getCodeMirror().focus();
  40. if (editor.hasFocus()) {
  41. clearInterval(intervalId);
  42. }
  43. }, 100);
  44. }
  45. /**
  46. * set caret position of codemirror
  47. * @param {string} number
  48. */
  49. setCaretLine(line) {
  50. const editor = this.getCodeMirror();
  51. editor.setCursor({line: line-1}); // leave 'ch' field as null/undefined to indicate the end of line
  52. }
  53. /**
  54. * dispatch onSave event
  55. */
  56. dispatchSave() {
  57. if (this.props.onSave != null) {
  58. this.props.onSave();
  59. }
  60. }
  61. render() {
  62. return (
  63. <ReactCodeMirror
  64. ref="cm"
  65. value={this.state.value}
  66. options={{
  67. mode: 'gfm',
  68. theme: 'eclipse',
  69. lineNumbers: true,
  70. tabSize: 4,
  71. indentUnit: 4,
  72. lineWrapping: true,
  73. autoRefresh: true,
  74. autoCloseTags: true,
  75. matchBrackets: true,
  76. matchTags: {bothTags: true},
  77. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  78. highlightSelectionMatches: {annotateScrollbar: true},
  79. // markdown mode options
  80. highlightFormatting: true,
  81. // continuelist, indentlist
  82. extraKeys: {
  83. "Enter": "newlineAndIndentContinueMarkdownList",
  84. "Tab": "indentMore",
  85. "Shift-Tab": "indentLess",
  86. }
  87. }}
  88. onScroll={(editor, data) => {
  89. if (this.props.onScroll != null) {
  90. this.props.onScroll(data);
  91. }
  92. }}
  93. onChange={(editor, data, value) => {
  94. if (this.props.onChange != null) {
  95. this.props.onChange(value);
  96. }
  97. }}
  98. />
  99. )
  100. }
  101. }
  102. Editor.propTypes = {
  103. value: PropTypes.string,
  104. onChange: PropTypes.func,
  105. onScroll: PropTypes.func,
  106. onSave: PropTypes.func,
  107. };