all files / src/dashboard/components/ CssEditor.jsx

86.21% Statements 25/29
66.67% Branches 4/6
42.86% Functions 3/7
86.21% Lines 25/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                                                                                                                                      
import React from 'react';
import PropTypes from 'prop-types';
import Select from 'react-select';
 
import AceEditor from 'react-ace';
import 'brace/mode/css';
import 'brace/theme/github';
 
import ModalTrigger from '../../components/ModalTrigger';
import { t } from '../../locales';
 
const propTypes = {
  initialCss: PropTypes.string,
  triggerNode: PropTypes.node.isRequired,
  onChange: PropTypes.func,
  templates: PropTypes.array,
};
 
const defaultProps = {
  initialCss: '',
  onChange: () => {},
  templates: [],
};
 
class CssEditor extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      css: props.initialCss,
      cssTemplateOptions: [],
    };
  }
  changeCss(css) {
    this.setState({ css }, () => {
      this.props.onChange(css);
    });
  }
  changeCssTemplate(opt) {
    this.changeCss(opt.css);
  }
  renderTemplateSelector() {
    if (Ethis.props.templates) {
      return (
        <div style={{ zIndex: 10 }}>
          <h5>{t('Load a template')}</h5>
          <Select
            options={this.props.templates}
            placeholder={t('Load a CSS template')}
            onChange={this.changeCssTemplate.bind(this)}
          />
        </div>
      );
    }
    return null;
  }
  render() {
    return (
      <ModalTrigger
        triggerNode={this.props.triggerNode}
        modalTitle={t('CSS')}
        isMenuItem
        modalBody={
          <div>
            {this.renderTemplateSelector()}
            <div style={{ zIndex: 1 }}>
              <h5>{t('Live CSS Editor')}</h5>
              <div style={{ border: 'solid 1px grey' }}>
                <AceEditor
                  mode="css"
                  theme="github"
                  minLines={8}
                  maxLines={30}
                  onChange={this.changeCss.bind(this)}
                  height="200px"
                  width="100%"
                  editorProps={{ $blockScrolling: true }}
                  enableLiveAutocompletion
                  value={this.state.css || ''}
                />
              </div>
            </div>
          </div>
        }
      />
    );
  }
}
CssEditor.propTypes = propTypes;
CssEditor.defaultProps = defaultProps;
 
export default CssEditor;