"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

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

var _instance = _interopRequireDefault(require("./instance.js"));

var _excluded = ["strokeStyle", "lineWidth", "quality", "width", "height"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var Handsign = /*#__PURE__*/function (_Component) {
  _inheritsLoose(Handsign, _Component);

  function Handsign(props) {
    return _Component.call(this, props) || this;
  }

  var _proto = Handsign.prototype;

  _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
    if (this.instance) {
      var params = {};

      if (prevProps.strokeStyle !== this.props.strokeStyle) {
        params.strokeStyle = this.props.strokeStyle;
      }

      if (prevProps.lineWidth !== this.props.lineWidth) {
        params.lineWidth = this.props.lineWidth;
      }

      if (prevProps.quality !== this.props.quality) {
        params.quality = this.props.quality;
      }

      if (prevProps.suffix !== this.props.suffix) {
        params.suffix = this.props.suffix;
      }

      if (Object.getOwnPropertyNames(params) && Object.getOwnPropertyNames(params).length) {
        this.instance.updateParams(params);
      }
    }
  };

  _proto.componentDidMount = function componentDidMount() {
    if (this.instance) return;
    var instance = new _instance["default"](this.$el, {
      strokeStyle: this.props.strokeStyle,
      lineWidth: this.props.lineWidth,
      quality: this.props.quality,
      suffix: this.props.suffix
    });
    this.instance = instance;
  };

  _proto.render = function render() {
    var _this = this;

    var _this$props = this.props,
        strokeStyle = _this$props.strokeStyle,
        lineWidth = _this$props.lineWidth,
        quality = _this$props.quality,
        width = _this$props.width,
        height = _this$props.height,
        others = _objectWithoutPropertiesLoose(_this$props, _excluded);

    return /*#__PURE__*/_react["default"].createElement("canvas", _extends({
      ref: function ref(el) {
        _this.$el = el;
      }
    }, others, {
      width: width,
      height: height
    }), "Canvas\u753B\u677F");
  };

  return Handsign;
}(_react.Component);

exports["default"] = Handsign;

_defineProperty(Handsign, "defaultProps", {
  strokeStyle: '#000',
  lineWidth: 3,
  quality: 0.92,
  width: 300,
  height: 300
});

Handsign.propTypes = process.env.NODE_ENV !== "production" ? {
  strokeStyle: _propTypes["default"].string,
  lineWidth: _propTypes["default"].number,
  quality: _propTypes["default"].number,
  suffix: _propTypes["default"].string,
  // 不能使用style制定宽高,canvas用style的width|height会导致绘图位置不正确
  width: _propTypes["default"].number,
  // 宽度
  height: _propTypes["default"].number // 高度

} : {};
module.exports = exports.default;