'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _simplemde = require('simplemde');

var _simplemde2 = _interopRequireDefault(_simplemde);

require('simplemde/dist/simplemde.min.css');

require('./InstructionsEditor.css');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var InstructionsEditor = function (_Component) {
  _inherits(InstructionsEditor, _Component);

  function InstructionsEditor(props) {
    _classCallCheck(this, InstructionsEditor);

    var _this = _possibleConstructorReturn(this, (InstructionsEditor.__proto__ || Object.getPrototypeOf(InstructionsEditor)).call(this, props));

    _this.destroySimpleMDE = _this.destroySimpleMDE.bind(_this);
    return _this;
  }

  _createClass(InstructionsEditor, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _this2 = this;

      var _props = this.props,
          code = _props.code,
          onAnyCode = _props.onAnyCode;

      this.destroySimpleMDE();
      this.simplemde = new _simplemde2.default({
        element: this.textarea,
        initialValue: code,
        renderingConfig: {
          codeSyntaxHighlighting: true
        },
        showIcons: ['code', 'table'],
        hideIcons: ['preview'],
        shortcuts: {
          'drawLink': 'Cmd-U'
        }
      });
      this.simplemde.codemirror.on('change', function () {
        onAnyCode(_this2.simplemde.value());
      });
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      this.destroySimpleMDE();
    }
  }, {
    key: 'destroySimpleMDE',
    value: function destroySimpleMDE() {
      var simplemde = this.simplemde;
      if (simplemde) {
        simplemde.toTextArea();
        this.simplemde = null;
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _this3 = this;

      return _react2.default.createElement('textarea', {
        id: 'instructions-markdown-editor',
        ref: function ref(textarea) {
          _this3.textarea = textarea;
        }
      });
    }
  }]);

  return InstructionsEditor;
}(_react.Component);

InstructionsEditor.propTypes = {
  code: _propTypes2.default.string,
  onAnyCode: _propTypes2.default.func
};

InstructionsEditor.defaultProps = {
  code: ''
};

exports.default = InstructionsEditor;