Commit e868e440 authored by StyleZhang's avatar StyleZhang

help line

parent 43768139
...@@ -8,7 +8,7 @@ const Page = () => { ...@@ -8,7 +8,7 @@ const Page = () => {
{ {
id: '1', id: '1',
type: 'custom', type: 'custom',
position: { x: 180, y: 180 }, position: { x: 0, y: 0 },
data: { type: 'start' }, data: { type: 'start' },
}, },
] ]
......
...@@ -6,10 +6,12 @@ import type { ...@@ -6,10 +6,12 @@ import type {
NodeMouseHandler, NodeMouseHandler,
OnConnect, OnConnect,
OnEdgesChange, OnEdgesChange,
Viewport,
} from 'reactflow' } from 'reactflow'
import { import {
getConnectedEdges, getConnectedEdges,
getIncomers, getIncomers,
useReactFlow,
useStoreApi, useStoreApi,
} from 'reactflow' } from 'reactflow'
import type { import type {
...@@ -23,6 +25,7 @@ import { useStore } from './store' ...@@ -23,6 +25,7 @@ import { useStore } from './store'
export const useWorkflow = () => { export const useWorkflow = () => {
const store = useStoreApi() const store = useStoreApi()
const reactFlow = useReactFlow()
const handleLayout = useCallback(async () => { const handleLayout = useCallback(async () => {
const { const {
...@@ -45,6 +48,10 @@ export const useWorkflow = () => { ...@@ -45,6 +48,10 @@ export const useWorkflow = () => {
setNodes(newNodes) setNodes(newNodes)
}, [store]) }, [store])
const handleSetViewport = useCallback((viewPort: Viewport) => {
reactFlow.setViewport(viewPort)
}, [reactFlow])
const handleNodeDragStart = useCallback<NodeDragHandler>(() => { const handleNodeDragStart = useCallback<NodeDragHandler>(() => {
useStore.getState().setIsDragging(true) useStore.getState().setIsDragging(true)
}, []) }, [])
...@@ -54,6 +61,7 @@ export const useWorkflow = () => { ...@@ -54,6 +61,7 @@ export const useWorkflow = () => {
getNodes, getNodes,
setNodes, setNodes,
} = store.getState() } = store.getState()
const { setHelpLine } = useStore.getState()
e.stopPropagation() e.stopPropagation()
const nodes = getNodes() const nodes = getNodes()
...@@ -65,10 +73,53 @@ export const useWorkflow = () => { ...@@ -65,10 +73,53 @@ export const useWorkflow = () => {
}) })
setNodes(newNodes) setNodes(newNodes)
const showVerticalHelpLine = nodes.find((n) => {
if (n.id === node.id)
return false
if (
n.position.x === node.position.x
|| n.position.x + n.width! === node.position.x
|| n.position.x === node.position.x + node.width!
)
return true
return false
})
const showHorizontalHelpLine = nodes.find((n) => {
if (n.id === node.id)
return false
if (
n.position.y === node.position.y
|| n.position.y === node.position.y + node.height!
|| n.position.y + n.height! === node.position.y
|| n.position.y + n.height! === node.position.y + node.height!
)
return true
return false
})
if (showVerticalHelpLine || showHorizontalHelpLine) {
setHelpLine({
x: showVerticalHelpLine ? node.position.x : undefined,
y: showHorizontalHelpLine ? node.position.y : undefined,
})
}
else {
setHelpLine()
}
}, [store]) }, [store])
const handleNodeDragStop = useCallback<NodeDragHandler>(() => { const handleNodeDragStop = useCallback<NodeDragHandler>(() => {
useStore.getState().setIsDragging(false) const {
setIsDragging,
setHelpLine,
} = useStore.getState()
setIsDragging(false)
setHelpLine()
}, []) }, [])
const handleNodeEnter = useCallback<NodeMouseHandler>((_, node) => { const handleNodeEnter = useCallback<NodeMouseHandler>((_, node) => {
...@@ -353,6 +404,7 @@ export const useWorkflow = () => { ...@@ -353,6 +404,7 @@ export const useWorkflow = () => {
return { return {
handleLayout, handleLayout,
handleSetViewport,
handleNodeDragStart, handleNodeDragStart,
handleNodeDrag, handleNodeDrag,
......
...@@ -6,6 +6,7 @@ type State = { ...@@ -6,6 +6,7 @@ type State = {
showFeaturesPanel: boolean showFeaturesPanel: boolean
runStaus: string runStaus: string
isDragging: boolean isDragging: boolean
helpLine?: { x?: number; y?: number }
} }
type Action = { type Action = {
...@@ -13,6 +14,7 @@ type Action = { ...@@ -13,6 +14,7 @@ type Action = {
setShowFeaturesPanel: (showFeaturesPanel: boolean) => void setShowFeaturesPanel: (showFeaturesPanel: boolean) => void
setRunStaus: (runStaus: string) => void setRunStaus: (runStaus: string) => void
setIsDragging: (isDragging: boolean) => void setIsDragging: (isDragging: boolean) => void
setHelpLine: (helpLine?: { x?: number; y?: number }) => void
} }
export const useStore = create<State & Action>(set => ({ export const useStore = create<State & Action>(set => ({
...@@ -25,4 +27,6 @@ export const useStore = create<State & Action>(set => ({ ...@@ -25,4 +27,6 @@ export const useStore = create<State & Action>(set => ({
setRunStaus: runStaus => set(() => ({ runStaus })), setRunStaus: runStaus => set(() => ({ runStaus })),
isDragging: false, isDragging: false,
setIsDragging: isDragging => set(() => ({ isDragging })), setIsDragging: isDragging => set(() => ({ isDragging })),
helpLine: undefined,
setHelpLine: helpLine => set(() => ({ helpLine })),
})) }))
...@@ -34,7 +34,7 @@ export const nodesLevelOrderTraverse = ( ...@@ -34,7 +34,7 @@ export const nodesLevelOrderTraverse = (
callback({ node, depth, breath }) callback({ node, depth, breath })
const targetBranches = node.data.targetBranches const targetBranches = node.data._targetBranches
if (targetBranches?.length) { if (targetBranches?.length) {
const targetEdges = getConnectedEdges([node], edges) const targetEdges = getConnectedEdges([node], edges)
...@@ -133,9 +133,7 @@ export const getLayoutByDagre = (nodes: Node[], edges: Edge[]) => { ...@@ -133,9 +133,7 @@ export const getLayoutByDagre = (nodes: Node[], edges: Edge[]) => {
}) })
edges.forEach((edge) => { edges.forEach((edge) => {
dagreGraph.setEdge(edge.source, edge.target, { dagreGraph.setEdge(edge.source, edge.target)
weight: edge?.data?.weight || 1,
})
}) })
dagre.layout(dagreGraph) dagre.layout(dagreGraph)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment