From e33260d2e2945003beaf8c5bd10d19f4e6b77e8c Mon Sep 17 00:00:00 2001 From: StyleZhang Date: Fri, 15 Mar 2024 19:51:46 +0800 Subject: [PATCH] node value init --- .../workflow/hooks/use-nodes-data.ts | 4 +- .../components/workflow/hooks/use-workflow.ts | 2 +- web/app/components/workflow/utils.ts | 97 ------------------- 3 files changed, 4 insertions(+), 99 deletions(-) diff --git a/web/app/components/workflow/hooks/use-nodes-data.ts b/web/app/components/workflow/hooks/use-nodes-data.ts index 417cb3399c..66c6b9a8dc 100644 --- a/web/app/components/workflow/hooks/use-nodes-data.ts +++ b/web/app/components/workflow/hooks/use-nodes-data.ts @@ -16,13 +16,15 @@ export const useNodesInitialData = () => { return useMemo(() => produce(NODES_INITIAL_DATA, (draft) => { Object.keys(draft).forEach((key) => { draft[key as BlockEnum].title = t(`workflow.blocks.${key}`) - draft[key as BlockEnum]._isReady = true if (nodesDefaultConfigs[key as BlockEnum]) { draft[key as BlockEnum] = { ...draft[key as BlockEnum], ...nodesDefaultConfigs[key as BlockEnum], } } + else { + draft[key as BlockEnum]._isReady = true + } }) }), [t, nodesDefaultConfigs]) } diff --git a/web/app/components/workflow/hooks/use-workflow.ts b/web/app/components/workflow/hooks/use-workflow.ts index 07233fc369..7031829593 100644 --- a/web/app/components/workflow/hooks/use-workflow.ts +++ b/web/app/components/workflow/hooks/use-workflow.ts @@ -236,7 +236,7 @@ export const useWorkflowInit = () => { workflowStore.setState({ nodesDefaultConfigs: nodesDefaultConfigsData.reduce((acc, block) => { if (!acc[block.type]) - acc[block.type] = block.config + acc[block.type] = { ...block.config, _isReady: true } return acc }, {} as Record), }) diff --git a/web/app/components/workflow/utils.ts b/web/app/components/workflow/utils.ts index 8281164f9c..1082fef195 100644 --- a/web/app/components/workflow/utils.ts +++ b/web/app/components/workflow/utils.ts @@ -1,7 +1,6 @@ import { Position, getConnectedEdges, - getOutgoers, } from 'reactflow' import dagre from 'dagre' import { cloneDeep } from 'lodash-es' @@ -12,76 +11,6 @@ import type { import { BlockEnum } from './types' import type { QuestionClassifierNodeType } from './nodes/question-classifier/types' -export const nodesLevelOrderTraverse = ( - firstNode: Node, - nodes: Node[], - edges: Edge[], - callback: (n: any) => void, -) => { - const queue = [{ - node: firstNode, - depth: 0, - breath: 0, - }] - - let currenDepth = 0 - let currentBreath = 0 - while (queue.length) { - const { node, depth, breath } = queue.shift()! - - if (currenDepth !== depth) { - currenDepth = depth - currentBreath = 0 - } - - callback({ node, depth, breath }) - - const targetBranches = node.data._targetBranches - if (targetBranches?.length) { - const targetEdges = getConnectedEdges([node], edges) - - if (targetEdges.length) { - const sortedTargetEdges = targetEdges - .filter(edge => edge.source === node.id) - .sort((a, b) => { - const aIndex = targetBranches.findIndex(branch => branch.id === a.sourceHandle) - const bIndex = targetBranches.findIndex(branch => branch.id === b.sourceHandle) - - return aIndex - bIndex - }) - - const outgoers = getOutgoers(node, nodes, sortedTargetEdges) - - queue.push(...outgoers.map((outgoer, index) => { - return { - node: outgoer, - depth: depth + 1, - breath: currentBreath + index, - } - })) - - currentBreath += outgoers.length - } - else { - currentBreath += 1 - } - } - else { - const outgoers = getOutgoers(node, nodes, edges) - - if (outgoers.length === 1) { - queue.push({ - node: outgoers[0], - depth: depth + 1, - breath: 0, - }) - } - - currentBreath += 1 - } - } -} - export const initialNodes = (nodes: Node[], edges: Edge[]) => { return nodes.map((node) => { node.type = 'custom' @@ -121,34 +50,8 @@ export const initialEdges = (edges: Edge[]) => { }) } -export type PositionMap = { - index: { - x: number - y: number - } -} - -export const getNodesPositionMap = (nodes: Node[], edges: Edge[]) => { - const startNode = nodes.find((node: Node) => node.data.type === BlockEnum.Start) - const positionMap: Record = {} - - if (startNode) { - nodesLevelOrderTraverse(startNode, nodes, edges, ({ node, depth, breath }) => { - positionMap[node.id] = { - index: { - x: depth, - y: breath, - }, - } - }) - } - - return positionMap -} - const dagreGraph = new dagre.graphlib.Graph() dagreGraph.setDefaultEdgeLabel(() => ({})) - export const getLayoutByDagre = (originNodes: Node[], originEdges: Edge[]) => { const nodes = cloneDeep(originNodes) const edges = cloneDeep(originEdges)