Skip to content

Commit

Permalink
Remove deepAssign and improve config types
Browse files Browse the repository at this point in the history
Fixes #195
  • Loading branch information
simonwep committed Mar 30, 2023
1 parent 425199f commit 516ac77
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
38 changes: 24 additions & 14 deletions packages/vanilla/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {EventTarget} from './EventEmitter';
import type {AreaLocation, Coordinates, ScrollEvent, SelectionEvents, SelectionOptions, SelectionStore} from './types';
import {PartialSelectionOptions} from './types';
import {css, deepAssign, frames, Frames, intersects, isSafariBrowser, isTouchDevice, off, on, selectAll, SelectAllSelectors, simplifyEvent} from './utils';
import {css, frames, Frames, intersects, isSafariBrowser, isTouchDevice, off, on, selectAll, SelectAllSelectors, simplifyEvent} from './utils';

// Re-export types
export * from './types';
Expand Down Expand Up @@ -56,36 +56,47 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
constructor(opt: PartialSelectionOptions) {
super();

this._options = deepAssign<SelectionOptions>({
this._options = {
selectionAreaClass: 'selection-area',
selectionContainerClass: undefined,
selectables: [],
document: window.document,
startAreas: ['html'],
boundaries: ['html'],
container: 'body',
...opt,

behaviour: {
overlap: 'invert',
intersect: 'touch',
startThreshold: {x: 10, y: 10},
...opt.behaviour,
startThreshold: opt.behaviour?.startThreshold ?
typeof opt.behaviour.startThreshold === 'number' ?
opt.behaviour.startThreshold :
{x: 10, y: 10, ...opt.behaviour.startThreshold} : {x: 10, y: 10},
scrolling: {
speedDivider: 10,
manualSpeed: 750,
startScrollMargins: {x: 0, y: 0}
...opt.behaviour?.scrolling,
startScrollMargins: {
x: 0,
y: 0,
...opt.behaviour?.scrolling?.startScrollMargins,
}
}
},

features: {
range: true,
touch: true,
...opt.features,
singleTap: {
allow: true,
intersect: 'native'
intersect: 'native',
...opt.features?.singleTap,
}
},

startAreas: ['html'],
boundaries: ['html'],
container: 'body'
}, opt);
}
};

// Bind locale functions to instance
/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down Expand Up @@ -256,14 +267,13 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
const {x, y} = simplifyEvent(evt);

// Check pixel threshold
const thresholdType = typeof startThreshold;
if (

// Single number for both coordinates
(thresholdType === 'number' && abs((x + y) - (x1 + y1)) >= startThreshold) ||
(typeof startThreshold === 'number' && abs((x + y) - (x1 + y1)) >= startThreshold) ||

// Different x and y threshold
(thresholdType === 'object' && abs(x - x1) >= (startThreshold as Coordinates).x || abs(y - y1) >= (startThreshold as Coordinates).y)
(typeof startThreshold === 'object' && abs(x - x1) >= (startThreshold as Coordinates).x || abs(y - y1) >= (startThreshold as Coordinates).y)
) {
off(document, ['mousemove', 'touchmove'], this._delayedTapMove, {passive: false});

Expand Down
15 changes: 10 additions & 5 deletions packages/vanilla/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type SelectionArea from './index';
import type {Intersection} from './utils';

export type Quantify<T> = Readonly<T[]> | T;
/* eslint-disable @typescript-eslint/no-unused-vars */
export type DeepPartial<T> =
T extends (infer U)[] ? T :
T extends HTMLElement ? T :
{ [P in keyof T]?: DeepPartial<T[P]>; };

export type Quantify<T> = T[] | T;

export interface ScrollEvent extends MouseEvent {
deltaY: number;
Expand Down Expand Up @@ -88,7 +94,6 @@ export interface SelectionOptions {
features: Features;
}

export type PartialSelectionOptions = Partial<Omit<SelectionOptions, 'behaviour' | 'features'>> & {
behaviour?: Partial<Behaviour>;
features?: Partial<Features>;
}
export type PartialSelectionOptions = DeepPartial<Omit<SelectionOptions, 'document'>> & {
document?: Document;
};
16 changes: 0 additions & 16 deletions packages/vanilla/src/utils/deepAssign.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/vanilla/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export * from './events';
export * from './intersects';
export * from './selectAll';
export * from './constants';
export * from './deepAssign';
export * from './frames';

0 comments on commit 516ac77

Please sign in to comment.