1、找到节点相交的边
export const findIntersectsEdge = (graph, node) => {
const edges = graph.getEdges();
const bbox = node.getBBox();
const lines = [bbox.leftLine, bbox.rightLine, bbox.topLine, bbox.bottomLine];
let res = null;
edges.forEach((edge) => {
const view = graph.findViewByCell(edge);
lines.forEach((line) => {
if (view) {
if (view.path.intersectsWithLine(line)) {
res = edge;
}
}
});
});
return res;
};
2、对插入的节点进行判断、克隆、设置、插入
graph.on("node:added", ({ node }) => {
const e = findIntersectsEdge(graph, node);
if (e) {
if (e.store.data.source.cell) {
let newNode = graph.getCellById(node.id);
const sourceNode = e.getSourceNode();
const targetNode = e.getTargetNode();
let isInsertFlag = false;
if (
newNode.shape == "flow-chart-rect-end" ||
newNode.shape == "flow-chart-rect-start" ||
) {
isInsertFlag = false;
} else {
isInsertFlag = true;
}
if (sourceNode && targetNode && isInsertFlag) {
e.clone()
.setSource({
cell: sourceNode.id,
connectionPoint: {
name: "boundary",
args: {
sticky: true,
},
},
})
.setTarget({
cell: node.id,
connectionPoint: {
name: "boundary",
args: {
sticky: true,
},
},
})
.addTo(graph);
e.clone()
.setSource({
cell: node.id,
connectionPoint: {
name: "boundary",
args: {
sticky: true,
},
},
})
.setTarget({
cell: targetNode.id,
connectionPoint: {
name: "boundary",
args: {
sticky: true,
},
},
})
.addTo(graph);
graph.removeEdge(e);
}
}
}
});
效果图:
参考:https://blog.csdn.net/ZCJSGSZ/article/details/126545542