Skip to content

Commit aac1059

Browse files
fix(edge): 修复删除边不生效的问题
fix(edge): 修复删除边不生效的问题
2 parents c281aed + 68e77dc commit aac1059

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed

example/index.jsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import ReactDOM from 'react-dom';
44
import {Layout, Tooltip} from 'antd';
55
import {BrowserRouter as Router} from 'react-router-dom';
66

7-
import {nodeMenu, edgeMenu, actionMenu} from './menu';
87
import TableBuilding from '../src/index.tsx';
8+
import {nodeMenu, edgeMenu, actionMenu} from './menu';
99
import * as MockData from './mock_data/data.jsx';
1010

1111
import 'antd/dist/antd.css';
@@ -89,6 +89,31 @@ class Component extends React.Component {
8989
});
9090
}
9191

92+
onAddEdge = () => {
93+
const data = this.state.data;
94+
95+
data.edges.push({
96+
"id": 1,
97+
"sourceNode": "aaa",
98+
"targetNode": "bbb",
99+
"source": "field_1",
100+
"target": "field_2"
101+
});
102+
103+
this.setState({
104+
data: {...data}
105+
});
106+
}
107+
108+
onDelEdge = () => {
109+
const data = this.state.data;
110+
data.edges.pop();
111+
112+
this.setState({
113+
data: {...data}
114+
});
115+
}
116+
92117
render() {
93118
return (
94119
<TableBuilding
@@ -121,14 +146,16 @@ class Component extends React.Component {
121146
e.stopPropagation();
122147
console.log('自定义空状态');
123148
}}
124-
>+ 添加字段</p>
149+
>
150+
+ 添加字段
151+
</p>
125152
</div>
126153
}
127154

128155
// =========== 菜单相关属性 ===========
129156
nodeMenu={nodeMenu}
130157
edgeMenu={edgeMenu}
131-
actionMenu={actionMenu}
158+
actionMenu={actionMenu({onAddEdge: this.onAddEdge, onDelEdge: this.onDelEdge})}
132159

133160
// =========== 画布配置 ===========
134161
config={config}

example/menu.jsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import {StarOutlined} from '@ant-design/icons';
2+
import {StarOutlined, PlusOutlined, MinusOutlined} from '@ant-design/icons';
33

44
// 节点菜单
55
export const nodeMenu = [
@@ -41,7 +41,10 @@ export const edgeMenu= [
4141
}
4242
];
4343

44-
export const actionMenu = [
44+
export const actionMenu = ({
45+
onAddEdge,
46+
onDelEdge
47+
}) => [
4548
{
4649
key: 'zoom-in',
4750
disable: true
@@ -52,5 +55,21 @@ export const actionMenu = [
5255
onClick: () => {
5356
alert('点击收藏!')
5457
}
58+
},
59+
{
60+
icon: <PlusOutlined />,
61+
key: 'plus',
62+
title: '添加一条连线',
63+
onClick: () => {
64+
onAddEdge();
65+
}
66+
},
67+
{
68+
icon: <MinusOutlined />,
69+
key: 'minus',
70+
title: '删除一条连线',
71+
onClick: () => {
72+
onDelEdge();
73+
}
5574
}
5675
]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-visual-modeling",
3-
"version": "1.0.12",
3+
"version": "1.0.13",
44
"description": "一个基于React的数据可视化建模的DAG图,适用于UML,数据库建模,数据仓库建设等业务",
55
"main": "dist/index.js",
66
"pack": "pack/index.js",

src/adaptor.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,21 @@ export const transformInitData = (info) => {
2323

2424
let edges = (data.edges || []).map((item) => {
2525
return _.assign(item, {
26-
id: `${item.source}-${item.target}`,
26+
id: `${item.sourceNode}-${item.source}-${item.targetNode}-${item.target}`,
2727
type: 'endpoint',
2828
_config: config,
2929
_menu: edgeMenu,
3030
Class: Edge,
3131
label: item.label,
3232
});
33-
})
33+
});
3434

3535
return {
3636
nodes,
3737
edges
3838
}
3939
}
4040

41-
4241
export const diffPropsData = (newData, oldData, columns) => {
4342
const isSameNode = (a, b) => a.id === b.id;
4443
let addNodes = _.differenceWith(newData.nodes, oldData.nodes, isSameNode);
@@ -48,8 +47,8 @@ export const diffPropsData = (newData, oldData, columns) => {
4847
return (
4948
a.sourceNode === b.sourceNode &&
5049
a.targetNode === b.targetNode &&
51-
a.sourceEndpoint === b.sourceEndpoint &&
52-
a.targetEndpoint === b.targetEndpoint
50+
a.source === b.source &&
51+
a.target === b.target
5352
);
5453
}
5554

@@ -111,10 +110,12 @@ export const diffPropsData = (newData, oldData, columns) => {
111110
nodeId: newNode.id,
112111
fields: _addFields
113112
});
113+
114114
_rmFields.length > 0 && rmFields.push({
115115
nodeId: newNode.id,
116116
fields: _rmFields
117117
});
118+
118119
_updateFields.length > 0 && updateFields.push({
119120
nodeId: newNode.id,
120121
fields: _updateFields

src/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ export default class TableBuilding extends React.Component<ComProps, any> {
272272
});
273273

274274
let diffInfo = diffPropsData(result, this.canvasData, this.props.columns);
275+
275276
if (diffInfo.addNodes.length > 0) {
276277
this.canvas.addNodes(diffInfo.addNodes);
277278
}
@@ -281,8 +282,9 @@ export default class TableBuilding extends React.Component<ComProps, any> {
281282
if (diffInfo.addEdges.length > 0) {
282283
this.canvas.addEdges(diffInfo.addEdges);
283284
}
285+
284286
if (diffInfo.rmEdges.length > 0) {
285-
this.canvas.removeEdges(diffInfo.rmEdges);
287+
this.canvas.removeEdges(diffInfo.rmEdges.map(edge => edge.id));
286288
}
287289

288290
// 更新节点中的字段
@@ -298,7 +300,7 @@ export default class TableBuilding extends React.Component<ComProps, any> {
298300
}
299301

300302
this.canvasData = result;
301-
return false;
303+
return true;
302304
}
303305

304306
componentWillUnmount() {
@@ -356,6 +358,7 @@ export default class TableBuilding extends React.Component<ComProps, any> {
356358
let linksInfo = links.map((item) => {
357359
return item.options;
358360
});
361+
359362
this.props.onChange && this.props.onChange({
360363
type: 'system.link.delete',
361364
links: linksInfo

0 commit comments

Comments
 (0)