refactor node context menu III, added zustand
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { Dropdown, type MenuProps } from "antd";
|
||||
import type { EdgeModel, NodeContext } from "./Graph";
|
||||
import { defaultGraph, graphContext, type EdgeModel, type NodeContext } from "./Graph";
|
||||
import { useContext } from "react";
|
||||
import { cloneDeep } from "lodash";
|
||||
import { useGraphsStore } from "./GraphsCollection";
|
||||
|
||||
const items: MenuProps['items'] = [
|
||||
{
|
||||
@@ -34,6 +37,9 @@ export default function NodeContextMenu({
|
||||
return;
|
||||
}
|
||||
|
||||
const graphContextValue = useContext(graphContext)!;
|
||||
const graphsById = useGraphsStore((s) => s.graphsById);
|
||||
|
||||
function contextMenuOpenChange(open: boolean) {
|
||||
if (!open) {
|
||||
openContextMenu(false)
|
||||
@@ -51,7 +57,17 @@ export default function NodeContextMenu({
|
||||
break;
|
||||
}
|
||||
case 'subgraph': {
|
||||
graphsById.set(graphContextValue.graphId, cloneDeep(graphContextValue.graph));
|
||||
graphContextValue.selectGraphId(nodeContext.nodeId);
|
||||
let selectedGraph = graphsById.get(nodeContext.nodeId);
|
||||
|
||||
if (!selectedGraph) {
|
||||
selectedGraph = defaultGraph();
|
||||
graphsById.set(nodeContext.nodeId, selectedGraph);
|
||||
}
|
||||
|
||||
console.info(graphsById)
|
||||
graphContextValue.setGraph(selectedGraph);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -59,7 +75,7 @@ export default function NodeContextMenu({
|
||||
};
|
||||
|
||||
function removeNode(id: string) {
|
||||
nodeContext.setGraph(prev => ({ ...prev, nodes: [...prev.nodes.filter(n => n.id !== nodeContext.nodeId)], edges: removeEdgesOfNode(id, prev.edges) }));
|
||||
graphContextValue.setGraph(prev => ({ ...prev, nodes: [...prev.nodes.filter(n => n.id !== nodeContext.nodeId)], edges: removeEdgesOfNode(id, prev.edges) }));
|
||||
}
|
||||
|
||||
function removeEdgesOfNode(nodeId: string, edges: EdgeModel[]): EdgeModel[] {
|
||||
|
||||
Reference in New Issue
Block a user