"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _slateReact = require("slate-react");
var _slate = require("slate");
var _extension = require("../../extension");
var _eventBus = _interopRequireDefault(require("../../utils/event-bus"));
var _eventHandler = _interopRequireDefault(require("../../utils/event-handler"));
var _withPropsEditor = _interopRequireDefault(require("./with-props-editor"));
var _core = require("../../extension/core");
var _common = require("../../utils/common");
var _useAttachments = _interopRequireDefault(require("../../hooks/use-attachments"));
var _userLinkClick = _interopRequireDefault(require("../../hooks/user-link-click"));
require("./style.css");
const isMacOS = (0, _common.isMac)();
const SimpleSlateEditor = _ref => {
  var _editor$api;
  let {
    value,
    focusEnd,
    editorApi,
    onSave,
    columns,
    onContentChanged,
    isSupportFormula,
    onExpandEditorToggle,
    onLinkClick
  } = _ref;
  const [slateValue, setSlateValue] = (0, _react.useState)(value);
  const editor = (0, _react.useMemo)(() => {
    const _editor = (0, _extension.baseEditor)();
    return (0, _withPropsEditor.default)(_editor, {
      editorApi,
      onSave,
      columns
    });
  }, [columns, editorApi, onSave]);
  const eventProxy = (0, _react.useMemo)(() => {
    return new _eventHandler.default(editor);
  }, [editor]);
  (0, _useAttachments.default)(editor);
  (0, _userLinkClick.default)(editor._id, editor === null || editor === void 0 ? void 0 : (_editor$api = editor.api) === null || _editor$api === void 0 ? void 0 : _editor$api.server, onLinkClick);
  const decorate = (0, _extension.useHighlight)(editor);
  const onChange = (0, _react.useCallback)(value => {
    setSlateValue(value);
    if (editor.forceNormalize) return;
    const operations = editor.operations;
    const modifyOps = operations.filter(o => o.type !== 'set_selection');
    if (modifyOps.length > 0) {
      onContentChanged && onContentChanged(value);
    }
    const eventBus = _eventBus.default.getInstance();
    eventBus.dispatch('change');
  }, [editor, onContentChanged]);
  const focusNode = (0, _react.useCallback)(editor => {
    const [firstNode] = editor.children;
    if (!firstNode) return;
    const [firstNodeFirstChild] = firstNode.children;
    if (firstNodeFirstChild) {
      const startOfFirstNode = _slate.Editor.start(editor, [0, 0]);
      let range = {
        anchor: startOfFirstNode,
        focus: startOfFirstNode
      };
      if (focusEnd) {
        const lastIndex = editor.children.length - 1;
        const [, path] = _slate.Editor.last(editor, [lastIndex]);
        const endOfLastNodePoint = _slate.Editor.end(editor, path);
        range = {
          anchor: endOfLastNodePoint,
          focus: endOfLastNodePoint
        };
      }
      (0, _core.focusEditor)(editor, range);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  const handleWheel = (0, _react.useCallback)(event => {
    event.stopPropagation();
  }, []);

  // const focusEndNode = useCallback((editor) => {
  //   const lastChildIndex = editor.children.length - 1;
  //   if (lastChildIndex < 0) return;
  //   const lastNode = editor.children[lastChildIndex];
  //   if (!lastNode) return;
  //   const [lastNodeFirstChild] = lastNode.children;
  //   if (lastNodeFirstChild) {
  //     const endOfFirstNode = Editor.end(editor, [lastChildIndex, 0]);
  //     const range = {
  //       anchor: endOfFirstNode,
  //       focus: endOfFirstNode,
  //     };
  //     focusEditor(editor, range);
  //   }
  // }, []);

  // useMount: focus editor
  (0, _react.useEffect)(() => {
    editor.forceNormalize = true;
    _slate.Editor.normalize(editor, {
      force: true
    });
    const timer = setTimeout(() => {
      editor.forceNormalize = false;
      focusNode(editor);
    }, 300);
    return () => {
      editor.forceNormalize = false;
      clearTimeout(timer);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  // willUnmount
  (0, _react.useEffect)(() => {
    return () => {
      editor.selection = null;
      editor.history = {
        redos: [],
        undos: []
      };
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  // Focus at start of document, when document is empty
  const onEditorClick = (0, _react.useCallback)(() => {
    const isDocEmpty = (0, _common.isDocumentEmpty)(editor);
    if (isDocEmpty) {
      focusNode(editor);
    }
  }, [editor, focusNode]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: "sf-simple-slate-editor-container"
  }, /*#__PURE__*/_react.default.createElement(_extension.Toolbar, {
    editor: editor,
    isSupportFormula: isSupportFormula,
    isSupportColumn: !!columns
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: "sf-slate-editor-content",
    onClick: onEditorClick
  }, /*#__PURE__*/_react.default.createElement(_slateReact.Slate, {
    editor: editor,
    initialValue: slateValue,
    onChange: onChange
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: `sf-slate-scroll-container ${isMacOS ? '' : 'isWin'}`
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "sf-slate-article-container",
    onWheel: handleWheel
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "article"
  }, /*#__PURE__*/_react.default.createElement(_extension.SetNodeToDecorations, null), /*#__PURE__*/_react.default.createElement(_slateReact.Editable, {
    decorate: decorate,
    renderElement: _extension.renderElement,
    renderLeaf: _extension.renderLeaf,
    onKeyDown: eventProxy.onKeyDown,
    onCopy: eventProxy.onCopy
  })))))));
};
var _default = exports.default = SimpleSlateEditor;