Files
FiDA_Front/src/views/home/agent/components/versionTree/tools/tools.js

145 lines
4.1 KiB
JavaScript
Raw Normal View History

2026-02-04 10:09:24 +08:00
// import dagre from '@dagrejs/dagre'
import dagre from 'dagre'
import { Position, useVueFlow } from '@vue-flow/core'
import { ref } from 'vue'
/**
* Composable to run the layout algorithm on the graph.
* It uses the `dagre` library to calculate the layout of the nodes and edges.
*/
export function useLayout() {
2026-02-05 10:43:18 +08:00
const { findNode } = useVueFlow()
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
const graph = ref(new dagre.graphlib.Graph())
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
const previousDirection = ref('LR')
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
function layout(nodes, edges, direction = 'LR') {
// 验证和规范化方向参数
const validDirections = ['TB', 'BT', 'LR', 'RL']
const layoutDirection = validDirections.includes(direction) ? direction : 'LR'
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
// we create a new graph instance, in case some nodes/edges were removed, otherwise dagre would act as if they were still there
const dagreGraph = new dagre.graphlib.Graph()
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
graph.value = dagreGraph
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
dagreGraph.setDefaultEdgeLabel(() => ({}))
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
// 根据方向判断是否为水平布局
const isHorizontal = layoutDirection === 'LR' || layoutDirection === 'RL'
dagreGraph.setGraph({ rankdir: layoutDirection })
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
previousDirection.value = layoutDirection
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
for (const node of nodes) {
// if you need width+height of nodes for your layout, you can use the dimensions property of the internal node (`GraphNode` type)
const graphNode = findNode(node.id)
2026-02-04 10:09:24 +08:00
2026-02-09 14:47:58 +08:00
dagreGraph.setNode(node.id, {
width: graphNode.dimensions.width || 150,
height: graphNode.dimensions.height || 50
})
2026-02-05 10:43:18 +08:00
}
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
for (const edge of edges) {
dagreGraph.setEdge(edge.source, edge.target)
}
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
dagre.layout(dagreGraph)
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
// set nodes with updated positions
return nodes.map((node) => {
const nodeWithPosition = dagreGraph.node(node.id)
2026-02-04 10:09:24 +08:00
2026-02-05 10:43:18 +08:00
// 根据方向动态计算连接点位置
let targetPosition, sourcePosition
switch (layoutDirection) {
case 'BT': // 从上到下 (Top to Bottom)
2026-02-09 14:47:58 +08:00
targetPosition = Position.Bottom // 目标节点连接点在下方
2026-02-05 10:43:18 +08:00
sourcePosition = Position.Top // 源节点连接点在上方
break
case 'TB': // 从下到上 (Bottom to Top)
2026-02-09 14:47:58 +08:00
targetPosition = Position.Top // 目标节点连接点在上方
2026-02-05 10:43:18 +08:00
sourcePosition = Position.Bottom // 源节点连接点在下方
break
case 'LR': // 从左到右 (Left to Right)
targetPosition = Position.Left
sourcePosition = Position.Right
break
case 'RL': // 从右到左 (Right to Left)
targetPosition = Position.Right
sourcePosition = Position.Left
break
default:
targetPosition = Position.Top
sourcePosition = Position.Bottom
}
return {
...node,
targetPosition,
sourcePosition,
2026-02-09 14:47:58 +08:00
position: { x: nodeWithPosition.x, y: nodeWithPosition.y }
2026-02-05 10:43:18 +08:00
}
})
}
return { graph, layout, previousDirection }
2026-02-09 14:47:58 +08:00
}
/**
* 递归查找指定ID的节点并添加子节点
* @param {Array} items - 要搜索的数组
* @param {string} targetId - 要查找的节点ID
* @param {Object} newChild - 要添加的新子节点
* @returns {boolean} 是否成功添加
*/
export function findAndAddChild(items, targetId, newChild) {
for (let i = 0; i < items.length; i++) {
const item = items[i]
// 如果找到目标节点
if (item.id === targetId) {
// 初始化child数组如果不存在
if (!item.child) {
item.child = []
}
// 添加新子节点
item.child.push(newChild)
return true
}
// 递归搜索子节点
if (item.child && item.child.length > 0) {
const found = findAndAddChild(item.child, targetId, newChild)
if (found) return true
}
}
return false
}
/**
* 递归删除指定ID的节点
* @param {Array} items - 要搜索的数组
* @param {string} targetId - 要删除的节点ID
* @returns {boolean} 是否成功删除
*/
export function findAndRemoveChild(items, targetId) {
for (let i = 0; i < items.length; i++) {
const item = items[i]
// 如果找到目标节点,从当前数组中删除
if (item.id === targetId) {
items.splice(i, 1)
return true
}
// 递归搜索子节点
if (item.child && item.child.length > 0) {
const found = findAndRemoveChild(item.child, targetId)
if (found) return true
}
}
return false
}