Parcourir la source

feat(free-layout): add ctx.document.batchAddFromJSON (#709)

xiamidaxia il y a 4 mois
Parent
commit
397ccadffa

+ 11 - 0
apps/docs/src/en/guide/advanced/free-layout/load.mdx

@@ -146,6 +146,17 @@ function App({ data }) {
   )
 }
 ```
+- Batch add nodes and lines
+
+```tsx pure
+ctx.document.batchAddFromJSON({
+  nodes: [...],
+  edges: [...]
+}, {
+  // Optionally, use this if you want to add to a subcanvas
+  parent: loopNode
+})
+```
 
 ## Listen for Changes and Auto-Save
 

+ 12 - 0
apps/docs/src/zh/guide/advanced/free-layout/load.mdx

@@ -151,6 +151,18 @@ function App({ data }) {
 }
 ```
 
+- 批量添加节点和线条
+
+```tsx pure
+ctx.document.batchAddFromJSON({
+  nodes: [...],
+  edges: [...]
+}, {
+  // 可选,如果要添加到子画布中用这个
+  parent: loopNode
+})
+```
+
 ## 监听变化并自动保存
 
 ```tsx pure

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

@@ -674,7 +674,10 @@ export class WorkflowDocument extends FlowDocument {
   }
 
   /**
-   * 逐层创建节点和线条
+   * 批量添加节点
+   * @deprecated use 'batchAddFromJSON' instead
+   * @param json
+   * @param options
    */
   public renderJSON(
     json: WorkflowJSON,
@@ -685,6 +688,22 @@ export class WorkflowDocument extends FlowDocument {
   ): {
     nodes: WorkflowNodeEntity[];
     edges: WorkflowLineEntity[];
+  } {
+    return this.batchAddFromJSON(json, options);
+  }
+
+  /**
+   * 批量添加节点
+   */
+  public batchAddFromJSON(
+    json: WorkflowJSON,
+    options?: {
+      parent?: WorkflowNodeEntity;
+      isClone?: boolean;
+    }
+  ): {
+    nodes: WorkflowNodeEntity[];
+    edges: WorkflowLineEntity[];
   } {
     const { parent = this.root, isClone = false } = options ?? {};
     // 创建节点