2021-06-26 01:35:40 +00:00
|
|
|
|
import type { VNode } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
|
import { getNodeChildren, convertTreeToEntities } from '../vc-tree/src/util';
|
2020-07-17 08:50:22 +00:00
|
|
|
|
import { getSlot } from '../_util/props-util';
|
2021-06-26 01:35:40 +00:00
|
|
|
|
import type { TreeDataItem } from './Tree';
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
2020-10-22 06:25:02 +00:00
|
|
|
|
enum Record {
|
|
|
|
|
None,
|
|
|
|
|
Start,
|
|
|
|
|
End,
|
|
|
|
|
}
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
2020-11-18 09:37:15 +00:00
|
|
|
|
type TreeKey = string | number;
|
|
|
|
|
|
2018-09-26 14:57:01 +00:00
|
|
|
|
// TODO: Move this logic into `rc-tree`
|
2020-11-18 09:37:15 +00:00
|
|
|
|
function traverseNodesKey(rootChildren: VNode[], callback?: Function) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const nodeList = getNodeChildren(rootChildren) || [];
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
2020-11-18 09:37:15 +00:00
|
|
|
|
function processNode(node: VNode) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const { key } = node;
|
2020-07-17 08:50:22 +00:00
|
|
|
|
const children = getSlot(node);
|
2020-03-07 11:45:13 +00:00
|
|
|
|
if (callback(key, node) !== false) {
|
2020-07-17 08:50:22 +00:00
|
|
|
|
traverseNodesKey(children, callback);
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
nodeList.forEach(processNode);
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 09:37:15 +00:00
|
|
|
|
export function getFullKeyList(children: VNode[]) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const { keyEntities } = convertTreeToEntities(children);
|
|
|
|
|
return [...keyEntities.keys()];
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 计算选中范围,只考虑expanded情况以优化性能 */
|
2020-11-18 09:37:15 +00:00
|
|
|
|
export function calcRangeKeys(
|
|
|
|
|
rootChildren: VNode[],
|
|
|
|
|
expandedKeys: TreeKey[],
|
|
|
|
|
startKey: TreeKey,
|
|
|
|
|
endKey: TreeKey,
|
|
|
|
|
) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
const keys = [];
|
|
|
|
|
let record = Record.None;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
|
|
|
|
if (startKey && startKey === endKey) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return [startKey];
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
if (!startKey || !endKey) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return [];
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 09:37:15 +00:00
|
|
|
|
function matchKey(key: TreeKey) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return key === startKey || key === endKey;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 09:37:15 +00:00
|
|
|
|
traverseNodesKey(rootChildren, (key: TreeKey) => {
|
2018-09-26 14:57:01 +00:00
|
|
|
|
if (record === Record.End) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return false;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (matchKey(key)) {
|
|
|
|
|
// Match test
|
2019-01-12 03:33:27 +00:00
|
|
|
|
keys.push(key);
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
|
|
|
|
if (record === Record.None) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
record = Record.Start;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
} else if (record === Record.Start) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
record = Record.End;
|
|
|
|
|
return false;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
} else if (record === Record.Start) {
|
|
|
|
|
// Append selection
|
2019-01-12 03:33:27 +00:00
|
|
|
|
keys.push(key);
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (expandedKeys.indexOf(key) === -1) {
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return false;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return true;
|
|
|
|
|
});
|
2018-09-26 14:57:01 +00:00
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
|
return keys;
|
2018-09-26 14:57:01 +00:00
|
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
2020-11-18 09:37:15 +00:00
|
|
|
|
export function convertDirectoryKeysToNodes(rootChildren: VNode[], keys: TreeKey[]) {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const restKeys = [...keys];
|
|
|
|
|
const nodes = [];
|
2020-11-18 09:37:15 +00:00
|
|
|
|
traverseNodesKey(rootChildren, (key: TreeKey, node: VNode) => {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
const index = restKeys.indexOf(key);
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
nodes.push(node);
|
|
|
|
|
restKeys.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !!restKeys.length;
|
|
|
|
|
});
|
|
|
|
|
return nodes;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 09:37:15 +00:00
|
|
|
|
export function getFullKeyListByTreeData(treeData: TreeDataItem[], replaceFields: any = {}) {
|
2020-03-07 11:45:13 +00:00
|
|
|
|
let keys = [];
|
2021-06-30 07:44:08 +00:00
|
|
|
|
const { key = 'key', children = 'children' } = replaceFields;
|
|
|
|
|
(treeData || []).forEach((item: TreeDataItem) => {
|
|
|
|
|
keys.push(item[key]);
|
|
|
|
|
if (item[children]) {
|
|
|
|
|
keys = [...keys, ...getFullKeyListByTreeData(item[children], replaceFields)];
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
return keys;
|
|
|
|
|
}
|