Skip to content

Commit 444a04c

Browse files
committed
feat(ports): updateNodePortName + interface inherits parent port names (#289)
1 parent 9d35919 commit 444a04c

4 files changed

Lines changed: 52 additions & 5 deletions

File tree

src/lib/stores/graph/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ export function deriveInterfaceNode(
6969
outputs: parentSubsystem.inputs.map((port, i) => ({
7070
id: `${interfaceNode.id}-output-${i}`,
7171
nodeId: interfaceNode.id,
72-
name: `in ${i}`,
72+
name: port.name,
7373
direction: 'output' as const,
7474
index: i,
7575
color: port.color
7676
})),
7777
inputs: parentSubsystem.outputs.map((port, i) => ({
7878
id: `${interfaceNode.id}-input-${i}`,
7979
nodeId: interfaceNode.id,
80-
name: `out ${i}`,
80+
name: port.name,
8181
direction: 'input' as const,
8282
index: i,
8383
color: port.color

src/lib/stores/graph/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export const graphStore = {
8484
removeInputPort: ports.removeInputPort,
8585
addOutputPort: ports.addOutputPort,
8686
removeOutputPort: ports.removeOutputPort,
87+
updateNodePortName: ports.updateNodePortName,
8788

8889
// ==================== ANNOTATION OPERATIONS ====================
8990
addAnnotation: annotations.addAnnotation,

src/lib/stores/graph/ports.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,47 @@ export function syncPortNamesFromLabels(
331331
updateNodeById(nodeId, () => updated);
332332
}
333333
}
334+
335+
/**
336+
* Rename a single port. For regular nodes this writes the new name into
337+
* `node.{inputs|outputs}[index].name`. For Interface nodes, port names are
338+
* derived from the parent Subsystem at read time, so we write to the parent
339+
* instead (with the direction flipped, because Interface input ↔ Subsystem
340+
* output).
341+
*/
342+
export function updateNodePortName(
343+
nodeId: string,
344+
direction: PortDirection,
345+
index: number,
346+
name: string
347+
): void {
348+
const currentGraph = getCurrentGraph();
349+
const node = currentGraph.nodes.get(nodeId);
350+
if (!node) return;
351+
352+
const path = get(currentPath);
353+
354+
if (node.type === NODE_TYPES.INTERFACE && path.length > 0) {
355+
const parentId = path[path.length - 1];
356+
const parentPath = path.slice(0, -1);
357+
const parentPortsKey = direction === 'input' ? 'outputs' : 'inputs';
358+
359+
updateParentSubsystem(parentPath, parentId, (parent) => {
360+
const ports = parent[parentPortsKey] as PortInstance[];
361+
if (index < 0 || index >= ports.length) return parent;
362+
if (ports[index].name === name) return parent;
363+
const newPorts = ports.map((p, i) => (i === index ? { ...p, name } : p));
364+
return { ...parent, [parentPortsKey]: newPorts };
365+
});
366+
return;
367+
}
368+
369+
updateNodeById(nodeId, (n) => {
370+
const portsKey = direction === 'input' ? 'inputs' : 'outputs';
371+
const ports = n[portsKey] as PortInstance[];
372+
if (index < 0 || index >= ports.length) return n;
373+
if (ports[index].name === name) return n;
374+
const newPorts = ports.map((p, i) => (i === index ? { ...p, name } : p));
375+
return { ...n, [portsKey]: newPorts };
376+
});
377+
}

src/lib/stores/graph/state.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ export function getCurrentGraph(): {
9595
...node,
9696
name: subsystem.name,
9797
color: subsystem.color,
98-
// Subsystem inputs → Interface outputs (signals coming in)
98+
// Subsystem inputs → Interface outputs (signals coming in).
99+
// Port names come from the parent — that's the single source
100+
// of truth so user-customised labels survive across renders.
99101
outputs: subsystem.inputs.map((port, i) => ({
100102
id: `${node.id}-output-${i}`,
101103
nodeId: node.id,
102-
name: `in ${i}`,
104+
name: port.name,
103105
direction: 'output' as const,
104106
index: i,
105107
color: port.color
@@ -108,7 +110,7 @@ export function getCurrentGraph(): {
108110
inputs: subsystem.outputs.map((port, i) => ({
109111
id: `${node.id}-input-${i}`,
110112
nodeId: node.id,
111-
name: `out ${i}`,
113+
name: port.name,
112114
direction: 'input' as const,
113115
index: i,
114116
color: port.color

0 commit comments

Comments
 (0)