2020-04-17 23:31:39 +08:00
|
|
|
|
import { DataNode, Key } from 'rc-tree/lib/interface';
|
2018-06-04 11:20:17 +08:00
|
|
|
|
|
|
|
|
|
enum Record {
|
|
|
|
|
None,
|
|
|
|
|
Start,
|
|
|
|
|
End,
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2018-12-07 16:17:45 +08:00
|
|
|
|
) {
|
2019-08-12 13:22:36 +08:00
|
|
|
|
function processNode(dataNode: DataNode) {
|
|
|
|
|
const { key, children } = dataNode;
|
|
|
|
|
if (callback(key, dataNode) !== false) {
|
|
|
|
|
traverseNodesKey(children || [], callback);
|
2018-07-30 12:05:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 13:22:36 +08:00
|
|
|
|
treeData.forEach(processNode);
|
2018-07-30 12:05:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-04 11:20:17 +08:00
|
|
|
|
/** 计算选中范围,只考虑expanded情况以优化性能 */
|
2018-12-07 16:17:45 +08:00
|
|
|
|
export function calcRangeKeys(
|
2019-08-12 13:22:36 +08:00
|
|
|
|
treeData: DataNode[],
|
2020-04-17 23:31:39 +08:00
|
|
|
|
expandedKeys: Key[],
|
|
|
|
|
startKey?: Key,
|
|
|
|
|
endKey?: Key,
|
|
|
|
|
): Key[] {
|
|
|
|
|
const keys: Key[] = [];
|
2018-06-04 11:20:17 +08:00
|
|
|
|
let record: Record = Record.None;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 23:31:39 +08:00
|
|
|
|
traverseNodesKey(treeData, (key: Key) => {
|
2018-06-04 11:20:17 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2018-07-30 12:05:29 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
2018-06-04 11:20:17 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
|
}
|
2019-04-09 14:20:59 +08:00
|
|
|
|
|
2020-04-17 23:31:39 +08:00
|
|
|
|
export function convertDirectoryKeysToNodes(treeData: DataNode[], keys: Key[]) {
|
|
|
|
|
const restKeys: Key[] = [...keys];
|
2019-08-12 13:22:36 +08:00
|
|
|
|
const nodes: DataNode[] = [];
|
2020-04-17 23:31:39 +08:00
|
|
|
|
traverseNodesKey(treeData, (key: Key, node: DataNode) => {
|
2019-04-09 14:20:59 +08:00
|
|
|
|
const index = restKeys.indexOf(key);
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
nodes.push(node);
|
|
|
|
|
restKeys.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !!restKeys.length;
|
|
|
|
|
});
|
|
|
|
|
return nodes;
|
2019-05-07 14:57:32 +08:00
|
|
|
|
}
|