Skip to content

Commit 4e2389f

Browse files
committed
Fix incorrect trigger matching if multiple are provided
Move trigger types to where they are used Closes #241
1 parent 5d37420 commit 4e2389f

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

packages/vanilla/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {intersects} from './utils/intersects';
88
import {isSafariBrowser, isTouchDevice} from './utils/browser';
99
import {on, off, simplifyEvent} from './utils/events';
1010
import {selectAll, SelectAllSelectors} from './utils/selectAll';
11-
import {shouldTrigger} from './utils/shouldTrigger';
11+
import {matchesTrigger} from './utils/matchesTrigger';
1212

1313
// Re-export types
1414
export * from './types';
@@ -168,7 +168,7 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
168168
const {document, startAreas, boundaries, features, behaviour} = this._options;
169169
const targetBoundingClientRect = target.getBoundingClientRect();
170170

171-
if (evt instanceof MouseEvent && !shouldTrigger(evt, behaviour.triggers)) {
171+
if (evt instanceof MouseEvent && !matchesTrigger(evt, behaviour.triggers)) {
172172
return;
173173
}
174174

packages/vanilla/src/types.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type SelectionArea from './index';
22
import type {Intersection} from './utils/intersects';
3+
import type {Trigger} from './utils/matchesTrigger';
34

45
export type DeepPartial<T> =
56
T extends unknown[] ? T :
@@ -54,24 +55,6 @@ export interface Coordinates {
5455
export type TapMode = 'touch' | 'native';
5556
export type OverlapMode = 'keep' | 'drop' | 'invert';
5657

57-
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button#value
58-
export type MouseButton = 0 // Main
59-
| 1 // Auxiliary
60-
| 2 // Secondary
61-
| 3 // Fourth
62-
| 4; // Fifth
63-
64-
export type Modifier = 'ctrl'
65-
| 'alt'
66-
| 'shift';
67-
68-
export type Trigger = MouseButton | MouseButtonWithModifiers;
69-
70-
export type MouseButtonWithModifiers = {
71-
button: MouseButton,
72-
modifiers: Modifier[]
73-
};
74-
7558
export interface Scrolling {
7659
speedDivider: number;
7760
manualSpeed: number;
Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
import {Trigger} from '../types';
1+
2+
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button#value
3+
export type MouseButton = 0 // Main
4+
| 1 // Auxiliary
5+
| 2 // Secondary
6+
| 3 // Fourth
7+
| 4; // Fifth
8+
9+
export type Modifier = 'ctrl'
10+
| 'alt'
11+
| 'shift';
12+
13+
export type Trigger = MouseButton | MouseButtonWithModifiers;
14+
15+
export type MouseButtonWithModifiers = {
16+
button: MouseButton,
17+
modifiers: Modifier[]
18+
};
219

320
/**
421
* Determines whether a MouseEvent should execute until completion depending on
@@ -8,18 +25,21 @@ import {Trigger} from '../types';
825
* @param triggers A list of Triggers that signify that the event should execute until completion
926
* @returns Whether the MouseEvent should execute until completion
1027
*/
11-
export const shouldTrigger = (event: MouseEvent, triggers: Trigger[]): boolean => {
12-
for (const trigger of triggers) {
28+
export const matchesTrigger = (event: MouseEvent, triggers: Trigger[]): boolean =>
29+
triggers.some((trigger) => {
30+
1331
// The trigger requires only a specific button to be pressed
1432
if (typeof trigger === 'number') {
1533
return event.button === trigger;
1634
}
1735

1836
// The trigger requires a specific button to be pressed AND some modifiers
1937
if (typeof trigger === 'object') {
20-
const reqButtonIsPressed = trigger.button === event.button;
38+
if (trigger.button !== event.button) {
39+
return false;
40+
}
2141

22-
const allReqModifiersArePressed = trigger.modifiers.every((modifier) => {
42+
return trigger.modifiers.every((modifier) => {
2343
switch (modifier) {
2444
case 'alt':
2545
return event.altKey;
@@ -29,11 +49,7 @@ export const shouldTrigger = (event: MouseEvent, triggers: Trigger[]): boolean =
2949
return event.shiftKey;
3050
}
3151
});
32-
33-
return reqButtonIsPressed && allReqModifiersArePressed;
3452
}
35-
}
3653

37-
// By default, we do not process the event
38-
return false;
39-
};
54+
return false;
55+
});

0 commit comments

Comments
 (0)