index.jsx 4.8 KB
Newer Older
J
Jason Park 已提交
1 2 3 4 5 6 7 8 9 10 11
import React from 'react';
import { connect } from 'react-redux';
import AceEditor from 'react-ace';
import 'brace/mode/javascript';
import 'brace/theme/tomorrow_night_eighties';
import FontAwesomeIcon from '@fortawesome/react-fontawesome'
import faInfoCircle from '@fortawesome/fontawesome-free-solid/faInfoCircle';
import { calculatePercentageHeight, classes } from '/common/util';
import { Divider, Ellipsis, TabBar } from '/components';
import { actions as envActions } from '/reducers/env';
import { tracerManager } from '/core';
12
import { DirectoryApi } from '/apis';
J
Jason Park 已提交
13 14 15
import styles from './stylesheet.scss';

@connect(
16
  ({ env }) => ({
J
Jason Park 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29
    env,
  }), {
    ...envActions,
  }
)
class EditorSection extends React.Component {
  constructor(props) {
    super(props);

    const { lineIndicator } = tracerManager;
    this.state = {
      dataContainerHeight: '30%',
      lineMarker: this.createLineMarker(lineIndicator),
30 31
      data: '',
      code: '',
J
Jason Park 已提交
32 33 34 35
    };
  }

  componentDidMount() {
36 37
    tracerManager.setDataGetter(() => this.state.data);
    tracerManager.setCodeGetter(() => this.state.code);
J
Jason Park 已提交
38
    tracerManager.setOnUpdateLineIndicator(lineIndicator => this.setState({ lineMarker: this.createLineMarker(lineIndicator) }));
J
Jason Park 已提交
39 40 41

    const { categoryKey, algorithmKey, fileKey } = this.props.env;
    this.loadCodeAndData(categoryKey, algorithmKey, fileKey);
J
Jason Park 已提交
42 43 44
  }

  componentWillUnmount() {
45 46
    tracerManager.setDataGetter(null);
    tracerManager.setCodeGetter(null);
J
Jason Park 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    tracerManager.setOnUpdateLineIndicator(null);
  }

  componentWillReceiveProps(nextProps) {
    const { categoryKey, algorithmKey, fileKey } = nextProps.env;
    if (categoryKey !== this.props.env.categoryKey ||
      algorithmKey !== this.props.env.algorithmKey ||
      fileKey !== this.props.env.fileKey) {
      this.loadCodeAndData(categoryKey, algorithmKey, fileKey);
    }
  }

  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,
    };
  }

  loadCodeAndData(categoryKey, algorithmKey, fileKey) {
75 76
    DirectoryApi.getFile(categoryKey, algorithmKey, fileKey)
      .then(({ data, code }) => {
J
Jason Park 已提交
77
        this.setState({ data, code }, () => tracerManager.runData());
78
      });
J
Jason Park 已提交
79 80 81 82 83 84 85 86
  }

  handleResizeDataContainer(x, y) {
    const dataContainerHeight = calculatePercentageHeight(this.elContent, y);
    this.setState({ dataContainerHeight });
  }

  handleChangeData(data) {
87
    this.setState({ data }, () => tracerManager.runData());
J
Jason Park 已提交
88 89 90
  }

  handleChangeCode(code) {
91
    this.setState({ code }, () => tracerManager.runData());
J
Jason Park 已提交
92 93 94
  }

  render() {
95
    const { dataContainerHeight, lineMarker, data, code } = this.state;
J
Jason Park 已提交
96
    const { className } = this.props;
97
    const { categories, categoryKey, algorithmKey, fileKey } = this.props.env;
J
Jason Park 已提交
98

99 100
    const category = categories.find(category => category.key === categoryKey);
    const algorithm = category.algorithms.find(algorithm => algorithm.key === algorithmKey);
J
Jason Park 已提交
101 102 103 104 105 106 107
    const tabs = algorithm.files.map(file => ({
      title: file.name,
      props: {
        to: `/${category.key}/${algorithm.key}/${file.key}`
      },
    }));
    const tabIndex = algorithm.files.findIndex(file => file.key === fileKey);
108
    const fileInfo = ''; // TODO
J
Jason Park 已提交
109 110 111

    return (
      <section className={classes(styles.editor_section, className)}>
J
Jason Park 已提交
112
        <TabBar tabs={tabs} tabIndex={tabIndex} />
J
Jason Park 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        <div className={styles.info_container}>
          <FontAwesomeIcon fixedWidth icon={faInfoCircle} className={styles.info_icon} />
          <Ellipsis className={styles.info_text}>{fileInfo}</Ellipsis>
        </div>
        <div className={styles.content} ref={ref => this.elContent = ref}>
          <div className={styles.data_container} style={{ height: dataContainerHeight }}>
            <AceEditor
              className={styles.editor}
              mode="javascript"
              theme="tomorrow_night_eighties"
              name="data_editor"
              editorProps={{ $blockScrolling: true }}
              onChange={value => this.handleChangeData(value)}
              value={data} />
          </div>
          <Divider horizontal onResize={(x, y) => this.handleResizeDataContainer(x, y)} />
          <div className={styles.code_container}>
            <AceEditor
              className={styles.editor}
              mode="javascript"
              theme="tomorrow_night_eighties"
              name="code_editor"
              editorProps={{ $blockScrolling: true }}
              onChange={value => this.handleChangeCode(value)}
              markers={lineMarker ? [lineMarker] : []}
              value={code} />
          </div>
        </div>
      </section>
    );
  }
}

export default EditorSection;