Skip to content
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

fix(event): 修复相同 type 事件触发两次 #3282

Merged
merged 2 commits into from
Feb 8, 2021
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
29 changes: 15 additions & 14 deletions src/chart/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1610,11 +1610,12 @@ export class View extends Base {

if (ALL_EVENTS.includes(type)) {
const currentInPlot = this.isPointInPlot(point);
const newEvent = e.clone();

if (currentInPlot) {
const TYPE = `plot:${type}`; // 组合 plot 事件
e.type = TYPE;
this.emit(TYPE, e);
newEvent.type = TYPE;
this.emit(TYPE, newEvent);
if (type === 'mouseleave' || type === 'touchend') {
// 在plot 内部却离开画布
this.isPreMouseInPlot = false;
Expand All @@ -1625,30 +1626,30 @@ export class View extends Base {
if (type === 'mousemove' || type === 'touchmove') {
if (this.isPreMouseInPlot && !currentInPlot) {
if (type === 'mousemove') {
e.type = PLOT_EVENTS.MOUSE_LEAVE;
this.emit(PLOT_EVENTS.MOUSE_LEAVE, e);
newEvent.type = PLOT_EVENTS.MOUSE_LEAVE;
this.emit(PLOT_EVENTS.MOUSE_LEAVE, newEvent);
}
e.type = PLOT_EVENTS.LEAVE;
this.emit(PLOT_EVENTS.LEAVE, e);
newEvent.type = PLOT_EVENTS.LEAVE;
this.emit(PLOT_EVENTS.LEAVE, newEvent);
} else if (!this.isPreMouseInPlot && currentInPlot) {
if (type === 'mousemove') {
e.type = PLOT_EVENTS.MOUSE_ENTER;
this.emit(PLOT_EVENTS.MOUSE_ENTER, e);
newEvent.type = PLOT_EVENTS.MOUSE_ENTER;
this.emit(PLOT_EVENTS.MOUSE_ENTER, newEvent);
}
e.type = PLOT_EVENTS.ENTER;
this.emit(PLOT_EVENTS.ENTER, e);
newEvent.type = PLOT_EVENTS.ENTER;
this.emit(PLOT_EVENTS.ENTER, newEvent);
}
// 赋新的状态值
this.isPreMouseInPlot = currentInPlot;
} else if (type === 'mouseleave' || type === 'touchend') {
// 可能不在 currentInPlot 中
if (this.isPreMouseInPlot) {
if (type === 'mouseleave') {
e.type = PLOT_EVENTS.MOUSE_LEAVE;
this.emit(PLOT_EVENTS.MOUSE_LEAVE, e);
newEvent.type = PLOT_EVENTS.MOUSE_LEAVE;
this.emit(PLOT_EVENTS.MOUSE_LEAVE, newEvent);
}
e.type = PLOT_EVENTS.LEAVE;
this.emit(PLOT_EVENTS.LEAVE, e);
newEvent.type = PLOT_EVENTS.LEAVE;
this.emit(PLOT_EVENTS.LEAVE, newEvent);

this.isPreMouseInPlot = false;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/chart/event/event-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,34 @@ describe('Event', () => {
expect(mousemoveEvent).toBeCalled();
expect(mousedownEvent).toBeCalled();
});

it('fix: not trigger same event twice', () => {
let plotClick = 0;
let plotClick2 = 0;
let dblClick = 0;

chart.on('*', (e) => {
if (e.type === 'plot:dblclick') {
plotClick += 1;
}
});

chart.on('dblclick', (e) => {
if (e.type === 'plot:dblclick') {
plotClick2 += 1;
}
if (e.type === 'dblclick') {
dblClick += 1;
}
});

simulateMouseEvent(chart.canvas.get('el'), 'dblclick', getClientPoint(chart.canvas, 134, 410));
expect(plotClick).toBe(1);
expect(plotClick2).toBe(0);
expect(dblClick).toBe(1);
});

afterAll(() => {
chart.destroy();
});
});