| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   memo, | 
					
						
							|  |  |  |   useMemo, | 
					
						
							|  |  |  | } from 'react' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { NodeProps } from 'reactflow' | 
					
						
							|  |  |  | import type { Node } from '../types' | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | import { CUSTOM_NODE } from '../constants' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   NodeComponentMap, | 
					
						
							|  |  |  |   PanelComponentMap, | 
					
						
							|  |  |  | } from './constants' | 
					
						
							|  |  |  | import BaseNode from './_base/node' | 
					
						
							|  |  |  | import BasePanel from './_base/panel' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | const CustomNode = (props: NodeProps) => { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const nodeData = props.data | 
					
						
							|  |  |  |   const NodeComponent = NodeComponentMap[nodeData.type] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |     <> | 
					
						
							|  |  |  |       <BaseNode { ...props }> | 
					
						
							|  |  |  |         <NodeComponent /> | 
					
						
							|  |  |  |       </BaseNode> | 
					
						
							|  |  |  |     </> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   ) | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | CustomNode.displayName = 'CustomNode' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const Panel = memo((props: Node) => { | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  |   const nodeClass = props.type | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const nodeData = props.data | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  |   const PanelComponent = useMemo(() => { | 
					
						
							|  |  |  |     if (nodeClass === CUSTOM_NODE) | 
					
						
							|  |  |  |       return PanelComponentMap[nodeData.type] | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  |     return () => null | 
					
						
							|  |  |  |   }, [nodeClass, nodeData.type]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (nodeClass === CUSTOM_NODE) { | 
					
						
							|  |  |  |     return ( | 
					
						
							|  |  |  |       <BasePanel key={props.id} {...props}> | 
					
						
							|  |  |  |         <PanelComponent /> | 
					
						
							|  |  |  |       </BasePanel> | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return null | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Panel.displayName = 'Panel' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default memo(CustomNode) |