- "@antv/x6": "^2.5.5",
-
"@antv/x6-plugin-clipboard": "^2.1.4",
-
"@antv/x6-plugin-dnd": "^2.0.4",
-
"@antv/x6-plugin-export": "^2.1.5",
-
"@antv/x6-plugin-history": "^2.2.0",
-
"@antv/x6-plugin-selection": "^2.1.5",
-
"@antv/x6-plugin-snapline": "^2.1.6",
-
"@antv/x6-plugin-stencil": "^2.0.2",
import { Stencil } from "@antv/x6-plugin-stencil";
import { Dnd } from "@antv/x6-plugin-dnd";
import { Export } from "@antv/x6-plugin-export";
import { Selection } from '@antv/x6-plugin-selection';
import { History } from '@antv/x6-plugin-history';
import { Snapline } from '@antv/x6-plugin-snapline';
//复制
import { Clipboard } from '@antv/x6-plugin-clipboard'
const container = document.querySelector(id);
this.graph = new Graph({
container: container,
autoResize: container,
// background: {
// color: "#F7F7FA"
// },
// width: 500,
// height: 500,
// 网格
grid: {
size: 10,
visible: false,
args: {
color: "#a0a0a0",
thickness: 1
}
},
// 画布调整
scroller: {
enabled: true,
pageVisible: true,
pageBreak: true,
pannable: true,
// modifiers: "ctrl",
autoResize: true
},
// 设置滚轮缩放画布
mousewheel: true,
snapline: true,
history: true, // 历史
clipboard: true, // 复制
keyboard: true,
// 按ctrl框选节点
selecting: {
multiple: true,
enabled: true,
rubberband: true,
modifiers: "ctrl",
strict: true,
movable: true,
showNodeSelectionBox: false,
showEdgeSelectionBox: false
},
// 配置全局连线规则
connecting: {
anchor: "center",
connectionPoint: "anchor",
allowBlank: false,
allowMulti: false,
highlight: true,
snap: {
radius: 25
},
createEdge() {
let newEdge = new Shape.Edge(cloneDeep(commonEdage));
return newEdge;
},
validateConnection({
sourceView,
targetView,
sourceMagnet,
targetMagnet
}) {
if (sourceView === targetView) {
return false;
}
if (!sourceMagnet) {
return false;
}
// 只能连接到输入链接桩
if (
!targetMagnet ||
targetMagnet.getAttribute("port-group") !== "in"
) {
return false;
}
return true;
},
highlighting: {
magnetAvailable: {
name: "stroke",
args: {
padding: 4,
attrs: {
strokeWidth: 4,
stroke: "#31a3ff" //rgba(223,234,255) #e52e1a
}
}
}
}
},
// 节点拖拽到另一节点,形成父子节点关系
embedding: {
enabled: true,
findParent({ node }) {
const bbox = node.getBBox();
return this.getNodes().filter((node) => {
// 只有 data.parent 为 true 的节点才是父节点
const data = node.getData();
if (data && data.parent) {
const targetBBox = node.getBBox();
return bbox.isIntersectWithRect(targetBBox);
}
return false;
});
}
}
});
// this.initStencil(stencilId);
// this.initShape();
this.initEvent();
//使用插件:导出+Selection+Keyboard
this.graph
.use(new Export())
.use(
new Selection({
enabled: true,
rubberband: true,
// showNodeSelectionBox: true,
}),
)
.use(
new History({
enabled: true,
}),
)
.use(
new Snapline({
enabled: true,
}),
)
.use(
new Clipboard({
enabled: true,
}),
);
// this.initAnimate();
return this.graph;
}
const { graph } = this;
graph.on("cell:selected", ({ cell }) => {
if (cell.isEdge()) {
cell.attr("line/stroke", "#07f5f5");
cell.attr("line/strokeWidth", 2.5);
} else {
cell.setAttrs({
body: {
fill: "#2cfeff",
stroke: "#2cfeff",
fillOpacity: "1",
strokeOpacity: "1"
},
label: {
fill: "#151B21"
},
path: {
fill: "#151B21"
},
path2: {
fill: "#151B21"
}
});
}
});
graph.on("cell:unselected", ({ cell }) => {
if (cell.isEdge()) {
cell.attr("line/stroke", "#2cfeff");
cell.attr("line/strokeWidth", 1);
} else {
console.log("unselected");
cell.setAttrs({
body: {
fill: "#2cfeff",
stroke: "#2cfeff",
fillOpacity: "0.15",
strokeOpacity: "0.6"
},
label: {
fill: "#2CFEFF"
},
path: {
fill: "#2CFEFF"
},
path2: {
fill: "#2CFEFF"
}
});
}
});
graph.on("edge:connected", ({ isNew, edge }) => {
if (isNew) {
const source = edge.getSourceCell();
const rightPorts = source.getPortsByGroup("out");
if (rightPorts.length === 2) {
const sourcePortId = edge.getSourcePortId();
const portIndex = source.getPortIndex(sourcePortId);
const label = portIndex == 1 ? "是" : "否";
edge.setLabels(label);
}
}
});
}