Skip to content

Commit db4aa2f

Browse files
committed
improvements to ocif import/export roundtrip
1 parent 0f95e8b commit db4aa2f

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

apps/vps-web/public/test.ocif.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"position": [100, 100],
77
"resource": "r1"
88
},
9+
910
{
1011
"id": "n2",
1112
"position": [100, 100],
@@ -15,6 +16,19 @@
1516
"radius": 50
1617
}
1718
]
19+
},
20+
{
21+
"id": "n3",
22+
"position": [200, 200],
23+
"resource": "r1",
24+
"data": [
25+
{
26+
"type": "rect-node",
27+
"strokeColor": "red",
28+
"strokeWidth": 2,
29+
"fillColor": "darkred"
30+
}
31+
]
1832
}
1933
],
2034
"resources": [

libs/app-canvas/src/app/exporters/export-ocwg.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ export class OCWGExporter extends BaseExporter<OCWGFile, OCWGInfo> {
3333
return structuredClone(ocwgEmptyFile);
3434
}
3535

36+
isOCIFNodeThatCodeFlowCanvasSupports(node: any): boolean {
37+
if (node.data && Array.isArray(node.data)) {
38+
const result = node.data.some((d: any) => d.type === 'rect-node');
39+
if (result) {
40+
return true;
41+
}
42+
}
43+
if (!node.data) {
44+
return true;
45+
}
46+
if (node.data && Array.isArray(node.data)) {
47+
return node.data.some(
48+
(d: any) =>
49+
d.type === nodeInfoPropertyName ||
50+
d.type === connectionNodeInfoPropertyName
51+
);
52+
}
53+
return false;
54+
}
55+
3656
isValidCodeFlowCanvasNode(node: any): boolean {
3757
if (node.data && Array.isArray(node.data)) {
3858
return node.data.some(
@@ -64,6 +84,30 @@ export class OCWGExporter extends BaseExporter<OCWGFile, OCWGInfo> {
6484
if (rootOCIF.nodes) {
6585
rootOCIF.nodes.forEach((node: any) => {
6686
if (
87+
this.isOCIFNodeThatCodeFlowCanvasSupports(node) &&
88+
this.doesRootOCIFNodeExistInFlow(node.id, elements)
89+
) {
90+
const codeFlowCanvasNode = this.file?.nodes.find(
91+
(n) => n.id === node.id
92+
);
93+
if (codeFlowCanvasNode && codeFlowCanvasNode.data) {
94+
node.data?.forEach((d: any) => {
95+
if (!codeFlowCanvasNode.data) {
96+
return;
97+
}
98+
if (
99+
d.type !== nodeInfoPropertyName &&
100+
d.type !== connectionNodeInfoPropertyName
101+
) {
102+
codeFlowCanvasNode.data.push(d);
103+
}
104+
});
105+
}
106+
107+
if (node.resource && codeFlowCanvasNode) {
108+
codeFlowCanvasNode.resource = node.resource;
109+
}
110+
} else if (
67111
!this.isValidCodeFlowCanvasNode(node) &&
68112
!this.doesRootOCIFNodeExistInFlow(node.id, elements)
69113
) {

libs/app-canvas/src/app/exporters/ocwg/ocwg-schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type OCWGNode = {
55
data?: {
66
[key: string]: any;
77
}[];
8+
resource?: string;
89
};
910

1011
export type OCWGEdge = {

libs/app-canvas/src/app/importers/ocif-importer.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import {
77

88
let rootOCIF: any = undefined;
99

10+
function isSupportedOCIFNode(ocifNode: any): boolean {
11+
return (
12+
ocifNode.data &&
13+
Array.isArray(ocifNode.data) &&
14+
ocifNode.data.some((d: any) => d.type === 'rect-node')
15+
);
16+
}
17+
1018
function isValidCodeFlowCanvasNode(node: any): boolean {
1119
if (node.data && Array.isArray(node.data)) {
1220
return node.data.some((d: any) => d.type === nodeInfoPropertyName);
@@ -25,10 +33,11 @@ function isValidCodeFlowCanvasConnection(node: any): boolean {
2533
function getNodeInfoFromOCIFNode(node: any): NodeInfo | false {
2634
const nodeInfo = node.data.find((d: any) => d.type === nodeInfoPropertyName);
2735
if (nodeInfo) {
28-
delete nodeInfo.type;
29-
nodeInfo.type = nodeInfo.nodeType;
30-
delete nodeInfo.nodeType;
31-
return nodeInfo;
36+
const clonedNodeInfo = structuredClone(nodeInfo);
37+
delete clonedNodeInfo.type;
38+
clonedNodeInfo.type = clonedNodeInfo.nodeType;
39+
delete clonedNodeInfo.nodeType;
40+
return clonedNodeInfo;
3241
}
3342
return false;
3443
}
@@ -38,16 +47,17 @@ function getConnectionInfoFromOCIFNode(node: any): any | false {
3847
(d: any) => d.type === connectionNodeInfoPropertyName
3948
);
4049
if (nodeInfo) {
41-
delete nodeInfo.type;
42-
nodeInfo.type = nodeInfo.nodeType;
43-
delete nodeInfo.nodeType;
50+
const clonedNodeInfo = structuredClone(nodeInfo);
51+
delete clonedNodeInfo.type;
52+
clonedNodeInfo.type = clonedNodeInfo.nodeType;
53+
delete clonedNodeInfo.nodeType;
4454
return {
4555
endX: node.position[0],
4656
endY: node.position[1],
47-
startNodeId: nodeInfo.start.connected_to,
48-
endNodeId: nodeInfo.end.connected_to,
49-
startThumbName: nodeInfo.start.port_name,
50-
endThumbName: nodeInfo.end.port_name,
57+
startNodeId: clonedNodeInfo.start.connected_to,
58+
endNodeId: clonedNodeInfo.end.connected_to,
59+
startThumbName: clonedNodeInfo.start.port_name,
60+
endThumbName: clonedNodeInfo.end.port_name,
5161
lineType: 'BezierCubic',
5262
layer: 1,
5363
nodeInfo: {},
@@ -94,6 +104,16 @@ export const importOCIF = (ocif: any) => {
94104
...connection,
95105
});
96106
}
107+
} else if (node.data && isSupportedOCIFNode(node)) {
108+
flow.flows['flow'].nodes.push({
109+
id: node.id,
110+
x: node.position[0],
111+
y: node.position[1],
112+
nodeType: 'Shape',
113+
nodeInfo: {
114+
type: 'expression',
115+
},
116+
});
97117
} else if (!node.data) {
98118
flow.flows['flow'].nodes.push({
99119
id: node.id,

0 commit comments

Comments
 (0)