index.jsx 2.6 KB
Newer Older
J
Jason Park 已提交
1 2
import React from 'react';
import AceEditor from 'react-ace';
3
import 'brace/mode/plain_text';
J
Jason Park 已提交
4
import 'brace/mode/markdown';
J
Jason Park 已提交
5
import 'brace/mode/javascript';
J
Jason Park 已提交
6 7 8
import 'brace/mode/c_cpp';
import 'brace/mode/java';
import 'brace/mode/python';
J
Jason Park 已提交
9
import 'brace/theme/tomorrow_night_eighties';
10
import 'brace/ext/searchbox';
J
Jason Park 已提交
11
import { tracerManager } from '/core';
J
Jason Park 已提交
12
import { classes, extension } from '/common/util';
J
Jason Park 已提交
13
import { ContributorsViewer } from '/components';
J
Jason Park 已提交
14 15 16 17
import { actions } from '/reducers';
import { connect } from 'react-redux';
import styles from './stylesheet.scss';
import { languages } from '/common/config';
J
Jason Park 已提交
18

J
Jason Park 已提交
19
@connect(({ current }) => ({ current }), actions)
20
class CodeEditor extends React.Component {
J
Jason Park 已提交
21 22 23 24
  constructor(props) {
    super(props);

    this.state = {
25
      lineMarker: null,
J
Jason Park 已提交
26 27 28 29
    };
  }

  componentDidMount() {
30
    const { file } = this.props;
31
    tracerManager.setFile(file);
32

J
Jason Park 已提交
33 34 35
    tracerManager.setOnUpdateLineIndicator(lineIndicator => this.setState({ lineMarker: this.createLineMarker(lineIndicator) }));
  }

J
Jason Park 已提交
36
  componentWillReceiveProps(nextProps) {
37
    const { file } = nextProps;
38 39 40
    if (file !== this.props.file) {
      tracerManager.setFile(file);
    }
J
Jason Park 已提交
41 42
  }

J
Jason Park 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  componentWillUnmount() {
    tracerManager.setOnUpdateLineIndicator(null);
  }

  createLineMarker(lineIndicator) {
    if (lineIndicator === null) return null;
    const { lineNumber, cursor } = lineIndicator;
    return {
      startRow: lineNumber,
      startCol: 0,
      endRow: lineNumber,
      endCol: Infinity,
      className: styles.current_line_marker,
      type: 'line',
      inFront: true,
      _key: cursor,
    };
  }

J
Jason Park 已提交
62
  handleChangeCode(code) {
J
Jason Park 已提交
63 64
    const { file } = this.props;
    this.props.modifyFile({ ...file, content: code });
J
Jason Park 已提交
65 66
  }

J
Jason Park 已提交
67
  render() {
68
    const { className, file } = this.props;
J
Jason Park 已提交
69 70 71 72 73
    const { lineMarker } = this.state;

    const fileExt = extension(file.name);
    const language = languages.find(language => language.ext === fileExt);
    const mode = language ? language.mode : fileExt === 'md' ? 'markdown' : 'plain_text';
J
Jason Park 已提交
74

75
    return (
J
Jason Park 已提交
76 77 78
      <div className={classes(styles.code_editor, className)}>
        <AceEditor
          className={styles.ace_editor}
J
Jason Park 已提交
79
          mode={mode}
J
Jason Park 已提交
80 81 82 83 84
          theme="tomorrow_night_eighties"
          name="code_editor"
          editorProps={{ $blockScrolling: true }}
          onChange={code => this.handleChangeCode(code)}
          markers={lineMarker ? [lineMarker] : []}
J
Jason Park 已提交
85
          value={file.content} />
J
Jason Park 已提交
86
        <ContributorsViewer contributors={file.contributors} />
87
      </div> // TODO: need resizing when parent resizes
J
Jason Park 已提交
88 89 90 91
    );
  }
}

92
export default CodeEditor;
J
Jason Park 已提交
93