amis-rpc-design/node_modules/rc-menu/es/hooks/useKeyRecords.js
2023-10-07 19:42:30 +08:00

109 lines
3.6 KiB
JavaScript

import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
import { useRef, useCallback } from 'react';
import warning from "rc-util/es/warning";
import { nextSlice } from "../utils/timeUtil";
var PATH_SPLIT = '__RC_UTIL_PATH_SPLIT__';
var getPathStr = function getPathStr(keyPath) {
return keyPath.join(PATH_SPLIT);
};
var getPathKeys = function getPathKeys(keyPathStr) {
return keyPathStr.split(PATH_SPLIT);
};
export var OVERFLOW_KEY = 'rc-menu-more';
export default function useKeyRecords() {
var _React$useState = React.useState({}),
_React$useState2 = _slicedToArray(_React$useState, 2),
internalForceUpdate = _React$useState2[1];
var key2pathRef = useRef(new Map());
var path2keyRef = useRef(new Map());
var _React$useState3 = React.useState([]),
_React$useState4 = _slicedToArray(_React$useState3, 2),
overflowKeys = _React$useState4[0],
setOverflowKeys = _React$useState4[1];
var updateRef = useRef(0);
var destroyRef = useRef(false);
var forceUpdate = function forceUpdate() {
if (!destroyRef.current) {
internalForceUpdate({});
}
};
var registerPath = useCallback(function (key, keyPath) {
// Warning for invalidate or duplicated `key`
if (process.env.NODE_ENV !== 'production') {
warning(!key2pathRef.current.has(key), "Duplicated key '".concat(key, "' used in Menu by path [").concat(keyPath.join(' > '), "]"));
}
// Fill map
var connectedPath = getPathStr(keyPath);
path2keyRef.current.set(connectedPath, key);
key2pathRef.current.set(key, connectedPath);
updateRef.current += 1;
var id = updateRef.current;
nextSlice(function () {
if (id === updateRef.current) {
forceUpdate();
}
});
}, []);
var unregisterPath = useCallback(function (key, keyPath) {
var connectedPath = getPathStr(keyPath);
path2keyRef.current.delete(connectedPath);
key2pathRef.current.delete(key);
}, []);
var refreshOverflowKeys = useCallback(function (keys) {
setOverflowKeys(keys);
}, []);
var getKeyPath = useCallback(function (eventKey, includeOverflow) {
var fullPath = key2pathRef.current.get(eventKey) || '';
var keys = getPathKeys(fullPath);
if (includeOverflow && overflowKeys.includes(keys[0])) {
keys.unshift(OVERFLOW_KEY);
}
return keys;
}, [overflowKeys]);
var isSubPathKey = useCallback(function (pathKeys, eventKey) {
return pathKeys.some(function (pathKey) {
var pathKeyList = getKeyPath(pathKey, true);
return pathKeyList.includes(eventKey);
});
}, [getKeyPath]);
var getKeys = function getKeys() {
var keys = _toConsumableArray(key2pathRef.current.keys());
if (overflowKeys.length) {
keys.push(OVERFLOW_KEY);
}
return keys;
};
/**
* Find current key related child path keys
*/
var getSubPathKeys = useCallback(function (key) {
var connectedPath = "".concat(key2pathRef.current.get(key)).concat(PATH_SPLIT);
var pathKeys = new Set();
_toConsumableArray(path2keyRef.current.keys()).forEach(function (pathKey) {
if (pathKey.startsWith(connectedPath)) {
pathKeys.add(path2keyRef.current.get(pathKey));
}
});
return pathKeys;
}, []);
React.useEffect(function () {
return function () {
destroyRef.current = true;
};
}, []);
return {
// Register
registerPath: registerPath,
unregisterPath: unregisterPath,
refreshOverflowKeys: refreshOverflowKeys,
// Util
isSubPathKey: isSubPathKey,
getKeyPath: getKeyPath,
getKeys: getKeys,
getSubPathKeys: getSubPathKeys
};
}