-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f47641d
commit 9847dfc
Showing
5 changed files
with
252 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { useRef, useEffect } from 'react'; | ||
import reduce from 'lodash/reduce'; | ||
import { useDeepCompareEffect, useUnmount } from '@pansy/react-hooks'; | ||
import isEqual from 'lodash/isEqual'; | ||
import { isFunction } from '@pansy/shared'; | ||
|
||
import type { SyntheticEvent } from 'react'; | ||
|
||
export type Instance = { | ||
on(type: string, handle: (...args: any[]) => void): void; | ||
off(type: string, handle: (...args: any[]) => void): void; | ||
}; | ||
|
||
export type Listeners<Events extends Record<string, string>> = { | ||
[T in keyof Events]: (evt: SyntheticEvent<any>) => void; | ||
}; | ||
|
||
const getPropsEvents = (props: Record<string, any> = {}) => { | ||
return reduce( | ||
props, | ||
(result, value, key) => { | ||
if (isFunction(value) && /^on[A-Z]/.test(key)) { | ||
// @ts-ignore | ||
result[key] = value; | ||
} | ||
|
||
return result; | ||
}, | ||
{}, | ||
); | ||
}; | ||
|
||
export const useEvents = <Ins extends Instance, Events extends Record<string, string>>( | ||
ins: Ins, | ||
events: Events, | ||
props: Record<string, any> = {}, | ||
) => { | ||
const listeners = useRef<Partial<Listeners<Events>>>({}); | ||
|
||
const propsEvents = getPropsEvents(props); | ||
|
||
useEffect(() => { | ||
if (ins) { | ||
listeners.current = listenEvents(events, props, ins); | ||
} | ||
}, [ins]); | ||
|
||
useDeepCompareEffect(() => { | ||
if (ins) { | ||
listeners.current = updateEvents(listeners.current, propsEvents, ins); | ||
} | ||
}, [propsEvents]); | ||
|
||
useUnmount(() => { | ||
unlistenEvents(listeners.current, ins); | ||
listeners.current = {}; | ||
}); | ||
|
||
const listenEvents = ( | ||
partialEvents: Record<string, string> = {}, | ||
props: Record<string, any> = {}, | ||
ins: Ins, | ||
) => | ||
Object.keys(partialEvents).reduce((listeners, event) => { | ||
const propEvent = props[event]; | ||
|
||
if (propEvent) { | ||
ins.on(partialEvents[event], propEvent); | ||
} | ||
|
||
return listeners; | ||
}, {}); | ||
|
||
const updateEvents = ( | ||
listeners: Partial<Listeners<Events>> = {}, | ||
currentProps: Record<string, any> = {}, | ||
ins: Ins, | ||
) => { | ||
const toListenOff = Object.keys(events).filter( | ||
(eventKey) => | ||
(listeners[eventKey] && typeof currentProps[eventKey] !== 'function') || | ||
!isEqual(listeners[eventKey], currentProps[eventKey]), | ||
); | ||
|
||
toListenOff.forEach((key) => { | ||
if (listeners[key]) { | ||
ins.off(events[key], listeners[key] as any); | ||
delete listeners[key]; | ||
} | ||
}); | ||
|
||
const toListenOn = Object.keys(events) | ||
.filter((key) => !listeners[key] && typeof currentProps[key] === 'function') | ||
// @ts-ignore | ||
.reduce((acc, next) => ((acc[next] = events[next]), acc), {}); | ||
|
||
const newListeners = listenEvents(toListenOn, currentProps, ins); | ||
|
||
return { ...listeners, ...newListeners }; | ||
}; | ||
|
||
const unlistenEvents = (listeners: Partial<Listeners<Events>> = {}, ins: Ins) => { | ||
const toListenOff = Object.keys(events).filter((eventKey) => listeners[eventKey]); | ||
|
||
toListenOff.forEach((key) => { | ||
// @ts-ignore | ||
ins.off(events[key], listeners[key]); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,76 @@ | ||
import type { ReactElement, CSSProperties } from 'react'; | ||
import type { MapboxOptions } from 'mapbox-gl'; | ||
import type { ReactElement } from 'react'; | ||
import type { MapboxOptions, MapEventType } from 'mapbox-gl'; | ||
|
||
export interface MapOptions extends MapboxOptions {} | ||
|
||
export interface MapProps extends Omit<MapOptions, 'container'> { | ||
className?: string; | ||
children?: ReactElement; | ||
} | ||
export type MapEvents = { | ||
onError: (e: MapEventType['error']) => void; | ||
|
||
onLoad: (e: MapEventType['load']) => void; | ||
onIdle: (e: MapEventType['idle']) => void; | ||
onRemove: (e: MapEventType['remove']) => void; | ||
onRender: (e: MapEventType['render']) => void; | ||
onResize: (e: MapEventType['resize']) => void; | ||
|
||
onWebglContextLost: (e: MapEventType['webglcontextlost']) => void; | ||
onWebglContextRestored: (e: MapEventType['webglcontextrestored']) => void; | ||
|
||
onDataloading: (e: MapEventType['dataloading']) => void; | ||
onData: (e: MapEventType['data']) => void; | ||
onTileDataLoading: (e: MapEventType['tiledataloading']) => void; | ||
onSourceDataLoading: (e: MapEventType['sourcedataloading']) => void; | ||
onStyleDataLoading: (e: MapEventType['styledataloading']) => void; | ||
onSourceData: (e: MapEventType['sourcedata']) => void; | ||
onStyleData: (e: MapEventType['styledata']) => void; | ||
|
||
onBoxZoomCancel: (e: MapEventType['boxzoomcancel']) => void; | ||
onBoxZoomStart: (e: MapEventType['boxzoomstart']) => void; | ||
onBoxZoomEnd: (e: MapEventType['boxzoomend']) => void; | ||
|
||
onTouchCancel: (e: MapEventType['touchcancel']) => void; | ||
onTouchMove: (e: MapEventType['touchmove']) => void; | ||
onTouchEnd: (e: MapEventType['touchend']) => void; | ||
onTouchStart: (e: MapEventType['touchstart']) => void; | ||
|
||
onClick: (e: MapEventType['click']) => void; | ||
onContextMenu: (e: MapEventType['contextmenu']) => void; | ||
onDoubleClick: (e: MapEventType['dblclick']) => void; | ||
onMouseMove: (e: MapEventType['mousemove']) => void; | ||
onMouseUp: (e: MapEventType['mouseup']) => void; | ||
onMouseDown: (e: MapEventType['mousedown']) => void; | ||
onMouseOut: (e: MapEventType['mouseout']) => void; | ||
onMouseOver: (e: MapEventType['mouseover']) => void; | ||
|
||
onMoveStart: (e: MapEventType['movestart']) => void; | ||
onMove: (e: MapEventType['move']) => void; | ||
onMoveEnd: (e: MapEventType['moveend']) => void; | ||
|
||
onZoomStart: (e: MapEventType['zoomstart']) => void; | ||
onZoom: (e: MapEventType['zoom']) => void; | ||
onZoomEnd: (e: MapEventType['zoomend']) => void; | ||
|
||
onRotateStart: (e: MapEventType['rotatestart']) => void; | ||
onRotate: (e: MapEventType['rotate']) => void; | ||
onRotateEnd: (e: MapEventType['rotateend']) => void; | ||
|
||
onDragStart: (e: MapEventType['dragstart']) => void; | ||
onDrag: (e: MapEventType['drag']) => void; | ||
onDragEnd: (e: MapEventType['dragend']) => void; | ||
|
||
onPitchStart: (e: MapEventType['pitchstart']) => void; | ||
onPitch: (e: MapEventType['pitch']) => void; | ||
onPitchEnd: (e: MapEventType['pitchend']) => void; | ||
|
||
onWheel: (e: MapEventType['wheel']) => void; | ||
}; | ||
|
||
export type EventMapping = { [T in keyof MapEvents]: string }; | ||
|
||
export type KeysOfUnion<T> = T extends T ? keyof T : never; | ||
|
||
export type PropKey = KeysOfUnion<MapboxOptions>; | ||
|
||
export interface MapProps extends Omit<MapOptions, 'container'>, Partial<MapEvents> { | ||
className?: string; | ||
children?: ReactElement; | ||
} |