|
@@ -11,9 +11,9 @@ import { selectTopic } from "@/utils/mindmapHander";
|
|
|
import { uuid } from "@/utils";
|
|
|
|
|
|
enum positionType {
|
|
|
- left = 'left',
|
|
|
- right = 'right',
|
|
|
- outside = 'outside',
|
|
|
+ left = "left",
|
|
|
+ right = "right",
|
|
|
+ outside = "outside",
|
|
|
}
|
|
|
// 拖拽主题时指示器
|
|
|
let indicatorNode: Node | undefined;
|
|
@@ -98,7 +98,6 @@ export const bindMindMapEvents = (
|
|
|
if (currentShadowNode && setMindProjectInfo) {
|
|
|
topicDragHander(
|
|
|
graph,
|
|
|
- setMindProjectInfo,
|
|
|
{ x: args.x, y: args.y },
|
|
|
args.node
|
|
|
);
|
|
@@ -108,13 +107,18 @@ export const bindMindMapEvents = (
|
|
|
graph.on("node:mouseup", (args) => {
|
|
|
// 拖拽结束
|
|
|
if (indicatorNode && insertNode) {
|
|
|
- graph.removeCell(args.node.id + '-edge');
|
|
|
- setMindProjectInfo && handleSwitchPosition(setMindProjectInfo, args.node.id, insertNode.id);
|
|
|
+ graph.removeCell(args.node.id + "-edge");
|
|
|
+ setMindProjectInfo &&
|
|
|
+ handleSwitchPosition(setMindProjectInfo, args.node.id, insertNode.id);
|
|
|
}
|
|
|
|
|
|
// 成为自由节点
|
|
|
- if(currentShadowNode && !indicatorNode && args.node.data?.parentId) {
|
|
|
- setMindProjectInfo && handleSwitchPosition(setMindProjectInfo, args.node.id, undefined, { x: args.x, y: args.y});
|
|
|
+ if (currentShadowNode && !indicatorNode && args.node.data?.parentId) {
|
|
|
+ setMindProjectInfo &&
|
|
|
+ handleSwitchPosition(setMindProjectInfo, args.node.id, undefined, {
|
|
|
+ x: args.x,
|
|
|
+ y: args.y,
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
currentShadowNode && setShadowMode(false, args.node, graph);
|
|
@@ -131,17 +135,16 @@ export const bindMindMapEvents = (
|
|
|
|
|
|
graph.on("node:move", (args) => {
|
|
|
// 自由节点拖拽
|
|
|
- if(!args.node.data?.parentId) {
|
|
|
+ if (!args.node.data?.parentId) {
|
|
|
setShadowMode(true, args.node, graph);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
graph.on("node:moving", (args) => {
|
|
|
- if(!args.node.data?.parentId && setMindProjectInfo) {
|
|
|
+ if (!args.node.data?.parentId && setMindProjectInfo) {
|
|
|
setShadowMode(false, args.node, graph);
|
|
|
topicDragHander(
|
|
|
graph,
|
|
|
- setMindProjectInfo,
|
|
|
{ x: args.x, y: args.y },
|
|
|
args.node
|
|
|
);
|
|
@@ -149,7 +152,7 @@ export const bindMindMapEvents = (
|
|
|
});
|
|
|
|
|
|
graph.on("node:moved", (args) => {
|
|
|
- if(!args.node.data?.parentId) {
|
|
|
+ if (!args.node.data?.parentId) {
|
|
|
setShadowMode(false, args.node, graph);
|
|
|
}
|
|
|
});
|
|
@@ -171,12 +174,14 @@ export const bindMindMapEvents = (
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- // 节点数据更改
|
|
|
+ /**
|
|
|
+ * 节点数据更改
|
|
|
+ */
|
|
|
graph.on("node:change:*", (args) => {
|
|
|
// console.log("node:change:*", args);
|
|
|
const { current, previous } = args;
|
|
|
if (args.key === "data") {
|
|
|
- // 收折子项
|
|
|
+ // 收折子项 setMindProjectInfo更新会重新渲染
|
|
|
if (current.collapsed !== previous.collapsed) {
|
|
|
setMindProjectInfo &&
|
|
|
updateTopic(
|
|
@@ -184,8 +189,24 @@ export const bindMindMapEvents = (
|
|
|
{ collapsed: current.collapsed },
|
|
|
setMindProjectInfo
|
|
|
);
|
|
|
- } else {
|
|
|
- updateTopic(args.cell.id, current, (info) => {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(current?.links && current.links.length !== previous?.links?.length) {
|
|
|
+ setMindProjectInfo &&
|
|
|
+ updateTopic(
|
|
|
+ args.cell.id,
|
|
|
+ { links: current.links },
|
|
|
+ setMindProjectInfo
|
|
|
+ );
|
|
|
+ }
|
|
|
+ // 本地缓存更新不会重新渲染
|
|
|
+ updateTopic(args.cell.id, current, (info) => {
|
|
|
+ localStorage.setItem("minMapProjectInfo", JSON.stringify(info));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if(args.key === "position") {
|
|
|
+ if(args.cell.isNode() && !args.cell.data.parentId) {
|
|
|
+ updateTopic(args.cell.id, {...args.cell.data, x: current.x, y: current.y}, (info) => {
|
|
|
localStorage.setItem("minMapProjectInfo", JSON.stringify(info));
|
|
|
});
|
|
|
}
|
|
@@ -203,6 +224,37 @@ export const bindMindMapEvents = (
|
|
|
);
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 连接线更改
|
|
|
+ */
|
|
|
+ graph.on("edge:change:*", args => {
|
|
|
+ if(args.key === "vertices" || args.key === "labels") {
|
|
|
+ const link = args.edge.toJSON();
|
|
|
+ const source = args.edge.getSourceCell();
|
|
|
+ source?.setData({
|
|
|
+ links: source.data?.links.map((item: Edge.Properties) => {
|
|
|
+ if(item.id === link.id) return link;
|
|
|
+ return item;
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 连接线删除
|
|
|
+ */
|
|
|
+ graph.on("edge:removed", (args) => {
|
|
|
+ if(args.edge.data?.isLink) {
|
|
|
+ // @ts-ignore
|
|
|
+ const source = graph.getCellById(args.edge.source?.cell as string);
|
|
|
+ source?.setData({
|
|
|
+ links: source.data?.links?.filter((item: Edge.Properties) => item.id !== args.edge.id)
|
|
|
+ }, {
|
|
|
+ deep: false
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
};
|
|
|
|
|
|
/**
|
|
@@ -276,8 +328,8 @@ const addIndicator = (
|
|
|
rx: 2,
|
|
|
ry: 2,
|
|
|
style: {
|
|
|
- opacity: 0.6
|
|
|
- }
|
|
|
+ opacity: 0.6,
|
|
|
+ },
|
|
|
},
|
|
|
},
|
|
|
});
|
|
@@ -313,8 +365,8 @@ const addIndicator = (
|
|
|
name: "",
|
|
|
},
|
|
|
style: {
|
|
|
- opacity: 0.6
|
|
|
- }
|
|
|
+ opacity: 0.6,
|
|
|
+ },
|
|
|
},
|
|
|
},
|
|
|
});
|
|
@@ -373,7 +425,6 @@ const setIndicator = (
|
|
|
}
|
|
|
// 外部位置
|
|
|
case positionType.outside: {
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
};
|
|
@@ -407,7 +458,6 @@ const isDescendantNode = (targetNode: Node, originNode: Node, graph: Graph) => {
|
|
|
*/
|
|
|
export const topicDragHander = (
|
|
|
graph: Graph,
|
|
|
- setMindProjectInfo: (info: MindMapProjectInfo) => void,
|
|
|
position: { x: number; y: number },
|
|
|
originNode: Node
|
|
|
) => {
|
|
@@ -440,11 +490,11 @@ export const topicDragHander = (
|
|
|
|
|
|
/**
|
|
|
* 拖拽完毕,切换主题位置
|
|
|
- * @param setMindProjectInfo
|
|
|
+ * @param setMindProjectInfo
|
|
|
* @param sourceId 拖拽的节点
|
|
|
* @param targetId 放置的节点
|
|
|
* @param position 自由节点放置的位置
|
|
|
- * @returns
|
|
|
+ * @returns
|
|
|
*/
|
|
|
const handleSwitchPosition = (
|
|
|
setMindProjectInfo: (info: MindMapProjectInfo) => void,
|
|
@@ -453,39 +503,45 @@ const handleSwitchPosition = (
|
|
|
position?: { x: number; y: number }
|
|
|
) => {
|
|
|
const mindmapProjectInfo = getMindMapProjectByLocal();
|
|
|
- if(!mindmapProjectInfo) return;
|
|
|
+ if (!mindmapProjectInfo) return;
|
|
|
|
|
|
// 找到要拖拽的节点并删除
|
|
|
let source: TopicItem | undefined;
|
|
|
- mindmapProjectInfo.topics.forEach(topic => {
|
|
|
- if(topic.id === sourceId) {
|
|
|
+ mindmapProjectInfo.topics.forEach((topic) => {
|
|
|
+ if (topic.id === sourceId) {
|
|
|
source = topic;
|
|
|
- };
|
|
|
- mindmapProjectInfo.topics = mindmapProjectInfo.topics.filter(item => item.id !== sourceId);
|
|
|
- })
|
|
|
- const topics = source
|
|
|
- ? mindmapProjectInfo.topics
|
|
|
- :traverseNode(mindmapProjectInfo.topics, (topic) => {
|
|
|
- const findItem = topic?.children?.find(item => item.id === sourceId);
|
|
|
- if(findItem) {
|
|
|
- source = findItem;
|
|
|
- topic.children = topic.children?.filter(item => item.id !== sourceId);
|
|
|
}
|
|
|
+ mindmapProjectInfo.topics = mindmapProjectInfo.topics.filter(
|
|
|
+ (item) => item.id !== sourceId
|
|
|
+ );
|
|
|
});
|
|
|
- if(!source) return;
|
|
|
+ const topics = source
|
|
|
+ ? mindmapProjectInfo.topics
|
|
|
+ : traverseNode(mindmapProjectInfo.topics, (topic) => {
|
|
|
+ const findItem = topic?.children?.find((item) => item.id === sourceId);
|
|
|
+ if (findItem) {
|
|
|
+ source = findItem;
|
|
|
+ topic.children = topic.children?.filter(
|
|
|
+ (item) => item.id !== sourceId
|
|
|
+ );
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (!source) return;
|
|
|
|
|
|
if (targetId) {
|
|
|
// 加入到目标节点下
|
|
|
mindmapProjectInfo.topics = traverseNode(topics, (topic) => {
|
|
|
- if(topic.id === targetId) {
|
|
|
- if(!topic.children) {
|
|
|
+ if (topic.id === targetId) {
|
|
|
+ if (!topic.children) {
|
|
|
topic.children = [];
|
|
|
}
|
|
|
- source && topic.children.push({
|
|
|
- ...source,
|
|
|
- type: topic.type === TopicType.main ? TopicType.branch : TopicType.sub,
|
|
|
- parentId: topic.id
|
|
|
- })
|
|
|
+ source &&
|
|
|
+ topic.children.push({
|
|
|
+ ...source,
|
|
|
+ type:
|
|
|
+ topic.type === TopicType.main ? TopicType.branch : TopicType.sub,
|
|
|
+ parentId: topic.id,
|
|
|
+ });
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
@@ -502,8 +558,16 @@ const handleSwitchPosition = (
|
|
|
x: position?.x,
|
|
|
y: position?.y,
|
|
|
type: TopicType.branch,
|
|
|
- parentId: null
|
|
|
- })
|
|
|
+ parentId: null,
|
|
|
+ links: (source.links || []).map(item => {
|
|
|
+ // 修改sourceId
|
|
|
+ item.source = {
|
|
|
+ ...item.source,
|
|
|
+ id
|
|
|
+ };
|
|
|
+ return item;
|
|
|
+ })
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
setMindProjectInfo(mindmapProjectInfo);
|