feat(web): 完成流程图外部加载

This commit is contained in:
2025-06-24 10:44:18 +08:00
parent ab56385c8a
commit 1cba0f4422
2 changed files with 99 additions and 30 deletions

View File

@@ -1,14 +1,5 @@
import {PlusCircleFilled, SaveFilled} from '@ant-design/icons'
import {
Background,
BackgroundVariant,
Controls,
type Edge,
MiniMap,
type Node,
type NodeProps,
ReactFlow,
} from '@xyflow/react'
import {Background, BackgroundVariant, Controls, MiniMap, type Node, type NodeProps, ReactFlow} from '@xyflow/react'
import {useMount} from 'ahooks'
import type {Schema} from 'amis'
import {Button, Drawer, Dropdown, message, Space} from 'antd'
@@ -38,22 +29,6 @@ const FlowableDiv = styled.div`
}
`
const initialNodes: Node[] = [
{
id: 'BMFP3Eov94',
type: 'start-amis-node',
position: {x: 10, y: 100},
data: {},
},
{
id: 'PYK8LjduQ1',
type: 'end-amis-node',
position: {x: 500, y: 100},
data: {},
},
]
const initialEdges: Edge[] = []
function FlowEditor() {
const [messageApi, contextHolder] = message.useMessage()
const [nodeDef] = useState<{
@@ -79,7 +54,7 @@ function FlowEditor() {
])
const [open, setOpen] = useState(false)
const {getData, getDataById, setDataById} = useDataStore()
const {data, setData, getDataById, setDataById} = useDataStore()
const {
nodes,
addNode,
@@ -153,7 +128,7 @@ function FlowEditor() {
},
],
},
]
],
},
getDataById(id),
),
@@ -163,6 +138,95 @@ function FlowEditor() {
}
useMount(() => {
// language=JSON
let initialData = JSON.parse(`{
"nodes": [
{
"id": "BMFP3Eov94",
"type": "start-amis-node",
"position": {
"x": 10,
"y": 100
},
"data": {},
"measured": {
"width": 256,
"height": 83
},
"selected": false
},
{
"id": "PYK8LjduQ1",
"type": "end-amis-node",
"position": {
"x": 654,
"y": 332
},
"data": {},
"measured": {
"width": 256,
"height": 83
},
"selected": false,
"dragging": false
},
{
"id": "nCm-ij5I6o",
"type": "llm-amis-node",
"position": {
"x": 318,
"y": 208
},
"data": {},
"measured": {
"width": 256,
"height": 83
},
"selected": true,
"dragging": false
}
],
"edges": [
{
"source": "BMFP3Eov94",
"target": "nCm-ij5I6o",
"id": "xy-edge__BMFP3Eov94-nCm-ij5I6o"
},
{
"source": "nCm-ij5I6o",
"target": "PYK8LjduQ1",
"id": "xy-edge__nCm-ij5I6o-PYK8LjduQ1"
}
],
"data": {
"BMFP3Eov94": {
"inputs": {
"文件名": {
"type": "text"
},
"文件描述": {
"type": "text",
"description": "文件描述"
}
}
},
"nCm-ij5I6o": {
"model": "qwen3",
"outputs": {
"text": {
"type": "string"
}
},
"systemPrompt": "你是个沙雕"
}
}
}`)
let initialNodes = initialData['nodes'] ?? []
let initialEdges = initialData['edges'] ?? []
let initialNodeData = initialData['data'] ?? {}
setData(initialNodeData)
for (let node of initialNodes) {
node.data = {
getDataById,
@@ -179,7 +243,10 @@ function FlowEditor() {
<FlowableDiv>
{contextHolder}
<Space className="toolbar">
<Button type="primary" onClick={() => console.log(JSON.stringify(getData()))}>
<Button type="primary" onClick={() => {
let saveData = {nodes, edges, data}
console.log(JSON.stringify(saveData, null, 2))
}}>
<SaveFilled/>
</Button>

View File

@@ -9,7 +9,9 @@ export const useDataStore = create<{
}>((set, get) => ({
data: {},
getData: () => get().data,
setData: (data) => set(data),
setData: (data) => set({
data: data
}),
getDataById: id => get().data[id],
setDataById: (id, data) => {
let updateData = get().data