diff --git a/dist/types/muuri.d.ts b/dist/types/muuri.d.ts new file mode 100644 index 0000000..12f65ad --- /dev/null +++ b/dist/types/muuri.d.ts @@ -0,0 +1,641 @@ +declare module 'muuri'; + +interface StyleDeclaration { + [styleProperty: string]: string; +} + +type EventListener1 = (...args: any[]) => any; + +interface DraggerCssProps { + touchAction?: string; + userSelect?: string; + userDrag?: string; + tapHighlightColor?: string; + touchCallout?: string; + contentZooming?: string; +} + +interface DraggerEvent { + type: 'start' | 'move' | 'end' | 'cancel'; + srcEvent: PointerEvent | TouchEvent | MouseEvent; + distance: number; + deltaX: number; + deltaY: number; + deltaTime: number; + isFirst: boolean; + isFinal: boolean; + pointerType: 'mouse' | 'pen' | 'touch'; + identifier: number; + screenX: number; + screenY: number; + clientX: number; + clientY: number; + pageX: number; + pageY: number; + target: HTMLElement; +} + +interface DraggerStartEvent extends DraggerEvent { + type: 'start'; + distance: 0; + deltaX: 0; + deltaY: 0; + deltaTime: 0; + isFirst: true; + isFinal: false; +} + +interface DraggerMoveEvent extends DraggerEvent { + type: 'move'; + isFirst: false; + isFinal: false; +} + +interface DraggerEndEvent extends DraggerEvent { + type: 'end'; + isFirst: false; + isFinal: true; +} + +interface DraggerCancelEvent extends DraggerEvent { + type: 'cancel'; + isFirst: false; + isFinal: true; +} + +interface DraggerEvents { + start(event: DraggerStartEvent): any; + move(event: DraggerMoveEvent): any; + end(event: DraggerMoveEvent): any; + cancel(event: DraggerCancelEvent): any; +} + +interface ScrollEvent extends Event { + type: 'scroll'; +} + +interface GridEvents { + synchronize(): any; + layoutStart(items: Item[], isInstant: boolean): any; + layoutEnd(items: Item[]): any; + layoutAbort(items: Item[]): any; + add(items: Item[]): any; + remove(items: Item[], indices: number[]): any; + showStart(items: Item[]): any; + showEnd(items: Item[]): any; + hideStart(items: Item[]): any; + hideEnd(items: Item[]): any; + filter(shownItems: Item[], hiddenItems: Item[]): any; + sort(currentOrder: Item[], previousOrder: Item[]): any; + move(data: { + item: Item; + fromIndex: number; + toIndex: number; + action: 'move' | 'swap'; + }): any; + send(data: { + item: Item; + fromGrid: Grid; + fromIndex: number; + toGrid: Grid; + toIndex: number; + }): any; + beforeSend(data: { + item: Item; + fromGrid: Grid; + fromIndex: number; + toGrid: Grid; + toIndex: number; + }): any; + receive(data: { + item: Item; + fromGrid: Grid; + fromIndex: number; + toGrid: Grid; + toIndex: number; + }): any; + beforeReceive(data: { + item: Item; + fromGrid: Grid; + fromIndex: number; + toGrid: Grid; + toIndex: number; + }): any; + dragInit(item: Item, event: DraggerStartEvent | DraggerMoveEvent): any; + dragStart(item: Item, event: DraggerStartEvent | DraggerMoveEvent): any; + dragMove(item: Item, event: DraggerMoveEvent): any; + dragScroll(item: Item, event: ScrollEvent): any; + dragEnd(item: Item, event: DraggerEndEvent | DraggerCancelEvent): any; + dragReleaseStart(item: Item): any; + dragReleaseEnd(item: Item): any; + destroy(): any; +} + +interface LayoutData { + id: number; + items: Item[]; + slots: number[]; + styles?: StyleDeclaration | null; + [key: string]: any; +} + +interface LayoutOptions { + fillGaps?: boolean; + horizontal?: boolean; + alignRight?: boolean; + alignBottom?: boolean; + rounding?: boolean; +} + +type LayoutOnFinish = (items: Item[], isAborted: boolean) => any; + +type LayoutFunctionCallback = (layout: LayoutData) => any; + +type LayoutFunctionCancel = (...args: any[]) => any; + +type LayoutFunction = ( + grid: Grid, + id: number, + items: Item[], + gridWidth: number, + gridHeight: number, + callback: LayoutFunctionCallback +) => void | undefined | LayoutFunctionCancel; + +type SortDataGetter = (item: Item, element: HTMLElement) => any; + +type DragStartPredicate = ( + item: Item, + event: + | DraggerStartEvent + | DraggerMoveEvent + | DraggerEndEvent + | DraggerCancelEvent +) => boolean | undefined; + +interface DragStartPredicateOptions { + distance?: number; + delay?: number; +} + +type DragSortGetter = ( + this: Grid, + item: Item +) => Grid[] | null | void | undefined; + +interface DragSortHeuristics { + sortInterval?: number; + minDragDistance?: number; + minBounceBackAngle?: number; +} + +type DragSortPredicateResult = { + grid: Grid; + index: number; + action: 'move' | 'swap'; +} | null; + +type DragSortPredicate = ( + item: Item, + event: DraggerMoveEvent +) => DragSortPredicateResult; + +interface DragSortPredicateOptions { + threshold?: number; + action?: 'move' | 'swap'; + migrateAction?: 'move' | 'swap'; +} + +interface DragReleaseOptions { + duration?: number; + easing?: string; + useDragContainer?: boolean; +} + +type DragPlaceholderCreateElement = (item: Item) => HTMLElement; + +type DragPlaceholderOnCreate = ( + item: Item, + placeholderElement: HTMLElement +) => any; + +type DragPlaceholderOnRemove = ( + item: Item, + placeholderElement: HTMLElement +) => any; + +interface DragPlaceholderOptions { + enabled?: boolean; + createElement?: DragPlaceholderCreateElement | null; + onCreate?: DragPlaceholderOnCreate | null; + onRemove?: DragPlaceholderOnRemove | null; +} + +interface DragAutoScrollTarget { + element: Window | HTMLElement; + axis?: number; + priority?: number; + threshold?: number; +} + +type DragAutoScrollTargets = Array; + +type DragAutoScrollTargetsGetter = (item: Item) => DragAutoScrollTargets; + +type DragAutoScrollOnStart = ( + item: Item, + scrollElement: Window | HTMLElement, + scrollDirection: number +) => any; + +type DragAutoScrollOnStop = ( + item: Item, + scrollElement: Window | HTMLElement, + scrollDirection: number +) => any; + +type DragAutoScrollHandle = ( + item: Item, + itemClientX: number, + itemClientY: number, + itemWidth: number, + itemHeight: number, + pointerClientX: number, + pointerClientY: number +) => { + left: number; + top: number; + width: number; + height: number; +}; + +type DragAutoScrollSpeed = ( + item: Item, + scrollElement: Window | HTMLElement, + scrollData: { + direction: number; + threshold: number; + distance: number; + value: number; + maxValue: number; + duration: number; + speed: number; + deltaTime: number; + isEnding: boolean; + } +) => number; + +interface DragAutoScrollOptions { + targets?: DragAutoScrollTargets | DragAutoScrollTargetsGetter; + handle?: DragAutoScrollHandle | null; + threshold?: number; + safeZone?: number; + speed?: number | DragAutoScrollSpeed; + sortDuringScroll?: boolean; + syncAfterScroll?: boolean; + smoothStop?: boolean; + onStart?: DragAutoScrollOnStart | null; + onStop?: DragAutoScrollOnStop | null; +} + +interface GridOptions { + items?: HTMLElement[] | NodeList | HTMLCollection | string; + showDuration?: number; + showEasing?: string; + visibleStyles?: StyleDeclaration; + hideDuration?: number; + hideEasing?: string; + hiddenStyles?: StyleDeclaration; + layout?: LayoutOptions | LayoutFunction; + layoutOnResize?: boolean | number; + layoutOnInit?: boolean; + layoutDuration?: number; + layoutEasing?: string; + sortData?: {[key: string]: SortDataGetter} | null; + dragEnabled?: boolean; + dragHandle?: string | null; + dragContainer?: HTMLElement | null; + dragStartPredicate?: DragStartPredicateOptions | DragStartPredicate; + dragAxis?: 'x' | 'y' | 'xy'; + dragSort?: boolean | DragSortGetter; + dragSortHeuristics?: DragSortHeuristics; + dragSortPredicate?: DragSortPredicateOptions | DragSortPredicate; + dragRelease?: DragReleaseOptions; + dragCssProps?: DraggerCssProps; + dragPlaceholder?: DragPlaceholderOptions; + dragAutoScroll?: DragAutoScrollOptions; + containerClass?: string; + itemClass?: string; + itemVisibleClass?: string; + itemHiddenClass?: string; + itemPositioningClass?: string; + itemDraggingClass?: string; + itemReleasingClass?: string; + itemPlaceholderClass?: string; +} + +// +// CLASSES +// + +export class Item { + constructor(grid: Grid, element: HTMLElement, isActive?: boolean); + getGrid(): Grid | undefined; + getElement(): HTMLElement | undefined; + getWidth(): number; + getHeight(): number; + getMargin(): {left: number; right: number; top: number; bottom: number}; + getPosition(): {left: number; top: number}; + isActive(): boolean; + isVisible(): boolean; + isShowing(): boolean; + isHiding(): boolean; + isPositioning(): boolean; + isDragging(): boolean; + isReleasing(): boolean; + isDestroyed(): boolean; +} + +export class ItemLayout { + constructor(item: Item); + start( + instant: boolean, + onFinish?: (isInterrupted: boolean, item: Item) => any + ): void; + stop(processCallbackQueue: boolean, targetStyles?: StyleDeclaration): void; + destroy(): void; +} + +export class ItemVisibility { + constructor(item: Item); + show( + instant: boolean, + onFinish?: (isInterrupted: boolean, item: Item) => any + ): void; + hide( + instant: boolean, + onFinish?: (isInterrupted: boolean, item: Item) => any + ): void; + stop(processCallbackQueue: boolean, applyCurrentStyles?: boolean): void; + setStyles(styles: StyleDeclaration): void; + destroy(): void; +} + +export class ItemMigrate { + constructor(item: Item); + start( + targetGrid: Grid, + position: HTMLElement | number | Item, + container?: HTMLElement + ): void; + stop(abort?: boolean, left?: number, top?: number): void; + destroy(): void; +} + +export class ItemDrag { + constructor(item: Item); + static autoScroller: AutoScroller; + static defaultStartPredicate( + item: Item, + event: DraggerEvent, + options?: DragStartPredicateOptions + ): boolean | undefined; + static defaultSortPredicate( + item: Item, + options?: DragSortPredicateOptions + ): DragSortPredicateResult; + stop(): void; + sort(force?: boolean): void; + destroy(): void; +} + +export class ItemDragRelease { + constructor(item: Item); + start(): void; + stop(abort?: boolean, left?: number, top?: number): void; + isJustReleased(): boolean; + destroy(): void; +} + +export class ItemDragPlaceholder { + constructor(item: Item); + create(): void; + reset(): void; + isActive(): boolean; + getElement(): HTMLElement | null; + updateDimensions(): void; + destroy(): void; +} + +export class Emitter { + constructor(); + on(event: string, listener: EventListener1): this; + off(event: string, listener: EventListener1): this; + clear(event: string): this; + emit(event: string, ...args: any[]): this; + burst(event: string, ...args: any[]): this; + countListeners(event: string): number; + destroy(): this; +} + +export class Animator { + constructor(element: HTMLElement); + start( + propsFrom: StyleDeclaration, + propsTo: StyleDeclaration, + options?: { + duration?: number; + easing?: string; + onFinish?: (...args: any[]) => any; + } + ): void; + stop(applyCurrentStyles?: boolean): void; + isAnimating(): boolean; + destroy(): void; +} + +export class Dragger { + constructor(element: HTMLElement, cssProps?: DraggerCssProps); + isActive(): boolean; + setTouchAction(touchAction: string): void; + setCssProps(props?: DraggerCssProps): void; + getDeltaX(): number; + getDeltaY(): number; + getDistance(): number; + getDeltaTime(): number; + on(event: T, listener: DraggerEvents[T]): void; + off( + event: T, + listener: DraggerEvents[T] + ): void; + destroy(): void; +} + +export class AutoScroller { + constructor(); + static AXIS_X: 1; + static AXIS_Y: 2; + static FORWARD: 4; + static BACKWARD: 8; + static LEFT: 9; + static RIGHT: 5; + static UP: 10; + static DOWN: 6; + static smoothSpeed( + maxSpeed: number, + acceleration: number, + deceleration: number + ): DragAutoScrollSpeed; + static pointerHandle(pointerSize: number): DragAutoScrollHandle; + addItem(item: Item): void; + updateItem(item: Item): void; + removeItem(item: Item): void; + isItemScrollingX(item: Item): boolean; + isItemScrollingY(item: Item): boolean; + isItemScrolling(item: Item): boolean; + destroy(): void; +} + +export class Packer { + constructor(numWorkers?: number, options?: LayoutOptions); + setOptions(options?: LayoutOptions): void; + createLayout( + grid: Grid, + id: number, + items: Item[], + width: number, + height: number, + callback: LayoutFunctionCallback + ): LayoutFunctionCancel | void; + cancelLayout(id: number): void; + destroy(): void; +} + +export class Grid { + constructor(element: string | HTMLElement, options?: GridOptions); + + static Item: typeof Item; + + static ItemLayout: typeof ItemLayout; + + static ItemVisibility: typeof ItemVisibility; + + static ItemMigrate: typeof ItemMigrate; + + static ItemDrag: typeof ItemDrag; + + static ItemDragRelease: typeof ItemDragRelease; + + static ItemDragPlaceholder: typeof ItemDragPlaceholder; + + static Emitter: typeof Emitter; + + static Animator: typeof Animator; + + static Dragger: typeof Dragger; + + static Packer: typeof Packer; + + static AutoScroller: typeof AutoScroller; + + static defaultPacker: Packer; + + static defaultOptions: GridOptions; + + on(event: T, listener: GridEvents[T]): this; + + off(event: T, listener: GridEvents[T]): this; + + getElement(): HTMLElement; + + getItem(target: HTMLElement | number | Item): Item | null; + + getItems( + targets?: HTMLElement | number | Item | Array + ): Item[]; + + refreshItems(items?: Item[], force?: boolean): this; + + refreshSortData(items?: Item[]): this; + + synchronize(): this; + + layout(instant?: boolean, onFinish?: LayoutOnFinish): this; + + add( + elements: HTMLElement | HTMLElement[] | NodeList | HTMLCollection, + options?: { + index?: number; + active?: boolean; + layout?: boolean | 'instant' | LayoutOnFinish; + } + ): Item[]; + + remove( + items: Item[], + options?: { + removeElements?: boolean; + layout?: boolean | 'instant' | LayoutOnFinish; + } + ): Item[]; + + show( + items: Item[], + options?: { + instant?: boolean; + syncWithLayout?: boolean; + onFinish?: (items: Item[]) => any; + layout?: boolean | 'instant' | LayoutOnFinish; + } + ): this; + + hide( + items: Item[], + options?: { + instant?: boolean; + syncWithLayout?: boolean; + onFinish?: (items: Item[]) => any; + layout?: boolean | 'instant' | LayoutOnFinish; + } + ): this; + + filter( + predicate: string | ((item: Item) => boolean), + options?: { + instant?: boolean; + syncWithLayout?: boolean; + onFinish?: (items: Item[]) => any; + layout?: boolean | 'instant' | LayoutOnFinish; + } + ): this; + + sort( + comparer: ((a: Item, b: Item) => number) | string | Item[], + options?: { + descending?: boolean; + layout?: boolean | 'instant' | LayoutOnFinish; + } + ): this; + + move( + item: HTMLElement | number | Item, + position: HTMLElement | number | Item, + options?: { + action?: 'move' | 'swap'; + layout?: boolean | 'instant' | LayoutOnFinish; + } + ): this; + + send( + item: HTMLElement | number | Item, + targetGrid: Grid, + position: HTMLElement | number | Item, + options?: { + appendTo?: HTMLElement; + layoutSender?: boolean | 'instant' | LayoutOnFinish; + layoutReceiver?: boolean | 'instant' | LayoutOnFinish; + } + ): this; + + destroy(removeElements?: boolean): this; +} diff --git a/package.json b/package.json index fb88b1e..ce6f5e4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "muuri-react", - "version": "3.1.5", + "version": "3.1.6", "description": "The layout engine for React.", "main": "dist/muuri-react.cjs.js", "module": "dist/muuri-react.esm.js", @@ -69,6 +69,7 @@ "react-dom": "^16.13.1", "rollup": "^2.3.2", "rollup-plugin-babel": "^4.4.0", + "rollup-plugin-copy": "^3.4.0", "rollup-plugin-terser": "^5.3.0", "typescript": "^3.8.3" }, diff --git a/rollup.config.js b/rollup.config.js index f440583..f7a98da 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,5 +1,6 @@ import babel from 'rollup-plugin-babel'; import {terser} from 'rollup-plugin-terser'; +import copy from 'rollup-plugin-copy'; import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; import replace from '@rollup/plugin-replace'; @@ -45,6 +46,9 @@ export default [ include: 'node_modules/**', }), replace({'process.env.NODE_ENV': JSON.stringify('development')}), + copy({ + targets: [{src: 'src/muuri.d.ts', dest: 'dist/types'}], + }), ], },