-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNodeData.ts
94 lines (84 loc) · 2.38 KB
/
NodeData.ts
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
// NodeData.ts
export interface ReactFlowNodeEXT {
type: string;
name?: string | undefined;
description?: string | undefined;
tool?: string | undefined;
prevs?: string[];
nexts?: string[];
true_next?: string | null | undefined;
false_next?: string | null | undefined;
info?: string | null;
}
export interface ReactNodeProps {
id: string;
width: number;
height: number;
position: { x: number, y: number }
data: ReactFlowNodeEXT;
onNodeDataChange?: (id: string, newData: ReactFlowNodeEXT) => void;
}
export interface JsonNodeData {
uniq_id: string;
name: string;
description: string;
nexts: string[];
type?: string;
tool: string;
true_next: string | null;
false_next: string | null;
ext: {
pos_x?: number;
pos_y?: number;
width?: number;
height?: number;
info?: string | null;
};
}
export const JsonToReactNode = (jsonData: JsonNodeData, position?: { x: number, y: number }): ReactNodeProps => {
const { uniq_id, ext, ...rest } = jsonData;
const reactNodeData: ReactFlowNodeEXT = {
type: rest.type || "STEP",
name: rest.name,
description: rest.description,
tool: rest.tool,
nexts: rest.nexts || [],
true_next: rest.true_next,
false_next: rest.false_next,
info: ext?.info ?? null,
prevs: [],
};
return {
id: uniq_id,
width: ext?.width ?? 200,
height: ext?.height ?? 200,
position: position ||
{
x: ext?.pos_x || 0,
y: ext?.pos_y || 0
},
data: reactNodeData,
};
};
export const ReactToJsonNode = (reactNode: ReactNodeProps): JsonNodeData => {
const { id, data, width, height, position } = reactNode;
const { type, name, description, tool, nexts, true_next, false_next, info } = data;
const ext: JsonNodeData['ext'] = {
pos_x: position.x,
pos_y: position.y,
width,
height,
info: info === undefined ? null : info // Set info to null if undefined
};
return {
uniq_id: id,
type,
name: name || "",
description: description || "",
tool: tool || "",
nexts: nexts || [],
true_next: true_next == undefined ? null : true_next,
false_next: false_next == undefined ? null : false_next,
ext
};
};