"use strict";

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

var _typeof = require("@babel/runtime/helpers/typeof");

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

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

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

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

var _Collapse = _interopRequireDefault(require("@material-ui/core/Collapse"));

var _List = _interopRequireDefault(require("@material-ui/core/List"));

var _ListItem = _interopRequireDefault(require("@material-ui/core/ListItem"));

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

var AlignLeft = function AlignLeft() {
  return /*#__PURE__*/_react["default"].createElement("svg", {
    width: "20",
    height: "20",
    viewBox: "0 0 66 66",
    fill: "none",
    xmlns: "http://www.w3.org/2000/svg"
  }, /*#__PURE__*/_react["default"].createElement("path", {
    d: "M42.1875 4.75C42.1875 7.38672 39.9902 9.4375 37.5 9.4375H4.6875C2.05078 9.4375 0 7.38672 0 4.75C0 2.25977 2.05078 0.0625 4.6875 0.0625H37.5C39.9902 0.0625 42.1875 2.25977 42.1875 4.75ZM42.1875 42.25C42.1875 44.8867 39.9902 46.9375 37.5 46.9375H4.6875C2.05078 46.9375 0 44.8867 0 42.25C0 39.7598 2.05078 37.5625 4.6875 37.5625H37.5C39.9902 37.5625 42.1875 39.7598 42.1875 42.25ZM0 23.5C0 21.0098 2.05078 18.8125 4.6875 18.8125H60.9375C63.4277 18.8125 65.625 21.0098 65.625 23.5C65.625 26.1367 63.4277 28.1875 60.9375 28.1875H4.6875C2.05078 28.1875 0 26.1367 0 23.5ZM65.625 61C65.625 63.6367 63.4277 65.6875 60.9375 65.6875H4.6875C2.05078 65.6875 0 63.6367 0 61C0 58.5098 2.05078 56.3125 4.6875 56.3125H60.9375C63.4277 56.3125 65.625 58.5098 65.625 61Z",
    fill: "currentColor"
  }));
};

exports.AlignLeft = AlignLeft;

var AlignRight = function AlignRight() {
  return /*#__PURE__*/_react["default"].createElement("svg", {
    width: "20",
    height: "20",
    viewBox: "0 0 66 66",
    fill: "none",
    xmlns: "http://www.w3.org/2000/svg"
  }, /*#__PURE__*/_react["default"].createElement("path", {
    d: "M65.625 4.75C65.625 7.38672 63.4277 9.4375 60.9375 9.4375H28.125C25.4883 9.4375 23.4375 7.38672 23.4375 4.75C23.4375 2.25977 25.4883 0.0625 28.125 0.0625H60.9375C63.4277 0.0625 65.625 2.25977 65.625 4.75ZM65.625 42.25C65.625 44.8867 63.4277 46.9375 60.9375 46.9375H28.125C25.4883 46.9375 23.4375 44.8867 23.4375 42.25C23.4375 39.7598 25.4883 37.5625 28.125 37.5625H60.9375C63.4277 37.5625 65.625 39.7598 65.625 42.25ZM0 23.5C0 21.0098 2.05078 18.8125 4.6875 18.8125H60.9375C63.4277 18.8125 65.625 21.0098 65.625 23.5C65.625 26.1367 63.4277 28.1875 60.9375 28.1875H4.6875C2.05078 28.1875 0 26.1367 0 23.5ZM65.625 61C65.625 63.6367 63.4277 65.6875 60.9375 65.6875H4.6875C2.05078 65.6875 0 63.6367 0 61C0 58.5098 2.05078 56.3125 4.6875 56.3125H60.9375C63.4277 56.3125 65.625 58.5098 65.625 61Z",
    fill: "currentColor"
  }));
};

exports.AlignRight = AlignRight;

var AlignCenter = function AlignCenter() {
  return /*#__PURE__*/_react["default"].createElement("svg", {
    width: "20",
    height: "20",
    viewBox: "0 0 66 66",
    fill: "none",
    xmlns: "http://www.w3.org/2000/svg"
  }, /*#__PURE__*/_react["default"].createElement("path", {
    d: "M51.5625 4.75C51.5625 7.38672 49.3652 9.4375 46.875 9.4375H18.75C16.1133 9.4375 14.0625 7.38672 14.0625 4.75C14.0625 2.25977 16.1133 0.0625 18.75 0.0625H46.875C49.3652 0.0625 51.5625 2.25977 51.5625 4.75ZM65.625 23.5C65.625 26.1367 63.4277 28.1875 60.9375 28.1875H4.6875C2.05078 28.1875 0 26.1367 0 23.5C0 21.0098 2.05078 18.8125 4.6875 18.8125H60.9375C63.4277 18.8125 65.625 21.0098 65.625 23.5ZM0 61C0 58.5098 2.05078 56.3125 4.6875 56.3125H60.9375C63.4277 56.3125 65.625 58.5098 65.625 61C65.625 63.6367 63.4277 65.6875 60.9375 65.6875H4.6875C2.05078 65.6875 0 63.6367 0 61ZM51.5625 42.25C51.5625 44.8867 49.3652 46.9375 46.875 46.9375H18.75C16.1133 46.9375 14.0625 44.8867 14.0625 42.25C14.0625 39.7598 16.1133 37.5625 18.75 37.5625H46.875C49.3652 37.5625 51.5625 39.7598 51.5625 42.25Z",
    fill: "currentColor"
  }));
};

exports.AlignCenter = AlignCenter;

var AlignJustify = function AlignJustify() {
  return /*#__PURE__*/_react["default"].createElement("svg", {
    width: "20",
    height: "20",
    viewBox: "0 0 66 66",
    fill: "none",
    xmlns: "http://www.w3.org/2000/svg"
  }, /*#__PURE__*/_react["default"].createElement("path", {
    d: "M65.625 4.75C65.625 7.38672 63.4277 9.4375 60.9375 9.4375H4.6875C2.05078 9.4375 0 7.38672 0 4.75C0 2.25977 2.05078 0.0625 4.6875 0.0625H60.9375C63.4277 0.0625 65.625 2.25977 65.625 4.75ZM65.625 42.25C65.625 44.8867 63.4277 46.9375 60.9375 46.9375H4.6875C2.05078 46.9375 0 44.8867 0 42.25C0 39.7598 2.05078 37.5625 4.6875 37.5625H60.9375C63.4277 37.5625 65.625 39.7598 65.625 42.25ZM0 23.5C0 21.0098 2.05078 18.8125 4.6875 18.8125H60.9375C63.4277 18.8125 65.625 21.0098 65.625 23.5C65.625 26.1367 63.4277 28.1875 60.9375 28.1875H4.6875C2.05078 28.1875 0 26.1367 0 23.5ZM65.625 61C65.625 63.6367 63.4277 65.6875 60.9375 65.6875H4.6875C2.05078 65.6875 0 63.6367 0 61C0 58.5098 2.05078 56.3125 4.6875 56.3125H60.9375C63.4277 56.3125 65.625 58.5098 65.625 61Z",
    fill: "currentColor"
  }));
};

exports.AlignJustify = AlignJustify;

var _default = function _default(_ref) {
  var getValue = _ref.getValue,
      onChange = _ref.onChange;

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      open = _useState2[0],
      setOpen = _useState2[1];

  var value = getValue();
  var text = value.texts.get(0);
  var type;

  if (text) {
    var _data$attributes;

    var blockParent = value.document.getParent(text.key);
    var data = blockParent.data.toJSON();

    if (data !== null && data !== void 0 && (_data$attributes = data.attributes) !== null && _data$attributes !== void 0 && _data$attributes.align) {
      var _data$attributes2;

      type = data === null || data === void 0 ? void 0 : (_data$attributes2 = data.attributes) === null || _data$attributes2 === void 0 ? void 0 : _data$attributes2.align;
    }
  }

  var icon;

  switch (type) {
    case 'right':
      icon = /*#__PURE__*/_react["default"].createElement(AlignRight, null);
      break;

    case 'center':
      icon = /*#__PURE__*/_react["default"].createElement(AlignCenter, null);
      break;

    case 'justify':
      icon = /*#__PURE__*/_react["default"].createElement(AlignJustify, null);
      break;

    default:
      icon = /*#__PURE__*/_react["default"].createElement(AlignLeft, null);
      break;
  }

  var applyAlignment = function applyAlignment(event) {
    if (value.texts.size) {
      var _event$target, _event$target$closest;

      var alignType = (_event$target = event.target) === null || _event$target === void 0 ? void 0 : (_event$target$closest = _event$target.closest('div')) === null || _event$target$closest === void 0 ? void 0 : _event$target$closest.getAttribute('value');

      if (alignType) {
        var c = value.change();
        value.texts.forEach(function (text) {
          var _blockParent$data;

          var blockParent = value.document.getParent(text.key);
          c = c.setNodeByKey(blockParent.key, {
            data: _objectSpread(_objectSpread({}, blockParent.data), {}, {
              attributes: _objectSpread(_objectSpread({}, (_blockParent$data = blockParent.data) === null || _blockParent$data === void 0 ? void 0 : _blockParent$data.attributes), {}, {
                align: alignType
              })
            })
          });
        });
        onChange(c);
      }
    }

    setOpen(false);
  };

  var onMouseDown = function onMouseDown(event) {
    event.preventDefault();
    event.stopPropagation();
    setOpen(!open);
  };

  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      display: 'flex',
      alignItems: 'center'
    },
    onClick: onMouseDown
  }, icon, /*#__PURE__*/_react["default"].createElement("span", {
    style: {
      marginLeft: '5px',
      fontSize: '8px'
    }
  }, "\u25BC")), /*#__PURE__*/_react["default"].createElement(_Collapse["default"], {
    "in": open,
    timeout: "auto",
    unmountOnExit: true,
    style: {
      position: 'absolute'
    }
  }, /*#__PURE__*/_react["default"].createElement(_List["default"], {
    component: "div",
    disablePadding: true,
    style: {
      background: '#fff',
      display: 'flex',
      flexDirection: 'row',
      padding: 0
    }
  }, /*#__PURE__*/_react["default"].createElement(_ListItem["default"], {
    button: true,
    type: "submit",
    value: "left",
    "aria-label": "Align text left",
    onClick: applyAlignment
  }, /*#__PURE__*/_react["default"].createElement(AlignLeft, null)), /*#__PURE__*/_react["default"].createElement(_ListItem["default"], {
    button: true,
    type: "submit",
    value: "center",
    "aria-label": "Align text center",
    onClick: applyAlignment
  }, /*#__PURE__*/_react["default"].createElement(AlignCenter, null)), /*#__PURE__*/_react["default"].createElement(_ListItem["default"], {
    button: true,
    type: "submit",
    value: "right",
    "aria-label": "Align text right",
    onClick: applyAlignment
  }, /*#__PURE__*/_react["default"].createElement(AlignRight, null)), /*#__PURE__*/_react["default"].createElement(_ListItem["default"], {
    button: true,
    type: "submit",
    value: "justify",
    "aria-label": "Justify text",
    onClick: applyAlignment
  }, /*#__PURE__*/_react["default"].createElement(AlignJustify, null)))));
};

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