"use strict";

exports.__esModule = true;
exports.default = void 0;
var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");
var _tslib = require("tslib");
var _react = _interopRequireWildcard(require("react"));
var _view = _interopRequireDefault(require("../view"));
var _image = require("@uni/image");
var _env = require("@uni/env");
var _utils = require("../utils");
var _hooks = require("../utils/hooks");
var _htmlParser = _interopRequireDefault(require("./html-parser"));
var _node = _interopRequireDefault(require("./node"));
var _util = require("./util");
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 ? Object.assign.bind() : 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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var RichText = /*#__PURE__*/function (_Component) {
  _inheritsLoose(RichText, _Component);
  function RichText() {
    var _this = _Component.apply(this, arguments) || this;
    _this.state = {
      nodes: []
    };
    _this.imgList = [];
    _this.videos = {};
    _this.plugins = [];
    _this.ref = /*#__PURE__*/(0, _react.createRef)();
    _this.wrapTable = function () {
      var rootNode = _this.ref.current;
      if (rootNode) {
        var tables = rootNode.getElementsByTagName('table');
        Array.from(tables).forEach(function (tableNode) {
          var tmp = document.createElement('div');
          tmp.style.overflowX = 'auto';
          tmp.style.padding = '1px';
          tmp.appendChild(tableNode.cloneNode(true));
          tableNode.parentNode.insertBefore(tmp, tableNode);
          tableNode.remove();
        });
      }
    };
    _this.replaceErrorImg = function () {
      var rootNode = _this.ref.current;
      if (rootNode) {
        [].concat(rootNode.getElementsByTagName('img')).forEach(function (img) {
          if (img && img.addEventListener) {
            img.addEventListener('error', function () {
              img.src = _this.props.errorImage;
            });
          }
        });
      }
    };
    _this.parseHtmlText = function (text) {
      var nodes = new _htmlParser.default(_assertThisInitialized(_this)).parse(text);
      _this.setState({
        nodes: nodes
      });
    };
    _this.handleImageClick = function (i) {
      if (_this.props.enableImagePreview) {
        (0, _image.previewImage)({
          urls: _this.imgList,
          current: i || 0
        }).then(function () {});
      }
    };
    _this.handleImageError = function (src, i) {
      var errorImage = _this.props.errorImage;
      _this.imgList[i] = errorImage;
    };
    _this.handleVideoLoad = function (id) {
      _this.videos[id] = (0, _util.getVideoContext)(id);
    };
    _this.handleVideoPlay = function (id) {
      if (_this.props.pauseVideo) {
        Object.entries(_this.videos).forEach(function (_ref) {
          var videoId = _ref[0],
            ctx = _ref[1];
          if (videoId !== id && ctx && ctx.pause) {
            ctx.pause();
          }
        });
      }
    };
    return _this;
  }
  var _proto = RichText.prototype;
  _proto.componentWillMount = function componentWillMount() {
    if (_utils.isMiniappPlatform) {
      var text = this.props.text;
      this.parseHtmlText(text);
    }
  };
  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    if (_utils.isMiniappPlatform) {
      var text = nextProps.text;
      this.parseHtmlText(text);
    }
  };
  _proto.componentDidMount = function componentDidMount() {
    if (_env.isWeb) {
      this.afterWebRender();
    }
  };
  _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
    if (_env.isWeb && this.props.text !== prevProps.text) {
      this.afterWebRender();
    }
  };
  _proto.afterWebRender = function afterWebRender() {
    var _this$props = this.props,
      enableTableScroll = _this$props.enableTableScroll,
      errorImage = _this$props.errorImage;
    if (enableTableScroll) {
      this.wrapTable();
    }
    if (errorImage) {
      this.replaceErrorImg();
    }
  };
  _proto.render = function render() {
    var _a = this.props,
      style = _a.style,
      lazyLoad = _a.lazyLoad,
      text = _a.text,
      otherProps = (0, _tslib.__rest)(_a, ["style", "lazyLoad", "text"]);
    return /*#__PURE__*/(0, _react.createElement)(_view.default, {
      id: "mtRichTextRoot-" + (0, _hooks.guid)(),
      ref: this.ref,
      className: this.props.prefix + "rich-text",
      style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(style)
    }, _utils.isMiniappPlatform ? /*#__PURE__*/(0, _react.createElement)(_node.default, _extends({
      lazyLoad: lazyLoad,
      nodes: this.state.nodes,
      onImageClick: this.handleImageClick,
      onImgError: this.handleImageError,
      onVideoLoad: this.handleVideoLoad,
      onVideoPlay: this.handleVideoPlay
    }, otherProps)) : /*#__PURE__*/(0, _react.createElement)("div", {
      dangerouslySetInnerHTML: {
        __html: text || ''
      }
    }));
  };
  return RichText;
}(_react.Component);
RichText.defaultProps = {
  prefix: 'mt-',
  text: '',
  errorImage: 'https://gw.alicdn.com/imgextra/i4/O1CN014BzOrR1EjHycne0VV_!!6000000000387-2-tps-200-200.png',
  lazyLoad: false,
  enableImagePreview: true,
  useAnchor: false,
  pauseVideo: true,
  enableTableScroll: false,
  tagStyle: {
    table: 'box-sizing: border-box; ',
    li: 'margin: 5px 0;',
    img: 'width: auto'
  }
};
var _default = RichText;
exports.default = _default;