Editor.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/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. }
  22. render() {
  23. return (
  24. <CodeMirror
  25. value={this.state.value}
  26. autoFocus={true}
  27. options={{
  28. mode: 'gfm',
  29. theme: 'eclipse',
  30. lineNumbers: true,
  31. tabSize: 4,
  32. indentUnit: 4,
  33. lineWrapping: true,
  34. autoRefresh: true,
  35. autoCloseTags: true,
  36. matchBrackets: true,
  37. matchTags: {bothTags: true},
  38. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  39. highlightSelectionMatches: {annotateScrollbar: true},
  40. // markdown mode options
  41. highlightFormatting: true,
  42. // continuelist, indentlist
  43. extraKeys: {
  44. "Enter": "newlineAndIndentContinueMarkdownList",
  45. "Tab": "autoIndentMarkdownList",
  46. "Shift-Tab": "autoUnindentMarkdownList"
  47. }
  48. }}
  49. onScroll={(editor, data) => {
  50. if (this.props.onScroll != null) {
  51. this.props.onScroll(editor, data);
  52. }
  53. }}
  54. onChange={(editor, data, value) => {
  55. if (this.props.onChange != null) {
  56. this.props.onChange(value);
  57. }
  58. }}
  59. />
  60. )
  61. }
  62. }
  63. Editor.propTypes = {
  64. value: PropTypes.string,
  65. onChange: PropTypes.func,
  66. onScroll: PropTypes.func,
  67. };