فهرست منبع

fix: branch end check, all branches are not allowed to be end (#146)

xiamidaxia 9 ماه پیش
والد
کامیت
4bb3270f2d
2فایلهای تغییر یافته به همراه30 افزوده شده و 2 حذف شده
  1. 29 1
      apps/demo-fixed-layout/src/nodes/end/index.ts
  2. 1 1
      packages/canvas-engine/document/src/typings/flow.ts

+ 29 - 1
apps/demo-fixed-layout/src/nodes/end/index.ts

@@ -1,4 +1,5 @@
 import { nanoid } from 'nanoid';
+import { FlowNodeBaseType } from '@flowgram.ai/fixed-layout-editor';
 
 import { FlowNodeRegistry } from '../../typings';
 import iconEnd from '../../assets/icon-end.jpg';
@@ -23,7 +24,34 @@ export const EndNodeRegistry: FlowNodeRegistry = {
   formMeta,
   canAdd(ctx, from) {
     // You can only add to the last node of the branch
-    return from.isLast;
+    if (!from.isLast) return false;
+    /**
+     * condition
+     *  blockIcon
+     *  inlineBlocks
+     *    block1
+     *      blockOrderIcon
+     *      <---- [add end]
+     *    block2
+     *      blockOrderIcon
+     *      end
+     */
+    // originParent can determine whether it is condition , and then determine whether it is the last one
+    // https://github.com/bytedance/flowgram.ai/pull/146
+    if (
+      from.parent &&
+      from.parent.parent?.flowNodeType === FlowNodeBaseType.INLINE_BLOCKS &&
+      from.parent.originParent &&
+      !from.parent.originParent.isLast
+    ) {
+      const allBranches = from.parent.parent!.blocks;
+      // Determine whether the last node of all branch is end, All branches are not allowed to be end
+      const branchEndCount = allBranches.filter(
+        (block) => block.blocks[block.blocks.length - 1]?.getNodeMeta().isNodeEnd
+      ).length;
+      return branchEndCount < allBranches.length - 1;
+    }
+    return true;
   },
   canDelete(ctx, node) {
     return node.parent === ctx.document.root;

+ 1 - 1
packages/canvas-engine/document/src/typings/flow.ts

@@ -8,7 +8,7 @@ export type FlowNodeType = string | number;
  */
 export interface FlowNodeJSON {
   id: string;
-  type?: FlowNodeType; // 如果缺省 则 为 block
+  type?: FlowNodeBaseType | FlowNodeSplitType | FlowNodeType; // 如果缺省 则 为 block
   data?: Record<string, any>; // 节点额外扩展的内容
   meta?: FlowNodeMeta;
   blocks?: FlowNodeJSON[]; // 子节点