node value init

This commit is contained in:
StyleZhang 2024-03-15 19:51:46 +08:00
parent 129a68bb06
commit e33260d2e2
3 changed files with 4 additions and 99 deletions

View File

@ -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])
}

View File

@ -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<string, any>),
})

View File

@ -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<string, PositionMap> = {}
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)