Add definition for @simonwep/selection-js (#36461)

* Add definition for @simonwep/selection-js

* Add `export as namespace`
This commit is contained in:
Mitsuka Hanakura a.k.a. ragg
2019-06-27 10:38:19 -07:00
committed by Benjamin Lichtman
parent a176b84419
commit 67d8557b10
4 changed files with 181 additions and 0 deletions
+98
View File
@@ -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 <https://github.com/ra-gg>
// 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<string>;
boundaries?: ReadonlyArray<string>;
selectionAreaContainer?: string | HTMLElement | ReadonlyArray<string | HTMLElement>;
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<string>,
fn: (ev: Event) => void,
options?: AddEventListenerOptions
): void;
off(
el: ElementList,
events: string | ReadonlyArray<string>,
fn: (ev: Event) => void,
options?: AddEventListenerOptions
): void;
css(el: Element, attr: Record<string, string | number>): any;
css(el: Element, attr: string, val: string | number): any;
intersects(a: Element, b: Element, mode: Mode): boolean;
selectAll(selector: string | HTMLElement | ReadonlyArray<string | HTMLElement>): 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<K extends keyof Selection.SelectionOptions>(
name: K,
value: Selection.SelectionOptions[K] | null
): Selection.SelectionOptions[K];
disable(): void;
destroy(): void;
enable(): void;
select(query: Parameters<Selection.SelectionUtils['selectAll']>[0]): this;
}
export = Selection;
export as namespace Selection;
@@ -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]);
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }