"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _prosemirrorState = require("prosemirror-state");

var _prosemirrorView = require("prosemirror-view");

var _prosemirrorModel = require("prosemirror-model");

var _schema = _interopRequireDefault(require("../../basic/schema"));

var _editor = _interopRequireDefault(require("../../models/editor"));

var _util = require("../../plugins/util");

var _event = require("../../utils/event");

var _events = require("../../consts/events");

var _plugins = require("../../plugins");

require("./index.css");

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

var _placeholder = _interopRequireDefault(require("../../plugins/extension/placeholder"));

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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var preCls = 'titaui-rich-editor';
var defaultPlugins = [new _plugins.HeadingPlugin(), new _plugins.StrongPlugin(), new _plugins.ColorPlugin(), new _plugins.UnderLinePlugin(), new _plugins.DeleteLinePlugin(), new _plugins.ListPlugin(), new _plugins.AlignmentPlugin(), new _plugins.BlockquotePlugin(), new _plugins.LinkPlugin(), new _placeholder["default"]()];

function RichEditor(_ref, ref) {
  var _ref$plugins = _ref.plugins,
      plugins = _ref$plugins === void 0 ? defaultPlugins : _ref$plugins,
      onInit = _ref.onInit,
      _ref$editable = _ref.editable,
      _editable = _ref$editable === void 0 ? true : _ref$editable,
      defaultState = _ref.defaultState,
      onChange = _ref.onChange,
      placeholder = _ref.placeholder;

  var container = (0, _react.useRef)();
  var dataRef = (0, _react.useRef)({});
  (0, _react.useEffect)(function () {
    var dispatcher = new _event.EventDispatcher();

    var _applyPlugins = (0, _util.applyPlugins)(plugins, _schema["default"], dispatcher, {
      placeholder: placeholder
    }),
        nativePlugins = _applyPlugins.nativePlugins,
        nodes = _applyPlugins.nodes,
        marks = _applyPlugins.marks,
        controllers = _applyPlugins.controllers;

    var pluginedSchema = new _prosemirrorModel.Schema({
      nodes: nodes,
      marks: marks
    });
    var stateConf = {
      schema: pluginedSchema,
      plugins: nativePlugins
    };

    var state = _prosemirrorState.EditorState.create(stateConf); // debugger


    if (defaultState) {
      try {
        var jsonState = typeof defaultState == 'string' ? JSON.parse(defaultState) : defaultState;
        state = _prosemirrorState.EditorState.fromJSON(stateConf, jsonState);
      } catch (e) {
        console.log(e);
      }
    }

    var pmEditor = new _prosemirrorView.EditorView(container.current, {
      state: state,
      editable: function editable() {
        return _editable;
      },
      dispatchTransaction: function dispatchTransaction(tr) {
        var _this$state$applyTran = this.state.applyTransaction(tr),
            state = _this$state$applyTran.state;

        this.updateState(state);
        dispatcher.emit(_events.EDITOR_STATE_CHANGE, state);

        if (tr.docChanged) {
          onChange === null || onChange === void 0 ? void 0 : onChange(state);
        }
      }
    });
    dataRef.current.editor = new _editor["default"]({
      view: pmEditor,
      controllers: controllers,
      dispatcher: dispatcher
    }); // window.editor = dataRef.current.editor

    onInit === null || onInit === void 0 ? void 0 : onInit(dataRef.current.editor);
    return function () {
      return dataRef.current.editor.destroy();
    };
  }, []);
  var handleClick = (0, _react.useCallback)(function (e) {
    var _dataRef$current$edit, _dataRef$current$edit2;

    if (e.target != e.currentTarget) return;
    var view = (_dataRef$current$edit = dataRef.current.editor) === null || _dataRef$current$edit === void 0 ? void 0 : _dataRef$current$edit.view;
    if (!view) return;
    var tr = view.state.tr;

    var selection = _prosemirrorState.TextSelection.atEnd(view.state.doc);

    view.dispatch(tr.setSelection(selection));
    view.focus();
    (_dataRef$current$edit2 = dataRef.current.editor) === null || _dataRef$current$edit2 === void 0 ? void 0 : _dataRef$current$edit2.view.focus();
  }, []);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      getState: function getState() {
        return dataRef.current.editor.state.toJSON();
      }
    };
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", {
    ref: container,
    className: (0, _classnames["default"])(preCls, _defineProperty({}, preCls + '--disabled', !_editable)),
    style: {
      backgroundColor: "#FFF"
    },
    spellCheck: false,
    onMouseUp: handleClick
  });
}

var _default = /*#__PURE__*/(0, _react.forwardRef)(RichEditor);

exports["default"] = _default;