dictUtil.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  2. import { fillFieldNames } from "rc-tree/es/utils/treeUtil";
  3. const RECORD_NONE = 0;
  4. const RECORD_START = 1;
  5. const RECORD_END = 2;
  6. function traverseNodesKey(treeData, callback, fieldNames) {
  7. const {
  8. key: fieldKey,
  9. children: fieldChildren
  10. } = fieldNames;
  11. function processNode(dataNode) {
  12. const key = dataNode[fieldKey];
  13. const children = dataNode[fieldChildren];
  14. if (callback(key, dataNode) !== false) {
  15. traverseNodesKey(children || [], callback, fieldNames);
  16. }
  17. }
  18. treeData.forEach(processNode);
  19. }
  20. /** 计算选中范围,只考虑expanded情况以优化性能 */
  21. export function calcRangeKeys({
  22. treeData,
  23. expandedKeys,
  24. startKey,
  25. endKey,
  26. fieldNames
  27. }) {
  28. const keys = [];
  29. let record = RECORD_NONE;
  30. if (startKey && startKey === endKey) {
  31. return [startKey];
  32. }
  33. if (!startKey || !endKey) {
  34. return [];
  35. }
  36. function matchKey(key) {
  37. return key === startKey || key === endKey;
  38. }
  39. traverseNodesKey(treeData, key => {
  40. if (record === RECORD_END) {
  41. return false;
  42. }
  43. if (matchKey(key)) {
  44. // Match test
  45. keys.push(key);
  46. if (record === RECORD_NONE) {
  47. record = RECORD_START;
  48. } else if (record === RECORD_START) {
  49. record = RECORD_END;
  50. return false;
  51. }
  52. } else if (record === RECORD_START) {
  53. // Append selection
  54. keys.push(key);
  55. }
  56. return expandedKeys.includes(key);
  57. }, fillFieldNames(fieldNames));
  58. return keys;
  59. }
  60. export function convertDirectoryKeysToNodes(treeData, keys, fieldNames) {
  61. const restKeys = _toConsumableArray(keys);
  62. const nodes = [];
  63. traverseNodesKey(treeData, (key, node) => {
  64. const index = restKeys.indexOf(key);
  65. if (index !== -1) {
  66. nodes.push(node);
  67. restKeys.splice(index, 1);
  68. }
  69. return !!restKeys.length;
  70. }, fillFieldNames(fieldNames));
  71. return nodes;
  72. }