Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
1b73632f
Commit
1b73632f
authored
Mar 01, 2024
by
StyleZhang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
utils
parent
0a7cbf6f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
9 deletions
+53
-9
index.tsx
web/app/components/workflow/index.tsx
+8
-1
utils.ts
web/app/components/workflow/utils.ts
+45
-8
No files found.
web/app/components/workflow/index.tsx
View file @
1b73632f
import
type
{
FC
}
from
'react'
import
type
{
FC
}
from
'react'
import
{
memo
}
from
'react'
import
{
memo
,
useEffect
}
from
'react'
import
{
useKeyPress
}
from
'ahooks'
import
{
useKeyPress
}
from
'ahooks'
import
ReactFlow
,
{
import
ReactFlow
,
{
Background
,
Background
,
ReactFlowProvider
,
ReactFlowProvider
,
useEdgesState
,
useEdgesState
,
useNodesInitialized
,
useNodesState
,
useNodesState
,
}
from
'reactflow'
}
from
'reactflow'
import
'reactflow/dist/style.css'
import
'reactflow/dist/style.css'
...
@@ -38,6 +39,12 @@ const Workflow: FC<WorkflowProps> = memo(({
...
@@ -38,6 +39,12 @@ const Workflow: FC<WorkflowProps> = memo(({
})
=>
{
})
=>
{
const
[
nodes
]
=
useNodesState
(
initialNodes
)
const
[
nodes
]
=
useNodesState
(
initialNodes
)
const
[
edges
,
_
,
onEdgesChange
]
=
useEdgesState
(
initialEdges
)
const
[
edges
,
_
,
onEdgesChange
]
=
useEdgesState
(
initialEdges
)
const
nodesInitialized
=
useNodesInitialized
()
useEffect
(()
=>
{
if
(
nodesInitialized
)
console
.
log
(
'initialed'
)
},
[
nodesInitialized
])
const
{
const
{
handleEnterNode
,
handleEnterNode
,
...
...
web/app/components/workflow/utils.ts
View file @
1b73632f
...
@@ -7,20 +7,31 @@ import type {
...
@@ -7,20 +7,31 @@ import type {
Edge
,
Edge
,
Node
,
Node
,
}
from
'./types'
}
from
'./types'
import
{
BlockEnum
}
from
'./types'
export
const
nodesLevelOrderTraverse
=
(
export
const
nodesLevelOrderTraverse
=
(
firstNode
:
Node
,
firstNode
:
Node
,
nodes
:
Node
[],
nodes
:
Node
[],
edges
:
Edge
[],
edges
:
Edge
[],
callback
:
(
n
:
Node
)
=>
void
,
callback
:
(
n
:
any
)
=>
void
,
)
=>
{
)
=>
{
const
queue
=
[{
const
queue
=
[{
node
:
firstNode
,
node
:
firstNode
,
depth
:
0
,
breath
:
0
,
}]
}]
let
currenDepth
=
0
let
currentBreath
=
0
while
(
queue
.
length
)
{
while
(
queue
.
length
)
{
const
{
node
}
=
queue
.
shift
()
!
const
{
node
,
depth
,
breath
}
=
queue
.
shift
()
!
callback
(
node
)
if
(
currenDepth
!==
depth
)
{
currenDepth
=
depth
currentBreath
=
0
}
callback
({
node
,
depth
,
breath
})
const
targetBranches
=
node
.
data
.
targetBranches
const
targetBranches
=
node
.
data
.
targetBranches
if
(
targetBranches
?.
length
)
{
if
(
targetBranches
?.
length
)
{
...
@@ -37,11 +48,19 @@ export const nodesLevelOrderTraverse = (
...
@@ -37,11 +48,19 @@ export const nodesLevelOrderTraverse = (
})
})
const
outgoers
=
getOutgoers
(
node
,
nodes
,
sortedTargetEdges
)
const
outgoers
=
getOutgoers
(
node
,
nodes
,
sortedTargetEdges
)
queue
.
push
(...
outgoers
.
map
((
outgoer
)
=>
{
queue
.
push
(...
outgoers
.
map
((
outgoer
,
index
)
=>
{
return
{
return
{
node
:
outgoer
,
node
:
outgoer
,
depth
:
depth
+
1
,
breath
:
currentBreath
+
index
,
}
}
}))
}))
currentBreath
+=
outgoers
.
length
}
else
{
currentBreath
+=
1
}
}
}
}
else
{
else
{
...
@@ -50,8 +69,12 @@ export const nodesLevelOrderTraverse = (
...
@@ -50,8 +69,12 @@ export const nodesLevelOrderTraverse = (
if
(
outgoers
.
length
===
1
)
{
if
(
outgoers
.
length
===
1
)
{
queue
.
push
({
queue
.
push
({
node
:
outgoers
[
0
],
node
:
outgoers
[
0
],
depth
:
depth
+
1
,
breath
:
0
,
})
})
}
}
currentBreath
+=
1
}
}
}
}
}
}
...
@@ -72,12 +95,26 @@ export const initialNodesAndEdges = (nodes: Node[], edges: Edge[]) => {
...
@@ -72,12 +95,26 @@ export const initialNodesAndEdges = (nodes: Node[], edges: Edge[]) => {
}
}
export
type
PositionMap
=
{
export
type
PositionMap
=
{
position
:
{
x
:
number
y
:
number
}
index
:
{
index
:
{
x
:
number
x
:
number
y
:
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment