11import React , { useState , useContext } from 'react' ;
22
33import { WorkflowPortRender } from '@flowgram.ai/free-layout-editor' ;
4+ import { useClientContext } from '@flowgram.ai/free-layout-editor' ;
45
56import { useNodeRenderContext } from '../../hooks' ;
67import { SidebarContext } from '../../context' ;
7- // import { scrollToView } from './utils'
8- // import { useClientContext } from '@flowgram.ai/free-layout-editor ';
8+ import { scrollToView } from './utils' ;
9+ import { NodeWrapperStyle } from './styles ' ;
910
1011export interface NodeWrapperProps {
12+ isScrollToView ?: boolean ;
1113 children : React . ReactNode ;
1214}
1315
@@ -16,15 +18,18 @@ export interface NodeWrapperProps {
1618 * 用于节点的拖拽/点击事件和点位渲染
1719 */
1820export const NodeWrapper : React . FC < NodeWrapperProps > = ( props ) => {
21+ const { children, isScrollToView = false } = props ;
1922 const nodeRender = useNodeRenderContext ( ) ;
2023 const { selected, startDrag, ports, selectNode, nodeRef, onFocus, onBlur } = nodeRender ;
2124 const [ isDragging , setIsDragging ] = useState ( false ) ;
2225 const sidebar = useContext ( SidebarContext ) ;
23- // const ctx = useClientContext()
26+ const form = nodeRender . form ;
27+ const ctx = useClientContext ( ) ;
2428
2529 return (
2630 < >
27- < div
31+ < NodeWrapperStyle
32+ className = { selected ? 'selected' : '' }
2833 ref = { nodeRef }
2934 draggable
3035 onDragStart = { ( e ) => {
@@ -35,18 +40,23 @@ export const NodeWrapper: React.FC<NodeWrapperProps> = (props) => {
3540 selectNode ( e ) ;
3641 if ( ! isDragging ) {
3742 sidebar . setNodeRender ( nodeRender ) ;
38- // 可选:如果需要让节点滚动到画布中间加上这个
39- // Optional: Add this if you want the node to scroll to the middle of the canvas
40- // scrollToView(ctx, nodeRender.node)
43+ // 可选:将 isScrollToView 设为 true,可以让节点选中后滚动到画布中间
44+ // Optional: Set isScrollToView to true to scroll the node to the center of the canvas after it is selected.
45+ if ( isScrollToView ) {
46+ scrollToView ( ctx , nodeRender . node ) ;
47+ }
4148 }
4249 } }
4350 onMouseUp = { ( ) => setIsDragging ( false ) }
4451 onFocus = { onFocus }
4552 onBlur = { onBlur }
4653 data-node-selected = { String ( selected ) }
54+ style = { {
55+ outline : form ?. state . invalid ? '1px solid red' : 'none' ,
56+ } }
4757 >
48- { props . children }
49- </ div >
58+ { children }
59+ </ NodeWrapperStyle >
5060 { ports . map ( ( p ) => (
5161 < WorkflowPortRender key = { p . id } entity = { p } />
5262 ) ) }
0 commit comments