1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-26 23:34:45 +00:00

Optimize/tree op validation (#8157)

* optimize tree operations validation

* optimize

* optimize

---------

Co-authored-by: zhouwenxuan <aries@Mac.local>
Co-authored-by: Michael An <1822852997@qq.com>
This commit is contained in:
Aries
2025-08-21 11:17:16 +08:00
committed by GitHub
parent 7751f590da
commit 6d82b7dee4
2 changed files with 23 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ class Tree {
} }
getNodeByPath(path) { getNodeByPath(path) {
if (!path || !this.root) return null;
let findNode = null; let findNode = null;
function callback(currentNode) { function callback(currentNode) {
if (currentNode.path === path) { if (currentNode.path === path) {
@@ -32,6 +33,7 @@ class Tree {
} }
getNodeChildrenObject(node) { getNodeChildrenObject(node) {
if (!node || !node.children) return [];
let objects = node.children.map(item => { let objects = node.children.map(item => {
let object = item.object; let object = item.object;
return object; return object;
@@ -45,36 +47,43 @@ class Tree {
} }
addNodeListToParent(nodeList, parentNode) { addNodeListToParent(nodeList, parentNode) {
if (!parentNode || !Array.isArray(nodeList)) return false;
nodeList.forEach(node => { nodeList.forEach(node => {
parentNode.addChild(node); parentNode.addChild(node);
}); });
} }
deleteNode(node) { deleteNode(node) {
if (!node || !node.parentNode) return false;
let parentNode = this.getNodeByPath(node.parentNode.path); let parentNode = this.getNodeByPath(node.parentNode.path);
parentNode.deleteChild(node); parentNode.deleteChild(node);
} }
deleteNodeList(nodeList) { deleteNodeList(nodeList) {
if (!Array.isArray(nodeList)) return false;
nodeList.forEach(node => { nodeList.forEach(node => {
this.deleteNode(node); this.deleteNode(node);
}); });
} }
renameNode(node, newName) { renameNode(node, newName) {
if (!node || !newName || typeof newName !== 'string') return false;
node.rename(newName); node.rename(newName);
} }
updateNode(node, keys, newValues) { updateNode(node, keys, newValues) {
if (!node || !Array.isArray(keys) || !Array.isArray(newValues) || keys.length !== newValues.length) return false;
node.updateObjectProperties(keys, newValues); node.updateObjectProperties(keys, newValues);
} }
moveNode(node, destNode) { moveNode(node, destNode) {
if (!node || !destNode || node === destNode) return false;
this.deleteNode(node); this.deleteNode(node);
destNode.addChild(node); destNode.addChild(node);
} }
copyNode(node, destNode) { copyNode(node, destNode) {
if (!node || !destNode) return false;
destNode.addChild(node); destNode.addChild(node);
} }
@@ -107,6 +116,7 @@ class Tree {
} }
expandNode(node) { expandNode(node) {
if (!node) return false;
node.isExpanded = true; node.isExpanded = true;
while (node.parentNode) { while (node.parentNode) {
node.parentNode.isExpanded = true; node.parentNode.isExpanded = true;
@@ -115,10 +125,12 @@ class Tree {
} }
collapseNode(node) { collapseNode(node) {
if (!node) return false;
node.isExpanded = false; node.isExpanded = false;
} }
isNodeChild(node, parentNode) { isNodeChild(node, parentNode) {
if (!node || !parentNode || !parentNode.children) return false;
return parentNode.children.some(item => { return parentNode.children.some(item => {
return item.path === node.path; return item.path === node.path;
}); });

View File

@@ -2032,7 +2032,18 @@ class LibContentView extends React.Component {
} }
}; };
isTreeReady = (path = null) => {
const { treeData, isTreeDataLoading } = this.state;
if (isTreeDataLoading || !treeData || !treeData.root) return false;
if (path) {
const node = treeData.getNodeByPath(path);
return node !== null;
}
return true;
};
addNodeToTree = (name, parentPath, type) => { addNodeToTree = (name, parentPath, type) => {
if (!this.isTreeReady(parentPath)) return;
let node = this.createTreeNode(name, type); let node = this.createTreeNode(name, type);
let tree = treeHelper.addNodeToParentByPath(this.state.treeData, node, parentPath); let tree = treeHelper.addNodeToParentByPath(this.state.treeData, node, parentPath);
this.setState({ treeData: tree }); this.setState({ treeData: tree });