-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextMenu.tsx
More file actions
175 lines (171 loc) · 5.32 KB
/
ContextMenu.tsx
File metadata and controls
175 lines (171 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import {
ClickAwayListener,
Divider,
MenuItem,
MenuList,
Paper,
} from "@mui/material";
import nullthrows from "nullthrows";
import invariant from "tiny-invariant";
import {
type CCComponentId,
CCComponentStore,
} from "../../../../store/component";
import {
type CCConnection,
CCConnectionStore,
} from "../../../../store/connection";
import {
type CCNode,
type CCNodeId,
CCNodeStore,
} from "../../../../store/node";
import { useStore } from "../../../../store/react";
import { useComponentEditorStore } from "../store";
export type CCComponentEditorContextMenuProps = {
onEditComponent: (componentId: CCComponentId) => void;
};
export default function CCComponentEditorContextMenu({
onEditComponent,
}: CCComponentEditorContextMenuProps) {
const { store } = useStore();
const componentEditorState = useComponentEditorStore()();
if (!componentEditorState.contextMenuState) return null;
return (
<ClickAwayListener onClickAway={componentEditorState.closeContextMenu}>
<MenuList
component={Paper}
dense
sx={{
position: "absolute",
top: `${componentEditorState.contextMenuState.position.y}px`,
left: `${componentEditorState.contextMenuState.position.x}px`,
width: "200px",
}}
>
{/* <MenuItem onClick={componentEditorState.closeContextMenu}>
Create a node
</MenuItem> */}
{componentEditorState.selectedNodeIds.size > 0 && (
<MenuItem
onClick={() => {
const oldNodes = [...componentEditorState.selectedNodeIds].map(
(nodeId) => {
const node = store.nodes.get(nodeId);
invariant(node);
return node;
},
);
const oldConnections = [
...componentEditorState.selectedConnectionIds,
].map((connectionId) => {
const connection = store.connections.get(connectionId);
invariant(connection);
return connection;
});
const newComponent = CCComponentStore.create({
name: "New Component",
});
store.components.register(newComponent);
const oldToNewNodeIdMap = new Map<CCNodeId, CCNodeId>();
const newNodes = oldNodes.map<CCNode>((oldNode) => {
const newNode = CCNodeStore.create({
parentComponentId: newComponent.id,
position: oldNode.position,
componentId: oldNode.componentId,
});
oldToNewNodeIdMap.set(oldNode.id, newNode.id);
return newNode;
});
for (const node of newNodes) store.nodes.register(node);
const newConnections = oldConnections.flatMap<CCConnection>(
(oldConnection) => {
const oldFromNodePin = nullthrows(
store.nodePins.get(oldConnection.from),
);
const oldToNodePin = nullthrows(
store.nodePins.get(oldConnection.to),
);
const newFromNodeId = nullthrows(
oldToNewNodeIdMap.get(oldFromNodePin.nodeId),
);
const newToNodeId = nullthrows(
oldToNewNodeIdMap.get(oldToNodePin.nodeId),
);
return CCConnectionStore.create({
parentComponentId: newComponent.id,
from: store.nodePins.getByImplementationNodeIdAndPinId(
newFromNodeId,
oldFromNodePin.componentPinId,
).id,
to: store.nodePins.getByImplementationNodeIdAndPinId(
newToNodeId,
oldToNodePin.componentPinId,
).id,
bentPortion: oldConnection.bentPortion,
});
},
);
for (const connection of newConnections)
store.connections.register(connection);
store.connections.unregister([
...componentEditorState.selectedConnectionIds,
]);
store.nodes.unregister([...componentEditorState.selectedNodeIds]);
componentEditorState.closeContextMenu();
onEditComponent(newComponent.id);
}}
>
Create a new component...
</MenuItem>
)}
{(componentEditorState.selectedNodeIds.size > 0 ||
componentEditorState.selectedConnectionIds.size > 0) && (
<MenuItem
onClick={() => {
if (componentEditorState.selectedNodeIds.size > 0)
store.nodes.unregister([
...componentEditorState.selectedNodeIds,
]);
if (componentEditorState.selectedConnectionIds.size > 0)
store.connections.unregister([
...componentEditorState.selectedConnectionIds,
]);
// componentEditorState.selectNode([], true);
componentEditorState.selectConnection([], false);
componentEditorState.closeContextMenu();
}}
>
Delete
</MenuItem>
)}
{(() => {
if (componentEditorState.selectedNodeIds.size !== 1) return undefined;
const iteratorResult = componentEditorState.selectedNodeIds
.values()
.next();
invariant(!iteratorResult.done);
const targetNode = store.nodes.get(iteratorResult.value);
invariant(targetNode);
const targetComponent = store.components.get(targetNode.componentId);
invariant(targetComponent);
if (targetComponent.intrinsicType) return undefined;
return (
<>
<Divider />
<MenuItem
onClick={() => {
invariant(targetNode);
componentEditorState.closeContextMenu();
onEditComponent(targetNode.componentId);
}}
>
Edit...
</MenuItem>
</>
);
})()}
</MenuList>
</ClickAwayListener>
);
}