mirror of
https://github.com/langgenius/dify.git
synced 2026-03-26 08:40:14 +08:00
Made-with: Cursor # Conflicts: # .devcontainer/post_create_command.sh # api/commands.py # api/core/agent/cot_agent_runner.py # api/core/agent/fc_agent_runner.py # api/core/app/apps/workflow_app_runner.py # api/core/app/entities/queue_entities.py # api/core/app/entities/task_entities.py # api/core/workflow/workflow_entry.py # api/dify_graph/enums.py # api/dify_graph/graph/graph.py # api/dify_graph/graph_events/node.py # api/dify_graph/model_runtime/entities/message_entities.py # api/dify_graph/node_events/node.py # api/dify_graph/nodes/agent/agent_node.py # api/dify_graph/nodes/base/__init__.py # api/dify_graph/nodes/base/entities.py # api/dify_graph/nodes/base/node.py # api/dify_graph/nodes/llm/entities.py # api/dify_graph/nodes/llm/node.py # api/dify_graph/nodes/tool/tool_node.py # api/pyproject.toml # api/uv.lock # web/app/components/base/avatar/__tests__/index.spec.tsx # web/app/components/base/avatar/index.tsx # web/app/components/base/date-and-time-picker/time-picker/__tests__/index.spec.tsx # web/app/components/base/file-uploader/file-from-link-or-local/index.tsx # web/app/components/base/prompt-editor/index.tsx # web/app/components/datasets/metadata/edit-metadata-batch/modal.tsx # web/app/components/header/account-dropdown/index.spec.tsx # web/app/components/share/text-generation/index.tsx # web/app/components/workflow/block-selector/tool/action-item.tsx # web/app/components/workflow/block-selector/trigger-plugin/action-item.tsx # web/app/components/workflow/hooks/use-edges-interactions.ts # web/app/components/workflow/hooks/use-nodes-interactions.ts # web/app/components/workflow/index.tsx # web/app/components/workflow/nodes/_base/components/editor/code-editor/index.tsx # web/app/components/workflow/nodes/http/components/key-value/key-value-edit/index.tsx # web/app/components/workflow/nodes/human-input/components/delivery-method/recipient/email-item.tsx # web/app/components/workflow/nodes/loop/use-interactions.ts # web/contract/router.ts # web/env.ts # web/eslint-suppressions.json # web/package.json # web/pnpm-lock.yaml
154 lines
5.3 KiB
TypeScript
154 lines
5.3 KiB
TypeScript
import type {
|
|
BlockEnum,
|
|
Node,
|
|
} from '../../types'
|
|
import { produce } from 'immer'
|
|
import { useCallback } from 'react'
|
|
import { useNodesMetaData } from '@/app/components/workflow/hooks'
|
|
import { useCollaborativeWorkflow } from '@/app/components/workflow/hooks/use-collaborative-workflow'
|
|
import {
|
|
LOOP_CHILDREN_Z_INDEX,
|
|
LOOP_PADDING,
|
|
} from '../../constants'
|
|
import {
|
|
generateNewNode,
|
|
getNodeCustomTypeByNodeDataType,
|
|
} from '../../utils'
|
|
import { CUSTOM_LOOP_START_NODE } from '../loop-start/constants'
|
|
|
|
export const useNodeLoopInteractions = () => {
|
|
const collaborativeWorkflow = useCollaborativeWorkflow()
|
|
const { nodesMap: nodesMetaDataMap } = useNodesMetaData()
|
|
|
|
const handleNodeLoopRerender = useCallback((nodeId: string) => {
|
|
const { nodes, setNodes } = collaborativeWorkflow.getState()
|
|
const currentNode = nodes.find(n => n.id === nodeId)!
|
|
const childrenNodes = nodes.filter(n => n.parentId === nodeId && n.type !== CUSTOM_LOOP_START_NODE)
|
|
if (!childrenNodes.length)
|
|
return
|
|
let rightNode: Node
|
|
let bottomNode: Node
|
|
|
|
childrenNodes.forEach((n) => {
|
|
if (rightNode) {
|
|
if (n.position.x + n.width! > rightNode.position.x + rightNode.width!)
|
|
rightNode = n
|
|
}
|
|
else {
|
|
rightNode = n
|
|
}
|
|
if (bottomNode) {
|
|
if (n.position.y + n.height! > bottomNode.position.y + bottomNode.height!)
|
|
bottomNode = n
|
|
}
|
|
else {
|
|
bottomNode = n
|
|
}
|
|
})
|
|
|
|
const widthShouldExtend = rightNode! && currentNode.width! < rightNode.position.x + rightNode.width!
|
|
const heightShouldExtend = bottomNode! && currentNode.height! < bottomNode.position.y + bottomNode.height!
|
|
|
|
if (widthShouldExtend || heightShouldExtend) {
|
|
const newNodes = produce(nodes, (draft) => {
|
|
draft.forEach((n) => {
|
|
if (n.id === nodeId) {
|
|
if (widthShouldExtend) {
|
|
n.data.width = rightNode.position.x + rightNode.width! + LOOP_PADDING.right
|
|
n.width = rightNode.position.x + rightNode.width! + LOOP_PADDING.right
|
|
}
|
|
if (heightShouldExtend) {
|
|
n.data.height = bottomNode.position.y + bottomNode.height! + LOOP_PADDING.bottom
|
|
n.height = bottomNode.position.y + bottomNode.height! + LOOP_PADDING.bottom
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
setNodes(newNodes)
|
|
}
|
|
}, [collaborativeWorkflow])
|
|
|
|
const handleNodeLoopChildDrag = useCallback((node: Node) => {
|
|
const { nodes } = collaborativeWorkflow.getState()
|
|
|
|
const restrictPosition: { x?: number, y?: number } = { x: undefined, y: undefined }
|
|
|
|
if (node.data.isInLoop) {
|
|
const parentNode = nodes.find(n => n.id === node.parentId)
|
|
|
|
if (parentNode) {
|
|
if (node.position.y < LOOP_PADDING.top)
|
|
restrictPosition.y = LOOP_PADDING.top
|
|
if (node.position.x < LOOP_PADDING.left)
|
|
restrictPosition.x = LOOP_PADDING.left
|
|
if (node.position.x + node.width! > parentNode!.width! - LOOP_PADDING.right)
|
|
restrictPosition.x = parentNode!.width! - LOOP_PADDING.right - node.width!
|
|
if (node.position.y + node.height! > parentNode!.height! - LOOP_PADDING.bottom)
|
|
restrictPosition.y = parentNode!.height! - LOOP_PADDING.bottom - node.height!
|
|
}
|
|
}
|
|
|
|
return {
|
|
restrictPosition,
|
|
}
|
|
}, [collaborativeWorkflow])
|
|
|
|
const handleNodeLoopChildSizeChange = useCallback((nodeId: string) => {
|
|
const { nodes } = collaborativeWorkflow.getState()
|
|
const currentNode = nodes.find(n => n.id === nodeId)!
|
|
const parentId = currentNode.parentId
|
|
|
|
if (parentId)
|
|
handleNodeLoopRerender(parentId)
|
|
}, [collaborativeWorkflow, handleNodeLoopRerender])
|
|
|
|
const handleNodeLoopChildrenCopy = useCallback((nodeId: string, newNodeId: string, idMapping: Record<string, string>) => {
|
|
const { nodes } = collaborativeWorkflow.getState()
|
|
const childrenNodes = nodes.filter(n => n.parentId === nodeId && n.type !== CUSTOM_LOOP_START_NODE)
|
|
const newIdMapping = { ...idMapping }
|
|
|
|
const copyChildren = childrenNodes.map((child, index) => {
|
|
const childNodeType = child.data.type as BlockEnum
|
|
const defaultValue = nodesMetaDataMap?.[childNodeType]?.defaultValue ?? {}
|
|
const nodesWithSameType = nodes.filter(node => node.data.type === childNodeType)
|
|
const { newNode } = generateNewNode({
|
|
type: getNodeCustomTypeByNodeDataType(childNodeType),
|
|
data: {
|
|
...defaultValue,
|
|
...child.data,
|
|
selected: false,
|
|
_isBundled: false,
|
|
_connectedSourceHandleIds: [],
|
|
_connectedTargetHandleIds: [],
|
|
_dimmed: false,
|
|
title: nodesWithSameType.length > 0 ? `${defaultValue.title} ${nodesWithSameType.length + 1}` : defaultValue.title,
|
|
isInLoop: true,
|
|
loop_id: newNodeId,
|
|
type: childNodeType,
|
|
},
|
|
position: child.position,
|
|
positionAbsolute: child.positionAbsolute,
|
|
parentId: newNodeId,
|
|
extent: child.extent,
|
|
zIndex: LOOP_CHILDREN_Z_INDEX,
|
|
})
|
|
newNode.id = `${newNodeId}${newNode.id + index}`
|
|
newIdMapping[child.id] = newNode.id
|
|
return newNode
|
|
})
|
|
|
|
return {
|
|
copyChildren,
|
|
newIdMapping,
|
|
}
|
|
}, [collaborativeWorkflow, nodesMetaDataMap])
|
|
|
|
return {
|
|
handleNodeLoopRerender,
|
|
handleNodeLoopChildDrag,
|
|
handleNodeLoopChildSizeChange,
|
|
handleNodeLoopChildrenCopy,
|
|
}
|
|
}
|