瀏覽代碼

fix(core): operation fromJSON cannot create inner-container nodes (#902)

Louis Young 3 月之前
父節點
當前提交
fc37b21010

+ 10 - 3
packages/canvas-engine/free-layout-core/src/service/workflow-operation-base-service.ts

@@ -61,6 +61,7 @@ export class WorkflowOperationBaseServiceImpl
     };
 
     const oldNodes = this.document.getAllNodes();
+    const oldEdges = this.linesManager.getAllLines();
     const oldPositionMap = new Map<string, IPoint>(
       oldNodes.map((node) => [
         node.id,
@@ -74,15 +75,21 @@ export class WorkflowOperationBaseServiceImpl
     const newNodes: WorkflowNodeEntity[] = [];
     const newEdges: WorkflowLineEntity[] = [];
 
-    // 清空线条
-    this.linesManager.getAllLines().map((line) => line.dispose());
-
     // 逐层渲染
     this.document.batchAddFromJSON(workflowJSON, {
       onNodeCreated: (node) => newNodes.push(node),
       onEdgeCreated: (edge) => newEdges.push(edge),
     });
 
+    const newEdgeIDSet = new Set<string>(newEdges.map((edge) => edge.id));
+    oldEdges.forEach((edge) => {
+      // 清空旧线条
+      if (!newEdgeIDSet.has(edge.id)) {
+        edge.dispose();
+        return;
+      }
+    });
+
     const newNodeIDSet = new Set<string>(newNodes.map((node) => node.id));
     oldNodes.forEach((node) => {
       // 清空旧节点

+ 3 - 1
packages/canvas-engine/free-layout-core/src/workflow-document.ts

@@ -740,13 +740,15 @@ export class WorkflowDocument extends FlowDocument {
     nodes: WorkflowNodeEntity[];
     edges: WorkflowLineEntity[];
   } {
-    const { parent = this.root } = options ?? {};
+    const { parent = this.root, onNodeCreated, onEdgeCreated } = options ?? {};
     // 创建节点
     const parentID = this.getNodeSubCanvas(parent)?.canvasNode.id ?? parent.id;
     const processedJSON = buildGroupJSON(json);
     const nodes = processedJSON.nodes.map((nodeJSON: WorkflowNodeJSON) =>
       this._createWorkflowNode(nodeJSON, {
         parentID,
+        onNodeCreated,
+        onEdgeCreated,
       })
     );
     // 创建线条