Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@filerobot/explorer / lib / components / FoldersView / FoldersTree / FoldersTreeListItem.js
Size: Mime:
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useContextMenu } from '@filerobot/core/lib/hooks';
import ignoreEvent from '@filerobot/utils/lib/ignoreEvent';
import { CONTEXTMENU_MODES, INVALID_FOLDER_OR_FILE_CHARS_REGEX } from '@filerobot/utils/lib/constants';
import handlePromise from '@filerobot/utils/lib/handlePromise';
import Styled from './FoldersTree.styled';
import FoldersTreeList from './FoldersTreeList';
import { selectFolderTreeFolderByUuid, toggleFolderExpansion } from '../../../slices/foldersTree.slice';
import { addNewFolder, openFolderByPath, renameFolder, selectCurrentFolder } from '../../../slices/folders.slice';
import DnD from '../../DnD';
import { itemRenameOpened, selectIsRenameItemFromSidebar, selectRenameItemFor } from '../../../slices/panels.slice';
import { useExplorer, useExplorerInformer } from '../../../hooks';

// only `uuid` is the required one, other props for customization/extending the functionality.
// disabledFoldersUuids => used to disable/lock operations for the folders with the uuids found in the array (not expandable/selectable/clickable...etc.)
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var FoldersTreeListItem = function FoldersTreeListItem(props) {
  var uuid = props.uuid,
    icon = props.icon,
    onFolderClick = props.onFolderClick,
    _props$setShowAddFold = props.setShowAddFolderInput,
    setShowAddFolderInput = _props$setShowAddFold === void 0 ? function () {} : _props$setShowAddFold,
    _props$showAddFolderI = props.showAddFolderInput,
    showAddFolderInput = _props$showAddFolderI === void 0 ? false : _props$showAddFolderI,
    openFolderOnDblClick = props.openFolderOnDblClick,
    _props$disabledFolder = props.disabledFoldersUuids,
    disabledFoldersUuids = _props$disabledFolder === void 0 ? [] : _props$disabledFolder,
    _props$noDrag = props.noDrag,
    noDrag = _props$noDrag === void 0 ? false : _props$noDrag,
    _props$noContextMenu = props.noContextMenu,
    noContextMenu = _props$noContextMenu === void 0 ? false : _props$noContextMenu,
    _props$selectedPath = props.selectedPath,
    selectedPath = _props$selectedPath === void 0 ? null : _props$selectedPath;
  var dispatch = useDispatch();
  var triggerContextMenu = useContextMenu();
  var info = useExplorerInformer();
  var _useExplorer = useExplorer(),
    i18n = _useExplorer.i18n,
    disableFoldersContextMenu = _useExplorer.opts.disableFoldersContextMenu;
  var folder = useSelector(function (state) {
    return selectFolderTreeFolderByUuid(state, uuid);
  });
  var isCurrentFolder = useSelector(function (state) {
    return selectCurrentFolder(state).uuid === uuid;
  });
  var isRenaming = useSelector(function (state) {
    return selectIsRenameItemFromSidebar(state) && selectRenameItemFor(state) === uuid;
  });
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isRenameLoading = _useState2[0],
    setIsRenameLoading = _useState2[1];
  if (!folder) {
    return null;
  }
  var name = folder.name,
    path = folder.path,
    childrenUuids = folder.childrenUuids,
    isExpanded = folder.isExpanded,
    isLoaded = folder.isLoaded,
    loading = folder.loading,
    parentUuid = folder.parentUuid;
  var isSelected = selectedPath ? selectedPath === path : isCurrentFolder;
  var isDisabled = disabledFoldersUuids.includes(uuid);
  var handleFolderClick = function handleFolderClick() {
    if (isDisabled || isRenameLoading || loading) {
      return;
    }
    if (typeof onFolderClick === 'function') {
      onFolderClick(folder);
      return;
    }
    dispatch(openFolderByPath({
      folderPath: path
    }));
  };
  var openFolderContextMenu = function openFolderContextMenu(event) {
    if (noContextMenu || isDisabled || loading) {
      return;
    }
    triggerContextMenu({
      event: event,
      mode: CONTEXTMENU_MODES.FOLDER,
      itemsPropName: 'folders',
      foldersUuids: [uuid],
      items: [folder],
      // as there is a possibility that the folder won't be inside (folders's Slice).entities then we are providing it as item
      fromSidebar: true
    });
  };
  var loadFolderChildren = function loadFolderChildren(e) {
    e.stopPropagation();
    var isBaseFolder = !parentUuid;
    if (loading || isBaseFolder || isRenameLoading) {
      return;
    }
    dispatch(toggleFolderExpansion(uuid));
  };
  var handleFolderRenaming = function handleFolderRenaming(event) {
    if (isRenameLoading || loading) {
      return;
    }
    var eventKey = event.key.toLowerCase();
    var newName = event.target.value;
    var newNameIsInvalid = INVALID_FOLDER_OR_FILE_CHARS_REGEX.test(newName);
    if (eventKey === 'escape') {
      dispatch(itemRenameOpened(null));
      return;
    }
    if (eventKey !== 'enter') {
      return;
    }
    var oldName = folder.name;
    if (!newName || newName === oldName) {
      dispatch(itemRenameOpened(null));
      return;
    }
    if (newNameIsInvalid) {
      info(i18n('mutualizedFolderInvalidNameError'));
    } else if (newName) {
      setIsRenameLoading(true);
      handlePromise(dispatch(renameFolder({
        uuid: uuid,
        name: newName
      })), function () {
        return info(i18n('mutualizedRenamedSuccessfullyInfo'), 'success');
      }, undefined, function () {
        setIsRenameLoading(false);
        dispatch(itemRenameOpened(null));
      });
    } else if (newName === '') {
      // 'else if' not 'else' Cuz maybe the user would cancel then we don't need to show the msg.
      info(i18n('explorerInvalidNameInfo'));
    }
  };
  var addFolder = function addFolder(e) {
    if (!selectedPath) {
      return;
    }
    var folderName = e.target.value;
    var key = e.key,
      currentTarget = e.currentTarget;
    e.stopPropagation();
    if (key === 'Enter') {
      currentTarget.disabled = true;
      handlePromise(dispatch(addNewFolder({
        path: selectedPath,
        name: folderName
      })), function (_ref) {
        var folder = _ref.folder;
        onFolderClick(folder);
        if (typeof setShowAddFolderInput === 'function') {
          setShowAddFolderInput();
        }
      }, function () {
        currentTarget.disabled = false;
      });
    }
    if (key === 'Escape' && typeof setShowAddFolderInput === 'function') {
      setShowAddFolderInput();
    }
  };
  var renderDefaultIcon = function renderDefaultIcon() {
    return !loading ? /*#__PURE__*/_jsx(Styled.ArrowWrapper, {
      hideTick: isLoaded && childrenUuids.length === 0,
      isExpanded: isExpanded,
      children: /*#__PURE__*/_jsx(Styled.ArrowIcon, {
        size: 11
      })
    }) : /*#__PURE__*/_jsx(Styled.LoadingIcon, {});
  };
  var renderRenameInput = function renderRenameInput() {
    return /*#__PURE__*/_jsx("input", {
      type: "text",
      ref: function ref(target) {
        if (target) {
          target.focus();
        }
      },
      style: {
        width: 'calc(100% - 16px)',
        fontSize: 13
      },
      defaultValue: name,
      onKeyUp: handleFolderRenaming,
      onKeyDown: ignoreEvent,
      onClick: ignoreEvent,
      onDoubleClick: ignoreEvent,
      onContextMenu: ignoreEvent,
      disabled: isRenameLoading
    });
  };
  var renderNewFolderInput = function renderNewFolderInput() {
    return /*#__PURE__*/_jsx(Styled.NewFolderInput, {
      type: "text",
      ref: function ref(target) {
        if (target) {
          target.focus();
          target.setSelectionRange(0, target.value.length);
        }
      },
      onKeyUp: function onKeyUp(e) {
        return addFolder(e);
      },
      onKeyDown: ignoreEvent,
      onClick: ignoreEvent,
      onDoubleClick: ignoreEvent,
      onContextMenu: ignoreEvent,
      defaultValue: "New folder"
    });
  };
  var disableDrag = noDrag || isRenaming;
  return /*#__PURE__*/_jsxs(Styled.FoldersListItem, {
    "aria-disabled": isDisabled,
    "data-testid": "folders-tree-list-item",
    children: [/*#__PURE__*/_jsx(DnD, {
      disableDrag: disableDrag || isDisabled || isRenameLoading || loading,
      item: folder,
      children: /*#__PURE__*/_jsxs(Styled.Item, {
        tabIndex: -1,
        onClick: handleFolderClick,
        onDoubleClick: openFolderOnDblClick ? loadFolderChildren : undefined,
        onContextMenu: noContextMenu || disableFoldersContextMenu ? undefined : openFolderContextMenu,
        isSelected: isSelected,
        isLoading: loading,
        isExpanded: isExpanded,
        "data-filerobot-focusable-uuid": uuid,
        children: [/*#__PURE__*/_jsx(Styled.IconWrapper, {
          onClick: loadFolderChildren,
          children: !icon || loading ? renderDefaultIcon() : icon
        }), /*#__PURE__*/_jsx(Styled.LabelWrapper, {
          children: isRenaming ? renderRenameInput() : /*#__PURE__*/_jsx(Styled.ItemLabel, {
            children: name
          })
        })]
      })
    }), showAddFolderInput && selectedPath && selectedPath === path && renderNewFolderInput(), !isDisabled && isExpanded && isLoaded && (childrenUuids === null || childrenUuids === void 0 ? void 0 : childrenUuids.length) > 0 && /*#__PURE__*/_jsx(Styled.SubFoldersWrapper, {
      children: /*#__PURE__*/_jsx(FoldersTreeList, _objectSpread(_objectSpread({}, props), {}, {
        uuids: childrenUuids,
        noRootFolder: true
      }))
    })]
  });
};
export default FoldersTreeListItem;