feat(web): 优化编辑界面

This commit is contained in:
2025-06-21 10:31:11 +08:00
parent b53ee57dc3
commit fcf5f8ad18
2 changed files with 122 additions and 68 deletions

View File

@@ -2,6 +2,7 @@ import {ProLayout} from '@ant-design/pro-components'
import React from 'react' import React from 'react'
import {Outlet, useLocation, useNavigate} from 'react-router' import {Outlet, useLocation, useNavigate} from 'react-router'
import {menus} from '../route.tsx' import {menus} from '../route.tsx'
import {ConfigProvider} from 'antd'
const App: React.FC = () => { const App: React.FC = () => {
const navigate = useNavigate() const navigate = useNavigate()
@@ -34,7 +35,18 @@ const App: React.FC = () => {
style={{minHeight: '100vh'}} style={{minHeight: '100vh'}}
contentStyle={{backgroundColor: 'white', padding: '10px 10px 10px 20px'}} contentStyle={{backgroundColor: 'white', padding: '10px 10px 10px 20px'}}
> >
<Outlet/> <ConfigProvider
theme={{
components: {
Card: {
bodyPadding: 0,
bodyPaddingSM: 0,
}
}
}}
>
<Outlet/>
</ConfigProvider>
</ProLayout> </ProLayout>
) )
} }

View File

@@ -1,4 +1,4 @@
import {DeleteFilled, EditFilled, PlusCircleFilled, SaveFilled} from '@ant-design/icons' import {DeleteFilled, EditFilled, FolderFilled, PlusCircleFilled, SaveFilled} from '@ant-design/icons'
import { import {
addEdge, addEdge,
applyEdgeChanges, applyEdgeChanges,
@@ -19,12 +19,13 @@ import {
} from '@xyflow/react' } from '@xyflow/react'
import {useMount} from 'ahooks' import {useMount} from 'ahooks'
import type {Schema} from 'amis' import type {Schema} from 'amis'
import {Button, Card, Dropdown, message, Popover} from 'antd' import {Button, Card, Drawer, Dropdown, message, Space} from 'antd'
import {arrToMap, contain, find, isEqual, randomId} from 'licia' import {arrToMap, filter, find, findIdx, isEqual, isNil, randomId} from 'licia'
import {type JSX, useState} from 'react' import {type JSX, useState} from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import '@xyflow/react/dist/style.css' import '@xyflow/react/dist/style.css'
import {create} from 'zustand/react' import {create} from 'zustand/react'
import {amisRender, commonInfo} from '../util/amis.tsx'
const FlowableDiv = styled.div` const FlowableDiv = styled.div`
height: 93vh; height: 93vh;
@@ -45,64 +46,48 @@ const AmisNode = (
columnSchema?: Schema[], columnSchema?: Schema[],
) => { ) => {
const {id, data} = props const {id, data} = props
const {setDataById} = data const {removeNode, editNode} = data
return ( return (
<div className="p-1 w-72"> <div className="p-1 w-64">
{/*{amisRender( <Card
{ title={name}
type: 'card', size="small"
header: { hoverable
title: name,
subTitle: description,
},
body: isNil(columnSchema)
? undefined
: {
debug: commonInfo.debug,
type: 'form',
className: 'nodrag nopan',
wrapWithPanel: false,
onEvent: {
change: {
actions: [
{
actionType: 'custom',
// @ts-ignore
script: (context, action, event) => {
// @ts-ignore
setDataById(id, context.props.data)
},
},
],
},
},
body: [
...(columnSchema ?? []),
{
type: 'hidden',
name: 'nodeId',
value: props.id,
},
],
},
},
)}*/}
<Popover
content={
<EditFilled/>
}
> >
<Card <Dropdown
size="small" trigger={['contextMenu']}
actions={[ menu={{
<EditFilled key="edit"/>, items: [
<DeleteFilled key="remove"/>, {
]} key: 'edit',
hoverable label: '编辑',
icon: <EditFilled className="text-gray-600 hover:text-blue-500"/>,
},
{
key: 'remove',
label: '删除',
icon: <DeleteFilled className="text-red-500 hover:text-red-500"/>,
},
],
onClick: menu => {
switch (menu.key) {
case 'edit':
// @ts-ignore
editNode(id, name, description, columnSchema)
break
case 'remove':
// @ts-ignore
removeNode(id)
break
}
}
}}
> >
<Card.Meta title={name} description={description}/> <div className="card-description p-2 text-secondary text-sm">
</Card> {description}
</Popover> </div>
</Dropdown>
</Card>
{isEqual(type, 'start') || isEqual(type, 'normal') {isEqual(type, 'start') || isEqual(type, 'normal')
? <Handle type="source" position={Position.Right}/> : undefined} ? <Handle type="source" position={Position.Right}/> : undefined}
{isEqual(type, 'end') || isEqual(type, 'normal') {isEqual(type, 'end') || isEqual(type, 'normal')
@@ -219,13 +204,13 @@ const initialNodes: Node[] = [
{ {
id: 'BMFP3Eov94', id: 'BMFP3Eov94',
type: 'start-amis-node', type: 'start-amis-node',
position: {x: 10, y: 50}, position: {x: 10, y: 100},
data: {}, data: {},
}, },
{ {
id: 'PYK8LjduQ1', id: 'PYK8LjduQ1',
type: 'end-amis-node', type: 'end-amis-node',
position: {x: 500, y: 50}, position: {x: 500, y: 100},
data: {}, data: {},
}, },
] ]
@@ -255,6 +240,7 @@ const useFlowStore = create<{
nodes: Node[], nodes: Node[],
onNodesChange: OnNodesChange, onNodesChange: OnNodesChange,
addNode: (node: Node) => void, addNode: (node: Node) => void,
removeNode: (id: string) => void,
setNodes: (nodes: Node[]) => void, setNodes: (nodes: Node[]) => void,
edges: Edge[], edges: Edge[],
@@ -270,6 +256,11 @@ const useFlowStore = create<{
}) })
}, },
addNode: node => set({nodes: get().nodes.concat(node)}), addNode: node => set({nodes: get().nodes.concat(node)}),
removeNode: id => {
set({
nodes: filter(get().nodes, node => !isEqual(node.id, id))
})
},
setNodes: nodes => set({nodes}), setNodes: nodes => set({nodes}),
edges: [], edges: [],
@@ -310,11 +301,14 @@ function Test() {
component: LlmAmisNode, component: LlmAmisNode,
}, },
]) ])
const [open, setOpen] = useState(false)
const onClose = () => setOpen(false)
const {getData, getDataById, setDataById} = useStore() const {getData, getDataById, setDataById} = useStore()
const { const {
nodes, nodes,
addNode, addNode,
removeNode,
setNodes, setNodes,
onNodesChange, onNodesChange,
edges, edges,
@@ -323,11 +317,46 @@ function Test() {
onConnect, onConnect,
} = useFlowStore() } = useFlowStore()
const [currentNodeForm, setCurrentNodeForm] = useState<JSX.Element>()
const editNode = (id: string, name: string, description: string, columnSchema?: Schema[]) => {
if (!isNil(columnSchema)) {
setCurrentNodeForm(
amisRender(
{
debug: commonInfo.debug,
title: name,
type: 'form',
wrapWithPanel: false,
onEvent: {
change: {
actions: [
{
actionType: 'custom',
// @ts-ignore
script: (context, action, event) => {
console.log(id, context.props.data)
setDataById(id, context.props.data)
},
}
]
}
},
body: columnSchema,
},
getDataById(id),
)
)
setOpen(true)
}
}
useMount(() => { useMount(() => {
for (let node of initialNodes) { for (let node of initialNodes) {
node.data = { node.data = {
getDataById, getDataById,
setDataById, setDataById,
removeNode,
editNode,
} }
} }
setNodes(initialNodes) setNodes(initialNodes)
@@ -337,8 +366,8 @@ function Test() {
return ( return (
<FlowableDiv> <FlowableDiv>
{contextHolder} {contextHolder}
<div className="toolbar space-x-2"> <Space className="toolbar">
<Button type="primary" onClick={() => console.log(getData())}> <Button type="primary" onClick={() => console.log(JSON.stringify(getData()))}>
<SaveFilled/> <SaveFilled/>
</Button> </Button>
@@ -346,9 +375,12 @@ function Test() {
menu={{ menu={{
items: nodeDef.map(def => ({key: def.key, label: def.name})), items: nodeDef.map(def => ({key: def.key, label: def.name})),
onClick: ({key}) => { onClick: ({key}) => {
if (isEqual(key, 'start-amis-node') || isEqual(key, 'end-amis-node')) { if (isEqual(key, 'start-amis-node') && findIdx(nodes, (node: Node) => isEqual(key, node.type)) > -1) {
contain(nodes, (node: Node) => isEqual(key, node.type)) messageApi.error('只能存在1个开始节点')
messageApi.error('只能存在1个开始/结束节点') return
}
if (isEqual(key, 'end-amis-node') && findIdx(nodes, (node: Node) => isEqual(key, node.type)) > -1) {
messageApi.error('只能存在1个结束节点')
return return
} }
@@ -359,6 +391,8 @@ function Test() {
data: { data: {
getDataById, getDataById,
setDataById, setDataById,
removeNode,
editNode,
}, },
}) })
}, },
@@ -369,7 +403,15 @@ function Test() {
</Button> </Button>
</Dropdown> </Dropdown>
</div> </Space>
<Drawer
title="节点编辑"
open={open}
onClose={onClose}
destroyOnHidden
>
{currentNodeForm}
</Drawer>
<ReactFlow <ReactFlow
nodes={nodes} nodes={nodes}
edges={edges} edges={edges}
@@ -384,7 +426,7 @@ function Test() {
> >
<Controls/> <Controls/>
<MiniMap/> <MiniMap/>
<Background variant={BackgroundVariant.Dots} gap={12} size={1}/> <Background variant={BackgroundVariant.Cross} gap={20} size={3}/>
</ReactFlow> </ReactFlow>
</FlowableDiv> </FlowableDiv>
) )