import { EdgeConfig, NodeConfig } from '@antv/g6';
|
import { FaultTreeShape } from '/@/projectCom/fault/tree/version/graph/types';
|
const PORT2ANCHOR = {
|
top: [0.5, 0],
|
left: [0, 0.5],
|
bottom: [0.5, 1],
|
right: [1, 0.5],
|
} as const;
|
|
export const SHAPE2TYPE = {
|
// edge: 'polyline-edge',
|
edge: 'polyline-edge',
|
|
// [FaultTreeShape.FaultEvent]: 'rect-node',
|
[FaultTreeShape.Gate]: 'or-node',
|
// [FaultTreeShape.Or]: 'or-node',
|
// [FaultTreeShape.And]: 'and-node',
|
[FaultTreeShape.MiddleEvent]: 'rect-node',
|
[FaultTreeShape.BottomEvent]: 'circle-node',
|
[FaultTreeShape.TopEvent]: 'rect-node',
|
} as const;
|
|
export const shapeWithInfo = ['rect-node', 'circle-node'];
|
/**
|
* x6 数据转换为 g6 数据
|
* @param x6Json
|
* @returns
|
*/
|
export const x6ToG6 = (x6Json) => {
|
const nodes = [] as NodeConfig[];
|
const edges = [] as EdgeConfig[];
|
const x6DataMap = new Map(
|
x6Json.map((item) => {
|
return [item.id, item];
|
})
|
);
|
for (let cell of x6Json) {
|
if (cell.shape === 'edge') {
|
let sourcePort = null;
|
let targetPort = null;
|
if (cell?.source?.cell) {
|
const sourceCell = x6DataMap.get(cell.source?.cell) as any;
|
const foundIndex = (sourceCell.ports.items as any[]).findIndex((item) => item.id === cell.source.port);
|
if (foundIndex !== -1) {
|
sourcePort = foundIndex;
|
}
|
}
|
|
if (cell?.target?.cell) {
|
const targetCell = x6DataMap.get(cell.target?.cell) as any;
|
const foundIndex = (targetCell.ports.items as any[]).findIndex((item) => item.id === cell.target.port);
|
if (foundIndex !== -1) {
|
targetPort = foundIndex;
|
}
|
}
|
edges.push({
|
id: cell.id,
|
source: cell.source.cell,
|
target: cell.target.cell,
|
type: SHAPE2TYPE[cell.shape],
|
sourceAnchor: sourcePort,
|
targetAnchor: targetPort,
|
});
|
} else {
|
let dx = 0;
|
let dy = 0;
|
if (cell.shape === FaultTreeShape.Gate) {
|
dy = 0;
|
dx = -5;
|
}
|
nodes.push({
|
id: cell.id,
|
x: cell.position.x + dx,
|
y: cell.position.y + dy,
|
type: SHAPE2TYPE[cell.shape],
|
width: cell.size.width,
|
height: cell.size.height,
|
label: cell?.attrs?.text?.text,
|
anchorPoints: cell.ports?.items?.map((port) => {
|
const group = port.group;
|
|
const portPosition = cell.ports?.groups[group]?.position;
|
return PORT2ANCHOR[portPosition];
|
}),
|
data: cell.data,
|
});
|
}
|
}
|
return { edges, nodes };
|
};
|