Editor.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/edit/indentlist');
  11. require('codemirror/mode/gfm/gfm');
  12. require('codemirror/theme/eclipse.css');
  13. export default class Editor extends React.Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. value: this.props.value,
  18. };
  19. }
  20. render() {
  21. return (
  22. <CodeMirror
  23. value={this.state.value}
  24. autoFocus={true}
  25. options={{
  26. mode: 'gfm',
  27. theme: 'eclipse',
  28. lineNumbers: true,
  29. tabSize: 4,
  30. indentUnit: 4,
  31. autoRefresh: true,
  32. autoCloseTags: true,
  33. matchBrackets: true,
  34. matchTags: {bothTags: true},
  35. lineWrapping: true,
  36. // markdown mode options
  37. highlightFormatting: true,
  38. // continuelist, indentlist
  39. extraKeys: {
  40. "Enter": "newlineAndIndentContinueMarkdownList",
  41. "Tab": "autoIndentMarkdownList",
  42. "Shift-Tab": "autoUnindentMarkdownList"
  43. }
  44. }}
  45. onScroll={(editor, data) => {
  46. if (this.props.onScroll != null) {
  47. this.props.onScroll(editor, data);
  48. }
  49. }}
  50. onChange={(editor, data, value) => {
  51. if (this.props.onChange != null) {
  52. this.props.onChange(value);
  53. }
  54. }}
  55. />
  56. )
  57. }
  58. }
  59. Editor.propTypes = {
  60. value: PropTypes.string,
  61. onChange: PropTypes.func,
  62. onScroll: PropTypes.func,
  63. };