import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import React, { Component } from 'react';
import { findDOMNode } from 'react-dom';
import LightBox from 'react-image-lightbox';
import omit from 'lodash/omit';
import { toJS } from 'mobx';
import classNames from 'classnames';
import 'react-quill/dist/quill.snow.css';
import { delta2Html } from './utils';

var RichTextViewer = /*#__PURE__*/function (_Component) {
  _inherits(RichTextViewer, _Component);

  var _super = _createSuper(RichTextViewer);

  function RichTextViewer() {
    var _this;

    _classCallCheck(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;
  }

  _createClass(RichTextViewer, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var deltaOps = this.props.deltaOps;
      var thisNode = findDOMNode(this);

      if (thisNode && deltaOps) {
        thisNode.addEventListener('click', this.open);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var thisNode = findDOMNode(this);
      if (thisNode) thisNode.removeEventListener('click', this.open);
    }
  }, {
    key: "getOtherProps",
    value: function getOtherProps() {
      return omit(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 = delta2Html(toJS(deltaOps));
      var classString = classNames(className, 'quill');
      return /*#__PURE__*/React.createElement("div", _extends({}, this.getOtherProps(), {
        className: classString
      }), /*#__PURE__*/React.createElement("div", {
        className: "ql-container ql-snow"
      }, /*#__PURE__*/React.createElement("div", {
        dangerouslySetInnerHTML: {
          __html: "".concat(this.escape(html))
        },
        className: "ql-editor"
      }), open ? /*#__PURE__*/React.createElement(LightBox, {
        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;
}(Component);

export default RichTextViewer;
//# sourceMappingURL=RichTextViewer.js.map
