2022-05-07 14:31:54 +08:00
|
|
|
|
import type { DataNode, Key } from 'rc-tree/lib/interface';
|
2023-10-11 17:03:15 +08:00
|
|
|
|
import { fillFieldNames } from 'rc-tree/lib/utils/treeUtil';
|
|
|
|
|
|
|
|
|
|
import type { TreeProps } from '../Tree';
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
2024-04-12 16:28:06 +08:00
|
|
|
|
const RECORD_NONE = 0;
|
|
|
|
|
const RECORD_START = 1;
|
|
|
|
|
const RECORD_END = 2;
|
|
|
|
|
|
|
|
|
|
type Record = typeof RECORD_NONE | typeof RECORD_START | typeof RECORD_END;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
2023-10-11 17:03:15 +08:00
|
|
|
|
type FieldNames = TreeProps['fieldNames'];
|
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
|
function traverseNodesKey(
|
2019-08-12 13:22:36 +08:00
|
|
|
|
treeData: DataNode[],
|
2020-04-17 23:31:39 +08:00
|
|
|
|
callback: (key: Key | number | null, node: DataNode) => boolean,
|
2023-10-11 17:03:15 +08:00
|
|
|
|
fieldNames: Required<NonNullable<FieldNames>>,
|
2018-12-07 16:17:45 +08:00
|
|
|
|
) {
|
2023-10-11 17:03:15 +08:00
|
|
|
|
const { key: fieldKey, children: fieldChildren } = fieldNames;
|
|
|
|
|
|
|
|
|
|
function processNode(dataNode: DataNode & FieldNames[keyof FieldNames]) {
|
|
|
|
|
const key = dataNode[fieldKey];
|
|
|
|
|
const children = dataNode[fieldChildren];
|
2019-08-12 13:22:36 +08:00
|
|
|
|
if (callback(key, dataNode) !== false) {
|
2023-10-11 17:03:15 +08:00
|
|
|
|
traverseNodesKey(children || [], callback, fieldNames);
|
2018-07-30 12:05:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
|
treeData.forEach(processNode as any);
|
2018-07-30 12:05:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** 计算选中范围,只考虑expanded情况以优化性能 */
|
2020-06-19 15:45:52 +08:00
|
|
|
|
export function calcRangeKeys({
|
|
|
|
|
treeData,
|
|
|
|
|
expandedKeys,
|
|
|
|
|
startKey,
|
|
|
|
|
endKey,
|
2023-10-11 17:03:15 +08:00
|
|
|
|
fieldNames,
|
2020-06-19 15:45:52 +08:00
|
|
|
|
}: {
|
|
|
|
|
treeData: DataNode[];
|
|
|
|
|
expandedKeys: Key[];
|
|
|
|
|
startKey?: Key;
|
|
|
|
|
endKey?: Key;
|
2023-10-11 17:03:15 +08:00
|
|
|
|
fieldNames?: FieldNames;
|
2020-06-19 15:45:52 +08:00
|
|
|
|
}): Key[] {
|
2020-04-17 23:31:39 +08:00
|
|
|
|
const keys: Key[] = [];
|
2024-04-12 16:28:06 +08:00
|
|
|
|
let record: Record = RECORD_NONE;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
|
|
if (startKey && startKey === endKey) {
|
|
|
|
|
return [startKey];
|
|
|
|
|
}
|
|
|
|
|
if (!startKey || !endKey) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 23:31:39 +08:00
|
|
|
|
function matchKey(key: Key) {
|
2018-06-04 11:20:17 +08:00
|
|
|
|
return key === startKey || key === endKey;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 17:03:15 +08:00
|
|
|
|
traverseNodesKey(
|
|
|
|
|
treeData,
|
2024-04-01 15:49:45 +08:00
|
|
|
|
(key) => {
|
2024-04-12 16:28:06 +08:00
|
|
|
|
if (record === RECORD_END) {
|
2023-10-11 17:03:15 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
|
if (matchKey(key as any)) {
|
2023-10-11 17:03:15 +08:00
|
|
|
|
// Match test
|
2024-04-01 15:49:45 +08:00
|
|
|
|
keys.push(key as any);
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
2024-04-12 16:28:06 +08:00
|
|
|
|
if (record === RECORD_NONE) {
|
|
|
|
|
record = RECORD_START;
|
|
|
|
|
} else if (record === RECORD_START) {
|
|
|
|
|
record = RECORD_END;
|
2023-10-11 17:03:15 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-04-12 16:28:06 +08:00
|
|
|
|
} else if (record === RECORD_START) {
|
2023-10-11 17:03:15 +08:00
|
|
|
|
// Append selection
|
2024-04-01 15:49:45 +08:00
|
|
|
|
keys.push(key as any);
|
2018-06-04 11:20:17 +08:00
|
|
|
|
}
|
2024-04-01 15:49:45 +08:00
|
|
|
|
return expandedKeys.includes(key as any);
|
2023-10-11 17:03:15 +08:00
|
|
|
|
},
|
|
|
|
|
fillFieldNames(fieldNames),
|
|
|
|
|
);
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
|
}
|
2019-04-09 14:20:59 +08:00
|
|
|
|
|
2023-10-11 17:03:15 +08:00
|
|
|
|
export function convertDirectoryKeysToNodes(
|
|
|
|
|
treeData: DataNode[],
|
|
|
|
|
keys: Key[],
|
|
|
|
|
fieldNames?: FieldNames,
|
|
|
|
|
) {
|
2020-04-17 23:31:39 +08:00
|
|
|
|
const restKeys: Key[] = [...keys];
|
2019-08-12 13:22:36 +08:00
|
|
|
|
const nodes: DataNode[] = [];
|
2023-10-11 17:03:15 +08:00
|
|
|
|
traverseNodesKey(
|
|
|
|
|
treeData,
|
2024-04-01 15:49:45 +08:00
|
|
|
|
(key, node) => {
|
|
|
|
|
const index = restKeys.indexOf(key as any);
|
2023-10-11 17:03:15 +08:00
|
|
|
|
if (index !== -1) {
|
|
|
|
|
nodes.push(node);
|
|
|
|
|
restKeys.splice(index, 1);
|
|
|
|
|
}
|
2019-04-09 14:20:59 +08:00
|
|
|
|
|
2023-10-11 17:03:15 +08:00
|
|
|
|
return !!restKeys.length;
|
|
|
|
|
},
|
|
|
|
|
fillFieldNames(fieldNames),
|
|
|
|
|
);
|
2019-04-09 14:20:59 +08:00
|
|
|
|
return nodes;
|
2019-05-07 14:57:32 +08:00
|
|
|
|
}
|