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

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.
Files changed (217) hide show
  1. package/Base/Base.d.ts +19 -10
  2. package/Base/Base.js +46 -36
  3. package/Base/Base.js.map +2 -2
  4. package/Base/managers/AbstractManager.d.ts +3 -8
  5. package/Base/managers/AbstractManager.js +4 -22
  6. package/Base/managers/AbstractManager.js.map +2 -2
  7. package/Base/managers/ChildrenManager.d.ts +25 -2
  8. package/Base/managers/ChildrenManager.js +79 -63
  9. package/Base/managers/ChildrenManager.js.map +2 -2
  10. package/Base/managers/EventsManager.js +4 -12
  11. package/Base/managers/EventsManager.js.map +2 -2
  12. package/Base/managers/OptionsManager.d.ts +16 -11
  13. package/Base/managers/OptionsManager.js +38 -40
  14. package/Base/managers/OptionsManager.js.map +2 -2
  15. package/Base/managers/RefsManager.d.ts +11 -1
  16. package/Base/managers/RefsManager.js +48 -36
  17. package/Base/managers/RefsManager.js.map +2 -2
  18. package/Base/managers/ResponsiveOptionsManager.d.ts +9 -1
  19. package/Base/managers/ResponsiveOptionsManager.js +30 -23
  20. package/Base/managers/ResponsiveOptionsManager.js.map +2 -2
  21. package/Base/managers/ServicesManager.d.ts +12 -13
  22. package/Base/managers/ServicesManager.js +16 -22
  23. package/Base/managers/ServicesManager.js.map +2 -2
  24. package/Base/utils.js.map +2 -2
  25. package/decorators/index.d.ts +1 -0
  26. package/decorators/index.js +1 -0
  27. package/decorators/index.js.map +2 -2
  28. package/decorators/withBreakpointObserver.js +0 -2
  29. package/decorators/withBreakpointObserver.js.map +2 -2
  30. package/decorators/withDrag.js +1 -1
  31. package/decorators/withDrag.js.map +2 -2
  32. package/decorators/withIntersectionObserver.js +0 -2
  33. package/decorators/withIntersectionObserver.js.map +2 -2
  34. package/decorators/withMountOnMediaQuery.js +0 -2
  35. package/decorators/withMountOnMediaQuery.js.map +2 -2
  36. package/decorators/withMountWhenInView.js +0 -2
  37. package/decorators/withMountWhenInView.js.map +2 -2
  38. package/decorators/withMutation.d.ts +13 -0
  39. package/decorators/withMutation.js +34 -0
  40. package/decorators/withMutation.js.map +7 -0
  41. package/decorators/withRelativePointer.js +1 -1
  42. package/decorators/withRelativePointer.js.map +2 -2
  43. package/decorators/withScrolledInView/withScrolledInView.js +1 -1
  44. package/decorators/withScrolledInView/withScrolledInView.js.map +2 -2
  45. package/helpers/createApp.d.ts +1 -1
  46. package/helpers/createApp.js +20 -1
  47. package/helpers/createApp.js.map +2 -2
  48. package/helpers/getClosestParent.d.ts +1 -1
  49. package/helpers/getClosestParent.js +2 -2
  50. package/helpers/getClosestParent.js.map +2 -2
  51. package/helpers/getInstanceFromElement.d.ts +1 -1
  52. package/helpers/getInstanceFromElement.js +1 -1
  53. package/helpers/getInstanceFromElement.js.map +2 -2
  54. package/helpers/importOnInteraction.d.ts +1 -1
  55. package/helpers/importOnInteraction.js +1 -1
  56. package/helpers/importOnInteraction.js.map +2 -2
  57. package/helpers/importOnMediaQuery.d.ts +1 -1
  58. package/helpers/importOnMediaQuery.js +1 -1
  59. package/helpers/importOnMediaQuery.js.map +2 -2
  60. package/helpers/importWhenIdle.d.ts +1 -1
  61. package/helpers/importWhenIdle.js +1 -1
  62. package/helpers/importWhenIdle.js.map +2 -2
  63. package/helpers/importWhenPrefersMotion.d.ts +1 -1
  64. package/helpers/importWhenPrefersMotion.js +2 -2
  65. package/helpers/importWhenPrefersMotion.js.map +2 -2
  66. package/helpers/importWhenVisible.d.ts +1 -1
  67. package/helpers/importWhenVisible.js +1 -1
  68. package/helpers/importWhenVisible.js.map +2 -2
  69. package/helpers/index.d.ts +8 -8
  70. package/helpers/index.js +8 -18
  71. package/helpers/index.js.map +3 -3
  72. package/package.json +1 -1
  73. package/services/AbstractService.d.ts +10 -10
  74. package/services/AbstractService.js +10 -10
  75. package/services/AbstractService.js.map +3 -3
  76. package/services/DragService.js +5 -1
  77. package/services/DragService.js.map +2 -2
  78. package/services/KeyService.js +1 -1
  79. package/services/KeyService.js.map +2 -2
  80. package/services/MutationService.d.ts +20 -0
  81. package/services/MutationService.js +34 -0
  82. package/services/MutationService.js.map +7 -0
  83. package/services/PointerService.js +1 -1
  84. package/services/PointerService.js.map +2 -2
  85. package/services/ResizeService.js +2 -2
  86. package/services/ResizeService.js.map +2 -2
  87. package/services/ScrollService.js +1 -1
  88. package/services/ScrollService.js.map +2 -2
  89. package/services/index.d.ts +1 -0
  90. package/services/index.js +1 -0
  91. package/services/index.js.map +2 -2
  92. package/tsconfig.build.tsbuildinfo +1 -1
  93. package/utils/cache.d.ts +4 -0
  94. package/utils/cache.js +18 -0
  95. package/utils/cache.js.map +7 -0
  96. package/utils/collide/boundingRectToCircle.d.ts +1 -1
  97. package/utils/collide/boundingRectToCircle.js +1 -1
  98. package/utils/collide/boundingRectToCircle.js.map +2 -2
  99. package/utils/collide/collideCircleCircle.d.ts +1 -1
  100. package/utils/collide/collideCircleCircle.js +1 -1
  101. package/utils/collide/collideCircleCircle.js.map +2 -2
  102. package/utils/collide/collideCircleRect.d.ts +1 -1
  103. package/utils/collide/collideCircleRect.js +1 -1
  104. package/utils/collide/collideCircleRect.js.map +2 -2
  105. package/utils/collide/collidePointCircle.d.ts +1 -1
  106. package/utils/collide/collidePointCircle.js +1 -1
  107. package/utils/collide/collidePointCircle.js.map +2 -2
  108. package/utils/collide/collidePointRect.d.ts +1 -1
  109. package/utils/collide/collidePointRect.js +1 -1
  110. package/utils/collide/collidePointRect.js.map +2 -2
  111. package/utils/collide/collideRectRect.d.ts +1 -1
  112. package/utils/collide/collideRectRect.js +1 -1
  113. package/utils/collide/collideRectRect.js.map +2 -2
  114. package/utils/collide/index.d.ts +6 -6
  115. package/utils/collide/index.js +12 -12
  116. package/utils/collide/index.js.map +3 -3
  117. package/utils/css/animate.js.map +2 -2
  118. package/utils/css/getOffsetSizes.d.ts +1 -1
  119. package/utils/css/getOffsetSizes.js +1 -1
  120. package/utils/css/getOffsetSizes.js.map +2 -2
  121. package/utils/css/index.d.ts +3 -3
  122. package/utils/css/index.js +6 -6
  123. package/utils/css/index.js.map +3 -3
  124. package/utils/css/matrix.d.ts +1 -1
  125. package/utils/css/matrix.js +1 -1
  126. package/utils/css/matrix.js.map +2 -2
  127. package/utils/css/transition.d.ts +1 -1
  128. package/utils/css/transition.js +2 -2
  129. package/utils/css/transition.js.map +2 -2
  130. package/utils/debounce.d.ts +1 -1
  131. package/utils/debounce.js +1 -1
  132. package/utils/debounce.js.map +2 -2
  133. package/utils/dom/createElement.d.ts +10 -0
  134. package/utils/dom/createElement.js +21 -0
  135. package/utils/dom/createElement.js.map +7 -0
  136. package/utils/dom/index.d.ts +1 -0
  137. package/utils/dom/index.js +1 -0
  138. package/utils/dom/index.js.map +2 -2
  139. package/utils/getComponentResolver.d.ts +1 -1
  140. package/utils/getComponentResolver.js +1 -1
  141. package/utils/getComponentResolver.js.map +2 -2
  142. package/utils/index.d.ts +6 -5
  143. package/utils/index.js +11 -10
  144. package/utils/index.js.map +3 -3
  145. package/utils/keyCodes.d.ts +1 -2
  146. package/utils/keyCodes.js +2 -2
  147. package/utils/keyCodes.js.map +2 -2
  148. package/utils/math/clamp.d.ts +1 -1
  149. package/utils/math/clamp.js +1 -1
  150. package/utils/math/clamp.js.map +2 -2
  151. package/utils/math/clamp01.d.ts +1 -1
  152. package/utils/math/clamp01.js +2 -2
  153. package/utils/math/clamp01.js.map +2 -2
  154. package/utils/math/createEases.js.map +2 -2
  155. package/utils/math/damp.d.ts +1 -1
  156. package/utils/math/damp.js +1 -1
  157. package/utils/math/damp.js.map +2 -2
  158. package/utils/math/index.d.ts +7 -7
  159. package/utils/math/index.js +14 -14
  160. package/utils/math/index.js.map +3 -3
  161. package/utils/math/inertiaFinalValue.d.ts +1 -1
  162. package/utils/math/inertiaFinalValue.js +2 -2
  163. package/utils/math/inertiaFinalValue.js.map +2 -2
  164. package/utils/math/lerp.d.ts +1 -1
  165. package/utils/math/lerp.js +1 -1
  166. package/utils/math/lerp.js.map +2 -2
  167. package/utils/math/map.d.ts +1 -1
  168. package/utils/math/map.js +1 -1
  169. package/utils/math/map.js.map +2 -2
  170. package/utils/math/round.d.ts +1 -1
  171. package/utils/math/round.js +1 -1
  172. package/utils/math/round.js.map +2 -2
  173. package/utils/memoize.d.ts +1 -1
  174. package/utils/memoize.js +1 -1
  175. package/utils/memoize.js.map +2 -2
  176. package/utils/nextFrame.js.map +2 -2
  177. package/utils/object/getAllProperties.d.ts +1 -1
  178. package/utils/object/getAllProperties.js +1 -1
  179. package/utils/object/getAllProperties.js.map +2 -2
  180. package/utils/string/index.d.ts +10 -10
  181. package/utils/string/index.js +10 -22
  182. package/utils/string/index.js.map +3 -3
  183. package/utils/string/withLeadingCharacters.d.ts +1 -1
  184. package/utils/string/withLeadingCharacters.js +2 -2
  185. package/utils/string/withLeadingCharacters.js.map +2 -2
  186. package/utils/string/withLeadingSlash.d.ts +1 -1
  187. package/utils/string/withLeadingSlash.js +2 -2
  188. package/utils/string/withLeadingSlash.js.map +2 -2
  189. package/utils/string/withTrailingCharacters.d.ts +1 -1
  190. package/utils/string/withTrailingCharacters.js +2 -2
  191. package/utils/string/withTrailingCharacters.js.map +2 -2
  192. package/utils/string/withTrailingSlash.d.ts +1 -1
  193. package/utils/string/withTrailingSlash.js +2 -2
  194. package/utils/string/withTrailingSlash.js.map +2 -2
  195. package/utils/string/withoutLeadingCharacters.d.ts +1 -1
  196. package/utils/string/withoutLeadingCharacters.js +1 -1
  197. package/utils/string/withoutLeadingCharacters.js.map +2 -2
  198. package/utils/string/withoutLeadingCharactersRecursive.d.ts +1 -1
  199. package/utils/string/withoutLeadingCharactersRecursive.js +2 -2
  200. package/utils/string/withoutLeadingCharactersRecursive.js.map +2 -2
  201. package/utils/string/withoutLeadingSlash.d.ts +1 -1
  202. package/utils/string/withoutLeadingSlash.js +2 -2
  203. package/utils/string/withoutLeadingSlash.js.map +2 -2
  204. package/utils/string/withoutTrailingCharacters.d.ts +1 -1
  205. package/utils/string/withoutTrailingCharacters.js +1 -1
  206. package/utils/string/withoutTrailingCharacters.js.map +2 -2
  207. package/utils/string/withoutTrailingCharactersRecursive.d.ts +1 -1
  208. package/utils/string/withoutTrailingCharactersRecursive.js +2 -2
  209. package/utils/string/withoutTrailingCharactersRecursive.js.map +2 -2
  210. package/utils/string/withoutTrailingSlash.d.ts +1 -1
  211. package/utils/string/withoutTrailingSlash.js +2 -2
  212. package/utils/string/withoutTrailingSlash.js.map +2 -2
  213. package/utils/throttle.d.ts +1 -1
  214. package/utils/throttle.js +1 -1
  215. package/utils/throttle.js.map +2 -2
  216. package/utils/trapFocus.js +1 -1
  217. package/utils/trapFocus.js.map +2 -2
@@ -1,9 +1,9 @@
1
+ export * from './createApp.js';
2
+ export * from './getClosestParent.js';
1
3
  export * from './getDirectChildren.js';
2
- export { default as createApp } from './createApp.js';
3
- export { default as getClosestParent } from './getClosestParent.js';
4
- export { default as getInstanceFromElement } from './getInstanceFromElement.js';
5
- export { default as importOnInteraction } from './importOnInteraction.js';
6
- export { default as importWhenIdle } from './importWhenIdle.js';
7
- export { default as importWhenVisible } from './importWhenVisible.js';
8
- export { default as importOnMediaQuery } from './importOnMediaQuery.js';
9
- export { default as importWhenPrefersMotion } from './importWhenPrefersMotion.js';
4
+ export * from './getInstanceFromElement.js';
5
+ export * from './importOnInteraction.js';
6
+ export * from './importOnMediaQuery.js';
7
+ export * from './importWhenIdle.js';
8
+ export * from './importWhenPrefersMotion.js';
9
+ export * from './importWhenVisible.js';
package/helpers/index.js CHANGED
@@ -1,20 +1,10 @@
1
+ export * from "./createApp.js";
2
+ export * from "./getClosestParent.js";
1
3
  export * from "./getDirectChildren.js";
2
- import { default as default2 } from "./createApp.js";
3
- import { default as default3 } from "./getClosestParent.js";
4
- import { default as default4 } from "./getInstanceFromElement.js";
5
- import { default as default5 } from "./importOnInteraction.js";
6
- import { default as default6 } from "./importWhenIdle.js";
7
- import { default as default7 } from "./importWhenVisible.js";
8
- import { default as default8 } from "./importOnMediaQuery.js";
9
- import { default as default9 } from "./importWhenPrefersMotion.js";
10
- export {
11
- default2 as createApp,
12
- default3 as getClosestParent,
13
- default4 as getInstanceFromElement,
14
- default5 as importOnInteraction,
15
- default8 as importOnMediaQuery,
16
- default6 as importWhenIdle,
17
- default9 as importWhenPrefersMotion,
18
- default7 as importWhenVisible
19
- };
4
+ export * from "./getInstanceFromElement.js";
5
+ export * from "./importOnInteraction.js";
6
+ export * from "./importOnMediaQuery.js";
7
+ export * from "./importWhenIdle.js";
8
+ export * from "./importWhenPrefersMotion.js";
9
+ export * from "./importWhenVisible.js";
20
10
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/helpers/index.ts"],
4
- "sourcesContent": ["export * from './getDirectChildren.js';\nexport { default as createApp } from './createApp.js';\nexport { default as getClosestParent } from './getClosestParent.js';\nexport { default as getInstanceFromElement } from './getInstanceFromElement.js';\nexport { default as importOnInteraction } from './importOnInteraction.js';\nexport { default as importWhenIdle } from './importWhenIdle.js';\nexport { default as importWhenVisible } from './importWhenVisible.js';\nexport { default as importOnMediaQuery } from './importOnMediaQuery.js';\nexport { default as importWhenPrefersMotion } from './importWhenPrefersMotion.js';\n"],
5
- "mappings": "AAAA,cAAc;AACd,SAAoB,WAAXA,gBAA4B;AACrC,SAAoB,WAAXA,gBAAmC;AAC5C,SAAoB,WAAXA,gBAAyC;AAClD,SAAoB,WAAXA,gBAAsC;AAC/C,SAAoB,WAAXA,gBAAiC;AAC1C,SAAoB,WAAXA,gBAAoC;AAC7C,SAAoB,WAAXA,gBAAqC;AAC9C,SAAoB,WAAXA,gBAA0C;",
6
- "names": ["default"]
4
+ "sourcesContent": ["export * from './createApp.js';\nexport * from './getClosestParent.js';\nexport * from './getDirectChildren.js';\nexport * from './getInstanceFromElement.js';\nexport * from './importOnInteraction.js';\nexport * from './importOnMediaQuery.js';\nexport * from './importWhenIdle.js';\nexport * from './importWhenPrefersMotion.js';\nexport * from './importWhenVisible.js';\n"],
5
+ "mappings": "AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
6
+ "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studiometa/js-toolkit",
3
- "version": "3.0.0-beta.1",
3
+ "version": "3.0.0-beta.3",
4
4
  "description": "A set of useful little bits of JavaScript to boost your project! 🚀",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -2,15 +2,15 @@ export interface ServiceInterface<T> {
2
2
  /**
3
3
  * Remove a function from the resize service by its key.
4
4
  */
5
- remove(key: string): void;
5
+ remove(key: string | symbol): void;
6
6
  /**
7
7
  * Add a callback to the service. The callback will receive the current service props as parameter.
8
8
  */
9
- add(key: string, callback: (props: T) => void): void;
9
+ add(key: string | symbol, callback: (props: T) => void): void;
10
10
  /**
11
11
  * Test if the service has alreaydy a callback for the given key.
12
12
  */
13
- has(key: string): boolean;
13
+ has(key: string | symbol): boolean;
14
14
  /**
15
15
  * Get the service's props.
16
16
  */
@@ -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
- ((instance: AbstractService) => EventTarget),
23
+ (instance: AbstractService) => EventTarget,
24
24
  [
25
25
  string,
26
26
  AddEventListenerOptions?
@@ -46,7 +46,7 @@ export declare class AbstractService<PropsType = any> {
46
46
  /**
47
47
  * Get a service instance as a singleton based on the given key.
48
48
  */
49
- static getInstance<T extends ServiceInterface<any>>(key?: any, ...args: any[]): any;
49
+ static getInstance<T extends ServiceInterface<any>>(keys?: any, ...args: any[]): T;
50
50
  /**
51
51
  * Is the service active or not?
52
52
  */
@@ -58,23 +58,23 @@ export declare class AbstractService<PropsType = any> {
58
58
  /**
59
59
  * Holds all the callbacks that will be triggered.
60
60
  */
61
- callbacks: Map<string, (props: PropsType) => unknown>;
61
+ callbacks: Map<string | symbol, (props: PropsType) => unknown>;
62
62
  /**
63
63
  * Does the service has the given key?
64
64
  */
65
- has(key: string): boolean;
65
+ has(key: string | symbol): boolean;
66
66
  /**
67
67
  * Get a service callback by its key.
68
68
  */
69
- get(key: string): (props: PropsType) => unknown;
69
+ get(key: string | symbol): (props: PropsType) => unknown;
70
70
  /**
71
71
  * Add a callback to the service.
72
72
  */
73
- add(key: string, callback: (props: PropsType) => unknown): void;
73
+ add(key: string | symbol, callback: (props: PropsType) => unknown): void;
74
74
  /**
75
75
  * Remove a callback from the service by its key.
76
76
  */
77
- remove(key: string): void;
77
+ remove(key: string | symbol): void;
78
78
  /**
79
79
  * Trigger all service callbacks with service props.
80
80
  */
@@ -1,3 +1,4 @@
1
+ import { cache } from "../utils/cache.js";
1
2
  class AbstractService {
2
3
  /**
3
4
  * Service configuration.
@@ -10,17 +11,16 @@ class AbstractService {
10
11
  /**
11
12
  * Get a service instance as a singleton based on the given key.
12
13
  */
13
- static getInstance(key = this, ...args) {
14
- if (!this.__instances.has(key)) {
14
+ static getInstance(keys = [this], ...args) {
15
+ return cache(keys, () => {
15
16
  const instance = new this(...args);
16
- this.__instances.set(key, {
17
- add: (key2, callback) => instance.add(key2, callback),
18
- remove: (key2) => instance.remove(key2),
19
- has: (key2) => instance.has(key2),
17
+ return {
18
+ add: (key, callback) => instance.add(key, callback),
19
+ remove: (key) => instance.remove(key),
20
+ has: (key) => instance.has(key),
20
21
  props: () => instance.props
21
- });
22
- }
23
- return this.__instances.get(key);
22
+ };
23
+ });
24
24
  }
25
25
  /**
26
26
  * Is the service active or not?
@@ -51,7 +51,7 @@ class AbstractService {
51
51
  */
52
52
  add(key, callback) {
53
53
  if (this.has(key)) {
54
- console.warn(`The key \`${key}\` has already been added.`);
54
+ console.warn(`The key \`${String(key)}\` has already been added.`);
55
55
  return;
56
56
  }
57
57
  if (this.callbacks.size === 0 && !this.__isInit) {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/services/AbstractService.ts"],
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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ACpC,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;",
6
- "names": ["key"]
4
+ "sourcesContent": ["import { cache } from '../utils/cache.js';\n\nexport interface ServiceInterface<T> {\n /**\n * Remove a function from the resize service by its key.\n */\n remove(key: string | symbol): void;\n /**\n * Add a callback to the service. The callback will receive the current service props as parameter.\n */\n add(key: string | symbol, callback: (props: T) => void): void;\n /**\n * Test if the service has alreaydy a callback for the given key.\n */\n has(key: string | symbol): 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>>(keys: any = [this], ...args: any[]) {\n return cache(keys, () => {\n // @ts-ignore\n const instance = new this(...args);\n return {\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\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 | symbol, (props: PropsType) => unknown> = new Map();\n\n /**\n * Does the service has the given key?\n */\n has(key: string | symbol): boolean {\n return this.callbacks.has(key);\n }\n\n /**\n * Get a service callback by its key.\n */\n get(key: string | symbol): (props: PropsType) => unknown {\n return this.callbacks.get(key);\n }\n\n /**\n * Add a callback to the service.\n */\n add(key: string | symbol, callback: (props: PropsType) => unknown) {\n if (this.has(key)) {\n console.warn(`The key \\`${String(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 | symbol) {\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": "AAAA,SAAS,aAAa;AAgCf,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,OAAY,CAAC,IAAI,MAAM,MAAa;AACtF,WAAO,MAAM,MAAM,MAAM;AAEvB,YAAM,WAAW,IAAI,KAAK,GAAG,IAA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A,EAE1B;AAAA;AAAA;AAAA;AAAA,EAKA,eAAe,MAAwB;AACrC,eAAW,CAAC,QAAQ,MAAM,KAAK,KAAK,YAAY,QAAQ;AACtD,YAAM,iBAAiB,OAAO,IAAI;AAClC,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;",
6
+ "names": []
7
7
  }
@@ -208,7 +208,11 @@ class DragService extends AbstractService {
208
208
  }
209
209
  }
210
210
  function useDrag(target, options) {
211
- return DragService.getInstance(target, target, options);
211
+ return DragService.getInstance(
212
+ [target, JSON.stringify(options)],
213
+ target,
214
+ options
215
+ );
212
216
  }
213
217
  export {
214
218
  DragService,
@@ -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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AAS,IAAI,MAAM,IAAI,MAAM,OAAO;AAC1C,YAAM,SAAS,IAAI,MAAM,IAAI,MAAM,OAAO;AAC1C,YAAM,OAAO,YAAY,MAAM;AAE/B,WAAK,QAAQ,KAAK;AAClB,WAAK,gBAAgB;AAAA,IACvB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,YAAY,OAA8C;AACxD,YAAQ,MAAM,MAAM;AAAA,MAClB,KAAK;AACH,cAAM,eAAe;AACrB;AAAA,MACF,KAAK;AACH,YAAI,KAAK,oBAAoB;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;",
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>(\n [target, JSON.stringify(options)],\n target,\n options,\n );\n}\n"],
5
+ "mappings": "AACA,SAAS,uBAAuB;AAChC,SAAS,cAAc;AACvB,SAAS,WAAW,yBAAyB;AAC7C,SAAS,uBAAuB,6BAA6B;AAsD7D,IAAI,QAAQ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AAI,SAAS;AACzB,YAAM,MAAM,IAAI,SAAS;AACzB,YAAM,SAAS,IAAI,MAAM,IAAI,MAAM,OAAO;AAC1C,YAAM,SAAS,IAAI,MAAM,IAAI,MAAM,OAAO;AAC1C,YAAM,OAAO,YAAY,MAAM;AAE/B,WAAK,QAAQ,KAAK;AAClB,WAAK,gBAAgB;AAAA,IACvB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,YAAY,OAA8C;AACxD,YAAQ,MAAM,MAAM;AAAA,MAClB,KAAK;AACH,cAAM,eAAe;AACrB;AAAA,MACF,KAAK;AACH,YAAI,KAAK,oBAAoB;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;AAAA,IACjB,CAAC,QAAQ,KAAK,UAAU,OAAO,CAAC;AAAA,IAChC;AAAA,IACA;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
@@ -1,5 +1,5 @@
1
1
  import { AbstractService } from "./AbstractService.js";
2
- import keyCodes from "../utils/keyCodes.js";
2
+ import { keyCodes } from "../utils/keyCodes.js";
3
3
  function getInitialKeyCodes() {
4
4
  return Object.fromEntries(Object.keys(keyCodes).map((key) => [key, false]));
5
5
  }
@@ -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,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;",
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,SAAS,gBAAgB;AAEzB,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
  }
@@ -0,0 +1,20 @@
1
+ import type { ServiceInterface } from './AbstractService.js';
2
+ import { AbstractService } from './AbstractService.js';
3
+ export interface MutationServiceProps {
4
+ mutations: MutationRecord[];
5
+ }
6
+ export type MutationServiceInterface = ServiceInterface<MutationServiceProps>;
7
+ export type MutationServiceOptions = MutationObserverInit;
8
+ export declare class MutationService extends AbstractService<MutationServiceProps> {
9
+ props: MutationServiceProps;
10
+ target: Node;
11
+ options: MutationObserverInit;
12
+ observer: MutationObserver;
13
+ constructor(target?: Node, options?: MutationObserverInit);
14
+ init(): void;
15
+ kill(): void;
16
+ }
17
+ /**
18
+ * Use the mutation service.
19
+ */
20
+ export declare function useMutation(target?: Node, options?: MutationObserverInit): MutationServiceInterface;
@@ -0,0 +1,34 @@
1
+ import { AbstractService } from "./AbstractService.js";
2
+ import { isEmpty } from "../utils/is.js";
3
+ class MutationService extends AbstractService {
4
+ props = {
5
+ mutations: []
6
+ };
7
+ target;
8
+ options;
9
+ observer;
10
+ constructor(target, options) {
11
+ super();
12
+ this.target = target ?? document.documentElement;
13
+ this.options = options;
14
+ }
15
+ init() {
16
+ this.observer = new MutationObserver((mutations) => {
17
+ this.props.mutations = mutations;
18
+ this.trigger(this.props);
19
+ });
20
+ this.observer.observe(this.target, isEmpty(this.options) ? { attributes: true } : this.options);
21
+ }
22
+ kill() {
23
+ this.observer.disconnect();
24
+ this.observer = null;
25
+ }
26
+ }
27
+ function useMutation(target, options) {
28
+ return MutationService.getInstance([target, JSON.stringify(options)], target, options);
29
+ }
30
+ export {
31
+ MutationService,
32
+ useMutation
33
+ };
34
+ //# sourceMappingURL=MutationService.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../packages/js-toolkit/services/MutationService.ts"],
4
+ "sourcesContent": ["import type { ServiceInterface } from './AbstractService.js';\nimport { AbstractService } from './AbstractService.js';\nimport { isEmpty } from '../utils/is.js';\n\nexport interface MutationServiceProps {\n mutations: MutationRecord[];\n}\n\nexport type MutationServiceInterface = ServiceInterface<MutationServiceProps>;\n\nexport type MutationServiceOptions = MutationObserverInit;\n\nexport class MutationService extends AbstractService<MutationServiceProps> {\n props: MutationServiceProps = {\n mutations: [],\n };\n\n target: Node;\n\n options: MutationObserverInit;\n\n observer: MutationObserver;\n\n constructor(target?: Node, options?: MutationObserverInit) {\n super();\n this.target = target ?? document.documentElement;\n this.options = options;\n }\n\n init() {\n this.observer = new MutationObserver((mutations) => {\n this.props.mutations = mutations;\n this.trigger(this.props);\n });\n this.observer.observe(this.target, isEmpty(this.options) ? { attributes: true } : this.options);\n }\n\n kill() {\n this.observer.disconnect();\n this.observer = null;\n }\n}\n\n/**\n * Use the mutation service.\n */\nexport function useMutation(\n target?: Node,\n options?: MutationObserverInit,\n): MutationServiceInterface {\n return MutationService.getInstance([target, JSON.stringify(options)], target, options);\n}\n"],
5
+ "mappings": "AACA,SAAS,uBAAuB;AAChC,SAAS,eAAe;AAUjB,MAAM,wBAAwB,gBAAsC;AAAA,EACzE,QAA8B;AAAA,IAC5B,WAAW,CAAC;AAAA,EACd;AAAA,EAEA;AAAA,EAEA;AAAA,EAEA;AAAA,EAEA,YAAY,QAAe,SAAgC;AACzD,UAAM;AACN,SAAK,SAAS,UAAU,SAAS;AACjC,SAAK,UAAU;AAAA,EACjB;AAAA,EAEA,OAAO;AACL,SAAK,WAAW,IAAI,iBAAiB,CAAC,cAAc;AAClD,WAAK,MAAM,YAAY;AACvB,WAAK,QAAQ,KAAK,KAAK;AAAA,IACzB,CAAC;AACD,SAAK,SAAS,QAAQ,KAAK,QAAQ,QAAQ,KAAK,OAAO,IAAI,EAAE,YAAY,KAAK,IAAI,KAAK,OAAO;AAAA,EAChG;AAAA,EAEA,OAAO;AACL,SAAK,SAAS,WAAW;AACzB,SAAK,WAAW;AAAA,EAClB;AACF;AAKO,SAAS,YACd,QACA,SAC0B;AAC1B,SAAO,gBAAgB,YAAY,CAAC,QAAQ,KAAK,UAAU,OAAO,CAAC,GAAG,QAAQ,OAAO;AACvF;",
6
+ "names": []
7
+ }
@@ -120,7 +120,7 @@ class PointerService extends AbstractService {
120
120
  }
121
121
  }
122
122
  function usePointer(target = window) {
123
- return PointerService.getInstance(target, target);
123
+ return PointerService.getInstance([target], target);
124
124
  }
125
125
  export {
126
126
  PointerService,
@@ -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": "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,CAA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ACJ;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): PointerService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,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,CAAC,MAAM,GAAG,MAAM;AACpD;",
6
6
  "names": []
7
7
  }
@@ -1,6 +1,6 @@
1
1
  import { AbstractService } from "./AbstractService.js";
2
2
  import { features } from "../Base/features.js";
3
- import debounce from "../utils/debounce.js";
3
+ import { debounce } from "../utils/debounce.js";
4
4
  class ResizeService extends AbstractService {
5
5
  static config = [[() => window, [["resize"]]]];
6
6
  breakpoints;
@@ -57,7 +57,7 @@ class ResizeService extends AbstractService {
57
57
  }
58
58
  }
59
59
  function useResize(breakpoints) {
60
- return ResizeService.getInstance(breakpoints, breakpoints);
60
+ return ResizeService.getInstance([breakpoints], breakpoints);
61
61
  }
62
62
  export {
63
63
  ResizeService,
@@ -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,MAAM,QAAQ,CAAC,CAAC,QAAQ,CAAC,CAAC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KO,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,SAAS,gBAAgB;AAelB,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,CAAC,WAAW,GAAG,WAAW;AAC7D;",
6
6
  "names": []
7
7
  }
@@ -1,5 +1,5 @@
1
1
  import { AbstractService } from "./AbstractService.js";
2
- import debounce from "../utils/debounce.js";
2
+ import { debounce } from "../utils/debounce.js";
3
3
  import { PASSIVE_CAPTURE_EVENT_OPTIONS } from "./utils.js";
4
4
  class ScrollService extends AbstractService {
5
5
  static config = [[() => document, [["scroll", PASSIVE_CAPTURE_EVENT_OPTIONS]]]];
@@ -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,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,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U,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 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AAM,MAAM;AAC/C,UAAM,QAAQ,IAAI,MAAM,WAAW,MAAM,MAAM;AAC/C,UAAM,KAAK,IAAI,MAAM,QAAQ;AAC7B,UAAM,KAAK,IAAI,MAAM,QAAQ;AAC7B,UAAM,MAAM,IAAI,MAAM,SAAS,MAAM,IAAI;AACzC,UAAM,MAAM,IAAI,MAAM,SAAS,MAAM,IAAI;AACzC,UAAM,IAAI,IAAI,MAAM,OAAO,SAAS,iBAAiB,cAAc,OAAO;AAC1E,UAAM,IAAI,IAAI,MAAM,OAAO,SAAS,iBAAiB,eAAe,OAAO;AAC3E,UAAM,SAAS,IAAI,MAAM,YAAY,MAAM,IAAI,MAAM,IAAI,IAAI,MAAM,IAAI,MAAM,IAAI;AACjF,UAAM,SAAS,IAAI,MAAM,YAAY,MAAM,IAAI,MAAM,IAAI,IAAI,MAAM,IAAI,MAAM,IAAI;AACjF,UAAM,OAAO,MAAM,IAAI;AACvB,UAAM,UAAU,MAAM,IAAI;AAC1B,UAAM,SAAS,MAAM,IAAI;AACzB,UAAM,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;",
6
6
  "names": []
7
7
  }
@@ -2,6 +2,7 @@ export * from './AbstractService.js';
2
2
  export * from './DragService.js';
3
3
  export * from './KeyService.js';
4
4
  export * from './LoadService.js';
5
+ export * from './MutationService.js';
5
6
  export * from './PointerService.js';
6
7
  export * from './RafService.js';
7
8
  export * from './ResizeService.js';
package/services/index.js CHANGED
@@ -2,6 +2,7 @@ export * from "./AbstractService.js";
2
2
  export * from "./DragService.js";
3
3
  export * from "./KeyService.js";
4
4
  export * from "./LoadService.js";
5
+ export * from "./MutationService.js";
5
6
  export * from "./PointerService.js";
6
7
  export * from "./RafService.js";
7
8
  export * from "./ResizeService.js";
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/services/index.ts"],
4
- "sourcesContent": ["export * from './AbstractService.js';\nexport * from './DragService.js';\nexport * from './KeyService.js';\nexport * from './LoadService.js';\nexport * from './PointerService.js';\nexport * from './RafService.js';\nexport * from './ResizeService.js';\nexport * from './ScrollService.js';\n"],
5
- "mappings": "AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
4
+ "sourcesContent": ["export * from './AbstractService.js';\nexport * from './DragService.js';\nexport * from './KeyService.js';\nexport * from './LoadService.js';\nexport * from './MutationService.js';\nexport * from './PointerService.js';\nexport * from './RafService.js';\nexport * from './ResizeService.js';\nexport * from './ScrollService.js';\n"],
5
+ "mappings": "AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
6
6
  "names": []
7
7
  }