"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

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

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

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

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

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

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

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

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

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

var _reactImageLightbox = _interopRequireDefault(require("react-image-lightbox"));

var _omit = _interopRequireDefault(require("lodash/omit"));

var _mobx = require("mobx");

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

require("react-quill/dist/quill.snow.css");

var _utils = require("./utils");

var RichTextViewer = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(RichTextViewer, _Component);

  var _super = (0, _createSuper2["default"])(RichTextViewer);

  function RichTextViewer() {
    var _this;

    (0, _classCallCheck2["default"])(this, RichTextViewer);
    _this = _super.apply(this, arguments);
    _this.state = {
      open: false,
      images: [],
      srcIndex: 0
    };

    _this.open = function (e) {
      var target = e.target;

      if (target && target.nodeName === 'IMG') {
        e.stopPropagation();
        var deltaOps = _this.props.deltaOps;
        var imgArr = [];
        deltaOps.forEach(function (item) {
          var image = item.insert.image;

          if (image) {
            imgArr.push(image);
          }
        });
        var src = target.src;
        var index = imgArr.findIndex(function (img) {
          return img === src;
        });

        _this.setState({
          open: true,
          images: imgArr,
          srcIndex: index
        });
      }
    };

    _this.escape = function (str) {
      return str.replace(/<\/script/g, '<\\/script').replace(/<!--/g, '<\\!--');
    };

    return _this;
  }

  (0, _createClass2["default"])(RichTextViewer, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var deltaOps = this.props.deltaOps;
      var thisNode = (0, _reactDom.findDOMNode)(this);

      if (thisNode && deltaOps) {
        thisNode.addEventListener('click', this.open);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var thisNode = (0, _reactDom.findDOMNode)(this);
      if (thisNode) thisNode.removeEventListener('click', this.open);
    }
  }, {
    key: "getOtherProps",
    value: function getOtherProps() {
      return (0, _omit["default"])(this.props, ['deltaOps']);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          deltaOps = _this$props.deltaOps,
          className = _this$props.className;
      var _this$state = this.state,
          open = _this$state.open,
          images = _this$state.images,
          srcIndex = _this$state.srcIndex;
      var html = (0, _utils.delta2Html)((0, _mobx.toJS)(deltaOps));
      var classString = (0, _classnames["default"])(className, 'quill');
      return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, this.getOtherProps(), {
        className: classString
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: "ql-container ql-snow"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        dangerouslySetInnerHTML: {
          __html: "".concat(this.escape(html))
        },
        className: "ql-editor"
      }), open ? /*#__PURE__*/_react["default"].createElement(_reactImageLightbox["default"], {
        mainSrc: images[srcIndex],
        onCloseRequest: function onCloseRequest() {
          return _this2.setState({
            open: false
          });
        },
        imageTitle: "images",
        prevSrc: images[srcIndex - 1],
        nextSrc: images[srcIndex + 1],
        onMovePrevRequest: function onMovePrevRequest() {
          _this2.setState({
            srcIndex: srcIndex - 1
          });
        },
        onMoveNextRequest: function onMoveNextRequest() {
          _this2.setState({
            srcIndex: srcIndex + 1
          });
        }
      }) : null));
    }
  }]);
  return RichTextViewer;
}(_react.Component);

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