mirror of
https://github.com/langgenius/dify.git
synced 2026-05-11 23:18:39 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: jimcody1995 <jjimcody@gmail.com> Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: jerryzai <jerryzh8710@protonmail.com> Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com> Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com> Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
229 lines
6.1 KiB
TypeScript
229 lines
6.1 KiB
TypeScript
import type { Node } from '../types'
|
|
import { useCallback } from 'react'
|
|
import { useStoreApi } from 'reactflow'
|
|
import { useWorkflowStore } from '../store'
|
|
import { BlockEnum, isTriggerNode } from '../types'
|
|
|
|
// Entry node (Start/Trigger) wrapper offsets
|
|
// The EntryNodeContainer adds a wrapper with status indicator above the actual node
|
|
// These offsets ensure alignment happens on the inner node, not the wrapper
|
|
const ENTRY_NODE_WRAPPER_OFFSET = {
|
|
x: 0, // No horizontal padding on wrapper (px-0)
|
|
y: 21, // Actual measured: pt-0.5 (2px) + status bar height (~19px)
|
|
} as const
|
|
|
|
type HelpLineNodeCollections = {
|
|
showHorizontalHelpLineNodes: Node[]
|
|
showVerticalHelpLineNodes: Node[]
|
|
}
|
|
|
|
type NodeAlignPosition = {
|
|
x: number
|
|
y: number
|
|
}
|
|
|
|
const ALIGN_THRESHOLD = 5
|
|
|
|
const getEntryNodeDimension = (
|
|
node: Node,
|
|
dimension: 'width' | 'height',
|
|
) => {
|
|
const offset = dimension === 'width'
|
|
? ENTRY_NODE_WRAPPER_OFFSET.x
|
|
: ENTRY_NODE_WRAPPER_OFFSET.y
|
|
|
|
return (node[dimension] ?? 0) - offset
|
|
}
|
|
|
|
const getAlignedNodes = ({
|
|
nodes,
|
|
node,
|
|
nodeAlignPos,
|
|
axis,
|
|
getNodeAlignPosition,
|
|
}: {
|
|
nodes: Node[]
|
|
node: Node
|
|
nodeAlignPos: NodeAlignPosition
|
|
axis: 'x' | 'y'
|
|
getNodeAlignPosition: (node: Node) => NodeAlignPosition
|
|
}) => {
|
|
return nodes.filter((candidate) => {
|
|
if (candidate.id === node.id)
|
|
return false
|
|
if (candidate.data.isInIteration || candidate.data.isInLoop)
|
|
return false
|
|
|
|
const candidateAlignPos = getNodeAlignPosition(candidate)
|
|
const diff = Math.ceil(candidateAlignPos[axis]) - Math.ceil(nodeAlignPos[axis])
|
|
return diff < ALIGN_THRESHOLD && diff > -ALIGN_THRESHOLD
|
|
}).sort((a, b) => {
|
|
const aPos = getNodeAlignPosition(a)
|
|
const bPos = getNodeAlignPosition(b)
|
|
return aPos.x - bPos.x
|
|
})
|
|
}
|
|
|
|
const buildHorizontalHelpLine = ({
|
|
alignedNodes,
|
|
node,
|
|
nodeAlignPos,
|
|
getNodeAlignPosition,
|
|
isEntryNode,
|
|
}: {
|
|
alignedNodes: Node[]
|
|
node: Node
|
|
nodeAlignPos: NodeAlignPosition
|
|
getNodeAlignPosition: (node: Node) => NodeAlignPosition
|
|
isEntryNode: (node: Node) => boolean
|
|
}) => {
|
|
if (!alignedNodes.length)
|
|
return undefined
|
|
|
|
const first = alignedNodes[0]
|
|
const last = alignedNodes[alignedNodes.length - 1]
|
|
const firstPos = getNodeAlignPosition(first!)
|
|
const lastPos = getNodeAlignPosition(last!)
|
|
const helpLine = {
|
|
top: firstPos.y,
|
|
left: firstPos.x,
|
|
width: lastPos.x + (isEntryNode(last!) ? getEntryNodeDimension(last!, 'width') : last!.width ?? 0) - firstPos.x,
|
|
}
|
|
|
|
if (nodeAlignPos.x < firstPos.x) {
|
|
helpLine.left = nodeAlignPos.x
|
|
helpLine.width = firstPos.x + (isEntryNode(first!) ? getEntryNodeDimension(first!, 'width') : first!.width ?? 0) - nodeAlignPos.x
|
|
}
|
|
|
|
if (nodeAlignPos.x > lastPos.x)
|
|
helpLine.width = nodeAlignPos.x + (isEntryNode(node) ? getEntryNodeDimension(node, 'width') : node.width ?? 0) - firstPos.x
|
|
|
|
return helpLine
|
|
}
|
|
|
|
const buildVerticalHelpLine = ({
|
|
alignedNodes,
|
|
node,
|
|
nodeAlignPos,
|
|
getNodeAlignPosition,
|
|
isEntryNode,
|
|
}: {
|
|
alignedNodes: Node[]
|
|
node: Node
|
|
nodeAlignPos: NodeAlignPosition
|
|
getNodeAlignPosition: (node: Node) => NodeAlignPosition
|
|
isEntryNode: (node: Node) => boolean
|
|
}) => {
|
|
if (!alignedNodes.length)
|
|
return undefined
|
|
|
|
const first = alignedNodes[0]
|
|
const last = alignedNodes[alignedNodes.length - 1]
|
|
const firstPos = getNodeAlignPosition(first!)
|
|
const lastPos = getNodeAlignPosition(last!)
|
|
const helpLine = {
|
|
top: firstPos.y,
|
|
left: firstPos.x,
|
|
height: lastPos.y + (isEntryNode(last!) ? getEntryNodeDimension(last!, 'height') : last!.height ?? 0) - firstPos.y,
|
|
}
|
|
|
|
if (nodeAlignPos.y < firstPos.y) {
|
|
helpLine.top = nodeAlignPos.y
|
|
helpLine.height = firstPos.y + (isEntryNode(first!) ? getEntryNodeDimension(first!, 'height') : first!.height ?? 0) - nodeAlignPos.y
|
|
}
|
|
|
|
if (nodeAlignPos.y > lastPos.y)
|
|
helpLine.height = nodeAlignPos.y + (isEntryNode(node) ? getEntryNodeDimension(node, 'height') : node.height ?? 0) - firstPos.y
|
|
|
|
return helpLine
|
|
}
|
|
|
|
export const useHelpline = () => {
|
|
const store = useStoreApi()
|
|
const workflowStore = useWorkflowStore()
|
|
|
|
// Check if a node is an entry node (Start or Trigger)
|
|
const isEntryNode = useCallback((node: Node): boolean => {
|
|
return isTriggerNode(node.data.type as any) || node.data.type === BlockEnum.Start
|
|
}, [])
|
|
|
|
// Get the actual alignment position of a node (accounting for wrapper offset)
|
|
const getNodeAlignPosition = useCallback((node: Node) => {
|
|
if (isEntryNode(node)) {
|
|
return {
|
|
x: node.position.x + ENTRY_NODE_WRAPPER_OFFSET.x,
|
|
y: node.position.y + ENTRY_NODE_WRAPPER_OFFSET.y,
|
|
}
|
|
}
|
|
return {
|
|
x: node.position.x,
|
|
y: node.position.y,
|
|
}
|
|
}, [isEntryNode])
|
|
|
|
const handleSetHelpline = useCallback((node: Node) => {
|
|
const { getNodes } = store.getState()
|
|
const nodes = getNodes()
|
|
const {
|
|
setHelpLineHorizontal,
|
|
setHelpLineVertical,
|
|
} = workflowStore.getState()
|
|
|
|
if (node.data.isInIteration) {
|
|
return {
|
|
showHorizontalHelpLineNodes: [],
|
|
showVerticalHelpLineNodes: [],
|
|
}
|
|
}
|
|
|
|
if (node.data.isInLoop) {
|
|
return {
|
|
showHorizontalHelpLineNodes: [],
|
|
showVerticalHelpLineNodes: [],
|
|
}
|
|
}
|
|
|
|
// Get the actual alignment position for the dragging node
|
|
const nodeAlignPos = getNodeAlignPosition(node)
|
|
|
|
const showHorizontalHelpLineNodes = getAlignedNodes({
|
|
nodes,
|
|
node,
|
|
nodeAlignPos,
|
|
axis: 'y',
|
|
getNodeAlignPosition,
|
|
})
|
|
const showVerticalHelpLineNodes = getAlignedNodes({
|
|
nodes,
|
|
node,
|
|
nodeAlignPos,
|
|
axis: 'x',
|
|
getNodeAlignPosition,
|
|
})
|
|
|
|
setHelpLineHorizontal(buildHorizontalHelpLine({
|
|
alignedNodes: showHorizontalHelpLineNodes,
|
|
node,
|
|
nodeAlignPos,
|
|
getNodeAlignPosition,
|
|
isEntryNode,
|
|
}))
|
|
setHelpLineVertical(buildVerticalHelpLine({
|
|
alignedNodes: showVerticalHelpLineNodes,
|
|
node,
|
|
nodeAlignPos,
|
|
getNodeAlignPosition,
|
|
isEntryNode,
|
|
}))
|
|
|
|
return {
|
|
showHorizontalHelpLineNodes,
|
|
showVerticalHelpLineNodes,
|
|
} satisfies HelpLineNodeCollections
|
|
}, [store, workflowStore, getNodeAlignPosition, isEntryNode])
|
|
|
|
return {
|
|
handleSetHelpline,
|
|
}
|
|
}
|