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: MOUSE_UP action was ignored if any other mouse button was pressed #941

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
71 changes: 47 additions & 24 deletions packages/react-canvas-core/src/core-actions/ActionEventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export class ActionEventBus {
protected actions: { [id: string]: Action };
protected engine: CanvasEngine;
protected keys: { [key: string]: boolean };
protected mouseButtonPressed: number;

constructor(engine: CanvasEngine) {
this.actions = {};
this.engine = engine;
this.keys = {};
this.mouseButtonPressed = null;
}

getKeys(): string[] {
Expand Down Expand Up @@ -47,31 +49,52 @@ export class ActionEventBus {

getActionsForEvent(actionEvent: ActionEvent): Action[] {
const { event } = actionEvent;
if (event.type === 'mousedown') {
return this.getActionsForType(InputType.MOUSE_DOWN);
} else if (event.type === 'mouseup') {
return this.getActionsForType(InputType.MOUSE_UP);
} else if (event.type === 'keydown') {
// store the recorded key
this.keys[(event as KeyboardEvent).key.toLowerCase()] = true;
return this.getActionsForType(InputType.KEY_DOWN);
} else if (event.type === 'keyup') {
// delete the recorded key
delete this.keys[(event as KeyboardEvent).key.toLowerCase()];
return this.getActionsForType(InputType.KEY_UP);
} else if (event.type === 'mousemove') {
return this.getActionsForType(InputType.MOUSE_MOVE);
} else if (event.type === 'wheel') {
return this.getActionsForType(InputType.MOUSE_WHEEL);
} else if (event.type === 'touchstart') {
return this.getActionsForType(InputType.TOUCH_START);
} else if (event.type === 'touchend') {
return this.getActionsForType(InputType.TOUCH_END);
} else if (event.type === 'touchmove') {
return this.getActionsForType(InputType.TOUCH_MOVE);
}
const eventActions = {
// Mouse events
mousedown: () => {
if (this.mouseButtonPressed === null || this.mouseButtonPressed === (event as MouseEvent).button) {
// store the pressed mouse button
this.mouseButtonPressed = (event as MouseEvent).button;
return this.getActionsForType(InputType.MOUSE_DOWN);
}
},
mouseup: () => {
if (this.mouseButtonPressed === (event as MouseEvent).button) {
// delete the pressed mouse button
this.mouseButtonPressed = null;
return this.getActionsForType(InputType.MOUSE_UP);
}
},
mousemove: () => {
return this.getActionsForType(InputType.MOUSE_MOVE);
},
wheel: () => {
return this.getActionsForType(InputType.MOUSE_WHEEL);
},
// Keyboard events
keydown: () => {
// store the recorded key
this.keys[(event as KeyboardEvent).key.toLowerCase()] = true;
return this.getActionsForType(InputType.KEY_DOWN);
},
keyup: () => {
// delete the recorded key
delete this.keys[(event as KeyboardEvent).key.toLowerCase()];
return this.getActionsForType(InputType.KEY_UP);
},
// Touch events
touchstart: () => {
return this.getActionsForType(InputType.TOUCH_START);
},
touchend: () => {
return this.getActionsForType(InputType.TOUCH_END);
},
touchmove: () => {
return this.getActionsForType(InputType.TOUCH_MOVE);
}
};

return [];
return eventActions[event.type]?.() || [];
}

fireAction(actionEvent: ActionEvent) {
Expand Down