-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNode.js
164 lines (140 loc) · 4.59 KB
/
Node.js
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
// Node.js
import { memo, useCallback, useState, useEffect, useRef } from 'react';
import NodeLayout from './NodeLayout';
import { useGraphManager } from './GraphManager';
// Helper functions to remove references
export const removePrevs = (nodes, nodeId) => {
const nodeToRemovePrevs = nodes.find(node => node.id === nodeId);
if (!nodeToRemovePrevs) return nodes;
try {
nodeToRemovePrevs.prevs.forEach(prevId => {
const prevNode = nodes.find(node => node.id === prevId);
if (prevNode) {
prevNode.data.nexts = prevNode.data.nexts.filter(id => id !== nodeId);
if (prevNode.data.true_next === nodeId) prevNode.data.true_next = null;
if (prevNode.data.false_next === nodeId) prevNode.data.false_next = null;
}
});
nodeToRemovePrevs.prevs = [];
}
catch (error) {
// workaround if a node have no prevs
}
return nodes;
};
export const removeTrueFalse = (nodes, nodeId) => {
const nodeToRemoveTrueFalse = nodes.find(node => node.id === nodeId);
if (!nodeToRemoveTrueFalse) return nodes;
const trueNextId = nodeToRemoveTrueFalse.data.true_next;
if (trueNextId) {
const trueNextNode = nodes.find(node => node.id === trueNextId);
if (trueNextNode) {
trueNextNode.prevs = trueNextNode.prevs.filter(id => id !== nodeId);
}
}
const falseNextId = nodeToRemoveTrueFalse.data.false_next;
if (falseNextId) {
const falseNextNode = nodes.find(node => node.id === falseNextId);
if (falseNextNode) {
falseNextNode.prevs = falseNextNode.prevs.filter(id => id !== nodeId);
}
}
nodeToRemoveTrueFalse.data.true_next = null;
nodeToRemoveTrueFalse.data.false_next = null;
return nodes;
};
export const removeNexts = (nodes, nodeId) => {
const nodeToRemoveNexts = nodes.find(node => node.id === nodeId);
if (!nodeToRemoveNexts) return nodes;
nodeToRemoveNexts.data.nexts.forEach(nextId => {
const nextNode = nodes.find(node => node.id === nextId);
if (nextNode) {
nextNode.prevs = nextNode.prevs.filter(id => id !== nodeId);
}
});
nodeToRemoveNexts.data.nexts = [];
return nodes;
};
function Node({ data, isConnectable, id, prevs }) {
const {
setNodes,
} = useGraphManager();
const [nodeData, setNodeData] = useState(data);
const changeBuffer = useRef({});
useEffect(() => {
setNodeData(data);
}, [data]);
const handleChange = useCallback((event) => {
const name = event.target.name;
const value = event.target.value;
const isComposingEvent = event.nativeEvent.isComposing;
if (isComposingEvent) {
changeBuffer.current = { ...changeBuffer.current, [name]: value };
}
else {
updateNodeData((prevData) => ({ ...prevData, ...changeBuffer.current, [name]: value }));
changeBuffer.current = {};
}
}, [id, setNodes]);
const updateNodeData = (updateFn) => {
setNodes((nds) => {
return nds.map((node) => {
if (node.id === id) {
return {
...node,
data: updateFn(node.data),
};
}
return node;
});
});
};
const onResize = useCallback((width, height) => {
updateNodeData((prevData) => ({ ...prevData, width, height }));
}, [id, setNodes]);
return (
<NodeLayout
data={nodeData}
isConnectable={isConnectable}
handleChange={handleChange}
onResize={onResize}
prevs={prevs}
/>
);
}
export const addNode = (nodes, setNodes, nodeIdCounter, setNodeIdCounter, newPosition) => {
const newNode = {
id: nodeIdCounter.toString(),
type: 'textUpdater',
data: {
name: `Node ${nodeIdCounter}`,
description: '',
type: 'STEP',
ext: { info: '' }, // Initialize ext.info for new nodes
nexts: [],
true_next: null,
false_next: null,
width: 200,
height: 200
},
position: newPosition,
prevs: []
};
setNodes((nds) => nds.concat(newNode));
setNodeIdCounter(nodeIdCounter + 1);
};
export const deleteNode = (nodes, setNodes, edges, setEdges, nodeId) => {
let updatedNodes = [...nodes];
const nodeToDelete = updatedNodes.find((node) => node.id === nodeId);
if (!nodeToDelete) return;
// Use helper functions to clean up references
updatedNodes = removePrevs(updatedNodes, nodeId);
updatedNodes = removeTrueFalse(updatedNodes, nodeId);
updatedNodes = removeNexts(updatedNodes, nodeId);
// Remove the node itself
updatedNodes = updatedNodes.filter((node) => node.id !== nodeId);
setNodes(updatedNodes);
// Remove edges connected to this node
setEdges((eds) => eds.filter((edge) => edge.source !== nodeId && edge.target !== nodeId));
};
export default memo(Node);