Editor.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. require('../../../../local_modules/codemirror-markdown-list-autoindentlist');
  16. export default class Editor extends React.Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. value: this.props.value,
  21. };
  22. this.getCodeMirror = this.getCodeMirror.bind(this);
  23. this.setCaretLine = this.setCaretLine.bind(this);
  24. this.forceToFocus = this.forceToFocus.bind(this);
  25. this.dispatchSave = this.dispatchSave.bind(this);
  26. }
  27. componentDidMount() {
  28. // initialize caret line
  29. this.setCaretLine(0);
  30. // set save handler
  31. codemirror.commands.save = this.dispatchSave;
  32. }
  33. getCodeMirror() {
  34. return this.refs.cm.editor;
  35. }
  36. forceToFocus() {
  37. const editor = this.getCodeMirror();
  38. // use setInterval with reluctance -- 2018.01.11 Yuki Takei
  39. const intervalId = setInterval(() => {
  40. this.getCodeMirror().focus();
  41. if (editor.hasFocus()) {
  42. clearInterval(intervalId);
  43. }
  44. }, 100);
  45. }
  46. /**
  47. * set caret position of codemirror
  48. * @param {string} number
  49. */
  50. setCaretLine(line) {
  51. const editor = this.getCodeMirror();
  52. editor.setCursor({line: line-1}); // leave 'ch' field as null/undefined to indicate the end of line
  53. }
  54. /**
  55. * dispatch onSave event
  56. */
  57. dispatchSave() {
  58. if (this.props.onSave != null) {
  59. this.props.onSave();
  60. }
  61. }
  62. render() {
  63. return (
  64. <ReactCodeMirror
  65. ref="cm"
  66. value={this.state.value}
  67. options={{
  68. mode: 'gfm',
  69. theme: 'eclipse',
  70. lineNumbers: true,
  71. tabSize: 4,
  72. indentUnit: 4,
  73. lineWrapping: true,
  74. autoRefresh: true,
  75. autoCloseTags: true,
  76. matchBrackets: true,
  77. matchTags: {bothTags: true},
  78. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  79. highlightSelectionMatches: {annotateScrollbar: true},
  80. // markdown mode options
  81. highlightFormatting: true,
  82. // continuelist, indentlist
  83. extraKeys: {
  84. "Enter": "newlineAndIndentContinueMarkdownList",
  85. "Tab": "autoIndentMarkdownList",
  86. }
  87. }}
  88. onScroll={(editor, data) => {
  89. if (this.props.onScroll != null) {
  90. this.props.onScroll(editor, 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. };