Jelajahi Sumber

feat(tree): 增加moveTo方法

vian 5 tahun lalu
induk
melakukan
7638d92fb0
2 mengubah file dengan 58 tambahan dan 2 penghapusan
  1. 1 1
      tree/package.json
  2. 57 1
      tree/src/tree.ts

+ 1 - 1
tree/package.json

@@ -1,6 +1,6 @@
 {
 {
   "name": "@sc/tree",
   "name": "@sc/tree",
-  "version": "1.0.4",
+  "version": "1.0.6",
   "description": "a template for npm package coding",
   "description": "a template for npm package coding",
   "main": "./dist/index.cjs.js",
   "main": "./dist/index.cjs.js",
   "module": "./dist/index.esm.js",
   "module": "./dist/index.esm.js",

+ 57 - 1
tree/src/tree.ts

@@ -468,10 +468,12 @@ export class Tree {
     }
     }
     // 最末节点的所有后兄弟节点,成为最末节点的子节点
     // 最末节点的所有后兄弟节点,成为最末节点的子节点
     const lastNodeNextBrothers = lastNode.getCtx().nextBrothers();
     const lastNodeNextBrothers = lastNode.getCtx().nextBrothers();
+    const lastNodeLastChild = lastNode.getCtx().lastChild();
+    const lastNodeLastChildSeq = lastNodeLastChild ? lastNodeLastChild.seq : 0;
     lastNodeNextBrothers.forEach((node, index) => {
     lastNodeNextBrothers.forEach((node, index) => {
       updateData.push({
       updateData.push({
         ID: node.ID,
         ID: node.ID,
-        update: { parentID: lastNode.ID, seq: index },
+        update: { parentID: lastNode.ID, seq: index + lastNodeLastChildSeq },
       });
       });
     });
     });
     return updateData;
     return updateData;
@@ -536,5 +538,59 @@ export class Tree {
       ...nodes
       ...nodes
     );
     );
     this.updateValue(updateData);
     this.updateValue(updateData);
+    this.reGenData();
+  }
+
+  // 准备移动节点块(连续的兄弟节点),不维护seq连号
+  prepareMoveTo(
+    nodes: TreeNode[],
+    parent: TreeNode | None,
+    next: TreeNode | None
+  ): UpdateData[] {
+    const updateData: UpdateData[] = [];
+    let prev: TreeNode | None;
+    if (next) {
+      prev = next.getCtx().prev();
+    } else {
+      prev = parent ? parent.getCtx().lastChild() : null;
+    }
+    const baseSeq = prev ? prev.seq + 1 : this.seqStartIndex;
+    updateData.push(
+      ...nodes.map((node, index) => ({
+        ID: node.ID,
+        update: {
+          parentID: (parent && parent.ID) || this.rootID,
+          seq: baseSeq + index,
+        },
+      }))
+    );
+    const curBaseSeq = baseSeq + nodes.length;
+    const nextBrothers = prev ? prev.getCtx().nextBrothers() : [];
+    updateData.push(
+      ...nextBrothers.map((node, index) => ({
+        ID: node.ID,
+        update: {
+          seq: curBaseSeq + index,
+        },
+      }))
+    );
+    return updateData;
+  }
+
+  moveTo(
+    nodes: TreeNode[],
+    parent: TreeNode | None,
+    updateData: UpdateData[]
+  ): void {
+    const firstNode = nodes[0];
+    const newParentID = parent ? parent.ID : this.rootID;
+    const orgParentID = firstNode.parentID;
+    const orgBrothers = this.parentMap[orgParentID];
+    orgBrothers.splice(orgBrothers.indexOf(firstNode), nodes.length);
+    (this.parentMap[newParentID] || (this.parentMap[newParentID] = [])).push(
+      ...nodes
+    );
+    this.updateValue(updateData);
+    this.resortDataByID([orgParentID, newParentID]);
   }
   }
 }
 }