antv/x6 官方文档上自动扩展父节点
给出的示例仅适用于简单的单层嵌套结构,一旦复杂嵌套结构就会出现各种问题。如下视频:
扩展父节点
现要实现多层嵌套下的自动拓展父节点,需对其代码进行更改。
graph.on("node:change:size", ({ node, options }) => {
if (options.skipParentHandler) {
return;
}
const children = node.getChildren();
if (children && children.length) {
node.prop("originSize", node.getSize());
}
});
const parentPosition = (node: Node<Node.Properties>) => {
let embedPadding = 20;
const parent = node.getParent();
if (
parent &&
parent.isNode() &&
parent.shape == "groupNode" &&
!(parent as NodeGroup).isCollapsed()
) {
let originSize = parent.prop("originSize");
if (originSize == null) {
originSize = parent.getSize();
parent.prop("originSize", originSize);
}
let originPosition = parent.prop("originPosition");
if (originPosition == null) {
originPosition = parent.getPosition();
parent.prop("originPosition", originPosition);
}
let originRec = new Rectangle(
originPosition.x,
originPosition.y,
originSize.width,
originSize.height
);
const children = parent.getChildren();
const bboxs = children
?.filter((item: any) => item.isNode())
.map((child: any) => child.getBBox());
if (!bboxs || bboxs.length == 0) {
return;
}
let x0 =
Math.min(...bboxs.map((bbox: any) => bbox.left)) - embedPadding;
let y0 =
Math.min(...bboxs.map((bbox: any) => bbox.top)) - embedPadding * 2;
let x1 =
Math.max(...bboxs.map((bbox: any) => bbox.right)) - x0 + embedPadding;
let y1 =
Math.max(...bboxs.map((bbox: any) => bbox.bottom)) -
y0 +
embedPadding;
let currentRec = new Rectangle(x0, y0, x1, y1);
if (!currentRec.equals(originRec)) {
parent.setPosition(currentRec.topLeft, {
skipParentHandler: true,
});
parent.resize(currentRec.width, currentRec.height, {
skipParentHandler: true,
});
}
}
parent && parentPosition(parent as Node);
};
graph.on("node:change:position", ({ node, options }) => {
if (options.skipParentHandler || ctrlPressed) {
return;
}
const children = node.getChildren();
if (children && children.length) {
node.prop("originPosition", node.getPosition());
}
parentPosition(node);
});
最终效果如下所示:
多层嵌套结构自动拓展父节点