import { Range as SlateRange } from "slate";
import { useIsomorphicLayoutEffect } from "../../../../../components/hooks/useIsomorphicLayoutEffect";
import { ReactEditor } from "slate-react";
function useSyncDOMSelection(editor, isEnabled) {
  useIsomorphicLayoutEffect(() => {
    if (!isEnabled.current || editor.selection == null || ReactEditor.isFocused(editor))
      return;
    try {
      const root = ReactEditor.findDocumentOrShadowRoot(editor);
      const domSelection = root.getSelection();
      const newDomRange = ReactEditor.toDOMRange(editor, editor.selection);
      if (newDomRange) {
        if (SlateRange.isBackward(editor.selection)) {
          domSelection?.setBaseAndExtent(
            newDomRange.endContainer,
            newDomRange.endOffset,
            newDomRange.startContainer,
            newDomRange.startOffset
          );
        } else {
          domSelection?.setBaseAndExtent(
            newDomRange.startContainer,
            newDomRange.startOffset,
            newDomRange.endContainer,
            newDomRange.endOffset
          );
        }
      } else {
        domSelection?.removeAllRanges();
      }
    } catch (e) {
      console.error(e);
    }
  });
}
export {
  useSyncDOMSelection
};
//# sourceMappingURL=useSyncDOMSelection.js.map