Editor.js 1.5 KB

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