Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"debounce": "^2.2.0",
"es-toolkit": "^1.52.0",
"eventemitter3": "^5.0.4",
"memoize-one": "^6.0.0",
"mnemonist": "^0.40.3",
"nullthrows": "^1.1.1",
"react": "^19.1.1",
Expand Down
62 changes: 25 additions & 37 deletions src/pages/edit/Editor/components/ContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export default function CCComponentEditorContextMenu({

if (!componentEditorState.contextMenuState) return null;

// The selection can still hold objects removed since, e.g. along with a node pin
const selectedNodeIds = [...componentEditorState.selectedNodeIds].filter(
(nodeId) => store.nodes.get(nodeId),
);
const selectedConnectionIds = [
...componentEditorState.selectedConnectionIds,
].filter((connectionId) => store.connections.get(connectionId));

return (
<ClickAwayListener onClickAway={componentEditorState.closeContextMenu}>
<MenuList
Expand All @@ -50,23 +58,15 @@ export default function CCComponentEditorContextMenu({
{/* <MenuItem onClick={componentEditorState.closeContextMenu}>
Create a node
</MenuItem> */}
{componentEditorState.selectedNodeIds.size > 0 && (
{selectedNodeIds.length > 0 && (
<MenuItem
onClick={() => {
const oldNodes = [...componentEditorState.selectedNodeIds].map(
(nodeId) => {
const node = store.nodes.get(nodeId);
invariant(node);
return node;
},
const oldNodes = selectedNodeIds.map((nodeId) =>
nullthrows(store.nodes.get(nodeId)),
);
const oldConnections = selectedConnectionIds.map((connectionId) =>
nullthrows(store.connections.get(connectionId)),
);
const oldConnections = [
...componentEditorState.selectedConnectionIds,
].map((connectionId) => {
const connection = store.connections.get(connectionId);
invariant(connection);
return connection;
});
const newComponent = CCComponentStore.create({
name: "New Component",
});
Expand Down Expand Up @@ -116,45 +116,33 @@ export default function CCComponentEditorContextMenu({
store.connections.register(connection, {
allowBitWidthConflict: true,
});
store.connections.unregister([
...componentEditorState.selectedConnectionIds,
]);
store.nodes.unregister([...componentEditorState.selectedNodeIds]);
store.connections.unregister(selectedConnectionIds);
store.nodes.unregister(selectedNodeIds);
componentEditorState.closeContextMenu();
onEditComponent(newComponent.id);
}}
>
Create a new component...
</MenuItem>
)}
{(componentEditorState.selectedNodeIds.size > 0 ||
componentEditorState.selectedConnectionIds.size > 0) && (
{(selectedNodeIds.length > 0 || selectedConnectionIds.length > 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);
if (selectedNodeIds.length > 0)
store.nodes.unregister(selectedNodeIds);
if (selectedConnectionIds.length > 0)
store.connections.unregister(selectedConnectionIds);
componentEditorState.selectConnection([], true);
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 [targetNodeId] = selectedNodeIds;
if (selectedNodeIds.length !== 1 || !targetNodeId) return undefined;
const targetNode = nullthrows(store.nodes.get(targetNodeId));
const targetComponent = store.components.get(targetNode.componentId);
invariant(targetComponent);
if (targetComponent.intrinsicType) return undefined;
Expand Down
133 changes: 66 additions & 67 deletions src/pages/edit/Editor/renderer/Connection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Vector2 } from "../../../../../common/vector2";
import type { CCComponentPinType } from "../../../../../store/componentPin";
import type { CCConnectionId } from "../../../../../store/connection";
import { useStore } from "../../../../../store/react";
import ensureStoreItem from "../../../../../store/react/error";
import {
useIsConnectionInBitWidthConflict,
useNode,
Expand Down Expand Up @@ -85,7 +84,9 @@ export function CCComponentEditorRendererConnectionCore({
}
e.preventDefault();
e.stopPropagation();
componentEditorState.selectConnection([connectionId], true);
if (!componentEditorState.selectedConnectionIds.has(connectionId)) {
componentEditorState.selectConnection([connectionId], true);
}
componentEditorState.openContextMenu(e);
}}
id={connectionId}
Expand All @@ -98,73 +99,71 @@ export function CCComponentEditorRendererConnectionCore({
export type CCComponentEditorRendererConnectionProps = {
connectionId: CCConnectionId;
};
const CCComponentEditorRendererConnection = ensureStoreItem(
(props, store) => store.connections.get(props.connectionId),
({ connectionId }: CCComponentEditorRendererConnectionProps) => {
const { store } = useStore();
const componentEditorState = useComponentEditorStore()();
const connection = nullthrows(store.connections.get(connectionId));
const fromNodePin = nullthrows(store.nodePins.get(connection.from));
const fromComponentPin = nullthrows(
store.componentPins.get(fromNodePin.componentPinId),
);
const toNodePin = nullthrows(store.nodePins.get(connection.to));
const toComponentPin = nullthrows(
store.componentPins.get(toNodePin.componentPinId),
);
const fromNode = useNode(fromNodePin.nodeId);
const toNode = useNode(toNodePin.nodeId);
const fromNodeGeometry = getCCComponentEditorRendererNodeGeometry(
export default function CCComponentEditorRendererConnection({
connectionId,
}: CCComponentEditorRendererConnectionProps) {
const { store } = useStore();
const componentEditorState = useComponentEditorStore()();
const connection = store.connections.get(connectionId);
const fromNodePin = connection && store.nodePins.get(connection.from);
const toNodePin = connection && store.nodePins.get(connection.to);
const fromNode = useNode(fromNodePin?.nodeId);
const toNode = useNode(toNodePin?.nodeId);
const isInBitWidthConflict = useIsConnectionInBitWidthConflict(connectionId);
const [isHovered, setIsHovered] = useState(false);

if (!connection || !fromNodePin || !toNodePin || !fromNode || !toNode) {
return null;
}
const fromComponentPin = nullthrows(
store.componentPins.get(fromNodePin.componentPinId),
);
const toComponentPin = nullthrows(
store.componentPins.get(toNodePin.componentPinId),
);
const fromNodePinPosition = nullthrows(
getCCComponentEditorRendererNodeGeometry(
store,
fromNode.id,
);
const toNodeGeometry = getCCComponentEditorRendererNodeGeometry(
).nodePinPositionById.get(fromNodePin.id),
);
const toNodePinPosition = nullthrows(
getCCComponentEditorRendererNodeGeometry(
store,
toNode.id,
);
const fromNodePinPosition = nullthrows(
fromNodeGeometry.nodePinPositionById.get(fromNodePin.id),
);
const toNodePinPosition = nullthrows(
toNodeGeometry.nodePinPositionById.get(toNodePin.id),
);

const [isHovered, setIsHovered] = useState(false);
const isInBitWidthConflict =
useIsConnectionInBitWidthConflict(connectionId);
).nodePinPositionById.get(toNodePin.id),
);

return (
<>
<CCComponentEditorRendererConnectionCore
from={{
direction: fromComponentPin.type,
position: fromNodePinPosition,
}}
to={{ direction: toComponentPin.type, position: toNodePinPosition }}
connectionId={connectionId}
isInBitWidthConflict={isInBitWidthConflict}
onMouseEnter={() => {
setIsHovered(true);
}}
onMouseLeave={() => {
setIsHovered(false);
}}
/>
{isHovered && componentEditorState.editorMode === "play" && (
<text fontSize={10} fill={theme.colors.textPrimary}>
<textPath
href={`#${connectionId}`}
startOffset="50%"
textAnchor="middle"
>
{stringifySimulationValue(
nullthrows(componentEditorState.getNodePinValue(toNodePin.id)),
)}
</textPath>
</text>
)}
</>
);
},
);
export default CCComponentEditorRendererConnection;
return (
<>
<CCComponentEditorRendererConnectionCore
from={{
direction: fromComponentPin.type,
position: fromNodePinPosition,
}}
to={{ direction: toComponentPin.type, position: toNodePinPosition }}
connectionId={connectionId}
isInBitWidthConflict={isInBitWidthConflict}
onMouseEnter={() => {
setIsHovered(true);
}}
onMouseLeave={() => {
setIsHovered(false);
}}
/>
{isHovered && componentEditorState.editorMode === "play" && (
<text fontSize={10} fill={theme.colors.textPrimary}>
<textPath
href={`#${connectionId}`}
startOffset="50%"
textAnchor="middle"
>
{stringifySimulationValue(
nullthrows(componentEditorState.getNodePinValue(toNodePin.id)),
)}
</textPath>
</text>
)}
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ export function CCComponentEditorRendererNodeInterfaceRenderer(
const { store } = useStore();
const editorState = useComponentEditorStore()();

const nodePin = nullthrows(
store.nodePins.getManyByNodeId(props.node.id)[0],
`Interface node ${props.node.id} is missing its pin`,
);
const nodePin = store.nodePins.getManyByNodeId(props.node.id)[0];
const componentPin = useComponentPin(
nullthrows(store.componentPins.getByImplementation(nodePin.id)).id,
nodePin && store.componentPins.getByImplementation(nodePin.id)?.id,
);
if (!nodePin || !componentPin) return null;
const isEditMode = editorState.editorMode === "edit";

const simulation = isEditMode
Expand Down
Loading
Loading