"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _loading = _interopRequireDefault(require("../../containers/loading"));
var _slateConvert = require("../../slate-convert");
var _slateViewer = _interopRequireDefault(require("../../editors/slate-viewer"));
require("./style.css");
// Windows old Wechat (3.0 or earlier) inner core is chrome 53 and don't support ECMA6, can't use seafile-editor markdownViewer
// Windows new Wechat (lastest version 3.3.5) support seafile-editor markdownViewer
// so use dangerouslySetInnerHTML to preview
class MarkdownPreview extends _react.default.PureComponent {
  constructor(props) {
    super(props);
    (0, _defineProperty2.default)(this, "convertMarkdown", mdFile => {
      _slateConvert.processor.process(mdFile).then(result => {
        let innerHtml = String(result).replace(/<a /ig, '<a target="_blank" tabindex="-1"');
        this.setState({
          innerHtml
        });
      });
    });
    this.state = {
      innerHtml: null
    };
  }
  componentDidMount() {
    const {
      isWindowsWechat,
      value
    } = this.props;
    if (isWindowsWechat) {
      this.convertMarkdown(value);
    }
  }
  render() {
    const {
      isWindowsWechat,
      value,
      isShowOutline
    } = this.props;
    const {
      innerHtml
    } = this.state;
    if (isWindowsWechat && innerHtml === null) {
      return /*#__PURE__*/_react.default.createElement(_loading.default, null);
    }
    return /*#__PURE__*/_react.default.createElement("div", {
      className: "longtext-preview-container"
    }, isWindowsWechat && /*#__PURE__*/_react.default.createElement("div", {
      className: "article",
      dangerouslySetInnerHTML: {
        __html: this.state.innerHtml
      }
    }), !isWindowsWechat && /*#__PURE__*/_react.default.createElement(_slateViewer.default, {
      value: (0, _slateConvert.mdStringToSlate)(value),
      isShowOutline: isShowOutline
    }));
  }
}
var _default = exports.default = MarkdownPreview;