Skip to content

Trigger plotly_clickannotation when editable:true #3979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/components/annotations/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,25 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
var editTextPosition = edits[options.showarrow ? 'annotationTail' : 'annotationPosition'];
var textEvents = options.captureevents || edits.annotationText || editTextPosition;

function makeEventData(initialEvent) {
var eventData = {
index: index,
annotation: options._input,
fullAnnotation: options,
event: initialEvent
};
if(subplotId) {
eventData.subplotId = subplotId;
}
return eventData;
}

var annTextGroupInner = annTextGroup.append('g')
.style('pointer-events', textEvents ? 'all' : null)
.call(setCursor, 'pointer')
.on('click', function() {
gd._dragging = false;

var eventData = {
index: index,
annotation: options._input,
fullAnnotation: options,
event: d3.event
};

if(subplotId) {
eventData.subplotId = subplotId;
}

gd.emit('plotly_clickannotation', eventData);
gd.emit('plotly_clickannotation', makeEventData(d3.event));
});

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

setCursor(annTextGroupInner, csr);
},
clickFn: function(_, initialEvent) {
if(options.captureevents) {
gd.emit('plotly_clickannotation', makeEventData(initialEvent));
}
},
doneFn: function() {
setCursor(annTextGroupInner);
Registry.call('_guiRelayout', gd, getUpdateObj());
Expand Down
65 changes: 56 additions & 9 deletions test/jasmine/tests/annotations_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var drag = require('../assets/drag');
var mouseEvent = require('../assets/mouse_event');
var click = require('../assets/click');


describe('Test annotations', function() {
'use strict';

Expand Down Expand Up @@ -1344,10 +1343,7 @@ describe('annotation effects', function() {
function _click(pos, opts) {
return new Promise(function(resolve) {
click(pos[0], pos[1], opts);

setTimeout(function() {
resolve();
}, DBLCLICKDELAY * 1.1);
setTimeout(resolve, DBLCLICKDELAY * 1.1);
});
}

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

gd.on('plotly_clickannotation', function(evt) {
expect(evt.event).toEqual(jasmine.objectContaining({type: 'click'}));
evt.eventType = evt.event.type;
evt.button = evt.event.button;
if(evt.event.ctrlKey) evt.ctrlKey = true;
delete evt.event;
Expand Down Expand Up @@ -1448,7 +1444,8 @@ describe('annotation effects', function() {
index: 1,
annotation: gd.layout.annotations[1],
fullAnnotation: gd._fullLayout.annotations[1],
button: 0
button: 0,
eventType: 'click'
}]);

expect(gd._fullLayout.annotations[0].hoverlabel).toBeUndefined();
Expand All @@ -1473,7 +1470,8 @@ describe('annotation effects', function() {
index: 0,
annotation: gd.layout.annotations[0],
fullAnnotation: gd._fullLayout.annotations[0],
button: 0
button: 0,
eventType: 'click'
}]);

return Plotly.relayout(gd, {
Expand Down Expand Up @@ -1528,7 +1526,56 @@ describe('annotation effects', function() {
index: 1,
annotation: gd.layout.annotations[1],
fullAnnotation: gd._fullLayout.annotations[1],
button: 0
button: 0,
eventType: 'click'
}]);
})
.catch(failTest)
.then(done);
});

it('should register clicks even in editable:true mode', function(done) {
function clickForTextEdit(pos) {
return new Promise(function(resolve, reject) {
gd.once('plotly_relayout', resolve);

setTimeout(function() {
reject('Did not trigger plotly_relayout');
}, DBLCLICKDELAY * 2);

click(pos[0], pos[1]);

setTimeout(function() {
var input = d3.select('.plugin-editable.editable');
input.node().dispatchEvent(new KeyboardEvent('blur'));
}, DBLCLICKDELAY * 1.1);
});
}

makePlot([
{x: 50, y: 50, text: 'hi', width: 50, height: 40, ax: 0, ay: -40, xshift: -50, yshift: 50},
{x: 20, y: 20, text: 'bye', height: 40, showarrow: false},
{x: 80, y: 80, text: 'why?', ax: 0, ay: -40}
], {editable: true})
.then(initClickTests)
.then(function() { return clickForTextEdit(pos0); })
.then(function() {
assertClickData([]);
})
.then(function() {
return Plotly.relayout(gd, {
'annotations[1].captureevents': true,
'annotations[2].captureevents': true
});
})
.then(function() { return clickForTextEdit(pos1); })
.then(function() {
assertClickData([{
index: 1,
annotation: gd.layout.annotations[1],
fullAnnotation: gd._fullLayout.annotations[1],
button: 0,
eventType: 'mousedown'
}]);
})
.catch(failTest)
Expand Down