You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
1.8 KiB
88 lines
1.8 KiB
5 years ago
|
import { DataNode } from 'rc-tree/lib/interface';
|
||
7 years ago
|
|
||
|
enum Record {
|
||
|
None,
|
||
|
Start,
|
||
|
End,
|
||
|
}
|
||
|
|
||
6 years ago
|
function traverseNodesKey(
|
||
5 years ago
|
treeData: DataNode[],
|
||
|
callback: (key: string | number | null, node: DataNode) => boolean,
|
||
6 years ago
|
) {
|
||
5 years ago
|
function processNode(dataNode: DataNode) {
|
||
|
const { key, children } = dataNode;
|
||
|
if (callback(key, dataNode) !== false) {
|
||
|
traverseNodesKey(children || [], callback);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
treeData.forEach(processNode);
|
||
6 years ago
|
}
|
||
|
|
||
7 years ago
|
/** 计算选中范围,只考虑expanded情况以优化性能 */
|
||
6 years ago
|
export function calcRangeKeys(
|
||
5 years ago
|
treeData: DataNode[],
|
||
6 years ago
|
expandedKeys: string[],
|
||
|
startKey?: string,
|
||
|
endKey?: string,
|
||
|
): string[] {
|
||
7 years ago
|
const keys: string[] = [];
|
||
|
let record: Record = Record.None;
|
||
|
|
||
|
if (startKey && startKey === endKey) {
|
||
|
return [startKey];
|
||
|
}
|
||
|
if (!startKey || !endKey) {
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
function matchKey(key: string) {
|
||
|
return key === startKey || key === endKey;
|
||
|
}
|
||
|
|
||
5 years ago
|
traverseNodesKey(treeData, (key: string) => {
|
||
7 years ago
|
if (record === Record.End) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (matchKey(key)) {
|
||
|
// Match test
|
||
|
keys.push(key);
|
||
|
|
||
|
if (record === Record.None) {
|
||
|
record = Record.Start;
|
||
|
} else if (record === Record.Start) {
|
||
|
record = Record.End;
|
||
|
return false;
|
||
|
}
|
||
|
} else if (record === Record.Start) {
|
||
|
// Append selection
|
||
|
keys.push(key);
|
||
|
}
|
||
|
|
||
|
if (expandedKeys.indexOf(key) === -1) {
|
||
|
return false;
|
||
|
}
|
||
6 years ago
|
|
||
|
return true;
|
||
7 years ago
|
});
|
||
|
|
||
|
return keys;
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
export function convertDirectoryKeysToNodes(treeData: DataNode[], keys: string[]) {
|
||
6 years ago
|
const restKeys: string[] = [...keys];
|
||
5 years ago
|
const nodes: DataNode[] = [];
|
||
|
traverseNodesKey(treeData, (key: string, node: DataNode) => {
|
||
6 years ago
|
const index = restKeys.indexOf(key);
|
||
|
if (index !== -1) {
|
||
|
nodes.push(node);
|
||
|
restKeys.splice(index, 1);
|
||
|
}
|
||
|
|
||
|
return !!restKeys.length;
|
||
|
});
|
||
|
return nodes;
|
||
6 years ago
|
}
|