|
@@ -1,68 +1,43 @@
|
|
import { Graph, Node, EventArgs, Cell } from "@antv/x6";
|
|
import { Graph, Node, EventArgs, Cell } from "@antv/x6";
|
|
-import { BatchAddFlowchartElement, BatchEditFlowchartElement, BatchDeleteFlowchartElement } from "@/api/systemDesigner";
|
|
|
|
-import { getDefaultDataByTheme } from "@/config/data";
|
|
|
|
-import { differenceWith, toPairs, isEqual } from "lodash-es";
|
|
|
|
-
|
|
|
|
-// 对比查找默认数据修改的部分
|
|
|
|
-export const getDifferences = (source: any, target: any) => {
|
|
|
|
- const differences = differenceWith(
|
|
|
|
- toPairs(source),
|
|
|
|
- toPairs(target),
|
|
|
|
- isEqual
|
|
|
|
- );
|
|
|
|
-
|
|
|
|
- return differences;
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-// 获取基础数据
|
|
|
|
-export const getBasicData = (cellMeta: Cell.Properties) => {
|
|
|
|
- const projectInfo = sessionStorage.getItem("system-design-project");
|
|
|
|
- if(!projectInfo) {
|
|
|
|
- return cellMeta;
|
|
|
|
- }
|
|
|
|
- // 先根据主题获取通用数据,然后返回差异部分
|
|
|
|
- const themeKey = JSON.parse(projectInfo)?.graph?.theme || '1';
|
|
|
|
- const defaultData = getDefaultDataByTheme(themeKey);
|
|
|
|
- if(cellMeta.shape === "edge") {
|
|
|
|
- return getDifferences(cellMeta, defaultData.nodeDefaultData);
|
|
|
|
- } else {
|
|
|
|
- return getDifferences(cellMeta.data || {}, defaultData.nodeDefaultData);
|
|
|
|
- }
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
|
|
+import {
|
|
|
|
+ BatchAddFlowchartElement,
|
|
|
|
+ BatchEditFlowchartElement,
|
|
|
|
+ BatchDeleteFlowchartElement,
|
|
|
|
+} from "@/api/systemDesigner";
|
|
|
|
+import { getBasicData } from "@/utils/diffrence";
|
|
export const handleGraphEvent = (graph: Graph) => {
|
|
export const handleGraphEvent = (graph: Graph) => {
|
|
// 边开始拖拽点
|
|
// 边开始拖拽点
|
|
const sourceArrowhead = {
|
|
const sourceArrowhead = {
|
|
name: "source-arrowhead",
|
|
name: "source-arrowhead",
|
|
args: {
|
|
args: {
|
|
attrs: {
|
|
attrs: {
|
|
- d: 'M -5,-5 5,-5 5,5 -5,5 Z',
|
|
|
|
- fill: '#fff',
|
|
|
|
- stroke: '#239edd',
|
|
|
|
- 'stroke-width': 1,
|
|
|
|
|
|
+ d: "M -5,-5 5,-5 5,5 -5,5 Z",
|
|
|
|
+ fill: "#fff",
|
|
|
|
+ stroke: "#239edd",
|
|
|
|
+ "stroke-width": 1,
|
|
},
|
|
},
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ },
|
|
|
|
+ };
|
|
// 边结束拖拽点
|
|
// 边结束拖拽点
|
|
const targetArrowhead = {
|
|
const targetArrowhead = {
|
|
name: "target-arrowhead",
|
|
name: "target-arrowhead",
|
|
args: {
|
|
args: {
|
|
attrs: {
|
|
attrs: {
|
|
- d: 'M -5,-5 5,-5 5,5 -5,5 Z',
|
|
|
|
- fill: '#fff',
|
|
|
|
- stroke: '#239edd',
|
|
|
|
- 'stroke-width': 1,
|
|
|
|
|
|
+ d: "M -5,-5 5,-5 5,5 -5,5 Z",
|
|
|
|
+ fill: "#fff",
|
|
|
|
+ stroke: "#239edd",
|
|
|
|
+ "stroke-width": 1,
|
|
},
|
|
},
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ },
|
|
|
|
+ };
|
|
// 边选中
|
|
// 边选中
|
|
graph.on("edge:selected", (args) => {
|
|
graph.on("edge:selected", (args) => {
|
|
- args.edge.addTools(['edge-editor', sourceArrowhead, targetArrowhead]);
|
|
|
|
|
|
+ args.edge.addTools(["edge-editor", sourceArrowhead, targetArrowhead]);
|
|
});
|
|
});
|
|
// 边取消选中
|
|
// 边取消选中
|
|
graph.on("edge:unselected", (args) => {
|
|
graph.on("edge:unselected", (args) => {
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
- args.edge.removeTools(['edge-editor', sourceArrowhead, targetArrowhead]);
|
|
|
|
|
|
+ args.edge.removeTools(["edge-editor", sourceArrowhead, targetArrowhead]);
|
|
}, 100);
|
|
}, 100);
|
|
});
|
|
});
|
|
|
|
|
|
@@ -113,7 +88,7 @@ export const handleGraphEvent = (graph: Graph) => {
|
|
|
|
|
|
// 处理添加节点事件 noCreate为不用创建节点
|
|
// 处理添加节点事件 noCreate为不用创建节点
|
|
graph.on("node:added", ({ node }) => {
|
|
graph.on("node:added", ({ node }) => {
|
|
- if(node.getData()?.noCreate) {
|
|
|
|
|
|
+ if (node.getData()?.noCreate) {
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
graph.removeNode(node.id);
|
|
graph.removeNode(node.id);
|
|
}, 50);
|
|
}, 50);
|
|
@@ -121,20 +96,64 @@ export const handleGraphEvent = (graph: Graph) => {
|
|
});
|
|
});
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+const getCellData = (cellData: Cell.Properties) => {
|
|
|
|
+ const cellBasicData = getBasicData(cellData);
|
|
|
|
+ const {
|
|
|
|
+ id,
|
|
|
|
+ zIndex,
|
|
|
|
+ graphId,
|
|
|
|
+ parent,
|
|
|
|
+ position,
|
|
|
|
+ shape,
|
|
|
|
+ size,
|
|
|
|
+ view,
|
|
|
|
+ source,
|
|
|
|
+ target,
|
|
|
|
+ ports,
|
|
|
|
+ } = cellData;
|
|
|
|
+
|
|
|
|
+ const { attrs, data, router } = cellBasicData;
|
|
|
|
+ return cellData.shape === "edge"
|
|
|
|
+ ? // 边
|
|
|
|
+ {
|
|
|
|
+ zIndex,
|
|
|
|
+ source,
|
|
|
|
+ target,
|
|
|
|
+ id,
|
|
|
|
+ graphId,
|
|
|
|
+ data,
|
|
|
|
+ attrs,
|
|
|
|
+ router,
|
|
|
|
+ shape,
|
|
|
|
+ }
|
|
|
|
+ : // 节点
|
|
|
|
+ {
|
|
|
|
+ id,
|
|
|
|
+ graphId,
|
|
|
|
+ parent,
|
|
|
|
+ position,
|
|
|
|
+ shape,
|
|
|
|
+ size,
|
|
|
|
+ view,
|
|
|
|
+ zIndex,
|
|
|
|
+ source,
|
|
|
|
+ target,
|
|
|
|
+ ports: ports?.items ? { items: ports.items } : undefined,
|
|
|
|
+ data: cellBasicData,
|
|
|
|
+ };
|
|
|
|
+};
|
|
|
|
+
|
|
export const handleGraphApiEvent = (graph: Graph) => {
|
|
export const handleGraphApiEvent = (graph: Graph) => {
|
|
let timer2: any;
|
|
let timer2: any;
|
|
let map2: Record<string, any> = {};
|
|
let map2: Record<string, any> = {};
|
|
graph.on("cell:added", (args) => {
|
|
graph.on("cell:added", (args) => {
|
|
- if(args.cell?.data?.isPage) return;
|
|
|
|
|
|
+ if (args.cell?.data?.isPage) return;
|
|
const data = args.cell.toJSON();
|
|
const data = args.cell.toJSON();
|
|
- const cellBasicData = getBasicData(data);
|
|
|
|
- console.log('新增数据:', cellBasicData)
|
|
|
|
const setData = () => {
|
|
const setData = () => {
|
|
const id = args.cell.id;
|
|
const id = args.cell.id;
|
|
- delete data.tools;
|
|
|
|
- map2[id] = data;
|
|
|
|
- }
|
|
|
|
- if(timer2) {
|
|
|
|
|
|
+ map2[id] = getCellData(data);
|
|
|
|
+ };
|
|
|
|
+ if (timer2) {
|
|
setData();
|
|
setData();
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -142,13 +161,15 @@ export const handleGraphApiEvent = (graph: Graph) => {
|
|
timer2 = setTimeout(() => {
|
|
timer2 = setTimeout(() => {
|
|
const graphId = sessionStorage.getItem("projectId");
|
|
const graphId = sessionStorage.getItem("projectId");
|
|
const list = Object.values(map2);
|
|
const list = Object.values(map2);
|
|
- if(graphId && list.length > 0) {
|
|
|
|
- BatchAddFlowchartElement(list.map(item => {
|
|
|
|
- return {
|
|
|
|
- ...item,
|
|
|
|
- graphId
|
|
|
|
- }
|
|
|
|
- }));
|
|
|
|
|
|
+ if (graphId && list.length > 0) {
|
|
|
|
+ BatchAddFlowchartElement(
|
|
|
|
+ list.map((item) => {
|
|
|
|
+ return {
|
|
|
|
+ ...item,
|
|
|
|
+ graphId,
|
|
|
|
+ };
|
|
|
|
+ })
|
|
|
|
+ );
|
|
}
|
|
}
|
|
timer2 = null;
|
|
timer2 = null;
|
|
map2 = {};
|
|
map2 = {};
|
|
@@ -159,16 +180,13 @@ export const handleGraphApiEvent = (graph: Graph) => {
|
|
let timer1: any;
|
|
let timer1: any;
|
|
let map: Record<string, any> = {};
|
|
let map: Record<string, any> = {};
|
|
graph.on("cell:change:*", (args: EventArgs["cell:change:*"]) => {
|
|
graph.on("cell:change:*", (args: EventArgs["cell:change:*"]) => {
|
|
- if(args.cell?.data?.isPage || args.key === 'tools' || timer2) return;
|
|
|
|
|
|
+ if (args.cell?.data?.isPage || args.key === "tools" || timer2) return;
|
|
const setData = () => {
|
|
const setData = () => {
|
|
const id = args.cell.id;
|
|
const id = args.cell.id;
|
|
const data = args.cell.toJSON();
|
|
const data = args.cell.toJSON();
|
|
- delete data.tools;
|
|
|
|
- map[id] = data;
|
|
|
|
- const cellBasicData = getBasicData(data);
|
|
|
|
- console.log('修改数据:', cellBasicData)
|
|
|
|
- }
|
|
|
|
- if(timer1) {
|
|
|
|
|
|
+ map[id] = getCellData(data);
|
|
|
|
+ };
|
|
|
|
+ if (timer1) {
|
|
setData();
|
|
setData();
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -176,32 +194,34 @@ export const handleGraphApiEvent = (graph: Graph) => {
|
|
timer1 = setTimeout(() => {
|
|
timer1 = setTimeout(() => {
|
|
const graphId = sessionStorage.getItem("projectId");
|
|
const graphId = sessionStorage.getItem("projectId");
|
|
const list = Object.values(map);
|
|
const list = Object.values(map);
|
|
- if(graphId && list.length > 0) {
|
|
|
|
- BatchEditFlowchartElement(list.map(item => {
|
|
|
|
- return {
|
|
|
|
- ...item,
|
|
|
|
- graphId
|
|
|
|
- }
|
|
|
|
- }));
|
|
|
|
|
|
+ if (graphId && list.length > 0) {
|
|
|
|
+ BatchEditFlowchartElement(
|
|
|
|
+ list.map((item) => {
|
|
|
|
+ return {
|
|
|
|
+ ...item,
|
|
|
|
+ graphId,
|
|
|
|
+ };
|
|
|
|
+ })
|
|
|
|
+ );
|
|
}
|
|
}
|
|
timer1 = null;
|
|
timer1 = null;
|
|
map = {};
|
|
map = {};
|
|
}, 1000);
|
|
}, 1000);
|
|
});
|
|
});
|
|
|
|
|
|
- let timer: any
|
|
|
|
|
|
+ let timer: any;
|
|
const ids: string[] = [];
|
|
const ids: string[] = [];
|
|
graph.on("cell:removed", (args) => {
|
|
graph.on("cell:removed", (args) => {
|
|
// 加入防抖 如果连续多个删除调用批量删除
|
|
// 加入防抖 如果连续多个删除调用批量删除
|
|
- if(timer) {
|
|
|
|
|
|
+ if (timer) {
|
|
ids.push(args.cell.id);
|
|
ids.push(args.cell.id);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
ids.push(args.cell.id);
|
|
ids.push(args.cell.id);
|
|
timer = setTimeout(() => {
|
|
timer = setTimeout(() => {
|
|
timer = null;
|
|
timer = null;
|
|
- if(ids.length > 0) {
|
|
|
|
- BatchDeleteFlowchartElement({ids});
|
|
|
|
|
|
+ if (ids.length > 0) {
|
|
|
|
+ BatchDeleteFlowchartElement({ ids });
|
|
}
|
|
}
|
|
ids.splice(0, ids.length);
|
|
ids.splice(0, ids.length);
|
|
}, 500);
|
|
}, 500);
|