|
|
@@ -1,13 +1,15 @@
|
|
|
import React, { useState, useContext } from 'react';
|
|
|
|
|
|
import { WorkflowPortRender } from '@flowgram.ai/free-layout-editor';
|
|
|
+import { useClientContext } from '@flowgram.ai/free-layout-editor';
|
|
|
|
|
|
import { useNodeRenderContext } from '../../hooks';
|
|
|
import { SidebarContext } from '../../context';
|
|
|
-// import { scrollToView } from './utils'
|
|
|
-// import { useClientContext } from '@flowgram.ai/free-layout-editor';
|
|
|
+import { scrollToView } from './utils';
|
|
|
+import { NodeWrapperStyle } from './styles';
|
|
|
|
|
|
export interface NodeWrapperProps {
|
|
|
+ isScrollToView?: boolean;
|
|
|
children: React.ReactNode;
|
|
|
}
|
|
|
|
|
|
@@ -16,15 +18,18 @@ export interface NodeWrapperProps {
|
|
|
* 用于节点的拖拽/点击事件和点位渲染
|
|
|
*/
|
|
|
export const NodeWrapper: React.FC<NodeWrapperProps> = (props) => {
|
|
|
+ const { children, isScrollToView = false } = props;
|
|
|
const nodeRender = useNodeRenderContext();
|
|
|
const { selected, startDrag, ports, selectNode, nodeRef, onFocus, onBlur } = nodeRender;
|
|
|
const [isDragging, setIsDragging] = useState(false);
|
|
|
const sidebar = useContext(SidebarContext);
|
|
|
- // const ctx = useClientContext()
|
|
|
+ const form = nodeRender.form;
|
|
|
+ const ctx = useClientContext();
|
|
|
|
|
|
return (
|
|
|
<>
|
|
|
- <div
|
|
|
+ <NodeWrapperStyle
|
|
|
+ className={selected ? 'selected' : ''}
|
|
|
ref={nodeRef}
|
|
|
draggable
|
|
|
onDragStart={(e) => {
|
|
|
@@ -35,18 +40,23 @@ export const NodeWrapper: React.FC<NodeWrapperProps> = (props) => {
|
|
|
selectNode(e);
|
|
|
if (!isDragging) {
|
|
|
sidebar.setNodeRender(nodeRender);
|
|
|
- // 可选:如果需要让节点滚动到画布中间加上这个
|
|
|
- // Optional: Add this if you want the node to scroll to the middle of the canvas
|
|
|
- // scrollToView(ctx, nodeRender.node)
|
|
|
+ // 可选:将 isScrollToView 设为 true,可以让节点选中后滚动到画布中间
|
|
|
+ // Optional: Set isScrollToView to true to scroll the node to the center of the canvas after it is selected.
|
|
|
+ if (isScrollToView) {
|
|
|
+ scrollToView(ctx, nodeRender.node);
|
|
|
+ }
|
|
|
}
|
|
|
}}
|
|
|
onMouseUp={() => setIsDragging(false)}
|
|
|
onFocus={onFocus}
|
|
|
onBlur={onBlur}
|
|
|
data-node-selected={String(selected)}
|
|
|
+ style={{
|
|
|
+ outline: form?.state.invalid ? '1px solid red' : 'none',
|
|
|
+ }}
|
|
|
>
|
|
|
- {props.children}
|
|
|
- </div>
|
|
|
+ {children}
|
|
|
+ </NodeWrapperStyle>
|
|
|
{ports.map((p) => (
|
|
|
<WorkflowPortRender key={p.id} entity={p} />
|
|
|
))}
|