Skip to content

Commit 25356df

Browse files
authored
fix(free-demo): drag node to container selected style (bytedance#134)
1 parent 288c48e commit 25356df

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

apps/demo-free-layout/src/components/base-node/index.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FlowNodeEntity, useNodeRender } from '@flowgram.ai/free-layout-editor';
44
import { ConfigProvider } from '@douyinfe/semi-ui';
55

66
import { NodeRenderContext } from '../../context';
7-
import { BaseNodeStyle, ErrorIcon } from './styles';
7+
import { ErrorIcon } from './styles';
88
import { NodeWrapper } from './node-wrapper';
99

1010
export const BaseNode = ({ node }: { node: FlowNodeEntity }) => {
@@ -30,15 +30,7 @@ export const BaseNode = ({ node }: { node: FlowNodeEntity }) => {
3030
<NodeRenderContext.Provider value={nodeRender}>
3131
<NodeWrapper>
3232
{form?.state.invalid && <ErrorIcon />}
33-
<BaseNodeStyle
34-
className={nodeRender.selected ? 'selected' : ''}
35-
style={{
36-
borderRadius: 8,
37-
outline: form?.state.invalid ? '1px solid red' : 'none',
38-
}}
39-
>
40-
{form?.render()}
41-
</BaseNodeStyle>
33+
{form?.render()}
4234
</NodeWrapper>
4335
</NodeRenderContext.Provider>
4436
</ConfigProvider>

apps/demo-free-layout/src/components/base-node/node-wrapper.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import React, { useState, useContext } from 'react';
22

33
import { WorkflowPortRender } from '@flowgram.ai/free-layout-editor';
4+
import { useClientContext } from '@flowgram.ai/free-layout-editor';
45

56
import { useNodeRenderContext } from '../../hooks';
67
import { 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

1011
export interface NodeWrapperProps {
12+
isScrollToView?: boolean;
1113
children: React.ReactNode;
1214
}
1315

@@ -16,15 +18,18 @@ export interface NodeWrapperProps {
1618
* 用于节点的拖拽/点击事件和点位渲染
1719
*/
1820
export 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
))}

apps/demo-free-layout/src/components/base-node/styles.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import styled from 'styled-components';
22
import { IconInfoCircle } from '@douyinfe/semi-icons';
33

4-
export const BaseNodeStyle = styled.div`
4+
export const NodeWrapperStyle = styled.div`
55
align-items: flex-start;
66
background-color: #fff;
77
border: 1px solid rgba(6, 7, 9, 0.15);
@@ -13,7 +13,7 @@ export const BaseNodeStyle = styled.div`
1313
position: relative;
1414
min-width: 360px;
1515
width: 100%;
16-
height: 100%;
16+
height: auto;
1717
1818
&.selected {
1919
border: 1px solid #4e40e5;

0 commit comments

Comments
 (0)