|
@@ -3,7 +3,7 @@ import { Cell, Edge, EventArgs, Graph, Node } from "@antv/x6";
|
|
|
import { message } from "antd";
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
|
import { Selection } from "@repo/x6-plugin-selection";
|
|
|
-import { Keyboard } from "@antv/x6-plugin-keyboard";
|
|
|
+import { Keyboard } from "@antv/x6-plugin-keyboard";
|
|
|
import { Transform } from "@antv/x6-plugin-transform";
|
|
|
import { Scroller } from "@antv/x6-plugin-scroller";
|
|
|
import { Clipboard } from "@antv/x6-plugin-clipboard";
|
|
@@ -14,7 +14,7 @@ import { renderMindMap } from "@/utils/mindmap";
|
|
|
import { TopicType } from "@/enum";
|
|
|
import { isEqual, cloneDeep } from "lodash-es";
|
|
|
import { bindMindmapKeys } from "@/utils/fastKey";
|
|
|
-import { handleCreateCorrelationEdge } from "@/utils/mindmapHander";
|
|
|
+import { handleCreateCorrelationEdge, traverseNode } from "@/utils/mindmapHander";
|
|
|
import { Dnd } from "@antv/x6-plugin-dnd";
|
|
|
import { EditGraph, BatchEditMindMapElement } from "@/api/systemDesigner";
|
|
|
export default function mindMapModel() {
|
|
@@ -35,18 +35,46 @@ export default function mindMapModel() {
|
|
|
const projectInfoRef = useRef<MindMapProjectInfo>();
|
|
|
const timer = useRef<any>();
|
|
|
|
|
|
+ // 处理数据异常
|
|
|
+ const handleDataException = (data: MindMapProjectInfo) => {
|
|
|
+ const topIds = data.topics.map((item) => item.id);
|
|
|
+ // 处理顶层节点的parentId
|
|
|
+ return {
|
|
|
+ ...data,
|
|
|
+ topics: data.topics.map((topic) => {
|
|
|
+ traverseNode(topic.children ||[], (child) => {
|
|
|
+ child.children = child.children?.filter((item) => !topIds.includes(item.id));
|
|
|
+ });
|
|
|
+ return topic;
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
const setMindProjectInfo = (
|
|
|
- info: MindMapProjectInfo,
|
|
|
+ data: MindMapProjectInfo,
|
|
|
init?: boolean,
|
|
|
isSetting?: boolean,
|
|
|
ignoreRender?: boolean,
|
|
|
ignoreHistory?: boolean
|
|
|
) => {
|
|
|
- setProjectInfo(cloneDeep(info));
|
|
|
+ const info = handleDataException(cloneDeep(data));
|
|
|
+ if(isEqual(info, projectInfoRef.current)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ setProjectInfo(info);
|
|
|
projectInfoRef.current = info;
|
|
|
sessionStorage.setItem("mindMapProjectInfo", JSON.stringify(info));
|
|
|
+ console.log(
|
|
|
+ "setMindProjectInfo",
|
|
|
+ info,
|
|
|
+ init,
|
|
|
+ isSetting,
|
|
|
+ ignoreRender,
|
|
|
+ ignoreHistory
|
|
|
+ );
|
|
|
+
|
|
|
// 初始化加载数据 清空画布
|
|
|
- if(init) {
|
|
|
+ if (init) {
|
|
|
graph?.clearCells();
|
|
|
historyRef.current = [];
|
|
|
graph?.centerContent();
|
|
@@ -62,13 +90,14 @@ export default function mindMapModel() {
|
|
|
clearTimeout(timer.current);
|
|
|
timer.current = setTimeout(() => {
|
|
|
BatchEditMindMapElement(
|
|
|
- info.topics.map((topic) => {
|
|
|
- return {
|
|
|
- ...topic,
|
|
|
- graphId,
|
|
|
- };
|
|
|
- })
|
|
|
- );
|
|
|
+ info.topics.map((topic) => {
|
|
|
+ return {
|
|
|
+ ...topic,
|
|
|
+ parentId: null,
|
|
|
+ graphId,
|
|
|
+ };
|
|
|
+ })
|
|
|
+ );
|
|
|
}, 500);
|
|
|
}
|
|
|
}
|
|
@@ -92,22 +121,22 @@ export default function mindMapModel() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if(!ignoreRender && graphRef.current) {
|
|
|
+ if (!ignoreRender && graphRef.current) {
|
|
|
renderMindMap({
|
|
|
graph: graphRef.current,
|
|
|
setMindProjectInfo,
|
|
|
pageSetting: info?.pageSetting,
|
|
|
structure: info?.structure,
|
|
|
theme: info?.theme,
|
|
|
- topics: info?.topics,
|
|
|
+ topics: info.topics.map((item) => ({ ...item, parentId: null })),
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 添加记录
|
|
|
- if(!ignoreHistory && !ignoreRender) {
|
|
|
+ if (!ignoreHistory && !ignoreRender) {
|
|
|
historyRef.current?.push(info);
|
|
|
activeIndex.current = historyRef.current?.length - 1;
|
|
|
- if(historyRef.current?.length > 20) {
|
|
|
+ if (historyRef.current?.length > 20) {
|
|
|
historyRef.current?.shift();
|
|
|
activeIndex.current -= 1;
|
|
|
}
|
|
@@ -214,7 +243,7 @@ export default function mindMapModel() {
|
|
|
},
|
|
|
})
|
|
|
);
|
|
|
-
|
|
|
+
|
|
|
graphRef.current = instance;
|
|
|
|
|
|
dndRef.current = new Dnd({
|
|
@@ -282,8 +311,10 @@ export default function mindMapModel() {
|
|
|
|
|
|
// 能否重做
|
|
|
const canRedo = useMemo(() => {
|
|
|
- return historyRef.current?.length > 1 &&
|
|
|
- activeIndex.current < historyRef.current?.length - 1;
|
|
|
+ return (
|
|
|
+ historyRef.current?.length > 1 &&
|
|
|
+ activeIndex.current < historyRef.current?.length - 1
|
|
|
+ );
|
|
|
}, [historyRef.current, activeIndex.current]);
|
|
|
|
|
|
// 能否撤销
|