Skip to content

Commit 7f730d1

Browse files
committed
successfully loaded a template
Signed-off-by: Teo Koon Peng <[email protected]>
1 parent 403bd70 commit 7f730d1

File tree

7 files changed

+244
-213
lines changed

7 files changed

+244
-213
lines changed

diagram-editor/frontend/diagram-editor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ function DiagramEditor() {
132132
throw new Error(`template ${newMode.templateId} not found`);
133133
}
134134
const graph = loadTemplate(template);
135+
const changes = autoLayout(graph.nodes, graph.edges, LAYOUT_OPTIONS);
135136
// using callback form so that `nodes` and `edges` don't need to be part of dependencies.
136137
setNodes((prev) => {
137138
savedNodes.current = [...prev];
138-
return graph.nodes;
139+
return applyNodeChanges(changes, graph.nodes);
139140
});
140141
setEdges((prev) => {
141142
savedEdges.current = [...prev];

diagram-editor/frontend/node-manager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ export class NodeManager {
2525
this.namespacedOpIdMap.set(namespacedOpId, node);
2626
} else if (isBuiltinNode(node)) {
2727
this.namespacedOpIdMap.set(node.id, node);
28+
} else if (node.type === 'sectionOutput') {
29+
// This will conflict if there is an op in a template with the same id as an output.
30+
// However, this conflict exist in bevy_impulse as well, so we assume that it will not happen.
31+
this.namespacedOpIdMap.set(
32+
joinNamespaces(ROOT_NAMESPACE, node.data.outputId),
33+
node,
34+
);
2835
}
2936
}
3037
}

diagram-editor/frontend/utils/create-node.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export function createStartNode(
1818
position: XYPosition,
1919
): DiagramEditorNode {
2020
return {
21+
// NOTE: atm `NodeManager` relies on how the id of builtin nodes is computed to locate them.
2122
id: joinNamespaces(namespace, START_ID),
2223
type: 'start',
2324
position,
@@ -31,6 +32,7 @@ export function createTerminateNode(
3132
position: XYPosition,
3233
): DiagramEditorNode {
3334
return {
35+
// NOTE: atm `NodeManager` relies on how the id of builtin nodes is computed to locate them.
3436
id: joinNamespaces(namespace, TERMINATE_ID),
3537
type: 'terminate',
3638
position,

diagram-editor/frontend/utils/load-diagram.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NodeManager } from '../node-manager';
33
import { type OperationNode, START_ID, TERMINATE_ID } from '../nodes';
44
import { autoLayout } from './auto-layout';
55
import { LAYOUT_OPTIONS } from './layout';
6-
import { loadDiagramJson } from './load-diagram';
6+
import { loadDiagramJson, loadTemplate } from './load-diagram';
77
import testDiagram from './test-data/test-diagram.json';
88

99
test('load diagram json and auto layout', () => {
@@ -61,3 +61,23 @@ test('load diagram json and auto layout', () => {
6161
expect(terminate!.position.x).toBe(join!.position.x);
6262
expect(terminate!.position.y).toBeGreaterThan(join!.position.y);
6363
});
64+
65+
test('load template', () => {
66+
const graph = loadTemplate({
67+
inputs: {
68+
remapped_input: 'target_input',
69+
},
70+
outputs: ['output'],
71+
buffers: {},
72+
ops: {
73+
target_input: {
74+
type: 'node',
75+
builder: 'add',
76+
next: 'output',
77+
},
78+
},
79+
});
80+
81+
expect(graph.nodes.length).toBe(3);
82+
expect(graph.edges.length).toBe(2);
83+
});

diagram-editor/frontend/utils/load-diagram.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export function loadEmpty(): Graph {
5151
};
5252
}
5353

54-
function buildGraph(diagram: Diagram): Graph {
55-
const graph = loadEmpty();
54+
function buildGraph(diagram: Diagram, initialGraph?: Graph): Graph {
55+
const graph = initialGraph ?? loadEmpty();
5656
const nodes = graph.nodes;
5757

5858
interface State {
@@ -117,7 +117,7 @@ function buildGraph(diagram: Diagram): Graph {
117117
data: {},
118118
});
119119
}
120-
edges.push(...buildEdges(diagram, graph.nodes));
120+
edges.push(...buildEdges(graph.nodes));
121121

122122
return graph;
123123
}
@@ -127,21 +127,16 @@ const validate = getSchema<Diagram>('Diagram');
127127
export function loadTemplate(template: SectionTemplate): Graph {
128128
const stubDiagram = exportDiagram(new NodeManager([]), [], {});
129129
stubDiagram.ops = template.ops;
130-
const graph = buildGraph(stubDiagram);
131-
132-
// filter out builtin START and TERMINATE.
133-
const nodes = graph.nodes.filter((node) => {
134-
!node.parentId && node.type in ['start', 'terminate'];
135-
});
130+
const initialNodes: DiagramEditorNode[] = [];
136131

137132
if (template.inputs) {
138133
if (Array.isArray(template.inputs)) {
139134
for (const input of template.inputs) {
140-
nodes.push(createSectionInputNode(input, input, { x: 0, y: 0 }));
135+
initialNodes.push(createSectionInputNode(input, input, { x: 0, y: 0 }));
141136
}
142137
} else {
143138
for (const [remappedId, targetId] of Object.entries(template.inputs)) {
144-
nodes.push(
139+
initialNodes.push(
145140
createSectionInputNode(remappedId, targetId, { x: 0, y: 0 }),
146141
);
147142
}
@@ -151,11 +146,13 @@ export function loadTemplate(template: SectionTemplate): Graph {
151146
if (template.buffers) {
152147
if (Array.isArray(template.buffers)) {
153148
for (const buffer of template.buffers) {
154-
nodes.push(createSectionBufferNode(buffer, buffer, { x: 0, y: 0 }));
149+
initialNodes.push(
150+
createSectionBufferNode(buffer, buffer, { x: 0, y: 0 }),
151+
);
155152
}
156153
} else {
157154
for (const [remappedId, targetId] of Object.entries(template.buffers)) {
158-
nodes.push(
155+
initialNodes.push(
159156
createSectionBufferNode(remappedId, targetId, { x: 0, y: 0 }),
160157
);
161158
}
@@ -164,9 +161,9 @@ export function loadTemplate(template: SectionTemplate): Graph {
164161

165162
if (template.outputs) {
166163
for (const output of template.outputs) {
167-
nodes.push(createSectionOutputNode(output, { x: 0, y: 0 }));
164+
initialNodes.push(createSectionOutputNode(output, { x: 0, y: 0 }));
168165
}
169166
}
170167

171-
return { nodes, edges: graph.edges };
168+
return buildGraph(stubDiagram, { nodes: initialNodes, edges: [] });
172169
}

0 commit comments

Comments
 (0)