"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.Input = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _styles = require("@material-ui/core/styles");

var _debug = _interopRequireDefault(require("debug"));

var _classnames = _interopRequireDefault(require("classnames"));

var _customElements = require("./custom-elements");

var _mathquill = _interopRequireDefault(require("@pie-framework/mathquill"));

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var MQ;

if (typeof window !== 'undefined') {
  MQ = _mathquill["default"].getInterface(2);

  if (MQ && MQ.registerEmbed) {
    (0, _customElements.registerLineBreak)(MQ);
  }
}

var log = (0, _debug["default"])('math-input:mq:input');
/**
 * Wrapper for MathQuill MQ.MathField.
 */

var Input = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Input, _React$Component);

  var _super = _createSuper(Input);

  function Input() {
    var _this;

    (0, _classCallCheck2["default"])(this, Input);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onInputEdit", function () {
      log('[onInputEdit] ...');
      var onChange = _this.props.onChange;

      if (!_this.mathField) {
        return;
      }

      if (onChange) {
        onChange(_this.mathField.latex());
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "refresh", function () {
      _this.blur();

      _this.focus();
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onKeyPress", function (event) {
      var keys = Object.keys(_this.mathField.__controller.options);

      if (keys.indexOf('ignoreNextMousedown') < 0) {
        // It seems like the controller has the above handler as an option
        // when all the right events are set and everything works fine
        // this seems to work in all cases
        _this.refresh();
      }

      if (event.charCode === 13) {
        event.preventDefault();
        return;
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onClick", function (event) {
      var onClick = _this.props.onClick;

      _this.refresh();

      onClick && onClick(event);
    });
    return _this;
  }

  (0, _createClass2["default"])(Input, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (!MQ) {
        throw new Error('MQ is not defined - but component has mounted?');
      }

      this.mathField = MQ.MathField(this.input, {
        handlers: {
          edit: this.onInputEdit.bind(this)
        }
      });
      this.updateLatex();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.updateLatex();
    }
  }, {
    key: "updateLatex",
    value: function updateLatex() {
      if (!this.mathField) {
        return;
      }

      var latex = this.props.latex;

      if (latex !== undefined && latex !== null) {
        this.mathField.latex(latex);
      }
    }
  }, {
    key: "clear",
    value: function clear() {
      this.mathField.latex('');
      return '';
    }
  }, {
    key: "blur",
    value: function blur() {
      log('blur mathfield');
      this.mathField.blur();
    }
  }, {
    key: "focus",
    value: function focus() {
      log('focus mathfield...');
      this.mathField.focus();
    }
  }, {
    key: "command",
    value: function command(v) {
      var _this2 = this;

      log('command: ', v);

      if (Array.isArray(v)) {
        v.forEach(function (vv) {
          _this2.mathField.cmd(vv);
        });
      } else {
        this.mathField.cmd(v);
      }

      this.mathField.focus();
      return this.mathField.latex();
    }
  }, {
    key: "keystroke",
    value: function keystroke(v) {
      this.mathField.keystroke(v);
      this.mathField.focus();
      return this.mathField.latex();
    }
  }, {
    key: "write",
    value: function write(v) {
      log('write: ', v);
      this.mathField.write(v);
      this.mathField.focus();
      return this.mathField.latex();
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      log('next: ', nextProps.latex);
      log('current: ', this.mathField.latex());
      return nextProps.latex !== this.mathField.latex();
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _this$props = this.props,
          onFocus = _this$props.onFocus,
          onBlur = _this$props.onBlur,
          classes = _this$props.classes,
          className = _this$props.className;
      return /*#__PURE__*/_react["default"].createElement("span", {
        className: (0, _classnames["default"])(classes.input, className),
        onKeyDown: this.onKeyPress,
        onClick: this.onClick,
        onFocus: onFocus,
        onBlur: onBlur,
        ref: function ref(r) {
          return _this3.input = r;
        }
      });
    }
  }]);
  return Input;
}(_react["default"].Component);

exports.Input = Input;
(0, _defineProperty2["default"])(Input, "propTypes", {
  className: _propTypes["default"].string,
  classes: _propTypes["default"].object.isRequired,
  onClick: _propTypes["default"].func,
  onChange: _propTypes["default"].func,
  latex: _propTypes["default"].string,
  onFocus: _propTypes["default"].func,
  onBlur: _propTypes["default"].func
});

var styles = function styles() {
  return {};
};

var _default = (0, _styles.withStyles)(styles)(Input);

exports["default"] = _default;
//# sourceMappingURL=input.js.map