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