Editor.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.initCaretPosition = this.initCaretPosition.bind(this);
  23. }
  24. getCodeMirror() {
  25. return this.refs.cm.editor;
  26. }
  27. /**
  28. * initialize caret position of codemirror
  29. * @param {string} value
  30. */
  31. initCaretPosition(position) {
  32. const editor = this.getCodeMirror();
  33. console.log(editor);
  34. // editor.setCursor(0, position);
  35. editor.focus();
  36. editor.setCursor(3, 3);
  37. }
  38. render() {
  39. return (
  40. <CodeMirror
  41. ref="cm"
  42. value={this.state.value}
  43. options={{
  44. mode: 'gfm',
  45. theme: 'eclipse',
  46. lineNumbers: true,
  47. tabSize: 4,
  48. indentUnit: 4,
  49. lineWrapping: true,
  50. autoRefresh: true,
  51. autoCloseTags: true,
  52. matchBrackets: true,
  53. matchTags: {bothTags: true},
  54. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  55. highlightSelectionMatches: {annotateScrollbar: true},
  56. // markdown mode options
  57. highlightFormatting: true,
  58. // continuelist, indentlist
  59. extraKeys: {
  60. "Enter": "newlineAndIndentContinueMarkdownList",
  61. "Tab": "autoIndentMarkdownList",
  62. "Shift-Tab": "autoUnindentMarkdownList"
  63. }
  64. }}
  65. onScroll={(editor, data) => {
  66. if (this.props.onScroll != null) {
  67. this.props.onScroll(editor, data);
  68. }
  69. }}
  70. onChange={(editor, data, value) => {
  71. if (this.props.onChange != null) {
  72. this.props.onChange(value);
  73. }
  74. }}
  75. />
  76. )
  77. }
  78. }
  79. Editor.propTypes = {
  80. value: PropTypes.string,
  81. onChange: PropTypes.func,
  82. onScroll: PropTypes.func,
  83. };