Skip to content

Commit 411f61e

Browse files
authored
Merge pull request #3979 from plotly/clickannotation-evt-editable
Trigger `plotly_clickannotation` when `editable:true`
2 parents 795220f + ec35d93 commit 411f61e

File tree

2 files changed

+75
-22
lines changed

2 files changed

+75
-22
lines changed

src/components/annotations/draw.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,25 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
139139
var editTextPosition = edits[options.showarrow ? 'annotationTail' : 'annotationPosition'];
140140
var textEvents = options.captureevents || edits.annotationText || editTextPosition;
141141

142+
function makeEventData(initialEvent) {
143+
var eventData = {
144+
index: index,
145+
annotation: options._input,
146+
fullAnnotation: options,
147+
event: initialEvent
148+
};
149+
if(subplotId) {
150+
eventData.subplotId = subplotId;
151+
}
152+
return eventData;
153+
}
154+
142155
var annTextGroupInner = annTextGroup.append('g')
143156
.style('pointer-events', textEvents ? 'all' : null)
144157
.call(setCursor, 'pointer')
145158
.on('click', function() {
146159
gd._dragging = false;
147-
148-
var eventData = {
149-
index: index,
150-
annotation: options._input,
151-
fullAnnotation: options,
152-
event: d3.event
153-
};
154-
155-
if(subplotId) {
156-
eventData.subplotId = subplotId;
157-
}
158-
159-
gd.emit('plotly_clickannotation', eventData);
160+
gd.emit('plotly_clickannotation', makeEventData(d3.event));
160161
});
161162

162163
if(options.hovertext) {
@@ -661,6 +662,11 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
661662

662663
setCursor(annTextGroupInner, csr);
663664
},
665+
clickFn: function(_, initialEvent) {
666+
if(options.captureevents) {
667+
gd.emit('plotly_clickannotation', makeEventData(initialEvent));
668+
}
669+
},
664670
doneFn: function() {
665671
setCursor(annTextGroupInner);
666672
Registry.call('_guiRelayout', gd, getUpdateObj());

test/jasmine/tests/annotations_test.js

+56-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var drag = require('../assets/drag');
1717
var mouseEvent = require('../assets/mouse_event');
1818
var click = require('../assets/click');
1919

20-
2120
describe('Test annotations', function() {
2221
'use strict';
2322

@@ -1344,10 +1343,7 @@ describe('annotation effects', function() {
13441343
function _click(pos, opts) {
13451344
return new Promise(function(resolve) {
13461345
click(pos[0], pos[1], opts);
1347-
1348-
setTimeout(function() {
1349-
resolve();
1350-
}, DBLCLICKDELAY * 1.1);
1346+
setTimeout(resolve, DBLCLICKDELAY * 1.1);
13511347
});
13521348
}
13531349

@@ -1370,7 +1366,7 @@ describe('annotation effects', function() {
13701366
clickData = [];
13711367

13721368
gd.on('plotly_clickannotation', function(evt) {
1373-
expect(evt.event).toEqual(jasmine.objectContaining({type: 'click'}));
1369+
evt.eventType = evt.event.type;
13741370
evt.button = evt.event.button;
13751371
if(evt.event.ctrlKey) evt.ctrlKey = true;
13761372
delete evt.event;
@@ -1448,7 +1444,8 @@ describe('annotation effects', function() {
14481444
index: 1,
14491445
annotation: gd.layout.annotations[1],
14501446
fullAnnotation: gd._fullLayout.annotations[1],
1451-
button: 0
1447+
button: 0,
1448+
eventType: 'click'
14521449
}]);
14531450

14541451
expect(gd._fullLayout.annotations[0].hoverlabel).toBeUndefined();
@@ -1473,7 +1470,8 @@ describe('annotation effects', function() {
14731470
index: 0,
14741471
annotation: gd.layout.annotations[0],
14751472
fullAnnotation: gd._fullLayout.annotations[0],
1476-
button: 0
1473+
button: 0,
1474+
eventType: 'click'
14771475
}]);
14781476

14791477
return Plotly.relayout(gd, {
@@ -1528,7 +1526,56 @@ describe('annotation effects', function() {
15281526
index: 1,
15291527
annotation: gd.layout.annotations[1],
15301528
fullAnnotation: gd._fullLayout.annotations[1],
1531-
button: 0
1529+
button: 0,
1530+
eventType: 'click'
1531+
}]);
1532+
})
1533+
.catch(failTest)
1534+
.then(done);
1535+
});
1536+
1537+
it('should register clicks even in editable:true mode', function(done) {
1538+
function clickForTextEdit(pos) {
1539+
return new Promise(function(resolve, reject) {
1540+
gd.once('plotly_relayout', resolve);
1541+
1542+
setTimeout(function() {
1543+
reject('Did not trigger plotly_relayout');
1544+
}, DBLCLICKDELAY * 2);
1545+
1546+
click(pos[0], pos[1]);
1547+
1548+
setTimeout(function() {
1549+
var input = d3.select('.plugin-editable.editable');
1550+
input.node().dispatchEvent(new KeyboardEvent('blur'));
1551+
}, DBLCLICKDELAY * 1.1);
1552+
});
1553+
}
1554+
1555+
makePlot([
1556+
{x: 50, y: 50, text: 'hi', width: 50, height: 40, ax: 0, ay: -40, xshift: -50, yshift: 50},
1557+
{x: 20, y: 20, text: 'bye', height: 40, showarrow: false},
1558+
{x: 80, y: 80, text: 'why?', ax: 0, ay: -40}
1559+
], {editable: true})
1560+
.then(initClickTests)
1561+
.then(function() { return clickForTextEdit(pos0); })
1562+
.then(function() {
1563+
assertClickData([]);
1564+
})
1565+
.then(function() {
1566+
return Plotly.relayout(gd, {
1567+
'annotations[1].captureevents': true,
1568+
'annotations[2].captureevents': true
1569+
});
1570+
})
1571+
.then(function() { return clickForTextEdit(pos1); })
1572+
.then(function() {
1573+
assertClickData([{
1574+
index: 1,
1575+
annotation: gd.layout.annotations[1],
1576+
fullAnnotation: gd._fullLayout.annotations[1],
1577+
button: 0,
1578+
eventType: 'mousedown'
15321579
}]);
15331580
})
15341581
.catch(failTest)

0 commit comments

Comments
 (0)