Skip to content

Commit

Permalink
Fix incorrect trigger matching if multiple are provided
Browse files Browse the repository at this point in the history
Move trigger types to where they are used

Closes #241
  • Loading branch information
simonwep committed Jan 12, 2025
1 parent 5d37420 commit 4e2389f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/vanilla/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {intersects} from './utils/intersects';
import {isSafariBrowser, isTouchDevice} from './utils/browser';
import {on, off, simplifyEvent} from './utils/events';
import {selectAll, SelectAllSelectors} from './utils/selectAll';
import {shouldTrigger} from './utils/shouldTrigger';
import {matchesTrigger} from './utils/matchesTrigger';

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

if (evt instanceof MouseEvent && !shouldTrigger(evt, behaviour.triggers)) {
if (evt instanceof MouseEvent && !matchesTrigger(evt, behaviour.triggers)) {
return;
}

Expand Down
19 changes: 1 addition & 18 deletions packages/vanilla/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type SelectionArea from './index';
import type {Intersection} from './utils/intersects';
import type {Trigger} from './utils/matchesTrigger';

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

// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button#value
export type MouseButton = 0 // Main
| 1 // Auxiliary
| 2 // Secondary
| 3 // Fourth
| 4; // Fifth

export type Modifier = 'ctrl'
| 'alt'
| 'shift';

export type Trigger = MouseButton | MouseButtonWithModifiers;

export type MouseButtonWithModifiers = {
button: MouseButton,
modifiers: Modifier[]
};

export interface Scrolling {
speedDivider: number;
manualSpeed: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import {Trigger} from '../types';

// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button#value
export type MouseButton = 0 // Main
| 1 // Auxiliary
| 2 // Secondary
| 3 // Fourth
| 4; // Fifth

export type Modifier = 'ctrl'
| 'alt'
| 'shift';

export type Trigger = MouseButton | MouseButtonWithModifiers;

export type MouseButtonWithModifiers = {
button: MouseButton,
modifiers: Modifier[]
};

/**
* Determines whether a MouseEvent should execute until completion depending on
Expand All @@ -8,18 +25,21 @@ import {Trigger} from '../types';
* @param triggers A list of Triggers that signify that the event should execute until completion
* @returns Whether the MouseEvent should execute until completion
*/
export const shouldTrigger = (event: MouseEvent, triggers: Trigger[]): boolean => {
for (const trigger of triggers) {
export const matchesTrigger = (event: MouseEvent, triggers: Trigger[]): boolean =>
triggers.some((trigger) => {

// The trigger requires only a specific button to be pressed
if (typeof trigger === 'number') {
return event.button === trigger;
}

// The trigger requires a specific button to be pressed AND some modifiers
if (typeof trigger === 'object') {
const reqButtonIsPressed = trigger.button === event.button;
if (trigger.button !== event.button) {
return false;
}

const allReqModifiersArePressed = trigger.modifiers.every((modifier) => {
return trigger.modifiers.every((modifier) => {
switch (modifier) {
case 'alt':
return event.altKey;
Expand All @@ -29,11 +49,7 @@ export const shouldTrigger = (event: MouseEvent, triggers: Trigger[]): boolean =
return event.shiftKey;
}
});

return reqButtonIsPressed && allReqModifiersArePressed;
}
}

// By default, we do not process the event
return false;
};
return false;
});

0 comments on commit 4e2389f

Please sign in to comment.