Просмотр исходного кода

perf(demo): optimize the way loop create its block start & end node (#466)

Louis Young 6 месяцев назад
Родитель
Сommit
2c4a42a772

+ 0 - 47
apps/demo-free-layout/src/nodes/loop/create-built-in-nodes.ts

@@ -1,47 +0,0 @@
-/**
- * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
- * SPDX-License-Identifier: MIT
- */
-
-import { delay, WorkflowDocument, WorkflowNodeEntity } from '@flowgram.ai/free-layout-editor';
-
-import { WorkflowNodeType } from '../constants';
-
-export const createBuiltInNodes = async (node: WorkflowNodeEntity) => {
-  // wait for node render - 等待节点渲染
-  await delay(16);
-  if (node.blocks.length) {
-    return;
-  }
-  const document = node.document as WorkflowDocument;
-  document.createWorkflowNode(
-    {
-      id: `block_start_${node.id}`,
-      type: WorkflowNodeType.BlockStart,
-      meta: {
-        position: {
-          x: -80,
-          y: 0,
-        },
-      },
-      data: {},
-    },
-    false,
-    node.id
-  );
-  document.createWorkflowNode(
-    {
-      id: `block_end_${node.id}`,
-      type: WorkflowNodeType.BlockEnd,
-      meta: {
-        position: {
-          x: 80,
-          y: 0,
-        },
-      },
-      data: {},
-    },
-    false,
-    node.id
-  );
-};

+ 24 - 4
apps/demo-free-layout/src/nodes/loop/index.ts

@@ -16,7 +16,6 @@ import { FlowNodeRegistry } from '../../typings';
 import iconLoop from '../../assets/icon-loop.jpg';
 import { LoopFormRender } from './loop-form-render';
 import { WorkflowNodeType } from '../constants';
-import { createBuiltInNodes } from './create-built-in-nodes';
 
 let index = 0;
 export const LoopNodeRegistry: FlowNodeRegistry = {
@@ -74,11 +73,32 @@ export const LoopNodeRegistry: FlowNodeRegistry = {
       data: {
         title: `Loop_${++index}`,
       },
+      blocks: [
+        {
+          id: `block_start_${nanoid(5)}`,
+          type: WorkflowNodeType.BlockStart,
+          meta: {
+            position: {
+              x: -80,
+              y: 0,
+            },
+          },
+          data: {},
+        },
+        {
+          id: `block_end_${nanoid(5)}`,
+          type: WorkflowNodeType.BlockEnd,
+          meta: {
+            position: {
+              x: 80,
+              y: 0,
+            },
+          },
+          data: {},
+        },
+      ],
     };
   },
-  onCreate(node, json) {
-    createBuiltInNodes(node);
-  },
   formMeta: {
     ...defaultFormMeta,
     render: LoopFormRender,

+ 16 - 16
apps/demo-free-layout/src/shortcuts/copy/index.ts

@@ -57,6 +57,22 @@ export class CopyShortcut implements ShortcutsHandler {
     await this.write(data);
   }
 
+  /**
+   * create clipboard data - 转换为剪贴板数据
+   */
+  public toClipboardData(nodes?: WorkflowNodeEntity[]): WorkflowClipboardData {
+    const validNodes = this.getValidNodes(nodes ? nodes : this.selectedNodes);
+    const source = this.toSource();
+    const json = this.toJSON(validNodes);
+    const bounds = this.getEntireBounds(validNodes);
+    return {
+      type: WorkflowClipboardDataID,
+      source,
+      json,
+      bounds,
+    };
+  }
+
   /**
    *  has selected text - 是否有文字被选中
    */
@@ -93,22 +109,6 @@ export class CopyShortcut implements ShortcutsHandler {
     return true;
   }
 
-  /**
-   * create clipboard data - 转换为剪贴板数据
-   */
-  toClipboardData(nodes?: WorkflowNodeEntity[]): WorkflowClipboardData {
-    const validNodes = this.getValidNodes(nodes ? nodes : this.selectedNodes);
-    const source = this.toSource();
-    const json = this.toJSON(validNodes);
-    const bounds = this.getEntireBounds(validNodes);
-    return {
-      type: WorkflowClipboardDataID,
-      source,
-      json,
-      bounds,
-    };
-  }
-
   /**
    * get valid nodes - 获取有效的节点
    */