-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodePinPropertyEditor.tsx
More file actions
204 lines (197 loc) · 5.89 KB
/
NodePinPropertyEditor.tsx
File metadata and controls
204 lines (197 loc) · 5.89 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { Button, Popover, Stack, TextField, Typography } from "@mui/material";
import { zip } from "es-toolkit";
import nullthrows from "nullthrows";
import { useState } from "react";
import invariant from "tiny-invariant";
import { rect } from "../../../../common/rect";
import { IntrinsicComponentDefinition } from "../../../../store/intrinsics/base";
import { CCNodePinStore } from "../../../../store/nodePin";
import { useStore } from "../../../../store/react";
import getCCComponentEditorRendererNodeGeometry from "../renderer/Node.geometry";
import { useComponentEditorStore } from "../store";
export function CCComponentEditorNodePinPropertyEditor() {
const { store } = useStore();
const componentEditorStore = useComponentEditorStore();
const target = componentEditorStore((s) => s.nodePinPropertyEditorTarget);
const setTarget = componentEditorStore(
(s) => s.setNodePinPropertyEditorTarget,
);
const [newBitWidthList, setNewBitWidthList] = useState<number[] | null>(null); // null means no change
if (!target) return null;
const componentPin = nullthrows(
store.componentPins.get(target.componentPinId),
);
const component = nullthrows(store.components.get(componentPin.componentId));
const nodePins = store.nodePins
.getManyByNodeIdAndComponentPinId(target.nodeId, target.componentPinId)
.toSorted((a, b) => a.order - b.order);
invariant(
nodePins.every((p) => p.manualBitWidth !== null),
"NodePinPropertyEditor can only be used for node pins with user specified bit width",
);
const componentPinAttributes = nullthrows(
IntrinsicComponentDefinition.intrinsicComponentPinAttributesByComponentPinId.get(
target.componentPinId,
),
"NodePinPropertyEditor can only be used for intrinsic component pins",
);
const getBoundingClientRect = (): DOMRect => {
const geometry = getCCComponentEditorRendererNodeGeometry(
store,
target.nodeId,
);
const nodePinCanvasPositions = nodePins.map((nodePin) =>
componentEditorStore
.getState()
.fromStageToCanvas(
nullthrows(geometry.nodePinPositionById.get(nodePin.id)),
),
);
const bounds = rect.shift(
rect.bounds(nodePinCanvasPositions),
componentEditorStore.getState().getRendererPosition(),
);
return new DOMRect(
bounds.position.x,
bounds.position.y,
bounds.size.x,
bounds.size.y,
);
};
const bitWidthList =
newBitWidthList ??
nodePins.map((nodePin) => nullthrows(nodePin.manualBitWidth));
const isTouched = Boolean(newBitWidthList);
const isValid = bitWidthList.every((bitWidth) => bitWidth > 0);
const onClose = () => {
setTarget(null);
setNewBitWidthList(null);
};
return (
<Popover
open
anchorEl={{ nodeType: 1, getBoundingClientRect }}
transformOrigin={{
vertical: "center",
horizontal: componentPin.type === "input" ? "right" : "left",
}}
slotProps={{ paper: { sx: { p: 2, width: 250 } } }}
onClose={onClose}
>
<Typography variant="h6">
{componentPin.name} ({component.name})
</Typography>
<Typography variant="caption" color="text.secondary">
Specify the bit width to assign to the pin.
</Typography>
<form
onSubmit={(e) => {
e.preventDefault();
let maxOrder = 0;
for (const [nodePin, bitWidth] of zip(nodePins, bitWidthList)) {
// Create new NodePin
if (!nodePin && bitWidth) {
store.nodePins.register(
CCNodePinStore.create({
componentPinId: target.componentPinId,
nodeId: target.nodeId,
order: ++maxOrder,
manualBitWidth: bitWidth,
}),
);
continue;
}
// Delete old NodePin
if (nodePin && !bitWidth) {
store.nodePins.unregister(nodePin.id);
continue;
}
// Update NodePin
if (nodePin && bitWidth) {
maxOrder = nodePin.order; // nodePins are sorted by order
if (nodePin.manualBitWidth !== bitWidth) {
store.nodePins.update(nodePin.id, {
manualBitWidth: bitWidth,
});
const connections = store.connections.getConnectionsByNodePinId(
nodePin.id,
);
for (const connection of connections) {
const anotherNodePinId =
connection.from === nodePin.id
? connection.to
: connection.from;
if (
!store.nodePins.isConnectable(nodePin.id, anotherNodePinId)
) {
store.connections.unregister([connection.id]);
}
}
}
continue;
}
throw new Error("Unreachable");
}
onClose();
}}
>
<Stack gap={0.5} sx={{ mt: 1 }}>
{bitWidthList.map((bitWidth, index) => {
return (
<TextField
// biome-ignore lint/suspicious/noArrayIndexKey: bitWidth is only identified by index in this component
key={index}
type="number"
value={bitWidth || ""}
slotProps={{ htmlInput: { min: 1 } }}
onChange={(e) => {
const newValue = Number.parseInt(e.target.value, 10);
if (newValue >= 0 || e.target.value === "")
setNewBitWidthList(bitWidthList.with(index, newValue || 0));
}}
error={bitWidth <= 0}
size="small"
/>
);
})}
</Stack>
<Stack direction="row" gap={1} sx={{ mt: 1 }}>
{componentPinAttributes.isSplittable && (
<>
<Button
type="button"
variant="outlined"
size="small"
onClick={() => {
setNewBitWidthList([...bitWidthList, 1]);
}}
>
Add
</Button>
<Button
type="button"
variant="outlined"
size="small"
disabled={bitWidthList.length <= 1}
onClick={() => {
setNewBitWidthList(bitWidthList.slice(0, -1));
}}
>
Remove
</Button>
</>
)}
<Button
type="submit"
variant="contained"
size="small"
sx={{ ml: "auto" }}
disabled={!isTouched || !isValid}
>
Apply
</Button>
</Stack>
</form>
</Popover>
);
}