| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import React from 'react';
- import PropTypes from 'prop-types';
- import { UnControlled as CodeMirror } from 'react-codemirror2';
- require('codemirror/lib/codemirror.css');
- require('codemirror/addon/display/autorefresh');
- require('codemirror/addon/edit/matchbrackets');
- require('codemirror/addon/edit/matchtags');
- require('codemirror/addon/edit/closetag');
- require('codemirror/addon/edit/continuelist');
- require('codemirror/addon/search/match-highlighter');
- require('codemirror/addon/scroll/annotatescrollbar');
- require('codemirror/mode/gfm/gfm');
- require('codemirror/theme/eclipse.css');
- require('../../../../local_modules/codemirror-markdown-list-autoindentlist');
- export default class Editor extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- value: this.props.value,
- };
- this.getCodeMirror = this.getCodeMirror.bind(this);
- // this.initCaretPosition = this.initCaretPosition.bind(this);
- }
- getCodeMirror() {
- return this.refs.cm.editor;
- }
- /**
- * initialize caret position of codemirror
- * @param {string} value
- */
- initCaretPosition(position) {
- const editor = this.getCodeMirror();
- console.log(editor);
- // editor.setCursor(0, position);
- editor.focus();
- editor.setCursor(3, 3);
- }
- render() {
- return (
- <CodeMirror
- ref="cm"
- value={this.state.value}
- options={{
- mode: 'gfm',
- theme: 'eclipse',
- lineNumbers: true,
- tabSize: 4,
- indentUnit: 4,
- lineWrapping: true,
- autoRefresh: true,
- autoCloseTags: true,
- matchBrackets: true,
- matchTags: {bothTags: true},
- // match-highlighter, matchesonscrollbar, annotatescrollbar options
- highlightSelectionMatches: {annotateScrollbar: true},
- // markdown mode options
- highlightFormatting: true,
- // continuelist, indentlist
- extraKeys: {
- "Enter": "newlineAndIndentContinueMarkdownList",
- "Tab": "autoIndentMarkdownList",
- "Shift-Tab": "autoUnindentMarkdownList"
- }
- }}
- onScroll={(editor, data) => {
- if (this.props.onScroll != null) {
- this.props.onScroll(editor, data);
- }
- }}
- onChange={(editor, data, value) => {
- if (this.props.onChange != null) {
- this.props.onChange(value);
- }
- }}
- />
- )
- }
- }
- Editor.propTypes = {
- value: PropTypes.string,
- onChange: PropTypes.func,
- onScroll: PropTypes.func,
- };
|