Skip to content

Commit 659ca7c

Browse files
authored
fix(linkTools.Anchor): fix area bounding box when the link is connected to a link (#1941)
1 parent f91c544 commit 659ca7c

2 files changed

Lines changed: 93 additions & 2 deletions

File tree

src/linkTools/Anchor.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const Anchor = ToolView.extend({
111111
if (!isFinite(padding)) padding = 0;
112112
var bbox, angle, center;
113113
if (view.isNodeConnection(magnet)) {
114-
bbox = view.getBBox();
114+
bbox = view.getNodeBBox(magnet);
115115
angle = 0;
116116
center = bbox.center();
117117
} else {
@@ -129,7 +129,11 @@ const Anchor = ToolView.extend({
129129
areaNode.setAttribute('transform', 'translate(' + center.x + ',' + center.y + ') rotate(' + angle + ')');
130130
},
131131
toggleArea: function(visible) {
132-
this.childNodes.area.style.display = (visible) ? '' : 'none';
132+
var childNodes = this.childNodes;
133+
if (!childNodes) return;
134+
var areaNode = childNodes.area;
135+
if (!areaNode) return;
136+
areaNode.style.display = (visible) ? '' : 'none';
133137
},
134138
onPointerDown: function(evt) {
135139
if (this.guard(evt)) return;

test/jointjs/dia/linkTools.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,67 @@ QUnit.module('linkTools', function(hooks) {
6464
assert.deepEqual(link.prop(['target', 'anchor']), testCase.resultAnchor);
6565
});
6666
});
67+
68+
QUnit.test('No area', function(assert) {
69+
const CustomAnchor = joint.linkTools.TargetAnchor.extend({
70+
children: [{
71+
tagName: 'circle',
72+
selector: 'anchor',
73+
attributes: {
74+
'cursor': 'pointer'
75+
}
76+
}]
77+
});
78+
const anchor = new CustomAnchor();
79+
linkView.addTools(new joint.dia.ToolsView({ tools: [anchor] }));
80+
assert.ok(true, 'The anchor tool does not require an area node.');
81+
});
82+
83+
QUnit.test('No anchor', function(assert) {
84+
const CustomAnchor = joint.linkTools.TargetAnchor.extend({
85+
children: [{
86+
tagName: 'rect',
87+
selector: 'area',
88+
attributes: {
89+
'pointer-events': 'none',
90+
'fill': 'none',
91+
'stroke': '#33334F',
92+
'stroke-dasharray': '2,4',
93+
'rx': 5,
94+
'ry': 5
95+
}
96+
}]
97+
});
98+
const anchor = new CustomAnchor();
99+
linkView.addTools(new joint.dia.ToolsView({ tools: [anchor] }));
100+
assert.ok(true, 'The anchor tool does not require an anchor node.');
101+
});
102+
103+
QUnit.test('Element area bounding box', function(assert) {
104+
paper.translate(11, 13);
105+
const areaPadding = 7;
106+
const anchor = new joint.linkTools.TargetAnchor({ areaPadding });
107+
linkView.addTools(new joint.dia.ToolsView({ tools: [anchor] }));
108+
anchor.toggleArea(true);
109+
const areaBBox = V(anchor.childNodes.area).getBBox({ target: paper.viewport });
110+
assert.checkBboxApproximately(1e-6, areaBBox, link.getTargetCell().getBBox().inflate(areaPadding));
111+
});
112+
113+
QUnit.test('Link area bounding box', function(assert) {
114+
const link2 = new joint.shapes.standard.Link({
115+
source: { x: 100, y: 100 },
116+
target: { x: 200, y: 200 }
117+
});
118+
graph.addCell(link2);
119+
link.target(link2);
120+
paper.translate(11, 13);
121+
const areaPadding = 7;
122+
const anchor = new joint.linkTools.TargetAnchor({ areaPadding });
123+
linkView.addTools(new joint.dia.ToolsView({ tools: [anchor] }));
124+
anchor.toggleArea(true);
125+
const areaBBox = V(anchor.childNodes.area).getBBox({ target: paper.viewport });
126+
assert.checkBboxApproximately(1e-6, areaBBox, link.getTargetCell().getBBox().inflate(areaPadding));
127+
});
67128
});
68129

69130
QUnit.module('SourceAnchor', function() {
@@ -85,6 +146,32 @@ QUnit.module('linkTools', function(hooks) {
85146
assert.deepEqual(link.prop(['source', 'anchor']), testCase.resultAnchor);
86147
});
87148
});
149+
150+
QUnit.test('Element area bounding box', function(assert) {
151+
paper.translate(11, 13);
152+
const areaPadding = 7;
153+
const anchor = new joint.linkTools.SourceAnchor({ areaPadding });
154+
linkView.addTools(new joint.dia.ToolsView({ tools: [anchor] }));
155+
anchor.toggleArea(true);
156+
const areaBBox = V(anchor.childNodes.area).getBBox({ target: paper.viewport });
157+
assert.checkBboxApproximately(1e-6, areaBBox, link.getSourceCell().getBBox().inflate(areaPadding));
158+
});
159+
160+
QUnit.test('Link area bounding box', function(assert) {
161+
const link2 = new joint.shapes.standard.Link({
162+
source: { x: 100, y: 100 },
163+
target: { x: 200, y: 200 }
164+
});
165+
graph.addCell(link2);
166+
link.source(link2);
167+
paper.translate(11, 13);
168+
const areaPadding = 7;
169+
const anchor = new joint.linkTools.SourceAnchor({ areaPadding });
170+
linkView.addTools(new joint.dia.ToolsView({ tools: [anchor] }));
171+
anchor.toggleArea(true);
172+
const areaBBox = V(anchor.childNodes.area).getBBox({ target: paper.viewport });
173+
assert.checkBboxApproximately(1e-6, areaBBox, link.getSourceCell().getBBox().inflate(areaPadding));
174+
});
88175
});
89176

90177
QUnit.module('Connect', function() {

0 commit comments

Comments
 (0)