"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.ImageToolbar = 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 _propTypes = _interopRequireDefault(require("prop-types"));

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

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

var _reactDom = _interopRequireDefault(require("react-dom"));

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

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

var _toolbarButtons = require("../toolbar/toolbar-buttons");

var _altDialog = _interopRequireDefault(require("./alt-dialog"));

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 log = (0, _debug["default"])('@pie-lib:editable-html:plugins:image:image-toolbar');

var AlignmentButton = function AlignmentButton(_ref) {
  var alignment = _ref.alignment,
      active = _ref.active,
      onClick = _ref.onClick;
  return /*#__PURE__*/_react["default"].createElement(_toolbarButtons.MarkButton, {
    active: active,
    onToggle: function onToggle() {
      return onClick(alignment);
    },
    label: alignment
  }, alignment);
};

AlignmentButton.propTypes = {
  alignment: _propTypes["default"].string.isRequired,
  active: _propTypes["default"].bool.isRequired,
  onClick: _propTypes["default"].func.isRequired
};

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

  var _super = _createSuper(ImageToolbar);

  function ImageToolbar() {
    var _this;

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

    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), "onAltTextDone", function (newAlt) {
      log('[onAltTextDone]: alt:', newAlt);

      _this.props.onChange({
        alt: newAlt
      }, true);
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onAlignmentClick", function (alignment) {
      log('[onAlignmentClick]: alignment:', alignment);

      _this.props.onChange({
        alignment: alignment
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "renderDialog", function () {
      var alt = _this.props.alt;
      var popoverEl = document.createElement('div');

      var el = /*#__PURE__*/_react["default"].createElement(_altDialog["default"], {
        alt: alt,
        onDone: _this.onAltTextDone
      });

      _reactDom["default"].render(el, popoverEl);

      document.body.appendChild(popoverEl);
    });
    return _this;
  }

  (0, _createClass2["default"])(ImageToolbar, [{
    key: "render",
    value: function render() {
      var _classNames,
          _this2 = this;

      var _this$props = this.props,
          classes = _this$props.classes,
          alignment = _this$props.alignment,
          imageLoaded = _this$props.imageLoaded,
          disableImageAlignmentButtons = _this$props.disableImageAlignmentButtons;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.holder
      }, !disableImageAlignmentButtons && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(AlignmentButton, {
        alignment: 'left',
        active: alignment === 'left',
        onClick: this.onAlignmentClick
      }), /*#__PURE__*/_react["default"].createElement(AlignmentButton, {
        alignment: 'center',
        active: alignment === 'center',
        onClick: this.onAlignmentClick
      }), /*#__PURE__*/_react["default"].createElement(AlignmentButton, {
        alignment: 'right',
        active: alignment === 'right',
        onClick: this.onAlignmentClick
      })), /*#__PURE__*/_react["default"].createElement("span", {
        className: (0, _classnames["default"])((_classNames = {}, (0, _defineProperty2["default"])(_classNames, classes.disabled, !imageLoaded), (0, _defineProperty2["default"])(_classNames, classes.altButton, !disableImageAlignmentButtons), _classNames)),
        onMouseDown: function onMouseDown(event) {
          return imageLoaded && _this2.renderDialog(event);
        }
      }, "Alt text"));
    }
  }]);
  return ImageToolbar;
}(_react["default"].Component);

exports.ImageToolbar = ImageToolbar;
(0, _defineProperty2["default"])(ImageToolbar, "propTypes", {
  onChange: _propTypes["default"].func.isRequired,
  classes: _propTypes["default"].object.isRequired,
  alignment: _propTypes["default"].string,
  alt: _propTypes["default"].string,
  imageLoaded: _propTypes["default"].bool,
  disableImageAlignmentButtons: _propTypes["default"].bool
});

var styles = function styles(theme) {
  return {
    holder: {
      paddingLeft: theme.spacing.unit,
      display: 'flex',
      alignItems: 'center'
    },
    disabled: {
      opacity: 0.5
    },
    altButton: {
      borderLeft: '1px solid grey',
      paddingLeft: 8,
      marginLeft: 4
    }
  };
};

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

exports["default"] = _default;
//# sourceMappingURL=image-toolbar.js.map