forked from findyourmagic/dber
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink_path.js
More file actions
153 lines (134 loc) · 4.33 KB
/
link_path.js
File metadata and controls
153 lines (134 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const control = 20;
const padding = 5;
const gripWidth = 10;
const gripRadius = gripWidth / 2;
const margin = 3;
/**
* It takes a link object and returns a path element that connects the two tables
* @param props - The props object that is passed to the component.
* {
* link,
* tableDict,
* linkDict,
* TableWidth,
* setEditingLink,
* }
* @returns A svg path is being returned.
*/
export default function LinkPath(props) {
const {
link,
tableDict,
linkDict,
TableWidth: width,
setEditingLink,
editable,
} = props;
if (!tableDict) return null;
const { endpoints } = link;
const [sourceTable, targetTable] = [
tableDict[endpoints[0].id],
tableDict[endpoints[1].id],
];
const [sourceFieldIndex, targetFieldIndex] = [
sourceTable.fields.findIndex(field => field.id === endpoints[0].fieldId),
targetTable.fields.findIndex(field => field.id === endpoints[1].fieldId),
];
const sourceFieldPosition = {
x: sourceTable.x,
y: sourceTable.y + sourceFieldIndex * 32 + 50 + gripRadius + 24,
...endpoints[0],
};
const targetFieldPosition = {
x: targetTable.x,
y: targetTable.y + targetFieldIndex * 32 + 50 + gripRadius + 24,
...endpoints[1],
};
// const [source, target] = [sourceFieldPosition, targetFieldPosition];
const [source, target] = [sourceFieldPosition, targetFieldPosition].sort(
(a, b) => {
return a.x - b.x || a.y - b.y;
}
);
const sourceLeft = source.x + padding + gripRadius + margin;
const sourceRight = source.x + width - padding - gripRadius - margin;
let x = sourceLeft;
const y = source.y + gripRadius + margin;
const targetLeft = target.x + padding + gripRadius + margin;
const targetRight = target.x + width - padding - gripRadius - margin;
let minDistance = Math.abs(sourceLeft - targetLeft);
let x1 = targetLeft;
[
[sourceLeft, targetRight],
[sourceRight, targetLeft],
[sourceRight, targetRight],
].forEach(items => {
if (Math.abs(items[0] - items[1]) < minDistance) {
minDistance = Math.min(items[0] - items[1]);
x = items[0];
x1 = items[1];
}
});
const y1 = target.y + gripRadius + margin;
const midX = x1 - (x1 - x) / 2;
const midY = y1 - (y1 - y) / 2;
const handlerContextMenu = e => {
e.preventDefault();
e.stopPropagation();
};
return (
<>
<path
d={`M ${x} ${y}
C ${x + control} ${y} ${midX} ${midY} ${midX} ${midY}
C ${midX} ${midY} ${x1 - control} ${y1} ${x1} ${y1}`}
stroke="black"
strokeWidth="1"
fill="none"
className="path-line"
/>
<foreignObject
x={(x + control + midX) / 2 - 10}
y={(y + midY) / 2 - 10}
width={20}
height={20}
onMouseDown={() => {
if (!editable) return;
setEditingLink({
linkId: link.id,
fieldId: source.fieldId,
});
}}
onContextMenu={handlerContextMenu}
>
<div
style={{ cursor: editable ? 'pointer' : 'default', userSelect: 'none' }}
className="path-label"
>
{source.relation}
</div>
</foreignObject>
<foreignObject
x={(x1 - control + midX) / 2 - 10}
y={(y1 + midY) / 2 - 10}
width={20}
height={20}
onMouseDown={() => {
if (!editable) return;
setEditingLink({
linkId: link.id,
fieldId: target.fieldId,
});
}}
onContextMenu={handlerContextMenu}
>
<div
style={{ cursor: editable ? 'pointer' : 'default', userSelect: 'none' }}
className="path-label"
>
{target.relation}
</div>
</foreignObject>
</>
);
}