@studiometa/js-toolkit 3.0.0-beta.0 → 3.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studiometa/js-toolkit",
3
- "version": "3.0.0-beta.0",
3
+ "version": "3.0.0-beta.1",
4
4
  "description": "A set of useful little bits of JavaScript to boost your project! 🚀",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -20,7 +20,7 @@ export interface ServiceInterface<T> {
20
20
  * Service configuration of events to be attached to targets.
21
21
  */
22
22
  export type ServiceConfig = [
23
- EventTarget | ((instance: AbstractService) => EventTarget),
23
+ ((instance: AbstractService) => EventTarget),
24
24
  [
25
25
  string,
26
26
  AddEventListenerOptions?
@@ -1,4 +1,3 @@
1
- import { isFunction } from "../utils/index.js";
2
1
  class AbstractService {
3
2
  /**
4
3
  * Service configuration.
@@ -90,7 +89,7 @@ class AbstractService {
90
89
  */
91
90
  __manageEvents(mode) {
92
91
  for (const [target, events] of this.constructor.config) {
93
- const resolvedTarget = isFunction(target) ? target(this) : target;
92
+ const resolvedTarget = target(this);
94
93
  for (const [type, options] of events) {
95
94
  resolvedTarget[`${mode}EventListener`](type, this, options);
96
95
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/services/AbstractService.ts"],
4
- "sourcesContent": ["import { isFunction } from '../utils/index.js';\n\nexport interface ServiceInterface<T> {\n /**\n * Remove a function from the resize service by its key.\n */\n remove(key: string): void;\n /**\n * Add a callback to the service. The callback will receive the current service props as parameter.\n */\n add(key: string, callback: (props: T) => void): void;\n /**\n * Test if the service has alreaydy a callback for the given key.\n */\n has(key: string): boolean;\n /**\n * Get the service's props.\n */\n props(): T;\n}\n\n/**\n * Service configuration of events to be attached to targets.\n */\nexport type ServiceConfig = [\n EventTarget | ((instance: AbstractService) => EventTarget),\n [string, AddEventListenerOptions?][],\n][];\n\n/**\n * AbstractService class.\n */\nexport class AbstractService<PropsType = any> {\n /**\n * Used to type `this.constructor` correctly\n * @see https://github.com/microsoft/TypeScript/issues/3841#issuecomment-2381594311\n */\n declare ['constructor']: typeof AbstractService;\n\n /**\n * Service configuration.\n */\n static config: ServiceConfig = [];\n\n /**\n * Cache for the created instances.\n */\n static __instances = new Map();\n\n /**\n * Get a service instance as a singleton based on the given key.\n */\n static getInstance<T extends ServiceInterface<any>>(key: any = this, ...args: any[]) {\n if (!this.__instances.has(key)) {\n // @ts-ignore\n const instance = new this(...args);\n this.__instances.set(key, {\n add: (key, callback) => instance.add(key, callback),\n remove: (key) => instance.remove(key),\n has: (key) => instance.has(key),\n props: () => instance.props,\n } as T);\n }\n\n return this.__instances.get(key);\n }\n\n /**\n * Is the service active or not?\n */\n __isInit = false;\n\n /**\n * Props for the service.\n */\n props = {};\n\n /**\n * Holds all the callbacks that will be triggered.\n */\n callbacks: Map<string, (props: PropsType) => unknown> = new Map();\n\n /**\n * Does the service has the given key?\n */\n has(key: string): boolean {\n return this.callbacks.has(key);\n }\n\n /**\n * Get a service callback by its key.\n */\n get(key: string): (props: PropsType) => unknown {\n return this.callbacks.get(key);\n }\n\n /**\n * Add a callback to the service.\n */\n add(key: string, callback: (props: PropsType) => unknown) {\n if (this.has(key)) {\n console.warn(`The key \\`${key}\\` has already been added.`);\n return;\n }\n\n // Initialize the service when we add the first callback\n if (this.callbacks.size === 0 && !this.__isInit) {\n this.init();\n this.__isInit = true;\n }\n\n this.callbacks.set(key, callback);\n }\n\n /**\n * Remove a callback from the service by its key.\n */\n remove(key: string) {\n this.callbacks.delete(key);\n\n // Kill the service when we remove the last callback\n if (this.callbacks.size === 0 && this.__isInit) {\n this.kill();\n this.__isInit = false;\n }\n }\n\n /**\n * Trigger all service callbacks with service props.\n */\n trigger(props: PropsType) {\n for (const callback of this.callbacks.values()) {\n callback(props);\n }\n }\n\n /* v8 ignore next 6 */\n /**\n * Implements the EventListenerObject interface.\n */\n handleEvent(event: Event) {\n // Should be implemented.\n }\n\n /**\n * Add or remove event listeners based on the static `config` property.\n */\n __manageEvents(mode: 'add' | 'remove') {\n for (const [target, events] of this.constructor.config) {\n const resolvedTarget = isFunction(target) ? target(this) : target;\n for (const [type, options] of events) {\n resolvedTarget[`${mode}EventListener`](type, this, options);\n }\n }\n }\n\n /**\n * Triggered when the service is initialized.\n */\n init() {\n this.__manageEvents('add');\n }\n\n /**\n * Triggered when the service is killed.\n */\n kill() {\n this.__manageEvents('remove');\n }\n}\n"],
5
- "mappings": "AAAA,SAAS,kBAAkB;AAgCpB,MAAM,gBAAiC;AAAA;AAAA;AAAA;AAAA,EAU5C,OAAO,SAAwB,CAAC;AAAA;AAAA;AAAA;AAAA,EAKhC,OAAO,cAAc,oBAAI,IAAI;AAAA;AAAA;AAAA;AAAA,EAK7B,OAAO,YAA6C,MAAW,SAAS,MAAa;AACnF,QAAI,CAAC,KAAK,YAAY,IAAI,GAAG,GAAG;AAE9B,YAAM,WAAW,IAAI,KAAK,GAAG,IAAI;AACjC,WAAK,YAAY,IAAI,KAAK;AAAA,QACxB,KAAK,CAACA,MAAK,aAAa,SAAS,IAAIA,MAAK,QAAQ;AAAA,QAClD,QAAQ,CAACA,SAAQ,SAAS,OAAOA,IAAG;AAAA,QACpC,KAAK,CAACA,SAAQ,SAAS,IAAIA,IAAG;AAAA,QAC9B,OAAO,MAAM,SAAS;AAAA,MACxB,CAAM;AAAA,IACR;AAEA,WAAO,KAAK,YAAY,IAAI,GAAG;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA,EAKA,WAAW;AAAA;AAAA;AAAA;AAAA,EAKX,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA,EAKT,YAAwD,oBAAI,IAAI;AAAA;AAAA;AAAA;AAAA,EAKhE,IAAI,KAAsB;AACxB,WAAO,KAAK,UAAU,IAAI,GAAG;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,KAA4C;AAC9C,WAAO,KAAK,UAAU,IAAI,GAAG;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,KAAa,UAAyC;AACxD,QAAI,KAAK,IAAI,GAAG,GAAG;AACjB,cAAQ,KAAK,aAAa,GAAG,4BAA4B;AACzD;AAAA,IACF;AAGA,QAAI,KAAK,UAAU,SAAS,KAAK,CAAC,KAAK,UAAU;AAC/C,WAAK,KAAK;AACV,WAAK,WAAW;AAAA,IAClB;AAEA,SAAK,UAAU,IAAI,KAAK,QAAQ;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,KAAa;AAClB,SAAK,UAAU,OAAO,GAAG;AAGzB,QAAI,KAAK,UAAU,SAAS,KAAK,KAAK,UAAU;AAC9C,WAAK,KAAK;AACV,WAAK,WAAW;AAAA,IAClB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,QAAQ,OAAkB;AACxB,eAAW,YAAY,KAAK,UAAU,OAAO,GAAG;AAC9C,eAAS,KAAK;AAAA,IAChB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAAY,OAAc;AAAA,EAE1B;AAAA;AAAA;AAAA;AAAA,EAKA,eAAe,MAAwB;AACrC,eAAW,CAAC,QAAQ,MAAM,KAAK,KAAK,YAAY,QAAQ;AACtD,YAAM,iBAAiB,WAAW,MAAM,IAAI,OAAO,IAAI,IAAI;AAC3D,iBAAW,CAAC,MAAM,OAAO,KAAK,QAAQ;AACpC,uBAAe,GAAG,IAAI,eAAe,EAAE,MAAM,MAAM,OAAO;AAAA,MAC5D;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO;AACL,SAAK,eAAe,KAAK;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO;AACL,SAAK,eAAe,QAAQ;AAAA,EAC9B;AACF;",
4
+ "sourcesContent": ["export interface ServiceInterface<T> {\n /**\n * Remove a function from the resize service by its key.\n */\n remove(key: string): void;\n /**\n * Add a callback to the service. The callback will receive the current service props as parameter.\n */\n add(key: string, callback: (props: T) => void): void;\n /**\n * Test if the service has alreaydy a callback for the given key.\n */\n has(key: string): boolean;\n /**\n * Get the service's props.\n */\n props(): T;\n}\n\n/**\n * Service configuration of events to be attached to targets.\n */\nexport type ServiceConfig = [\n ((instance: AbstractService) => EventTarget),\n [string, AddEventListenerOptions?][],\n][];\n\n/**\n * AbstractService class.\n */\nexport class AbstractService<PropsType = any> {\n /**\n * Used to type `this.constructor` correctly\n * @see https://github.com/microsoft/TypeScript/issues/3841#issuecomment-2381594311\n */\n declare ['constructor']: typeof AbstractService;\n\n /**\n * Service configuration.\n */\n static config: ServiceConfig = [];\n\n /**\n * Cache for the created instances.\n */\n static __instances = new Map();\n\n /**\n * Get a service instance as a singleton based on the given key.\n */\n static getInstance<T extends ServiceInterface<any>>(key: any = this, ...args: any[]) {\n if (!this.__instances.has(key)) {\n // @ts-ignore\n const instance = new this(...args);\n this.__instances.set(key, {\n add: (key, callback) => instance.add(key, callback),\n remove: (key) => instance.remove(key),\n has: (key) => instance.has(key),\n props: () => instance.props,\n } as T);\n }\n\n return this.__instances.get(key);\n }\n\n /**\n * Is the service active or not?\n */\n __isInit = false;\n\n /**\n * Props for the service.\n */\n props = {};\n\n /**\n * Holds all the callbacks that will be triggered.\n */\n callbacks: Map<string, (props: PropsType) => unknown> = new Map();\n\n /**\n * Does the service has the given key?\n */\n has(key: string): boolean {\n return this.callbacks.has(key);\n }\n\n /**\n * Get a service callback by its key.\n */\n get(key: string): (props: PropsType) => unknown {\n return this.callbacks.get(key);\n }\n\n /**\n * Add a callback to the service.\n */\n add(key: string, callback: (props: PropsType) => unknown) {\n if (this.has(key)) {\n console.warn(`The key \\`${key}\\` has already been added.`);\n return;\n }\n\n // Initialize the service when we add the first callback\n if (this.callbacks.size === 0 && !this.__isInit) {\n this.init();\n this.__isInit = true;\n }\n\n this.callbacks.set(key, callback);\n }\n\n /**\n * Remove a callback from the service by its key.\n */\n remove(key: string) {\n this.callbacks.delete(key);\n\n // Kill the service when we remove the last callback\n if (this.callbacks.size === 0 && this.__isInit) {\n this.kill();\n this.__isInit = false;\n }\n }\n\n /**\n * Trigger all service callbacks with service props.\n */\n trigger(props: PropsType) {\n for (const callback of this.callbacks.values()) {\n callback(props);\n }\n }\n\n /* v8 ignore next 6 */\n /**\n * Implements the EventListenerObject interface.\n */\n handleEvent(event: Event) {\n // Should be implemented.\n }\n\n /**\n * Add or remove event listeners based on the static `config` property.\n */\n __manageEvents(mode: 'add' | 'remove') {\n for (const [target, events] of this.constructor.config) {\n const resolvedTarget = target(this);\n for (const [type, options] of events) {\n resolvedTarget[`${mode}EventListener`](type, this, options);\n }\n }\n }\n\n /**\n * Triggered when the service is initialized.\n */\n init() {\n this.__manageEvents('add');\n }\n\n /**\n * Triggered when the service is killed.\n */\n kill() {\n this.__manageEvents('remove');\n }\n}\n"],
5
+ "mappings": "AA8BO,MAAM,gBAAiC;AAAA;AAAA;AAAA;AAAA,EAU5C,OAAO,SAAwB,CAAC;AAAA;AAAA;AAAA;AAAA,EAKhC,OAAO,cAAc,oBAAI,IAAI;AAAA;AAAA;AAAA;AAAA,EAK7B,OAAO,YAA6C,MAAW,SAAS,MAAa;AACnF,QAAI,CAAC,KAAK,YAAY,IAAI,GAAG,GAAG;AAE9B,YAAM,WAAW,IAAI,KAAK,GAAG,IAAI;AACjC,WAAK,YAAY,IAAI,KAAK;AAAA,QACxB,KAAK,CAACA,MAAK,aAAa,SAAS,IAAIA,MAAK,QAAQ;AAAA,QAClD,QAAQ,CAACA,SAAQ,SAAS,OAAOA,IAAG;AAAA,QACpC,KAAK,CAACA,SAAQ,SAAS,IAAIA,IAAG;AAAA,QAC9B,OAAO,MAAM,SAAS;AAAA,MACxB,CAAM;AAAA,IACR;AAEA,WAAO,KAAK,YAAY,IAAI,GAAG;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA,EAKA,WAAW;AAAA;AAAA;AAAA;AAAA,EAKX,QAAQ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AK;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO;AACL,SAAK,eAAe,QAAQ;AAAA,EAC9B;AACF;",
6
6
  "names": ["key"]
7
7
  }
@@ -21,7 +21,7 @@ class DragService extends AbstractService {
21
21
  ]
22
22
  ],
23
23
  [
24
- window,
24
+ () => window,
25
25
  [
26
26
  ["pointerup", PASSIVE_EVENT_OPTIONS],
27
27
  ["touchend", PASSIVE_EVENT_OPTIONS]
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/services/DragService.ts"],
4
- "sourcesContent": ["import type { ServiceConfig, ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport { useRaf } from './RafService.js';\nimport { isDefined, inertiaFinalValue } from '../utils/index.js';\nimport { PASSIVE_EVENT_OPTIONS, CAPTURE_EVENT_OPTIONS } from './utils.js';\n\ntype DragLifecycle = 'start' | 'drag' | 'drop' | 'inertia' | 'stop';\n\nexport interface DragServiceProps {\n /**\n * The current mode of the dragging logic.\n */\n mode: DragLifecycle;\n /**\n * The target element of the drag.\n */\n target: HTMLElement;\n /**\n * Whether the user is currently grabbing the targeted element or not.\n */\n isGrabbing: boolean;\n /**\n * Whether the drag currently has inertia or not.\n */\n hasInertia: boolean;\n /**\n * The current horizontal position in the viewport.\n */\n x: number;\n /**\n * The current vertical position in the viewport.\n */\n y: number;\n /**\n * The delta between the current position and the last.\n */\n delta: { x: number; y: number };\n /**\n * The initial position where the dragging started.\n */\n origin: { x: number; y: number };\n /**\n * The distance from the current position to the origin.\n */\n distance: { x: number; y: number };\n /**\n * The final position that will be reached at the end of the inertia.\n */\n final: { x: number; y: number };\n}\n\nexport type DragServiceInterface = ServiceInterface<DragServiceProps>;\n\nexport interface DragServiceOptions {\n dampFactor?: number;\n dragTreshold?: number;\n}\n\nlet count = 0;\n\nexport class DragService extends AbstractService<DragServiceProps> {\n static MODES: Record<Uppercase<DragLifecycle>, DragLifecycle> = {\n START: 'start',\n DRAG: 'drag',\n DROP: 'drop',\n INERTIA: 'inertia',\n STOP: 'stop',\n };\n\n static config: ServiceConfig = [\n [\n (instance) => (instance as DragService).props.target,\n [\n ['dragstart', CAPTURE_EVENT_OPTIONS],\n ['click', CAPTURE_EVENT_OPTIONS],\n ['pointerdown', PASSIVE_EVENT_OPTIONS],\n ],\n ],\n [\n window,\n [\n ['pointerup', PASSIVE_EVENT_OPTIONS],\n ['touchend', PASSIVE_EVENT_OPTIONS],\n ],\n ],\n ];\n\n id: string;\n\n dampFactor = 0.85;\n\n dragTreshold = 10;\n\n previousEvent: TouchEvent | MouseEvent = null;\n\n props = {\n target: null,\n mode: undefined,\n isGrabbing: false,\n hasInertia: false,\n x: 0,\n y: 0,\n delta: {\n x: 0,\n y: 0,\n },\n origin: {\n x: 0,\n y: 0,\n },\n distance: {\n x: 0,\n y: 0,\n },\n final: {\n x: 0,\n y: 0,\n },\n } as DragServiceProps;\n\n /**\n * Test if we should allow click on links and buttons.\n */\n get shouldPreventClick(): boolean {\n return (\n Math.abs(this.props.distance.x) > this.dragTreshold ||\n Math.abs(this.props.distance.y) > this.dragTreshold\n );\n }\n\n constructor(\n target: HTMLElement,\n { dampFactor = 0.85, dragTreshold = 10 }: DragServiceOptions = {},\n ) {\n super();\n count += 1;\n this.id = `drag-${count}`;\n this.dampFactor = dampFactor;\n this.dragTreshold = dragTreshold;\n this.props.target = target;\n }\n\n /**\n * Get the client value for the given axis.\n */\n getEventPosition(event: MouseEvent | TouchEvent): { x: number; y: number } {\n // @ts-ignore\n const eventOrTouch = isDefined(event.touches) ? event.touches[0] : event;\n return {\n x: eventOrTouch.clientX,\n y: eventOrTouch.clientY,\n };\n }\n\n /**\n * Start the drag.\n *\n * @param {number} x The initial horizontal position.\n * @param {number} y The initial vertical position.\n */\n start(x: number, y: number) {\n const { props } = this;\n if (props.isGrabbing) {\n return;\n }\n\n // Reset state\n props.x = props.origin.x = props.final.x = x;\n props.y = props.origin.y = props.final.y = y;\n props.delta.x = props.distance.x = 0;\n props.delta.y = props.distance.y = 0;\n props.mode = DragService.MODES.START;\n props.isGrabbing = true;\n\n this.trigger(props);\n\n document.addEventListener('touchmove', this, PASSIVE_EVENT_OPTIONS);\n document.addEventListener('mousemove', this, PASSIVE_EVENT_OPTIONS);\n }\n\n /**\n * Stop the drag, or drop.\n */\n drop() {\n const { props, dampFactor, id } = this;\n\n if (!props.isGrabbing) {\n return;\n }\n\n document.removeEventListener('touchmove', this);\n document.removeEventListener('mousemove', this);\n\n props.isGrabbing = false;\n props.mode = DragService.MODES.DROP;\n props.hasInertia = true;\n props.final.x = inertiaFinalValue(props.x, props.delta.x, dampFactor);\n props.final.y = inertiaFinalValue(props.y, props.delta.y, dampFactor);\n this.previousEvent = null;\n\n this.trigger(props);\n\n setTimeout(() => {\n const raf = useRaf();\n raf.remove(id);\n raf.add(id, () => this.rafHandler());\n });\n }\n\n /**\n * Stop the drag.\n */\n stop() {\n const { props, id } = this;\n\n useRaf().remove(id);\n props.isGrabbing = false;\n props.hasInertia = false;\n props.mode = DragService.MODES.STOP;\n\n this.trigger(props);\n }\n\n /**\n * Raf service handler.\n */\n rafHandler() {\n const { props, dampFactor } = this;\n\n if (!props.isGrabbing) {\n props.x += props.delta.x;\n props.y += props.delta.y;\n props.distance.x = props.x - props.origin.x;\n props.distance.y = props.y - props.origin.y;\n props.delta.x *= dampFactor;\n props.delta.y *= dampFactor;\n props.mode = DragService.MODES.INERTIA;\n\n this.trigger(props);\n\n if (Math.abs(props.delta.x) < 0.1 && Math.abs(props.delta.y) < 0.1) {\n this.stop();\n }\n }\n }\n\n /**\n * Pointer service handler.\n */\n drag(event: TouchEvent | MouseEvent) {\n const { props } = this;\n\n if (props.isGrabbing) {\n const position = this.getEventPosition(event);\n props.x = position.x;\n props.y = position.y;\n\n if (this.previousEvent) {\n const previousPosition = this.getEventPosition(this.previousEvent);\n props.delta.x = position.x - previousPosition.x;\n props.delta.y = position.y - previousPosition.y;\n }\n\n props.final.x = position.x;\n props.final.y = position.y;\n props.distance.x = props.x - props.origin.x;\n props.distance.y = props.y - props.origin.y;\n props.mode = DragService.MODES.DRAG;\n\n this.trigger(props);\n this.previousEvent = event;\n }\n }\n\n /**\n * Handle any event.\n */\n handleEvent(event: MouseEvent | PointerEvent | DragEvent) {\n switch (event.type) {\n case 'dragstart':\n event.preventDefault();\n break;\n case 'click':\n if (this.shouldPreventClick) {\n event.stopImmediatePropagation();\n event.stopPropagation();\n event.preventDefault();\n }\n break;\n case 'pointerup':\n case 'touchend':\n this.drop();\n break;\n case 'touchmove':\n case 'mousemove':\n this.drag(event);\n break;\n default:\n if (event.button === 0) {\n this.start(event.x, event.y);\n }\n }\n }\n}\n\n/**\n * Use the drag service.\n *\n * ```js\n * import { useDrag } from '@studiometa/js-toolkit/services';\n * const element = document.querySelector('.draggable');\n * const { add, remove, props } = useDrag(element, { dampFactor: 0.5 });\n * add('key', (props) => console.log(props));\n * remove('key');\n * props();\n * ```\n */\nexport function useDrag(target: HTMLElement, options: DragServiceOptions): DragServiceInterface {\n return DragService.getInstance<DragServiceInterface>(target, target, options);\n}\n"],
5
- "mappings": "AACA,SAAS,uBAAuB;AAChC,SAAS,cAAc;AACvB,SAAS,WAAW,yBAAyB;AAC7C,SAAS,uBAAuB,6BAA6B;AAsD7D,IAAI,QAAQ;AAEL,MAAM,oBAAoB,gBAAkC;AAAA,EACjE,OAAO,QAAyD;AAAA,IAC9D,OAAO;AAAA,IACP,MAAM;AAAA,IACN,MAAM;AAAA,IACN,SAAS;AAAA,IACT,MAAM;AAAA,EACR;AAAA,EAEA,OAAO,SAAwB;AAAA,IAC7B;AAAA,MACE,CAAC,aAAc,SAAyB,MAAM;AAAA,MAC9C;AAAA,QACE,CAAC,aAAa,qBAAqB;AAAA,QACnC,CAAC,SAAS,qBAAqB;AAAA,QAC/B,CAAC,eAAe,qBAAqB;AAAA,MACvC;AAAA,IACF;AAAA,IACA;AAAA,MACE;AAAA,MACA;AAAA,QACE,CAA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AAC/B,gBAAM,gBAAgB;AACtB,gBAAM,eAAe;AAAA,QACvB;AACA;AAAA,MACF,KAAK;AAAA,MACL,KAAK;AACH,aAAK,KAAK;AACV;AAAA,MACF,KAAK;AAAA,MACL,KAAK;AACH,aAAK,KAAK,KAAK;AACf;AAAA,MACF;AACE,YAAI,MAAM,WAAW,GAAG;AACtB,eAAK,MAAM,MAAM,GAAG,MAAM,CAAC;AAAA,QAC7B;AAAA,IACJ;AAAA,EACF;AACF;AAcO,SAAS,QAAQ,QAAqB,SAAmD;AAC9F,SAAO,YAAY,YAAkC,QAAQ,QAAQ,OAAO;AAC9E;",
4
+ "sourcesContent": ["import type { ServiceConfig, ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport { useRaf } from './RafService.js';\nimport { isDefined, inertiaFinalValue } from '../utils/index.js';\nimport { PASSIVE_EVENT_OPTIONS, CAPTURE_EVENT_OPTIONS } from './utils.js';\n\ntype DragLifecycle = 'start' | 'drag' | 'drop' | 'inertia' | 'stop';\n\nexport interface DragServiceProps {\n /**\n * The current mode of the dragging logic.\n */\n mode: DragLifecycle;\n /**\n * The target element of the drag.\n */\n target: HTMLElement;\n /**\n * Whether the user is currently grabbing the targeted element or not.\n */\n isGrabbing: boolean;\n /**\n * Whether the drag currently has inertia or not.\n */\n hasInertia: boolean;\n /**\n * The current horizontal position in the viewport.\n */\n x: number;\n /**\n * The current vertical position in the viewport.\n */\n y: number;\n /**\n * The delta between the current position and the last.\n */\n delta: { x: number; y: number };\n /**\n * The initial position where the dragging started.\n */\n origin: { x: number; y: number };\n /**\n * The distance from the current position to the origin.\n */\n distance: { x: number; y: number };\n /**\n * The final position that will be reached at the end of the inertia.\n */\n final: { x: number; y: number };\n}\n\nexport type DragServiceInterface = ServiceInterface<DragServiceProps>;\n\nexport interface DragServiceOptions {\n dampFactor?: number;\n dragTreshold?: number;\n}\n\nlet count = 0;\n\nexport class DragService extends AbstractService<DragServiceProps> {\n static MODES: Record<Uppercase<DragLifecycle>, DragLifecycle> = {\n START: 'start',\n DRAG: 'drag',\n DROP: 'drop',\n INERTIA: 'inertia',\n STOP: 'stop',\n };\n\n static config: ServiceConfig = [\n [\n (instance) => (instance as DragService).props.target,\n [\n ['dragstart', CAPTURE_EVENT_OPTIONS],\n ['click', CAPTURE_EVENT_OPTIONS],\n ['pointerdown', PASSIVE_EVENT_OPTIONS],\n ],\n ],\n [\n () => window,\n [\n ['pointerup', PASSIVE_EVENT_OPTIONS],\n ['touchend', PASSIVE_EVENT_OPTIONS],\n ],\n ],\n ];\n\n id: string;\n\n dampFactor = 0.85;\n\n dragTreshold = 10;\n\n previousEvent: TouchEvent | MouseEvent = null;\n\n props = {\n target: null,\n mode: undefined,\n isGrabbing: false,\n hasInertia: false,\n x: 0,\n y: 0,\n delta: {\n x: 0,\n y: 0,\n },\n origin: {\n x: 0,\n y: 0,\n },\n distance: {\n x: 0,\n y: 0,\n },\n final: {\n x: 0,\n y: 0,\n },\n } as DragServiceProps;\n\n /**\n * Test if we should allow click on links and buttons.\n */\n get shouldPreventClick(): boolean {\n return (\n Math.abs(this.props.distance.x) > this.dragTreshold ||\n Math.abs(this.props.distance.y) > this.dragTreshold\n );\n }\n\n constructor(\n target: HTMLElement,\n { dampFactor = 0.85, dragTreshold = 10 }: DragServiceOptions = {},\n ) {\n super();\n count += 1;\n this.id = `drag-${count}`;\n this.dampFactor = dampFactor;\n this.dragTreshold = dragTreshold;\n this.props.target = target;\n }\n\n /**\n * Get the client value for the given axis.\n */\n getEventPosition(event: MouseEvent | TouchEvent): { x: number; y: number } {\n // @ts-ignore\n const eventOrTouch = isDefined(event.touches) ? event.touches[0] : event;\n return {\n x: eventOrTouch.clientX,\n y: eventOrTouch.clientY,\n };\n }\n\n /**\n * Start the drag.\n *\n * @param {number} x The initial horizontal position.\n * @param {number} y The initial vertical position.\n */\n start(x: number, y: number) {\n const { props } = this;\n if (props.isGrabbing) {\n return;\n }\n\n // Reset state\n props.x = props.origin.x = props.final.x = x;\n props.y = props.origin.y = props.final.y = y;\n props.delta.x = props.distance.x = 0;\n props.delta.y = props.distance.y = 0;\n props.mode = DragService.MODES.START;\n props.isGrabbing = true;\n\n this.trigger(props);\n\n document.addEventListener('touchmove', this, PASSIVE_EVENT_OPTIONS);\n document.addEventListener('mousemove', this, PASSIVE_EVENT_OPTIONS);\n }\n\n /**\n * Stop the drag, or drop.\n */\n drop() {\n const { props, dampFactor, id } = this;\n\n if (!props.isGrabbing) {\n return;\n }\n\n document.removeEventListener('touchmove', this);\n document.removeEventListener('mousemove', this);\n\n props.isGrabbing = false;\n props.mode = DragService.MODES.DROP;\n props.hasInertia = true;\n props.final.x = inertiaFinalValue(props.x, props.delta.x, dampFactor);\n props.final.y = inertiaFinalValue(props.y, props.delta.y, dampFactor);\n this.previousEvent = null;\n\n this.trigger(props);\n\n setTimeout(() => {\n const raf = useRaf();\n raf.remove(id);\n raf.add(id, () => this.rafHandler());\n });\n }\n\n /**\n * Stop the drag.\n */\n stop() {\n const { props, id } = this;\n\n useRaf().remove(id);\n props.isGrabbing = false;\n props.hasInertia = false;\n props.mode = DragService.MODES.STOP;\n\n this.trigger(props);\n }\n\n /**\n * Raf service handler.\n */\n rafHandler() {\n const { props, dampFactor } = this;\n\n if (!props.isGrabbing) {\n props.x += props.delta.x;\n props.y += props.delta.y;\n props.distance.x = props.x - props.origin.x;\n props.distance.y = props.y - props.origin.y;\n props.delta.x *= dampFactor;\n props.delta.y *= dampFactor;\n props.mode = DragService.MODES.INERTIA;\n\n this.trigger(props);\n\n if (Math.abs(props.delta.x) < 0.1 && Math.abs(props.delta.y) < 0.1) {\n this.stop();\n }\n }\n }\n\n /**\n * Pointer service handler.\n */\n drag(event: TouchEvent | MouseEvent) {\n const { props } = this;\n\n if (props.isGrabbing) {\n const position = this.getEventPosition(event);\n props.x = position.x;\n props.y = position.y;\n\n if (this.previousEvent) {\n const previousPosition = this.getEventPosition(this.previousEvent);\n props.delta.x = position.x - previousPosition.x;\n props.delta.y = position.y - previousPosition.y;\n }\n\n props.final.x = position.x;\n props.final.y = position.y;\n props.distance.x = props.x - props.origin.x;\n props.distance.y = props.y - props.origin.y;\n props.mode = DragService.MODES.DRAG;\n\n this.trigger(props);\n this.previousEvent = event;\n }\n }\n\n /**\n * Handle any event.\n */\n handleEvent(event: MouseEvent | PointerEvent | DragEvent) {\n switch (event.type) {\n case 'dragstart':\n event.preventDefault();\n break;\n case 'click':\n if (this.shouldPreventClick) {\n event.stopImmediatePropagation();\n event.stopPropagation();\n event.preventDefault();\n }\n break;\n case 'pointerup':\n case 'touchend':\n this.drop();\n break;\n case 'touchmove':\n case 'mousemove':\n this.drag(event);\n break;\n default:\n if (event.button === 0) {\n this.start(event.x, event.y);\n }\n }\n }\n}\n\n/**\n * Use the drag service.\n *\n * ```js\n * import { useDrag } from '@studiometa/js-toolkit/services';\n * const element = document.querySelector('.draggable');\n * const { add, remove, props } = useDrag(element, { dampFactor: 0.5 });\n * add('key', (props) => console.log(props));\n * remove('key');\n * props();\n * ```\n */\nexport function useDrag(target: HTMLElement, options: DragServiceOptions): DragServiceInterface {\n return DragService.getInstance<DragServiceInterface>(target, target, options);\n}\n"],
5
+ "mappings": "AACA,SAAS,uBAAuB;AAChC,SAAS,cAAc;AACvB,SAAS,WAAW,yBAAyB;AAC7C,SAAS,uBAAuB,6BAA6B;AAsD7D,IAAI,QAAQ;AAEL,MAAM,oBAAoB,gBAAkC;AAAA,EACjE,OAAO,QAAyD;AAAA,IAC9D,OAAO;AAAA,IACP,MAAM;AAAA,IACN,MAAM;AAAA,IACN,SAAS;AAAA,IACT,MAAM;AAAA,EACR;AAAA,EAEA,OAAO,SAAwB;AAAA,IAC7B;AAAA,MACE,CAAC,aAAc,SAAyB,MAAM;AAAA,MAC9C;AAAA,QACE,CAAC,aAAa,qBAAqB;AAAA,QACnC,CAAC,SAAS,qBAAqB;AAAA,QAC/B,CAAC,eAAe,qBAAqB;AAAA,MACvC;AAAA,IACF;AAAA,IACA;AAAA,MACE,MA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AAoB;AAC3B,gBAAM,yBAAyB;AAC/B,gBAAM,gBAAgB;AACtB,gBAAM,eAAe;AAAA,QACvB;AACA;AAAA,MACF,KAAK;AAAA,MACL,KAAK;AACH,aAAK,KAAK;AACV;AAAA,MACF,KAAK;AAAA,MACL,KAAK;AACH,aAAK,KAAK,KAAK;AACf;AAAA,MACF;AACE,YAAI,MAAM,WAAW,GAAG;AACtB,eAAK,MAAM,MAAM,GAAG,MAAM,CAAC;AAAA,QAC7B;AAAA,IACJ;AAAA,EACF;AACF;AAcO,SAAS,QAAQ,QAAqB,SAAmD;AAC9F,SAAO,YAAY,YAAkC,QAAQ,QAAQ,OAAO;AAC9E;",
6
6
  "names": []
7
7
  }
@@ -4,7 +4,7 @@ function getInitialKeyCodes() {
4
4
  return Object.fromEntries(Object.keys(keyCodes).map((key) => [key, false]));
5
5
  }
6
6
  class KeyService extends AbstractService {
7
- static config = [[document, [["keydown"], ["keyup"]]]];
7
+ static config = [[() => document, [["keydown"], ["keyup"]]]];
8
8
  previousEvent = null;
9
9
  props = {
10
10
  event: null,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/services/KeyService.ts"],
4
- "sourcesContent": ["import type { ServiceConfig, ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport keyCodes from '../utils/keyCodes.js';\n\nfunction getInitialKeyCodes(): Record<keyof typeof keyCodes, false> {\n return Object.fromEntries(Object.keys(keyCodes).map((key) => [key, false])) as Record<\n keyof typeof keyCodes,\n false\n >;\n}\n\nexport interface KeyServiceProps {\n event: KeyboardEvent;\n triggered: number;\n direction: 'up' | 'down' | 'none';\n isUp: boolean;\n isDown: boolean;\n ENTER: boolean;\n SPACE: boolean;\n TAB: boolean;\n ESC: boolean;\n LEFT: boolean;\n UP: boolean;\n RIGHT: boolean;\n DOWN: boolean;\n}\n\nexport type KeyServiceInterface = ServiceInterface<KeyServiceProps>;\n\nexport class KeyService extends AbstractService<KeyServiceProps> {\n static config: ServiceConfig = [[document, [['keydown'], ['keyup']]]];\n\n previousEvent: Event | null = null;\n\n props: KeyServiceProps = {\n event: null,\n triggered: 0,\n isUp: false,\n isDown: false,\n direction: 'none',\n ...getInitialKeyCodes(),\n };\n\n updateProps(event: KeyboardEvent): KeyServiceProps {\n const { props } = this;\n\n props.event = event;\n\n for (const [name, code] of Object.entries(keyCodes)) {\n props[name] = code === event.keyCode;\n }\n\n if (!this.previousEvent) {\n props.triggered = 0;\n }\n\n if (props.event.type === 'keydown' && this.previousEvent?.type === 'keydown') {\n props.triggered += 1;\n } else {\n props.triggered = 1;\n }\n\n this.previousEvent = props.event;\n\n props.direction = props.event.type === 'keydown' ? 'down' : 'up';\n props.isUp = props.event.type === 'keyup';\n props.isDown = props.event.type === 'keydown';\n\n return props;\n }\n\n handleEvent(event: KeyboardEvent) {\n this.trigger(this.updateProps(event));\n }\n}\n\n/**\n * Use the keyboard service.\n */\nexport function useKey(): KeyServiceInterface {\n return KeyService.getInstance();\n}\n"],
5
- "mappings": "AACA,SAAS,uBAAuB;AAChC,OAAO,cAAc;AAErB,SAAS,qBAA2D;AAClE,SAAO,OAAO,YAAY,OAAO,KAAK,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,KAAK,CAAC,CAAC;AAI5E;AAoBO,MAAM,mBAAmB,gBAAiC;AAAA,EAC/D,OAAO,SAAwB,CAAC,CAAC,UAAU,CAAC,CAAC,SAAS,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;AAAA,EAEpE,gBAA8B;AAAA,EAE9B,QAAyB;AAAA,IACvB,OAAO;AAAA,IACP,WAAW;AAAA,IACX,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,WAAW;AAAA,IACX,GAAG,mBAAmB;AAAA,EACxB;AAAA,EAEA,YAAY,OAAuC;AACjD,UAAM,EAAE,MAAM,IAAI;AAElB,UAAM,QAAQ;AAEd,eAAW,CAAC,MAAM,IAAI,KAAK,OAAO,QAAQ,QAAQ,GAAG;AACnD,YAAM,IAAI,IAAI,SAAS,MAAM;AAAA,IAC/B;AAEA,QAAI,CAAC,KAAK,eAAe;AACvB,YAAM,YAAY;AAAA,IACpB;AAEA,QAAI,MAAM,MAAM,SAAS,aAAa,KAAK,eAAe,SAAS,WAAW;AAC5E,YAAM,aAAa;AAAA,IACrB,OAAO;AACL,YAAM,YAAY;AAAA,IACpB;AAEA,SAAK,gBAAgB,MAAM;AAE3B,UAAM,YAAY,MAAM,MAAM,SAAS,YAAY,SAAS;AAC5D,UAAM,OAAO,MAAM,MAAM,SAAS;AAClC,UAAM,SAAS,MAAM,MAAM,SAAS;AAEpC,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,OAAsB;AAChC,SAAK,QAAQ,KAAK,YAAY,KAAK,CAAC;AAAA,EACtC;AACF;AAKO,SAAS,SAA8B;AAC5C,SAAO,WAAW,YAAY;AAChC;",
4
+ "sourcesContent": ["import type { ServiceConfig, ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport keyCodes from '../utils/keyCodes.js';\n\nfunction getInitialKeyCodes(): Record<keyof typeof keyCodes, false> {\n return Object.fromEntries(Object.keys(keyCodes).map((key) => [key, false])) as Record<\n keyof typeof keyCodes,\n false\n >;\n}\n\nexport interface KeyServiceProps {\n event: KeyboardEvent;\n triggered: number;\n direction: 'up' | 'down' | 'none';\n isUp: boolean;\n isDown: boolean;\n ENTER: boolean;\n SPACE: boolean;\n TAB: boolean;\n ESC: boolean;\n LEFT: boolean;\n UP: boolean;\n RIGHT: boolean;\n DOWN: boolean;\n}\n\nexport type KeyServiceInterface = ServiceInterface<KeyServiceProps>;\n\nexport class KeyService extends AbstractService<KeyServiceProps> {\n static config: ServiceConfig = [[() => document, [['keydown'], ['keyup']]]];\n\n previousEvent: Event | null = null;\n\n props: KeyServiceProps = {\n event: null,\n triggered: 0,\n isUp: false,\n isDown: false,\n direction: 'none',\n ...getInitialKeyCodes(),\n };\n\n updateProps(event: KeyboardEvent): KeyServiceProps {\n const { props } = this;\n\n props.event = event;\n\n for (const [name, code] of Object.entries(keyCodes)) {\n props[name] = code === event.keyCode;\n }\n\n if (!this.previousEvent) {\n props.triggered = 0;\n }\n\n if (props.event.type === 'keydown' && this.previousEvent?.type === 'keydown') {\n props.triggered += 1;\n } else {\n props.triggered = 1;\n }\n\n this.previousEvent = props.event;\n\n props.direction = props.event.type === 'keydown' ? 'down' : 'up';\n props.isUp = props.event.type === 'keyup';\n props.isDown = props.event.type === 'keydown';\n\n return props;\n }\n\n handleEvent(event: KeyboardEvent) {\n this.trigger(this.updateProps(event));\n }\n}\n\n/**\n * Use the keyboard service.\n */\nexport function useKey(): KeyServiceInterface {\n return KeyService.getInstance();\n}\n"],
5
+ "mappings": "AACA,SAAS,uBAAuB;AAChC,OAAO,cAAc;AAErB,SAAS,qBAA2D;AAClE,SAAO,OAAO,YAAY,OAAO,KAAK,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,KAAK,CAAC,CAAC;AAI5E;AAoBO,MAAM,mBAAmB,gBAAiC;AAAA,EAC/D,OAAO,SAAwB,CAAC,CAAC,MAAM,UAAU,CAAC,CAAC,SAAS,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;AAAA,EAE1E,gBAA8B;AAAA,EAE9B,QAAyB;AAAA,IACvB,OAAO;AAAA,IACP,WAAW;AAAA,IACX,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,WAAW;AAAA,IACX,GAAG,mBAAmB;AAAA,EACxB;AAAA,EAEA,YAAY,OAAuC;AACjD,UAAM,EAAE,MAAM,IAAI;AAElB,UAAM,QAAQ;AAEd,eAAW,CAAC,MAAM,IAAI,KAAK,OAAO,QAAQ,QAAQ,GAAG;AACnD,YAAM,IAAI,IAAI,SAAS,MAAM;AAAA,IAC/B;AAEA,QAAI,CAAC,KAAK,eAAe;AACvB,YAAM,YAAY;AAAA,IACpB;AAEA,QAAI,MAAM,MAAM,SAAS,aAAa,KAAK,eAAe,SAAS,WAAW;AAC5E,YAAM,aAAa;AAAA,IACrB,OAAO;AACL,YAAM,YAAY;AAAA,IACpB;AAEA,SAAK,gBAAgB,MAAM;AAE3B,UAAM,YAAY,MAAM,MAAM,SAAS,YAAY,SAAS;AAC5D,UAAM,OAAO,MAAM,MAAM,SAAS;AAClC,UAAM,SAAS,MAAM,MAAM,SAAS;AAEpC,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,OAAsB;AAChC,SAAK,QAAQ,KAAK,YAAY,KAAK,CAAC;AAAA,EACtC;AACF;AAKO,SAAS,SAA8B;AAC5C,SAAO,WAAW,YAAY;AAChC;",
6
6
  "names": []
7
7
  }
@@ -1,6 +1,6 @@
1
1
  import { AbstractService } from "./AbstractService.js";
2
2
  class LoadService extends AbstractService {
3
- static config = [[window, [["load"]]]];
3
+ static config = [[() => window, [["load"]]]];
4
4
  props = {
5
5
  time: performance.now()
6
6
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/services/LoadService.ts"],
4
- "sourcesContent": ["import type { ServiceInterface, ServiceConfig } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\n\nexport interface LoadServiceProps {\n time: DOMHighResTimeStamp;\n}\n\nexport type LoadServiceInterface = ServiceInterface<LoadServiceProps>;\n\nexport class LoadService extends AbstractService<LoadServiceProps> {\n static config: ServiceConfig = [[window, [['load']]]];\n\n props: LoadServiceProps = {\n time: performance.now(),\n };\n\n handleEvent() {\n this.props.time = window.performance.now();\n this.trigger(this.props);\n }\n}\n\n/**\n * Use the load service.\n */\nexport function useLoad(): LoadServiceInterface {\n return LoadService.getInstance();\n}\n"],
5
- "mappings": "AACA,SAAS,uBAAuB;AAQzB,MAAM,oBAAoB,gBAAkC;AAAA,EACjE,OAAO,SAAwB,CAAC,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAAA,EAEpD,QAA0B;AAAA,IACxB,MAAM,YAAY,IAAI;AAAA,EACxB;AAAA,EAEA,cAAc;AACZ,SAAK,MAAM,OAAO,OAAO,YAAY,IAAI;AACzC,SAAK,QAAQ,KAAK,KAAK;AAAA,EACzB;AACF;AAKO,SAAS,UAAgC;AAC9C,SAAO,YAAY,YAAY;AACjC;",
4
+ "sourcesContent": ["import type { ServiceInterface, ServiceConfig } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\n\nexport interface LoadServiceProps {\n time: DOMHighResTimeStamp;\n}\n\nexport type LoadServiceInterface = ServiceInterface<LoadServiceProps>;\n\nexport class LoadService extends AbstractService<LoadServiceProps> {\n static config: ServiceConfig = [[() => window, [['load']]]];\n\n props: LoadServiceProps = {\n time: performance.now(),\n };\n\n handleEvent() {\n this.props.time = window.performance.now();\n this.trigger(this.props);\n }\n}\n\n/**\n * Use the load service.\n */\nexport function useLoad(): LoadServiceInterface {\n return LoadService.getInstance();\n}\n"],
5
+ "mappings": "AACA,SAAS,uBAAuB;AAQzB,MAAM,oBAAoB,gBAAkC;AAAA,EACjE,OAAO,SAAwB,CAAC,CAAC,MAAM,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAAA,EAE1D,QAA0B;AAAA,IACxB,MAAM,YAAY,IAAI;AAAA,EACxB;AAAA,EAEA,cAAc;AACZ,SAAK,MAAM,OAAO,OAAO,YAAY,IAAI;AACzC,SAAK,QAAQ,KAAK,KAAK;AAAA,EACzB;AACF;AAKO,SAAS,UAAgC;AAC9C,SAAO,YAAY,YAAY;AACjC;",
6
6
  "names": []
7
7
  }
@@ -3,7 +3,7 @@ import { ONCE_CAPTURE_EVENT_OPTIONS, PASSIVE_CAPTURE_EVENT_OPTIONS } from "./uti
3
3
  class PointerService extends AbstractService {
4
4
  static config = [
5
5
  [
6
- document,
6
+ () => document,
7
7
  [
8
8
  ["mouseenter", ONCE_CAPTURE_EVENT_OPTIONS],
9
9
  ["mousemove", PASSIVE_CAPTURE_EVENT_OPTIONS],
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/services/PointerService.ts"],
4
- "sourcesContent": ["import type { ServiceConfig, ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport { ONCE_CAPTURE_EVENT_OPTIONS, PASSIVE_CAPTURE_EVENT_OPTIONS } from './utils.js';\n\nexport interface PointerServiceProps {\n event: MouseEvent | TouchEvent;\n isDown: boolean;\n x: number;\n y: number;\n changed: { x: boolean; y: boolean };\n last: { x: number; y: number };\n delta: { x: number; y: number };\n progress: { x: number; y: number };\n max: { x: number; y: number };\n}\n\nexport type PointerServiceInterface = ServiceInterface<PointerServiceProps>;\n\nexport class PointerService extends AbstractService<PointerServiceProps> {\n static config: ServiceConfig = [\n [\n document,\n [\n ['mouseenter', ONCE_CAPTURE_EVENT_OPTIONS],\n ['mousemove', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ['touchmove', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ['mousedown', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ['touchstart', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ['mouseup', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ['touchend', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ],\n ],\n ];\n\n target: HTMLElement | Window | undefined;\n\n props: PointerServiceProps = {\n event: null,\n isDown: false,\n x: 0,\n y: 0,\n changed: {\n x: false,\n y: false,\n },\n last: {\n x: 0,\n y: 0,\n },\n delta: {\n x: 0,\n y: 0,\n },\n progress: {\n x: 0.5,\n y: 0.5,\n },\n max: {\n x: 0,\n y: 0,\n },\n };\n\n constructor(target: HTMLElement | undefined) {\n super();\n this.target = target;\n const targetSize = this.getTargetSize();\n this.props.x = this.props.last.x = targetSize.x / 2;\n this.props.y = this.props.last.y = targetSize.y / 2;\n this.props.max.x = targetSize.width;\n this.props.max.y = targetSize.height;\n }\n\n isTouchEvent(event: TouchEvent | MouseEvent): boolean {\n return typeof TouchEvent !== 'undefined' && event instanceof TouchEvent;\n }\n\n getTargetSize() {\n const { target } = this;\n return target instanceof Element\n ? target.getBoundingClientRect()\n : {\n width: window.innerWidth,\n height: window.innerHeight,\n x: 0,\n y: 0,\n };\n }\n\n /**\n * Update the pointer positions.\n */\n updateProps(event: MouseEvent | TouchEvent): PointerServiceProps {\n const { props } = this;\n\n props.event = event;\n const yLast = props.y;\n const xLast = props.x;\n\n const targetSize = this.getTargetSize();\n\n // Check pointer Y\n // We either get data from a touch event `event.touches[0].clientY` or from\n // a mouse event `event.clientY`.\n let y = this.isTouchEvent(event)\n ? (event as TouchEvent).touches[0]?.clientY\n : (event as MouseEvent).clientY;\n y -= targetSize.y;\n if (y !== props.y) {\n props.y = y;\n }\n\n // Check pointer X\n // We either get data from a touch event `event.touches[0].clientX` or from\n // a mouse event `event.clientX`.\n let x = this.isTouchEvent(event)\n ? (event as TouchEvent).touches[0]?.clientX\n : (event as MouseEvent).clientX;\n x -= targetSize.x;\n if (x !== props.x) {\n props.x = x;\n }\n\n props.changed.x = props.x !== xLast;\n props.changed.y = props.y !== yLast;\n\n props.last.x = xLast;\n props.last.y = yLast;\n\n props.delta.x = props.x - xLast;\n props.delta.y = props.y - yLast;\n\n props.max.x = targetSize.width;\n props.max.y = targetSize.height;\n\n props.progress.x = props.x / props.max.x;\n props.progress.y = props.y / props.max.y;\n\n return props;\n }\n\n /**\n * Handle events.\n *\n * @todo handle scroll as well\n */\n handleEvent(event: MouseEvent | TouchEvent) {\n // eslint-disable-next-line default-case\n switch (event.type) {\n case 'mouseenter':\n case 'mousemove':\n case 'touchmove':\n this.trigger(this.updateProps(event));\n break;\n case 'mousedown':\n case 'touchstart':\n this.props.isDown = true;\n this.trigger(this.props);\n break;\n case 'mouseup':\n case 'touchend':\n this.props.isDown = false;\n this.trigger(this.props);\n break;\n }\n }\n}\n\n/**\n * Use the pointer service.\n */\nexport function usePointer(target: HTMLElement | Window = window): PointerServiceInterface {\n return PointerService.getInstance(target, target);\n}\n"],
5
- "mappings": "A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,KAAK;AAAA,MACL,KAAK;AACH,aAAK,MAAM,SAAS;AACpB,aAAK,QAAQ,KAAK,KAAK;AACvB;AAAA,MACF,KAAK;AAAA,MACL,KAAK;AACH,aAAK,MAAM,SAAS;AACpB,aAAK,QAAQ,KAAK,KAAK;AACvB;AAAA,IACJ;AAAA,EACF;AACF;AAKO,SAAS,WAAW,SAA+B,QAAiC;AACzF,SAAO,eAAe,YAAY,QAAQ,MAAM;AAClD;",
4
+ "sourcesContent": ["import type { ServiceConfig, ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport { ONCE_CAPTURE_EVENT_OPTIONS, PASSIVE_CAPTURE_EVENT_OPTIONS } from './utils.js';\n\nexport interface PointerServiceProps {\n event: MouseEvent | TouchEvent;\n isDown: boolean;\n x: number;\n y: number;\n changed: { x: boolean; y: boolean };\n last: { x: number; y: number };\n delta: { x: number; y: number };\n progress: { x: number; y: number };\n max: { x: number; y: number };\n}\n\nexport type PointerServiceInterface = ServiceInterface<PointerServiceProps>;\n\nexport class PointerService extends AbstractService<PointerServiceProps> {\n static config: ServiceConfig = [\n [\n () => document,\n [\n ['mouseenter', ONCE_CAPTURE_EVENT_OPTIONS],\n ['mousemove', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ['touchmove', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ['mousedown', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ['touchstart', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ['mouseup', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ['touchend', PASSIVE_CAPTURE_EVENT_OPTIONS],\n ],\n ],\n ];\n\n target: HTMLElement | Window | undefined;\n\n props: PointerServiceProps = {\n event: null,\n isDown: false,\n x: 0,\n y: 0,\n changed: {\n x: false,\n y: false,\n },\n last: {\n x: 0,\n y: 0,\n },\n delta: {\n x: 0,\n y: 0,\n },\n progress: {\n x: 0.5,\n y: 0.5,\n },\n max: {\n x: 0,\n y: 0,\n },\n };\n\n constructor(target: HTMLElement | undefined) {\n super();\n this.target = target;\n const targetSize = this.getTargetSize();\n this.props.x = this.props.last.x = targetSize.x / 2;\n this.props.y = this.props.last.y = targetSize.y / 2;\n this.props.max.x = targetSize.width;\n this.props.max.y = targetSize.height;\n }\n\n isTouchEvent(event: TouchEvent | MouseEvent): boolean {\n return typeof TouchEvent !== 'undefined' && event instanceof TouchEvent;\n }\n\n getTargetSize() {\n const { target } = this;\n return target instanceof Element\n ? target.getBoundingClientRect()\n : {\n width: window.innerWidth,\n height: window.innerHeight,\n x: 0,\n y: 0,\n };\n }\n\n /**\n * Update the pointer positions.\n */\n updateProps(event: MouseEvent | TouchEvent): PointerServiceProps {\n const { props } = this;\n\n props.event = event;\n const yLast = props.y;\n const xLast = props.x;\n\n const targetSize = this.getTargetSize();\n\n // Check pointer Y\n // We either get data from a touch event `event.touches[0].clientY` or from\n // a mouse event `event.clientY`.\n let y = this.isTouchEvent(event)\n ? (event as TouchEvent).touches[0]?.clientY\n : (event as MouseEvent).clientY;\n y -= targetSize.y;\n if (y !== props.y) {\n props.y = y;\n }\n\n // Check pointer X\n // We either get data from a touch event `event.touches[0].clientX` or from\n // a mouse event `event.clientX`.\n let x = this.isTouchEvent(event)\n ? (event as TouchEvent).touches[0]?.clientX\n : (event as MouseEvent).clientX;\n x -= targetSize.x;\n if (x !== props.x) {\n props.x = x;\n }\n\n props.changed.x = props.x !== xLast;\n props.changed.y = props.y !== yLast;\n\n props.last.x = xLast;\n props.last.y = yLast;\n\n props.delta.x = props.x - xLast;\n props.delta.y = props.y - yLast;\n\n props.max.x = targetSize.width;\n props.max.y = targetSize.height;\n\n props.progress.x = props.x / props.max.x;\n props.progress.y = props.y / props.max.y;\n\n return props;\n }\n\n /**\n * Handle events.\n *\n * @todo handle scroll as well\n */\n handleEvent(event: MouseEvent | TouchEvent) {\n // eslint-disable-next-line default-case\n switch (event.type) {\n case 'mouseenter':\n case 'mousemove':\n case 'touchmove':\n this.trigger(this.updateProps(event));\n break;\n case 'mousedown':\n case 'touchstart':\n this.props.isDown = true;\n this.trigger(this.props);\n break;\n case 'mouseup':\n case 'touchend':\n this.props.isDown = false;\n this.trigger(this.props);\n break;\n }\n }\n}\n\n/**\n * Use the pointer service.\n */\nexport function usePointer(target: HTMLElement | Window = window): PointerServiceInterface {\n return PointerService.getInstance(target, target);\n}\n"],
5
+ "mappings": "AACA,SAAS,uBAAuB;AAChC,SAAS,4BAA4B,qCAAqC;AAgBnE,MAAM,uBAAuB,gBAAqC;AAAA,EACvE,OAAO,SAAwB;AAAA,IAC7B;AAAA,MACE,MAAM;AAAA,MACN;AAAA,QACE,CAAC,cAAc,0BAA0B;AAAA,QACzC,CAAC,aAAa,6BAA6B;AAAA,QAC3C,CAAC,aAAa,6BAA6B;AAAA,QAC3C,CAAC,aAAa,6BAA6B;AAAA,QAC3C,CAAC,cAAc,6BAA6B;AAAA,QAC5C,CAAC,WAAW,6BAA6B;AAAA,QACzC,CAAC,YAAY,6BAA6B;AAAA,MAC5C;AAAA,IACF;AAAA,EACF;AAAA,EAEA;AAAA,EAEA,QAA6B;AAAA,IAC3B,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,GAAG;AAAA,IACH,GAAG;AAAA,IACH,SAAS;AAAA,MACP,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,IACA,MAAM;AAAA,MACJ,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,IACA,OAAO;AAAA,MACL,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,IACA,UAAU;AAAA,MACR,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,IACA,KAAK;AAAA,MACH,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EACF;AAAA,EAEA,YAAY,QAAiC;AAC3C,UAAM;AACN,SAAK,SAAS;AACd,UAAM,aAAa,KAAK,cAAc;AACtC,SAAK,MAAM,IAAI,KAAK,MAAM,KAAK,IAAI,WAAW,IAAI;AAClD,SAAK,MAAM,IAAI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MAAM,IAAI,MAAM,IAAI;AAEvC,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,YAAY,OAAgC;AAE1C,YAAQ,MAAM,MAAM;AAAA,MAClB,KAAK;AAAA,MACL,KAAK;AAAA,MACL,KAAK;AACH,aAAK,QAAQ,KAAK,YAAY,KAAK,CAAC;AACpC;AAAA,MACF,KAAK;AAAA,MACL,KAAK;AACH,aAAK,MAAM,SAAS;AACpB,aAAK,QAAQ,KAAK,KAAK;AACvB;AAAA,MACF,KAAK;AAAA,MACL,KAAK;AACH,aAAK,MAAM,SAAS;AACpB,aAAK,QAAQ,KAAK,KAAK;AACvB;AAAA,IACJ;AAAA,EACF;AACF;AAKO,SAAS,WAAW,SAA+B,QAAiC;AACzF,SAAO,eAAe,YAAY,QAAQ,MAAM;AAClD;",
6
6
  "names": []
7
7
  }
@@ -2,7 +2,7 @@ import { AbstractService } from "./AbstractService.js";
2
2
  import { features } from "../Base/features.js";
3
3
  import debounce from "../utils/debounce.js";
4
4
  class ResizeService extends AbstractService {
5
- static config = [[window, [["resize"]]]];
5
+ static config = [[() => window, [["resize"]]]];
6
6
  breakpoints;
7
7
  props = {
8
8
  width: window.innerWidth,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/services/ResizeService.ts"],
4
- "sourcesContent": ["import type { ServiceConfig, ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport type { Features } from '../Base/features.js';\nimport { features } from '../Base/features.js';\nimport debounce from '../utils/debounce.js';\n\nexport interface ResizeServiceProps<U extends Features['breakpoints'] = Features['breakpoints']> {\n width: number;\n height: number;\n ratio: number;\n orientation: 'square' | 'landscape' | 'portrait';\n breakpoint: string;\n breakpoints: string[];\n activeBreakpoints: Record<keyof U, boolean>;\n}\n\nexport type ResizeServiceInterface<U extends Features['breakpoints'] = Features['breakpoints']> =\n ServiceInterface<ResizeServiceProps<U>>;\n\nexport class ResizeService<\n T extends Features['breakpoints'] = Features['breakpoints'],\n> extends AbstractService<ResizeServiceProps> {\n static config: ServiceConfig = [[window, [['resize']]]];\n\n breakpoints: T;\n\n props: ResizeServiceProps<T> = {\n width: window.innerWidth,\n height: window.innerHeight,\n ratio: window.innerWidth / window.innerHeight,\n orientation: 'square',\n get breakpoint() {\n return Object.keys(this.activeBreakpoints)\n .toReversed()\n .find((name) => this.activeBreakpoints[name]);\n },\n get breakpoints() {\n return Object.keys(this.activeBreakpoints);\n },\n activeBreakpoints: {} as Record<keyof T, boolean>,\n };\n\n constructor(breakpoints: T) {\n super();\n this.breakpoints = breakpoints;\n\n Object.defineProperty(this.props, 'activeBreakpoints', {\n get: () => {\n const activeBreakpoints = {};\n\n for (const [name, breakpoint] of Object.entries(\n this.breakpoints ?? features.get('breakpoints'),\n )) {\n activeBreakpoints[name] = window.matchMedia(`(min-width: ${breakpoint})`).matches;\n }\n\n return activeBreakpoints as Record<keyof T, boolean>;\n },\n });\n }\n\n /**\n * Update props.\n */\n updateProps(): ResizeServiceProps<T> {\n const { props } = this;\n\n props.width = window.innerWidth;\n props.height = window.innerHeight;\n props.ratio = window.innerWidth / window.innerHeight;\n props.orientation = 'square';\n\n if (props.ratio > 1) {\n props.orientation = 'landscape';\n }\n\n if (props.ratio < 1) {\n props.orientation = 'portrait';\n }\n\n return props;\n }\n\n onResizeDebounce = debounce(() => {\n this.trigger(this.updateProps());\n }, 100);\n\n handleEvent() {\n this.onResizeDebounce();\n }\n}\n\n/**\n * Use the resize service.\n */\nexport function useResize<T extends Features['breakpoints'] = Features['breakpoints']>(\n breakpoints?: T,\n): ResizeServiceInterface<T> {\n return ResizeService.getInstance(breakpoints, breakpoints);\n}\n"],
5
- "mappings": "AACA,SAAS,uBAAuB;AAEhC,SAAS,gBAAgB;AACzB,OAAO,cAAc;AAed,MAAM,sBAEH,gBAAoC;AAAA,EAC5C,OAAO,SAAwB,CAAC,CAAC,QAAQ,CAAC,CAAC,QAAQ,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xB;AACF;AAKO,SAAS,UACd,aAC2B;AAC3B,SAAO,cAAc,YAAY,aAAa,WAAW;AAC3D;",
4
+ "sourcesContent": ["import type { ServiceConfig, ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport type { Features } from '../Base/features.js';\nimport { features } from '../Base/features.js';\nimport debounce from '../utils/debounce.js';\n\nexport interface ResizeServiceProps<U extends Features['breakpoints'] = Features['breakpoints']> {\n width: number;\n height: number;\n ratio: number;\n orientation: 'square' | 'landscape' | 'portrait';\n breakpoint: string;\n breakpoints: string[];\n activeBreakpoints: Record<keyof U, boolean>;\n}\n\nexport type ResizeServiceInterface<U extends Features['breakpoints'] = Features['breakpoints']> =\n ServiceInterface<ResizeServiceProps<U>>;\n\nexport class ResizeService<\n T extends Features['breakpoints'] = Features['breakpoints'],\n> extends AbstractService<ResizeServiceProps> {\n static config: ServiceConfig = [[() => window, [['resize']]]];\n\n breakpoints: T;\n\n props: ResizeServiceProps<T> = {\n width: window.innerWidth,\n height: window.innerHeight,\n ratio: window.innerWidth / window.innerHeight,\n orientation: 'square',\n get breakpoint() {\n return Object.keys(this.activeBreakpoints)\n .toReversed()\n .find((name) => this.activeBreakpoints[name]);\n },\n get breakpoints() {\n return Object.keys(this.activeBreakpoints);\n },\n activeBreakpoints: {} as Record<keyof T, boolean>,\n };\n\n constructor(breakpoints: T) {\n super();\n this.breakpoints = breakpoints;\n\n Object.defineProperty(this.props, 'activeBreakpoints', {\n get: () => {\n const activeBreakpoints = {};\n\n for (const [name, breakpoint] of Object.entries(\n this.breakpoints ?? features.get('breakpoints'),\n )) {\n activeBreakpoints[name] = window.matchMedia(`(min-width: ${breakpoint})`).matches;\n }\n\n return activeBreakpoints as Record<keyof T, boolean>;\n },\n });\n }\n\n /**\n * Update props.\n */\n updateProps(): ResizeServiceProps<T> {\n const { props } = this;\n\n props.width = window.innerWidth;\n props.height = window.innerHeight;\n props.ratio = window.innerWidth / window.innerHeight;\n props.orientation = 'square';\n\n if (props.ratio > 1) {\n props.orientation = 'landscape';\n }\n\n if (props.ratio < 1) {\n props.orientation = 'portrait';\n }\n\n return props;\n }\n\n onResizeDebounce = debounce(() => {\n this.trigger(this.updateProps());\n }, 100);\n\n handleEvent() {\n this.onResizeDebounce();\n }\n}\n\n/**\n * Use the resize service.\n */\nexport function useResize<T extends Features['breakpoints'] = Features['breakpoints']>(\n breakpoints?: T,\n): ResizeServiceInterface<T> {\n return ResizeService.getInstance(breakpoints, breakpoints);\n}\n"],
5
+ "mappings": "AACA,SAAS,uBAAuB;AAEhC,SAAS,gBAAgB;AACzB,OAAO,cAAc;AAed,MAAM,sBAEH,gBAAoC;AAAA,EAC5C,OAAO,SAAwB,CAAC,CAAC,MAAM,QAAQ,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;AAAA,EAE5D;AAAA,EAEA,QAA+B;AAAA,IAC7B,OAAO,OAAO;AAAA,IACd,QAAQ,OAAO;AAAA,IACf,OAAO,OAAO,aAAa,OAAO;AAAA,IAClC,aAAa;AAAA,IACb,IAAI,aAAa;AACf,aAAO,OAAO,KAAK,KAAK,iBAAiB,EACtC,WAAW,EACX,KAAK,CAAC,SAAS,KAAK,kBAAkB,IAAI,CAAC;AAAA,IAChD;AAAA,IACA,IAAI,cAAc;AAChB,aAAO,OAAO,KAAK,KAAK,iBAAiB;AAAA,IAC3C;AAAA,IACA,mBAAmB,CAAC;AAAA,EACtB;AAAA,EAEA,YAAY,aAAgB;AAC1B,UAAM;AACN,SAAK,cAAc;AAEnB,WAAO,eAAe,KAAK,OAAO,qBAAqB;AAAA,MACrD,KAAK,MAAM;AACT,cAAM,oBAAoB,CAAC;AAE3B,mBAAW,CAAC,MAAM,UAAU,KAAK,OAAO;AAAA,UACtC,KAAK,eAAe,SAAS,IAAI,aAAa;AAAA,QAChD,GAAG;AACD,4BAAkB,IAAI,IAAI,OAAO,WAAW,eAAe,UAAU,GAAG,EAAE;AAAA,QAC5E;AAEA,eAAO;AAAA,MACT;AAAA,IACF,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA,EAKA,cAAqC;AACnC,UAAM,EAAE,MAAM,IAAI;AAElB,UAAM,QAAQ,OAAO;AACrB,UAAM,SAAS,OAAO;AACtB,UAAM,QAAQ,OAAO,aAAa,OAAO;AACzC,UAAM,cAAc;AAEpB,QAAI,MAAM,QAAQ,GAAG;AACnB,YAAM,cAAc;AAAA,IACtB;AAEA,QAAI,MAAM,QAAQ,GAAG;AACnB,YAAM,cAAc;AAAA,IACtB;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,mBAAmB,SAAS,MAAM;AAChC,SAAK,QAAQ,KAAK,YAAY,CAAC;AAAA,EACjC,GAAG,GAAG;AAAA,EAEN,cAAc;AACZ,SAAK,iBAAiB;AAAA,EACxB;AACF;AAKO,SAAS,UACd,aAC2B;AAC3B,SAAO,cAAc,YAAY,aAAa,WAAW;AAC3D;",
6
6
  "names": []
7
7
  }
@@ -2,7 +2,7 @@ import { AbstractService } from "./AbstractService.js";
2
2
  import debounce from "../utils/debounce.js";
3
3
  import { PASSIVE_CAPTURE_EVENT_OPTIONS } from "./utils.js";
4
4
  class ScrollService extends AbstractService {
5
- static config = [[document, [["scroll", PASSIVE_CAPTURE_EVENT_OPTIONS]]]];
5
+ static config = [[() => document, [["scroll", PASSIVE_CAPTURE_EVENT_OPTIONS]]]];
6
6
  props = {
7
7
  x: window.scrollX,
8
8
  y: window.scrollY,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/services/ScrollService.ts"],
4
- "sourcesContent": ["import type { ServiceConfig, ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport debounce from '../utils/debounce.js';\nimport { PASSIVE_CAPTURE_EVENT_OPTIONS } from './utils.js';\n\nexport interface ScrollServiceProps {\n x: number;\n y: number;\n changed: { x: boolean; y: boolean };\n changedX: boolean;\n changedY: boolean;\n last: { x: number; y: number };\n lastX: number;\n lastY: number;\n delta: { x: number; y: number };\n deltaX: number;\n deltaY: number;\n progress: { x: number; y: number };\n progressX: number;\n progressY: number;\n max: { x: number; y: number };\n maxX: number;\n maxY: number;\n direction: { x: 'LEFT' | 'RIGHT' | 'NONE'; y: 'UP' | 'DOWN' | 'NONE' };\n isUp: boolean;\n isRight: boolean;\n isDown: boolean;\n isLeft: boolean;\n}\n\nexport type ScrollServiceInterface = ServiceInterface<ScrollServiceProps>;\n\nexport class ScrollService extends AbstractService<ScrollServiceProps> {\n static config: ServiceConfig = [[document, [['scroll', PASSIVE_CAPTURE_EVENT_OPTIONS]]]];\n\n props: ScrollServiceProps = {\n x: window.scrollX,\n y: window.scrollY,\n changed: {\n x: false,\n y: false,\n },\n changedX: false,\n changedY: false,\n last: {\n x: window.scrollX,\n y: window.scrollY,\n },\n lastX: window.scrollX,\n lastY: window.scrollY,\n delta: {\n x: 0,\n y: 0,\n },\n deltaX: 0,\n deltaY: 0,\n max: {\n x: document.scrollingElement.scrollWidth - window.innerWidth,\n y: document.scrollingElement.scrollHeight - window.innerHeight,\n },\n maxX: document.scrollingElement.scrollWidth - window.innerWidth,\n maxY: document.scrollingElement.scrollHeight - window.innerHeight,\n progress: {\n x: 0,\n y: 0,\n },\n progressX: 0,\n progressY: 0,\n direction: {\n x: 'NONE',\n y: 'NONE',\n },\n isUp: false,\n isRight: false,\n isDown: false,\n isLeft: false,\n };\n\n updateProps(): ScrollServiceProps {\n const { props } = this;\n const yLast = props.y;\n const xLast = props.x;\n\n props.y = window.scrollY;\n props.x = window.scrollX;\n props.changed.x = props.changedX = props.x !== xLast;\n props.changed.y = props.changedY = props.y !== yLast;\n props.last.x = props.lastX = xLast;\n props.last.y = props.lastY = yLast;\n props.delta.x = props.deltaX = props.x - xLast;\n props.delta.y = props.deltaY = props.y - yLast;\n props.max.x = props.maxX = document.scrollingElement.scrollWidth - window.innerWidth;\n props.max.y = props.maxY = document.scrollingElement.scrollHeight - window.innerHeight;\n props.progress.x = props.progressX = props.max.x === 0 ? 1 : props.x / props.max.x;\n props.progress.y = props.progressY = props.max.y === 0 ? 1 : props.y / props.max.y;\n props.isUp = props.y < yLast;\n props.isRight = props.x > xLast;\n props.isDown = props.y > yLast;\n props.isLeft = props.x < xLast;\n /* eslint-disable no-nested-ternary */\n props.direction.x = props.isRight ? 'RIGHT' : props.isLeft ? 'LEFT' : 'NONE';\n props.direction.y = props.isDown ? 'DOWN' : props.isUp ? 'UP' : 'NONE';\n /* eslint-enable no-nested-ternary */\n\n return props;\n }\n\n onScrollDebounced = debounce(() => {\n this.trigger(this.updateProps());\n }, 100);\n\n /**\n * Scroll handler.\n */\n handleEvent() {\n this.trigger(this.updateProps());\n this.onScrollDebounced();\n }\n}\n\n/**\n * Use the scroll service.\n *\n * ```js\n * import { useScroll } from '@studiometa/js-toolkit/services';\n * const { add, remove, props } = useScroll();\n * add(key, (props) => {});\n * remove(key);\n * props();\n * ```\n */\nexport function useScroll(): ScrollServiceInterface {\n return ScrollService.getInstance();\n}\n"],
5
- "mappings": "AACA,SAAS,uBAAuB;AAChC,OAAO,cAAc;AACrB,SAAS,qCAAqC;AA6BvC,MAAM,sBAAsB,gBAAoC;AAAA,EACrE,OAAO,SAAwB,CAAC,CAAC,UAAU,CAAC,CAAC,UAAU,6BAA6B,CAAC,CAAC,CAAC;AAAA,EAEvF,QAA4B;AAAA,IAC1B,GAAG,OAAO;AAAA,IACV,GAAG,OAAO;AAAA,IACV,SAAS;AAAA,MACP,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,IACA,UAAU;AAAA,IACV,UAAU;AAAA,IACV,MAAM;AAAA,MACJ,GAAG,OAAO;AAAA,MACV,GAAG,OAAO;AAAA,IACZ;AAAA,IACA,OAAO,OAAO;AAAA,IACd,OAAO,OAAO;AAAA,IACd,OAAO;AAAA,MACL,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,IACA,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,KAAK;AAAA,MACH,GAAG,SAAS,iBAA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,SAAS,MAAM,IAAI;AAEzB,UAAM,UAAU,IAAI,MAAM,UAAU,UAAU,MAAM,SAAS,SAAS;AACtE,UAAM,UAAU,IAAI,MAAM,SAAS,SAAS,MAAM,OAAO,OAAO;AAGhE,WAAO;AAAA,EACT;AAAA,EAEA,oBAAoB,SAAS,MAAM;AACjC,SAAK,QAAQ,KAAK,YAAY,CAAC;AAAA,EACjC,GAAG,GAAG;AAAA;AAAA;AAAA;AAAA,EAKN,cAAc;AACZ,SAAK,QAAQ,KAAK,YAAY,CAAC;AAC/B,SAAK,kBAAkB;AAAA,EACzB;AACF;AAaO,SAAS,YAAoC;AAClD,SAAO,cAAc,YAAY;AACnC;",
4
+ "sourcesContent": ["import type { ServiceConfig, ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport debounce from '../utils/debounce.js';\nimport { PASSIVE_CAPTURE_EVENT_OPTIONS } from './utils.js';\n\nexport interface ScrollServiceProps {\n x: number;\n y: number;\n changed: { x: boolean; y: boolean };\n changedX: boolean;\n changedY: boolean;\n last: { x: number; y: number };\n lastX: number;\n lastY: number;\n delta: { x: number; y: number };\n deltaX: number;\n deltaY: number;\n progress: { x: number; y: number };\n progressX: number;\n progressY: number;\n max: { x: number; y: number };\n maxX: number;\n maxY: number;\n direction: { x: 'LEFT' | 'RIGHT' | 'NONE'; y: 'UP' | 'DOWN' | 'NONE' };\n isUp: boolean;\n isRight: boolean;\n isDown: boolean;\n isLeft: boolean;\n}\n\nexport type ScrollServiceInterface = ServiceInterface<ScrollServiceProps>;\n\nexport class ScrollService extends AbstractService<ScrollServiceProps> {\n static config: ServiceConfig = [[() => document, [['scroll', PASSIVE_CAPTURE_EVENT_OPTIONS]]]];\n\n props: ScrollServiceProps = {\n x: window.scrollX,\n y: window.scrollY,\n changed: {\n x: false,\n y: false,\n },\n changedX: false,\n changedY: false,\n last: {\n x: window.scrollX,\n y: window.scrollY,\n },\n lastX: window.scrollX,\n lastY: window.scrollY,\n delta: {\n x: 0,\n y: 0,\n },\n deltaX: 0,\n deltaY: 0,\n max: {\n x: document.scrollingElement.scrollWidth - window.innerWidth,\n y: document.scrollingElement.scrollHeight - window.innerHeight,\n },\n maxX: document.scrollingElement.scrollWidth - window.innerWidth,\n maxY: document.scrollingElement.scrollHeight - window.innerHeight,\n progress: {\n x: 0,\n y: 0,\n },\n progressX: 0,\n progressY: 0,\n direction: {\n x: 'NONE',\n y: 'NONE',\n },\n isUp: false,\n isRight: false,\n isDown: false,\n isLeft: false,\n };\n\n updateProps(): ScrollServiceProps {\n const { props } = this;\n const yLast = props.y;\n const xLast = props.x;\n\n props.y = window.scrollY;\n props.x = window.scrollX;\n props.changed.x = props.changedX = props.x !== xLast;\n props.changed.y = props.changedY = props.y !== yLast;\n props.last.x = props.lastX = xLast;\n props.last.y = props.lastY = yLast;\n props.delta.x = props.deltaX = props.x - xLast;\n props.delta.y = props.deltaY = props.y - yLast;\n props.max.x = props.maxX = document.scrollingElement.scrollWidth - window.innerWidth;\n props.max.y = props.maxY = document.scrollingElement.scrollHeight - window.innerHeight;\n props.progress.x = props.progressX = props.max.x === 0 ? 1 : props.x / props.max.x;\n props.progress.y = props.progressY = props.max.y === 0 ? 1 : props.y / props.max.y;\n props.isUp = props.y < yLast;\n props.isRight = props.x > xLast;\n props.isDown = props.y > yLast;\n props.isLeft = props.x < xLast;\n /* eslint-disable no-nested-ternary */\n props.direction.x = props.isRight ? 'RIGHT' : props.isLeft ? 'LEFT' : 'NONE';\n props.direction.y = props.isDown ? 'DOWN' : props.isUp ? 'UP' : 'NONE';\n /* eslint-enable no-nested-ternary */\n\n return props;\n }\n\n onScrollDebounced = debounce(() => {\n this.trigger(this.updateProps());\n }, 100);\n\n /**\n * Scroll handler.\n */\n handleEvent() {\n this.trigger(this.updateProps());\n this.onScrollDebounced();\n }\n}\n\n/**\n * Use the scroll service.\n *\n * ```js\n * import { useScroll } from '@studiometa/js-toolkit/services';\n * const { add, remove, props } = useScroll();\n * add(key, (props) => {});\n * remove(key);\n * props();\n * ```\n */\nexport function useScroll(): ScrollServiceInterface {\n return ScrollService.getInstance();\n}\n"],
5
+ "mappings": "AACA,SAAS,uBAAuB;AAChC,OAAO,cAAc;AACrB,SAAS,qCAAqC;AA6BvC,MAAM,sBAAsB,gBAAoC;AAAA,EACrE,OAAO,SAAwB,CAAC,CAAC,MAAM,UAAU,CAAC,CAAC,UAAU,6BAA6B,CAAC,CAAC,CAAC;AAAA,EAE7F,QAA4B;AAAA,IAC1B,GAAG,OAAO;AAAA,IACV,GAAG,OAAO;AAAA,IACV,SAAS;AAAA,MACP,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,IACA,UAAU;AAAA,IACV,UAAU;AAAA,IACV,MAAM;AAAA,MACJ,GAAG,OAAO;AAAA,MACV,GAAG,OAAO;AAAA,IACZ;AAAA,IACA,OAAO,OAAO;AAAA,IACd,OAAO,OAAO;AAAA,IACd,OAAO;AAAA,MACL,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,IACA,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,KAAK;AAAA,MACH,GAAG,SAAS,iBAAiB,cAAc,OAAO;AAAA,MAClD,GAAG,SAAS,iBAAiB,eAAe,OAAO;AAAA,IACrD;AAAA,IACA,MAAM,SAAS,iBAAiB,cAAc,OAAO;AAAA,IACrD,MAAM,SAAS,iBAAiB,eAAe,OAAO;AAAA,IACtD,UAAU;AAAA,MACR,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,IACA,WAAW;AAAA,IACX,WAAW;AAAA,IACX,WAAW;AAAA,MACT,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,IACA,MAAM;AAAA,IACN,SAAS;AAAA,IACT,QAAQ;AAAA,IACR,QAAQ;AAAA,EACV;AAAA,EAEA,cAAkC;AAChC,UAAM,EAAE,MAAM,IAAI;AAClB,UAAM,QAAQ,MAAM;AACpB,UAAM,QAAQ,MAAM;AAEpB,UAAM,IAAI,OAAO;AACjB,UAAM,I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