"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _progress = _interopRequireDefault(require("@alifd/next/lib/progress"));
var _dialog = _interopRequireDefault(require("@alifd/next/lib/dialog"));
var _message = _interopRequireDefault(require("@alifd/next/lib/message"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireWildcard(require("react"));
var _joditReact = _interopRequireDefault(require("jodit-react"));
var _indexModule = _interopRequireDefault(require("./index.module.scss"));
var _left = _interopRequireDefault(require("./left"));
var _cosUpload = _interopRequireDefault(require("../../module/axios/cosUpload"));
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; }
var JoditEditorSetter = /*#__PURE__*/function (_Component) {
  (0, _inheritsLoose2["default"])(JoditEditorSetter, _Component);
  function JoditEditorSetter() {
    var _this2;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this2 = _Component.call.apply(_Component, [this].concat(args)) || this;
    _this2.state = {
      editorVisible: false,
      // 显示状态
      upload: {
        isLoadding: false,
        progress: 0
      },
      html: ''
    };
    return _this2;
  }
  var _proto = JoditEditorSetter.prototype;
  _proto.render = function render() {
    var _this3 = this;
    var _this$props = this.props,
      value = _this$props.value,
      onChange = _this$props.onChange;
    var html = this.state.html;

    // 编辑器打开事件
    var editorOpen = function editorOpen(_this) {
      _this.setState({
        editorVisible: true,
        html: value.data
      });
    };

    // 编辑器关闭事件
    var editorClose = function editorClose() {
      _this3.setState({
        // html:'',
        editorVisible: false
      });
    };

    // 返回内容
    var returnContent = function returnContent(e) {
      _this3.state.html = e;
      document.addEventListener('keyup', function (e) {
        if (e.key == 'Delete' && _this3.state.html != '') {
          setTimeout(function () {
            _this3.setState({
              html: _this3.state.html
            });
          }, 300);
        }
      });
    };

    // 提交内容事件
    var editorCommit = function editorCommit() {
      /* eslint-disable no-eval */
      var data = _this3.state.html;
      _this3.setState({
        html: data
      });
      onChange({
        data: data
      });
      editorClose();
    };
    var insetContent = function insetContent(el) {
      _this3.setState({
        html: html + (el !== null && el !== void 0 && el.innerHTML ? el.innerHTML : el)
      });
    };
    var setUploadStatus = function setUploadStatus(_ref) {
      var total = _ref.total,
        loaded = _ref.loaded;
      var progress = (loaded / total * 100).toFixed(2) - 0;
      if (!progress) return;
      _this3.setState({
        upload: {
          isLoadding: !(progress >= 100),
          progress: progress
        }
      });
    };
    // const editorConf = {
    //     // if(isJodit===true){
    //     //     console.log(make);
    //     //     debugger
    //     //     JoditEditor.make('.someselector', {
    //     //         extraButtons: [
    //     //             {
    //     //                 name: 'insertDate',
    //     //                 iconURL: 'http://xdsoft.net/jodit/logo.png',
    //     //                 exec: function (editor) {
    //     //                     editor.s.insertHTML(new Date().toDateString());
    //     //                 }
    //     //             }
    //     //         ]
    //     //     });
    //     // }
    // }
    return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_button["default"], {
      onClick: function onClick() {
        return editorOpen(_this3);
      }
    }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: "edit"
    }), "\u7F16\u8F91\u5185\u5BB9"), /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
      title: null,
      visible: this.state.editorVisible,
      onOk: editorCommit,
      onClose: editorClose,
      onCancel: editorClose,
      className: _indexModule["default"].dialog
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: _indexModule["default"].title
    }, "\u914D\u7F6E\u56FE\u6587\u5185\u5BB9"), /*#__PURE__*/_react["default"].createElement("div", {
      className: _indexModule["default"].editor
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: _indexModule["default"]['editor-left']
    }, /*#__PURE__*/_react["default"].createElement(_left["default"], {
      insetContent: insetContent
    })), /*#__PURE__*/_react["default"].createElement(_joditReact["default"], {
      className: _indexModule["default"]['editor-right'],
      value: this.state.html,
      onChange: function onChange(newContent) {
        returnContent(newContent);
        // this.setState({
        //     html: newContent
        // })
      },

      config: {
        language: "zh_cn",
        askBeforePasteFromWord: true,
        // "uploader": {
        //     "insertImageAsBase64URI": true
        // },
        "extraButtons": [{
          name: 'uploadImage',
          iconURL: 'https://img.maimairen.com/low-code/ali/material-icon/upload-image.svg?versionId=MTg0NDUwODM5OTMzMDM1MDQ5MzU',
          exec: function exec(editor) {
            var file = editor.c.element('input');
            file.type = 'file';
            file.onchange = function (ev) {
              var _ev$target;
              var succ = (0, _cosUpload["default"])((_ev$target = ev.target) === null || _ev$target === void 0 ? void 0 : _ev$target.files[0], {
                pathDir: 'img-text-tpl2' || '',
                isDateDir: true,
                reName: true,
                onProgress: function onProgress(_ref2) {
                  var total = _ref2.total,
                    loaded = _ref2.loaded;
                  setUploadStatus({
                    total: total,
                    loaded: loaded
                  });
                }
              });
              succ.then(function (_ref3) {
                var code = _ref3.code,
                  data = _ref3.data,
                  msg = _ref3.msg;
                if (code === '0000') {
                  insetContent("<img src=\"" + data.ossPath + "\" alt=\"" + data.imgName + "\">");
                } else {
                  _message["default"].error(msg);
                }
              });
            };
            file.click();
          }
        }]
      }
    }))), /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
      title: "\u6587\u4EF6\u4E0A\u4F20\u4E2D...",
      visible: this.state.upload.isLoadding,
      footer: /*#__PURE__*/_react["default"].createElement("div", null),
      closeMode: [],
      className: _indexModule["default"].uploading
    }, /*#__PURE__*/_react["default"].createElement(_progress["default"], {
      percent: this.state.upload.progress,
      shape: "circle"
    })));
  };
  return JoditEditorSetter;
}(_react.Component);
exports["default"] = JoditEditorSetter;
JoditEditorSetter.displayName = 'JoditEditorSetter';