Skip to content

Commit 48397ba

Browse files
author
昔梦
committed
feat:新增节点名称改为title+4位随机数
1 parent 9116f36 commit 48397ba

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

packages/x-flow/src/XFlow.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useEventListener, useMemoizedFn } from 'ahooks';
1010
import produce, { setAutoFreeze } from 'immer';
1111
import { debounce } from 'lodash';
1212
import type { FC } from 'react';
13-
import React, { memo, useEffect, useMemo, useRef, useState } from 'react';
13+
import React, { memo, useContext, useEffect, useMemo, useRef, useState } from 'react';
1414
import CandidateNode from './components/CandidateNode';
1515
import CustomEdge from './components/CustomEdge';
1616
import PanelContainer from './components/PanelContainer';
@@ -21,13 +21,14 @@ import { useStore, useStoreApi } from './hooks/useStore';
2121

2222
import Operator from './operator';
2323
import FlowProps from './types';
24-
import { uuid } from './utils';
24+
import { uuid, uuid4 } from './utils';
2525
import autoLayoutNodes from './utils/autoLayoutNodes';
2626

2727
import { shallow } from 'zustand/shallow';
2828
import NodeEditor from './components/NodeEditor';
2929
import './index.less';
3030
import { useTemporalStore } from './hooks/useTemporalStore';
31+
import { ConfigContext } from './models/context';
3132

3233
const CustomNode = memo(CustomNodeComponent);
3334
const edgeTypes = { buttonedge: memo(CustomEdge) };
@@ -73,6 +74,8 @@ const XFlow: FC<FlowProps> = memo(() => {
7374
);
7475
const { record } = useTemporalStore();
7576
const [activeNode, setActiveNode] = useState<any>(null);
77+
const { settingMap } = useContext(ConfigContext);
78+
7679
useEffect(() => {
7780
zoomTo(0.8);
7881
setAutoFreeze(false);
@@ -81,6 +84,7 @@ const XFlow: FC<FlowProps> = memo(() => {
8184
};
8285
}, []);
8386

87+
8488
useEventListener('keydown', e => {
8589
if ((e.key === 'd' || e.key === 'D') && (e.ctrlKey || e.metaKey))
8690
e.preventDefault();
@@ -123,10 +127,14 @@ const XFlow: FC<FlowProps> = memo(() => {
123127

124128
// 新增节点
125129
const handleAddNode = (data: any) => {
130+
const title = settingMap[data?._nodeType]?.title || data?._nodeType;
126131
const newNode = {
127132
id: uuid(),
128133
type: 'custom',
129-
data,
134+
data: {
135+
...data,
136+
title: `${title}_${uuid4()}`
137+
},
130138
position: {
131139
x: 0,
132140
y: 0,

packages/x-flow/src/components/CustomEdge/index.tsx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@ import produce from 'immer';
99
import React, { memo, useContext, useState } from 'react';
1010
import { shallow } from 'zustand/shallow';
1111
import { useStore } from '../../hooks/useStore';
12-
import { uuid } from '../../utils';
12+
import { ConfigContext } from '../../models/context';
13+
import { uuid, uuid4 } from '../../utils';
1314
import NodeSelectPopover from '../NodesPopover';
1415
import './index.less';
15-
import { ConfigContext } from '../../models/context';
1616

1717
export default memo((edge: any) => {
18-
const { id, selected, sourceX, sourceY, targetX, targetY, source, target, sourceHandleId } =
19-
edge;
18+
const {
19+
id,
20+
selected,
21+
sourceX,
22+
sourceY,
23+
targetX,
24+
targetY,
25+
source,
26+
target,
27+
sourceHandleId,
28+
} = edge;
2029

2130
const reactflow = useReactFlow();
2231
const [isHovered, setIsHovered] = useState(false);
@@ -27,7 +36,7 @@ export default memo((edge: any) => {
2736
targetY,
2837
});
2938

30-
const { globalConfig } = useContext(ConfigContext);
39+
const { globalConfig, settingMap } = useContext(ConfigContext);
3140
const hideEdgeAddBtn = globalConfig?.edge?.hideEdgeAddBtn ?? false;
3241

3342
const {
@@ -49,7 +58,7 @@ export default memo((edge: any) => {
4958
onEdgesChange: state.onEdgesChange,
5059
}),
5160
shallow
52-
);
61+
);
5362

5463
const handleAddNode = (data: any) => {
5564
const { screenToFlowPosition } = reactflow;
@@ -59,11 +68,16 @@ export default memo((edge: any) => {
5968
});
6069

6170
const targetId = uuid();
71+
const title = settingMap[data?._nodeType]?.title || data?._nodeType;
72+
6273
const newNodes = produce(nodes, (draft: any) => {
6374
draft.push({
6475
id: targetId,
6576
type: 'custom',
66-
data,
77+
data: {
78+
...data,
79+
title: `${title}_${uuid4()}`,
80+
},
6781
position: { x, y },
6882
});
6983
});
@@ -75,7 +89,7 @@ export default memo((edge: any) => {
7589
id: uuid(),
7690
source,
7791
target: targetId,
78-
...sourceHandleId && { sourceHandle: sourceHandleId }
92+
...(sourceHandleId && { sourceHandle: sourceHandleId }),
7993
},
8094
{
8195
id: uuid(),
@@ -127,13 +141,16 @@ export default memo((edge: any) => {
127141
>
128142
<CloseOutlined style={{ color: '#fff', fontSize: 10 }} />
129143
</div>
130-
{
131-
!hideEdgeAddBtn && <NodeSelectPopover placement="right" addNode={handleAddNode}>
144+
{!hideEdgeAddBtn && (
145+
<NodeSelectPopover
146+
placement="right"
147+
addNode={handleAddNode}
148+
>
132149
<div className="line-icon-box">
133150
<PlusOutlined style={{ color: '#fff', fontSize: 10 }} />
134151
</div>
135152
</NodeSelectPopover>
136-
}
153+
)}
137154
</div>
138155
</div>
139156
</EdgeLabelRenderer>

packages/x-flow/src/components/CustomNode/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React, { memo, useContext, useState } from 'react';
44
import { shallow } from 'zustand/shallow';
55
import { useStore } from '../../hooks/useStore';
66
import { ConfigContext } from '../../models/context';
7-
import { capitalize, uuid } from '../../utils';
7+
import { capitalize, uuid, uuid4 } from '../../utils';
88
import './index.less';
99
import SourceHandle from './sourceHandle';
1010

@@ -28,18 +28,22 @@ export default memo((props: any) => {
2828
);
2929
const isSwitchNode = type === 'Switch' || type === 'Parallel'; // 判断是否为条件节点/并行节点
3030
// 增加节点并进行联系
31-
const handleAddNode = (data: any, sourceHandle?:string) => {
31+
const handleAddNode = (data: any, sourceHandle?: string) => {
3232
const { screenToFlowPosition } = reactflow;
3333
const { x, y } = screenToFlowPosition({
3434
x: mousePosition.pageX + 100,
3535
y: mousePosition.pageY + 100,
3636
});
3737
const targetId = uuid();
38+
const title = settingMap[data?._nodeType]?.title || data?._nodeType;
3839

3940
const newNodes = {
4041
id: targetId,
4142
type: 'custom',
42-
data,
43+
data: {
44+
...data,
45+
title: `${title}_${uuid4()}`
46+
},
4347
position: { x, y },
4448
};
4549
const newEdges = {

packages/x-flow/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { version as antdVersion } from 'antd';
22
import { customAlphabet } from 'nanoid';
33
export const uuid = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 16);
4+
export const uuid4 = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 4);
45

56
import { isMatch, some, set, get, cloneDeep, has as _has, merge, mergeWith, isUndefined, omitBy } from 'lodash-es';
67

0 commit comments

Comments
 (0)