diff --git a/types/simonwep__selection-js/index.d.ts b/types/simonwep__selection-js/index.d.ts new file mode 100644 index 0000000000..04bcc276e0 --- /dev/null +++ b/types/simonwep__selection-js/index.d.ts @@ -0,0 +1,98 @@ +// Type definitions for simonwep__selection-js 0.2 +// Project: https://github.com/Simonwep/selection +// Definitions by: Mitsuka Hanakura a.k.a. ragg +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.4 + +type ElementList = Element | readonly Element[] | NodeList | HTMLCollection; + +declare namespace Selection { + interface SelectionEvent { + selection: Selection; + eventName: string; + areaElement: readonly Element[]; + originalEvent: MouseEvent | TouchEvent; + selectedElements: readonly Element[]; + changedElements: { + added: readonly Element[]; + removed: readonly Element[]; + }; + } + + interface SingleSelectionEvent extends SelectionEvent { + target: Element; + } + + interface SelectionFilterEvent { + eventName: 'selectionFilter'; + element: Element; + selection: Selection; + } + + type Mode = 'touch' | 'center' | 'cover'; + + interface SelectionOptions { + class?: string; + mode?: Mode; + startThreshold?: number; + singleClick?: boolean; + disableTouch?: boolean; + selectables?: string[]; + scrollSpeedDivider?: number; + startareas?: ReadonlyArray; + boundaries?: ReadonlyArray; + selectionAreaContainer?: string | HTMLElement | ReadonlyArray; + validateStart?(evt: MouseEvent | TouchEvent): boolean; + onStart?(evt: SelectionEvent): void; + onSelect?(evt: SingleSelectionEvent): void; + onMove?(evt: SelectionEvent): void; + onStop?(evt: SelectionEvent): void; + selectionFilter?(evt: SelectionFilterEvent): boolean; + } + + interface SelectionUtils { + on( + el: ElementList, + events: string | ReadonlyArray, + fn: (ev: Event) => void, + options?: AddEventListenerOptions + ): void; + off( + el: ElementList, + events: string | ReadonlyArray, + fn: (ev: Event) => void, + options?: AddEventListenerOptions + ): void; + css(el: Element, attr: Record): any; + css(el: Element, attr: string, val: string | number): any; + intersects(a: Element, b: Element, mode: Mode): boolean; + selectAll(selector: string | HTMLElement | ReadonlyArray): Element[]; + eventPath(evt: Event): EventTarget[]; + removeElement(arr: Element[], el: Element): void; + } +} + +declare class Selection { + static create(options: Selection.SelectionOptions): Selection; + static utils: Selection.SelectionUtils; + static version: string; + + constructor(options: Selection.SelectionOptions); + resolveSelectables(): void; + keepSelection(): void; + clearSelection(): void; + removeFromSelection(el: Element): void; + getSelection(): Element[]; + cancel(keepEvent?: boolean): void; + option( + name: K, + value: Selection.SelectionOptions[K] | null + ): Selection.SelectionOptions[K]; + disable(): void; + destroy(): void; + enable(): void; + select(query: Parameters[0]): this; +} + +export = Selection; +export as namespace Selection; diff --git a/types/simonwep__selection-js/simonwep__selection-js-tests.ts b/types/simonwep__selection-js/simonwep__selection-js-tests.ts new file mode 100644 index 0000000000..19b69c9324 --- /dev/null +++ b/types/simonwep__selection-js/simonwep__selection-js-tests.ts @@ -0,0 +1,55 @@ +import Selection = require('@simonwep/selection-js'); + +const selection = Selection.create({ + class: '.selection', + mode: 'touch', + boundaries: ['.boundary'], + selectables: ['.selectable'], + disableTouch: false, + selectionAreaContainer: document.body, + startareas: ['html'], + singleClick: true, + startThreshold: 10, + scrollSpeedDivider: 10, + onStart(ev) { + ev.originalEvent.currentTarget; + ev.changedElements.added.slice(); + ev.changedElements.removed.slice(); + }, + onSelect(ev) { + ev.target; + }, + onMove(ev) { + ev.selection; + }, + + validateStart(ev) { + return (ev.currentTarget as HTMLElement).matches('.selectable'); + }, + onStop(ev) { + ev.changedElements.added.slice(); + ev.changedElements.removed.slice(); + }, + selectionFilter(ev) { + return ev.element.matches('.selectable'); + }, +}); + +const selectable = document.createElement('div'); + +selection.cancel(); +selection.clearSelection(); +selection.destroy(); +selection.disable(); +selection.enable(); +selection.getSelection(); +selection.keepSelection(); +// 2nd args not accept undefined +// SEE: https://github.com/Simonwep/selection/blob/master/src/selection.js#L528 +selection.option('class', null); +selection.option('class', '.selection'); +selection.removeFromSelection(selectable); +selection.resolveSelectables(); +selection.select('.selectable'); +selection.select(selectable); +selection.select(['.selectable', selectable]); diff --git a/types/simonwep__selection-js/tsconfig.json b/types/simonwep__selection-js/tsconfig.json new file mode 100644 index 0000000000..2d99281626 --- /dev/null +++ b/types/simonwep__selection-js/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@simonwep/selection-js": ["simonwep__selection-js"] + } + }, + "files": [ + "index.d.ts", + "simonwep__selection-js-tests.ts" + ] +} diff --git a/types/simonwep__selection-js/tslint.json b/types/simonwep__selection-js/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/simonwep__selection-js/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }