|
@@ -5,6 +5,7 @@ import TopicComponent from "@/components/mindMap/Topic";
|
|
|
import Hierarchy from "@antv/hierarchy";
|
|
|
import { topicData } from "@/config/data";
|
|
|
import { uuid } from "@/utils";
|
|
|
+import { SetState } from "ahooks/lib/useSetState";
|
|
|
|
|
|
interface HierarchyResult {
|
|
|
id: string;
|
|
@@ -13,12 +14,17 @@ interface HierarchyResult {
|
|
|
data: TopicItem;
|
|
|
children?: HierarchyResult[];
|
|
|
}
|
|
|
+/**
|
|
|
+ * 渲染思维导图项目
|
|
|
+ * @param graph
|
|
|
+ */
|
|
|
export const renderMindMap = (
|
|
|
- projectInfo: MindMapProjectInfo,
|
|
|
- graph: Graph
|
|
|
+ graph: Graph,
|
|
|
+ setMindProjectInfo: () => void
|
|
|
) => {
|
|
|
+ const projectInfo = getMindMapProjectByLocal();
|
|
|
+ if(!projectInfo) return;
|
|
|
const { topics, pageSetting } = projectInfo;
|
|
|
- console.log("主题:", topics);
|
|
|
const cells: Cell[] = [];
|
|
|
topics.forEach((topic) => {
|
|
|
// 遍历出层次结构
|
|
@@ -31,24 +37,23 @@ export const renderMindMap = (
|
|
|
return d.width;
|
|
|
},
|
|
|
getHGap(d: TopicItem) {
|
|
|
- if (d.type === TopicType.main) return 0;
|
|
|
- if (d.type === TopicType.branch) return pageSetting.branchY;
|
|
|
- if (d.type === TopicType.sub) return pageSetting.subTopicY;
|
|
|
- return 0;
|
|
|
+ if (d.type === TopicType.main) return pageSetting.branchX;
|
|
|
+ if (d.type === TopicType.branch) return pageSetting.subTopicX;
|
|
|
+ if (d.type === TopicType.sub) return pageSetting.subTopicX;
|
|
|
+ return 40;
|
|
|
},
|
|
|
getVGap(d: TopicItem) {
|
|
|
- if (d.type === TopicType.main) return 0;
|
|
|
- if (d.type === TopicType.branch) return pageSetting.branchX;
|
|
|
- if (d.type === TopicType.sub) return pageSetting.subTopicX;
|
|
|
- return 0;
|
|
|
+ if (d.type === TopicType.main) return pageSetting.subTopicY;
|
|
|
+ if (d.type === TopicType.branch) return pageSetting.subTopicY;
|
|
|
+ if (d.type === TopicType.sub) return pageSetting.subTopicY;
|
|
|
+ return 20;
|
|
|
},
|
|
|
getSide: () => {
|
|
|
return "right";
|
|
|
},
|
|
|
});
|
|
|
const traverse = (
|
|
|
- hierarchyItem: HierarchyResult,
|
|
|
- parent?: HierarchyResult
|
|
|
+ hierarchyItem: HierarchyResult
|
|
|
) => {
|
|
|
if (hierarchyItem) {
|
|
|
const { data, children, x, y } = hierarchyItem;
|
|
@@ -59,8 +64,11 @@ export const renderMindMap = (
|
|
|
...TopicComponent,
|
|
|
width: data.width,
|
|
|
height: data.height,
|
|
|
- // parentId: parent?.id,
|
|
|
- data,
|
|
|
+ data: {
|
|
|
+ ...data,
|
|
|
+ // 节点内部执行数据更新方法
|
|
|
+ setMindProjectInfo,
|
|
|
+ },
|
|
|
id,
|
|
|
x,
|
|
|
y,
|
|
@@ -69,7 +77,6 @@ export const renderMindMap = (
|
|
|
|
|
|
if (children) {
|
|
|
children.forEach((item: HierarchyResult) => {
|
|
|
- // const { id, data } = item;
|
|
|
cells.push(
|
|
|
// 创建连线
|
|
|
graph.createEdge({
|
|
@@ -97,12 +104,20 @@ export const renderMindMap = (
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
- console.log('渲染结果', result)
|
|
|
+
|
|
|
traverse(result);
|
|
|
});
|
|
|
|
|
|
- graph.resetCells(cells);
|
|
|
- graph.centerContent();
|
|
|
+ cells.forEach((cell) => {
|
|
|
+ // 存在更新位置,否则添加
|
|
|
+ if (graph.hasCell(cell.id) && cell.isNode()) {
|
|
|
+ const oldCell = graph.getCellById(cell.id);
|
|
|
+ oldCell.isNode() && oldCell.position(cell.position().x, cell.position().y);
|
|
|
+ } else {
|
|
|
+ graph.addCell(cell);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // graph.centerContent();
|
|
|
};
|
|
|
|
|
|
/**
|
|
@@ -134,8 +149,8 @@ export const addTopic = (
|
|
|
};
|
|
|
|
|
|
traverse(projectInfo?.topics || []);
|
|
|
- console.log('新增结果:', projectInfo);
|
|
|
- localStorage.setItem("minMapProjectInfo", JSON.stringify(projectInfo));
|
|
|
+
|
|
|
+ parentData?.setMindProjectInfo(projectInfo);
|
|
|
};
|
|
|
|
|
|
const topicMap = {
|
|
@@ -162,19 +177,19 @@ export const buildTopic = (
|
|
|
id: uuid(),
|
|
|
type,
|
|
|
label: topicMap[type] || "自由主题",
|
|
|
- width: theme[type].width,
|
|
|
- height: theme[type].height,
|
|
|
+ width: theme[type]?.width || 206,
|
|
|
+ height: theme[type]?.height || 70,
|
|
|
fill: {
|
|
|
...topicData.fill,
|
|
|
- ...theme[type].fill,
|
|
|
+ ...theme[type]?.fill,
|
|
|
},
|
|
|
text: {
|
|
|
...topicData.text,
|
|
|
- ...theme[type].text,
|
|
|
+ ...theme[type]?.text,
|
|
|
},
|
|
|
stroke: {
|
|
|
...topicData.stroke,
|
|
|
- ...theme[type].stroke,
|
|
|
+ ...theme[type]?.stroke,
|
|
|
},
|
|
|
...options,
|
|
|
};
|
|
@@ -185,5 +200,5 @@ export const buildTopic = (
|
|
|
* @returns
|
|
|
*/
|
|
|
export const getMindMapProjectByLocal = (): MindMapProjectInfo | null => {
|
|
|
- return JSON.parse(localStorage.getItem("minMapProjectInfo") || "");
|
|
|
+ return JSON.parse(localStorage.getItem("minMapProjectInfo") || "null");
|
|
|
};
|