@studiometa/js-toolkit 2.0.0-beta.9 → 2.0.0

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 (238) hide show
  1. package/Base/index.cjs +284 -0
  2. package/Base/index.d.ts +36 -28
  3. package/Base/index.js +113 -197
  4. package/Base/managers/ChildrenManager.cjs +146 -0
  5. package/Base/managers/ChildrenManager.d.ts +11 -3
  6. package/Base/managers/ChildrenManager.js +60 -59
  7. package/Base/managers/EventsManager.cjs +183 -0
  8. package/Base/managers/EventsManager.d.ts +13 -20
  9. package/Base/managers/EventsManager.js +72 -112
  10. package/Base/managers/OptionsManager.cjs +173 -0
  11. package/Base/managers/OptionsManager.d.ts +20 -13
  12. package/Base/managers/OptionsManager.js +60 -91
  13. package/Base/managers/RefsManager.cjs +99 -0
  14. package/Base/managers/RefsManager.d.ts +4 -4
  15. package/Base/managers/RefsManager.js +28 -38
  16. package/Base/managers/ServicesManager.cjs +134 -0
  17. package/Base/managers/ServicesManager.d.ts +14 -8
  18. package/Base/managers/ServicesManager.js +42 -67
  19. package/Base/utils.cjs +41 -0
  20. package/Base/utils.d.ts +4 -4
  21. package/Base/utils.js +12 -11
  22. package/decorators/index.cjs +45 -0
  23. package/decorators/index.d.ts +1 -0
  24. package/decorators/index.js +20 -8
  25. package/decorators/withBreakpointManager.cjs +84 -0
  26. package/decorators/withBreakpointManager.d.ts +2 -2
  27. package/decorators/withBreakpointManager.js +18 -40
  28. package/decorators/withBreakpointObserver.cjs +114 -0
  29. package/decorators/withBreakpointObserver.d.ts +1 -1
  30. package/decorators/withBreakpointObserver.js +45 -88
  31. package/decorators/withDrag.cjs +53 -0
  32. package/decorators/withDrag.d.ts +1 -1
  33. package/decorators/withDrag.js +24 -26
  34. package/decorators/withExtraConfig.cjs +43 -0
  35. package/decorators/withExtraConfig.d.ts +1 -1
  36. package/decorators/withExtraConfig.js +17 -12
  37. package/decorators/withFreezedOptions.cjs +37 -0
  38. package/decorators/withFreezedOptions.d.ts +14 -0
  39. package/decorators/withFreezedOptions.js +15 -0
  40. package/decorators/withIntersectionObserver.cjs +61 -0
  41. package/decorators/withIntersectionObserver.d.ts +1 -1
  42. package/decorators/withIntersectionObserver.js +29 -33
  43. package/decorators/withMountWhenInView.cjs +68 -0
  44. package/decorators/withMountWhenInView.d.ts +1 -1
  45. package/decorators/withMountWhenInView.js +31 -53
  46. package/decorators/withScrolledInView.cjs +114 -0
  47. package/decorators/withScrolledInView.d.ts +1 -1
  48. package/decorators/withScrolledInView.js +35 -39
  49. package/decorators/withVue2.cjs +61 -0
  50. package/decorators/withVue2.js +27 -32
  51. package/helpers/createApp.cjs +52 -0
  52. package/helpers/createApp.d.ts +3 -26
  53. package/helpers/createApp.js +12 -12
  54. package/helpers/getInstanceFromElement.cjs +32 -0
  55. package/helpers/getInstanceFromElement.d.ts +14 -0
  56. package/helpers/getInstanceFromElement.js +10 -0
  57. package/helpers/importOnInteraction.cjs +52 -0
  58. package/helpers/importOnInteraction.d.ts +3 -5
  59. package/helpers/importOnInteraction.js +17 -18
  60. package/helpers/importWhenIdle.cjs +49 -0
  61. package/helpers/importWhenIdle.d.ts +3 -5
  62. package/helpers/importWhenIdle.js +14 -17
  63. package/helpers/importWhenVisible.cjs +50 -0
  64. package/helpers/importWhenVisible.d.ts +4 -6
  65. package/helpers/importWhenVisible.js +16 -18
  66. package/helpers/index.cjs +37 -0
  67. package/helpers/index.d.ts +1 -0
  68. package/helpers/index.js +12 -4
  69. package/helpers/utils.cjs +35 -0
  70. package/helpers/utils.d.ts +3 -4
  71. package/helpers/utils.js +7 -6
  72. package/index.cjs +33 -0
  73. package/index.js +7 -4
  74. package/package.json +14 -4
  75. package/services/Service.cjs +76 -0
  76. package/services/Service.d.ts +9 -9
  77. package/services/Service.js +19 -32
  78. package/services/drag.cjs +212 -0
  79. package/services/drag.d.ts +3 -2
  80. package/services/drag.js +64 -69
  81. package/services/index.cjs +41 -0
  82. package/services/index.js +16 -7
  83. package/services/key.cjs +104 -0
  84. package/services/key.d.ts +1 -1
  85. package/services/key.js +28 -36
  86. package/services/load.cjs +72 -0
  87. package/services/load.d.ts +1 -1
  88. package/services/load.js +17 -19
  89. package/services/pointer.cjs +162 -0
  90. package/services/pointer.d.ts +1 -1
  91. package/services/pointer.js +43 -60
  92. package/services/raf.cjs +83 -0
  93. package/services/raf.d.ts +1 -1
  94. package/services/raf.js +20 -28
  95. package/services/resize.cjs +108 -0
  96. package/services/resize.d.ts +1 -1
  97. package/services/resize.js +33 -42
  98. package/services/scroll.cjs +131 -0
  99. package/services/scroll.d.ts +1 -1
  100. package/services/scroll.js +26 -35
  101. package/utils/collide/boundingRectToCircle.cjs +34 -0
  102. package/utils/collide/boundingRectToCircle.d.ts +6 -6
  103. package/utils/collide/boundingRectToCircle.js +7 -10
  104. package/utils/collide/collideCircleCircle.cjs +34 -0
  105. package/utils/collide/collideCircleCircle.d.ts +4 -4
  106. package/utils/collide/collideCircleCircle.js +8 -7
  107. package/utils/collide/collideCircleRect.cjs +46 -0
  108. package/utils/collide/collideCircleRect.d.ts +8 -8
  109. package/utils/collide/collideCircleRect.js +10 -12
  110. package/utils/collide/collidePointCircle.cjs +31 -0
  111. package/utils/collide/collidePointCircle.d.ts +6 -6
  112. package/utils/collide/collidePointCircle.js +8 -5
  113. package/utils/collide/collidePointRect.cjs +28 -0
  114. package/utils/collide/collidePointRect.d.ts +7 -7
  115. package/utils/collide/collidePointRect.js +5 -2
  116. package/utils/collide/collideRectRect.cjs +28 -0
  117. package/utils/collide/collideRectRect.d.ts +5 -5
  118. package/utils/collide/collideRectRect.js +5 -2
  119. package/utils/collide/index.cjs +39 -0
  120. package/utils/collide/index.js +14 -6
  121. package/utils/css/classes.cjs +47 -0
  122. package/utils/css/classes.d.ts +13 -12
  123. package/utils/css/classes.js +19 -15
  124. package/utils/css/index.cjs +38 -0
  125. package/utils/css/index.d.ts +2 -2
  126. package/utils/css/index.js +13 -6
  127. package/utils/css/matrix.cjs +30 -0
  128. package/utils/css/matrix.d.ts +14 -15
  129. package/utils/css/matrix.js +7 -5
  130. package/utils/css/styles.cjs +44 -0
  131. package/utils/css/styles.d.ts +8 -20
  132. package/utils/css/styles.js +12 -12
  133. package/utils/css/transition.cjs +120 -0
  134. package/utils/css/transition.d.ts +15 -9
  135. package/utils/css/transition.js +70 -119
  136. package/utils/debounce.cjs +34 -0
  137. package/utils/debounce.d.ts +4 -4
  138. package/utils/debounce.js +7 -9
  139. package/utils/focusTrap.cjs +82 -0
  140. package/utils/focusTrap.d.ts +1 -0
  141. package/utils/focusTrap.js +31 -22
  142. package/utils/history.cjs +89 -0
  143. package/utils/history.d.ts +13 -11
  144. package/utils/history.js +28 -57
  145. package/utils/index.cjs +54 -0
  146. package/utils/index.d.ts +4 -1
  147. package/utils/index.js +32 -11
  148. package/utils/keyCodes.cjs +35 -0
  149. package/utils/keyCodes.js +5 -2
  150. package/utils/math/clamp.cjs +28 -0
  151. package/utils/math/clamp.d.ts +1 -1
  152. package/utils/math/clamp.js +5 -2
  153. package/utils/math/clamp01.cjs +32 -0
  154. package/utils/math/clamp01.d.ts +1 -1
  155. package/utils/math/clamp01.js +6 -3
  156. package/utils/math/damp.cjs +28 -0
  157. package/utils/math/damp.d.ts +6 -6
  158. package/utils/math/damp.js +5 -4
  159. package/utils/math/ease.cjs +88 -0
  160. package/utils/math/ease.d.ts +7 -7
  161. package/utils/math/ease.js +50 -25
  162. package/utils/math/index.cjs +43 -0
  163. package/utils/math/index.js +17 -8
  164. package/utils/math/inertiaFinalValue.cjs +37 -0
  165. package/utils/math/inertiaFinalValue.d.ts +5 -5
  166. package/utils/math/inertiaFinalValue.js +8 -10
  167. package/utils/math/lerp.cjs +28 -0
  168. package/utils/math/lerp.d.ts +4 -4
  169. package/utils/math/lerp.js +5 -2
  170. package/utils/math/map.cjs +28 -0
  171. package/utils/math/map.d.ts +6 -6
  172. package/utils/math/map.js +5 -2
  173. package/utils/math/round.cjs +28 -0
  174. package/utils/math/round.d.ts +4 -4
  175. package/utils/math/round.js +5 -3
  176. package/utils/memoize.cjs +43 -0
  177. package/utils/memoize.d.ts +1 -1
  178. package/utils/memoize.js +10 -19
  179. package/utils/nextFrame.cjs +38 -0
  180. package/utils/nextFrame.d.ts +6 -6
  181. package/utils/nextFrame.js +13 -10
  182. package/utils/nextMicrotask.cjs +28 -0
  183. package/utils/nextMicrotask.d.ts +8 -0
  184. package/utils/nextMicrotask.js +6 -0
  185. package/utils/nextTick.cjs +30 -0
  186. package/utils/nextTick.d.ts +8 -0
  187. package/utils/nextTick.js +8 -0
  188. package/utils/object/autoBind.cjs +49 -0
  189. package/utils/object/autoBind.d.ts +3 -3
  190. package/utils/object/autoBind.js +12 -22
  191. package/utils/object/getAllProperties.cjs +37 -0
  192. package/utils/object/getAllProperties.d.ts +3 -3
  193. package/utils/object/getAllProperties.js +10 -13
  194. package/utils/object/index.cjs +33 -0
  195. package/utils/object/index.js +8 -3
  196. package/utils/object/isObject.cjs +28 -0
  197. package/utils/object/isObject.d.ts +1 -1
  198. package/utils/object/isObject.js +6 -3
  199. package/utils/scrollTo.cjs +80 -0
  200. package/utils/scrollTo.d.ts +1 -1
  201. package/utils/scrollTo.js +20 -40
  202. package/utils/string/index.cjs +47 -0
  203. package/utils/string/index.js +22 -10
  204. package/utils/string/withLeadingCharacters.cjs +32 -0
  205. package/utils/string/withLeadingCharacters.d.ts +3 -2
  206. package/utils/string/withLeadingCharacters.js +7 -4
  207. package/utils/string/withLeadingSlash.cjs +32 -0
  208. package/utils/string/withLeadingSlash.d.ts +1 -1
  209. package/utils/string/withLeadingSlash.js +7 -4
  210. package/utils/string/withTrailingCharacters.cjs +32 -0
  211. package/utils/string/withTrailingCharacters.d.ts +3 -2
  212. package/utils/string/withTrailingCharacters.js +7 -4
  213. package/utils/string/withTrailingSlash.cjs +32 -0
  214. package/utils/string/withTrailingSlash.d.ts +1 -1
  215. package/utils/string/withTrailingSlash.js +7 -4
  216. package/utils/string/withoutLeadingCharacters.cjs +28 -0
  217. package/utils/string/withoutLeadingCharacters.d.ts +3 -2
  218. package/utils/string/withoutLeadingCharacters.js +6 -3
  219. package/utils/string/withoutLeadingCharactersRecursive.cjs +37 -0
  220. package/utils/string/withoutLeadingCharactersRecursive.d.ts +3 -2
  221. package/utils/string/withoutLeadingCharactersRecursive.js +8 -7
  222. package/utils/string/withoutLeadingSlash.cjs +32 -0
  223. package/utils/string/withoutLeadingSlash.d.ts +1 -1
  224. package/utils/string/withoutLeadingSlash.js +7 -4
  225. package/utils/string/withoutTrailingCharacters.cjs +28 -0
  226. package/utils/string/withoutTrailingCharacters.d.ts +3 -2
  227. package/utils/string/withoutTrailingCharacters.js +6 -3
  228. package/utils/string/withoutTrailingCharactersRecursive.cjs +37 -0
  229. package/utils/string/withoutTrailingCharactersRecursive.d.ts +3 -2
  230. package/utils/string/withoutTrailingCharactersRecursive.js +8 -7
  231. package/utils/string/withoutTrailingSlash.cjs +32 -0
  232. package/utils/string/withoutTrailingSlash.d.ts +1 -1
  233. package/utils/string/withoutTrailingSlash.js +7 -4
  234. package/utils/throttle.cjs +36 -0
  235. package/utils/throttle.d.ts +3 -3
  236. package/utils/throttle.js +9 -9
  237. package/index.modern.js +0 -1
  238. package/index.umd.js +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studiometa/js-toolkit",
3
- "version": "2.0.0-beta.9",
3
+ "version": "2.0.0",
4
4
  "description": "A set of useful little bits of JavaScript to boost your project! 🚀",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,18 +17,28 @@
17
17
  "homepage": "https://github.com/studiometa/js-toolkit#readme",
18
18
  "type": "module",
19
19
  "sideEffects": false,
20
- "unpkg": "./index.umd.js",
21
20
  "main": "./index.js",
22
21
  "module": "./index.js",
22
+ "types": "./index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "import": "./index.js",
26
+ "require": "./index.cjs",
27
+ "types": "./index.d.ts"
28
+ },
29
+ "./utils": {
30
+ "import": "./utils/index.js",
31
+ "require": "./utils/index.cjs",
32
+ "types": "./utils/index.d.ts"
33
+ }
34
+ },
23
35
  "keywords": [
24
36
  "js-toolkit",
25
37
  "component",
26
38
  "studiometa",
27
39
  "toolkit"
28
40
  ],
29
- "types": "index.d.ts",
30
41
  "dependencies": {
31
- "@babel/runtime": "^7.15.4",
32
42
  "deepmerge": "^4.2.2"
33
43
  }
34
44
  }
@@ -0,0 +1,76 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __publicField = (obj, key, value) => {
20
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
+ return value;
22
+ };
23
+
24
+ // packages/js-toolkit/services/Service.js
25
+ var Service_exports = {};
26
+ __export(Service_exports, {
27
+ default: () => Service
28
+ });
29
+ module.exports = __toCommonJS(Service_exports);
30
+ var Service = class {
31
+ constructor() {
32
+ __publicField(this, "props");
33
+ this.callbacks = /* @__PURE__ */ new Map();
34
+ this.isInit = false;
35
+ }
36
+ updateProps(...args) {
37
+ this.props = args;
38
+ throw new Error("The `updateProps` method must be implemented.");
39
+ }
40
+ init() {
41
+ throw new Error("The `init` method must be implemented.");
42
+ }
43
+ kill() {
44
+ throw new Error("The `kill` method must be implemented.");
45
+ }
46
+ add(key, callback) {
47
+ if (this.has(key)) {
48
+ throw new Error(`A callback with the key \`${key}\` has already been registered.`);
49
+ }
50
+ if (this.callbacks.size === 0 && !this.isInit) {
51
+ this.init();
52
+ this.isInit = true;
53
+ }
54
+ this.callbacks.set(key, callback);
55
+ }
56
+ has(key) {
57
+ return this.callbacks.has(key);
58
+ }
59
+ get(key) {
60
+ return this.callbacks.get(key);
61
+ }
62
+ remove(key) {
63
+ this.callbacks.delete(key);
64
+ if (this.callbacks.size === 0 && this.isInit) {
65
+ this.kill();
66
+ this.isInit = false;
67
+ }
68
+ }
69
+ trigger(...args) {
70
+ this.callbacks.forEach(function forEachCallback(callback) {
71
+ callback(...args);
72
+ });
73
+ return this;
74
+ }
75
+ };
76
+ if (module.exports.default) module.exports = module.exports.default;
@@ -13,43 +13,43 @@ export default class Service {
13
13
  * This method MUST be implemented by the service extending this class.
14
14
  *
15
15
  * @param {...any} args
16
- * @return {this['props']}
16
+ * @returns {this['props']}
17
17
  */
18
18
  updateProps(...args: any[]): unknown;
19
19
  /**
20
20
  * Method to initialize the service behaviors.
21
21
  * This method MUST be implemented by the service extending this class.
22
22
  *
23
- * @return {void}
23
+ * @returns {void}
24
24
  */
25
25
  init(): void;
26
26
  /**
27
27
  * Method to kill the service behaviors.
28
28
  * This method MUST be implemented by the service extending this class.
29
29
  *
30
- * @return {void}
30
+ * @returns {void}
31
31
  */
32
32
  kill(): void;
33
33
  /**
34
34
  * Add a callback.
35
35
  *
36
- * @param {String} key The callback's identifier
36
+ * @param {string} key The callback's identifier
37
37
  * @param {Function} callback The callback function
38
- * @return {void}
38
+ * @returns {void}
39
39
  */
40
40
  add(key: string, callback: Function): void;
41
41
  /**
42
42
  * Test if a callback with the given key has already been added.
43
43
  *
44
- * @param {String} key The identifier to test
45
- * @return {Boolean} Whether or not the identifier already exists
44
+ * @param {string} key The identifier to test
45
+ * @returns {boolean} Whether or not the identifier already exists
46
46
  */
47
47
  has(key: string): boolean;
48
48
  /**
49
49
  * Get the callback tied to the given key.
50
50
  *
51
51
  * @param {string} key The identifier to get
52
- * @return {Function} The callback function
52
+ * @returns {Function} The callback function
53
53
  */
54
54
  get(key: string): Function;
55
55
  /**
@@ -62,7 +62,7 @@ export default class Service {
62
62
  * Trigger each added callback with the given arguments.
63
63
  *
64
64
  * @param {...any} args All the arguments to apply to the callback
65
- * @return {this} The current instance
65
+ * @returns {this} The current instance
66
66
  */
67
67
  trigger(...args: any[]): this;
68
68
  }
@@ -1,68 +1,55 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- export default class Service {
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ return value;
6
+ };
7
+ class Service {
3
8
  constructor() {
4
- _defineProperty(this, "props", void 0);
5
-
6
- this.callbacks = new Map();
9
+ __publicField(this, "props");
10
+ this.callbacks = /* @__PURE__ */ new Map();
7
11
  this.isInit = false;
8
12
  }
9
-
10
- updateProps() {
11
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
12
- args[_key] = arguments[_key];
13
- }
14
-
13
+ updateProps(...args) {
15
14
  this.props = args;
16
- throw new Error('The `updateProps` method must be implemented.');
15
+ throw new Error("The `updateProps` method must be implemented.");
17
16
  }
18
-
19
17
  init() {
20
- throw new Error('The `init` method must be implemented.');
18
+ throw new Error("The `init` method must be implemented.");
21
19
  }
22
-
23
20
  kill() {
24
- throw new Error('The `kill` method must be implemented.');
21
+ throw new Error("The `kill` method must be implemented.");
25
22
  }
26
-
27
23
  add(key, callback) {
28
24
  if (this.has(key)) {
29
- throw new Error("A callback with the key `".concat(key, "` has already been registered."));
25
+ throw new Error(`A callback with the key \`${key}\` has already been registered.`);
30
26
  }
31
-
32
27
  if (this.callbacks.size === 0 && !this.isInit) {
33
28
  this.init();
34
29
  this.isInit = true;
35
30
  }
36
-
37
31
  this.callbacks.set(key, callback);
38
32
  }
39
-
40
33
  has(key) {
41
34
  return this.callbacks.has(key);
42
35
  }
43
-
44
36
  get(key) {
45
37
  return this.callbacks.get(key);
46
38
  }
47
-
48
39
  remove(key) {
49
40
  this.callbacks.delete(key);
50
-
51
41
  if (this.callbacks.size === 0 && this.isInit) {
52
42
  this.kill();
53
43
  this.isInit = false;
54
44
  }
55
45
  }
56
-
57
- trigger() {
58
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
59
- args[_key2] = arguments[_key2];
60
- }
61
-
46
+ trigger(...args) {
62
47
  this.callbacks.forEach(function forEachCallback(callback) {
63
48
  callback(...args);
64
49
  });
65
50
  return this;
66
51
  }
67
-
68
- }
52
+ }
53
+ export {
54
+ Service as default
55
+ };
@@ -0,0 +1,212 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var __publicField = (obj, key, value) => {
23
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
24
+ return value;
25
+ };
26
+
27
+ // packages/js-toolkit/services/drag.js
28
+ var drag_exports = {};
29
+ __export(drag_exports, {
30
+ default: () => useDrag
31
+ });
32
+ module.exports = __toCommonJS(drag_exports);
33
+ var import_Service = __toESM(require("./Service.cjs"), 1);
34
+ var import_pointer = __toESM(require("./pointer.cjs"), 1);
35
+ var import_raf = __toESM(require("./raf.cjs"), 1);
36
+ var import_inertiaFinalValue = __toESM(require("../utils/math/inertiaFinalValue.cjs"), 1);
37
+ var MODES = {
38
+ START: "start",
39
+ DRAG: "drag",
40
+ DROP: "drop",
41
+ INERTIA: "inertia",
42
+ STOP: "stop"
43
+ };
44
+ var id = 0;
45
+ var Drag = class extends import_Service.default {
46
+ constructor(target, options = {}) {
47
+ super();
48
+ __publicField(this, "id");
49
+ __publicField(this, "props", {
50
+ target: null,
51
+ mode: void 0,
52
+ isGrabbing: false,
53
+ hasInertia: false,
54
+ x: 0,
55
+ y: 0,
56
+ delta: {
57
+ x: 0,
58
+ y: 0
59
+ },
60
+ origin: {
61
+ x: 0,
62
+ y: 0
63
+ },
64
+ distance: {
65
+ x: 0,
66
+ y: 0
67
+ },
68
+ final: {
69
+ x: 0,
70
+ y: 0
71
+ }
72
+ });
73
+ __publicField(this, "options", {
74
+ dampFactor: 0.85,
75
+ dragTreshold: 10
76
+ });
77
+ id += 1;
78
+ this.id = `drag-${id}`;
79
+ this.props.target = target;
80
+ this.options = { ...this.options, ...options };
81
+ this.pointerHandler = this.pointerHandler.bind(this);
82
+ this.rafHandler = this.rafHandler.bind(this);
83
+ }
84
+ init() {
85
+ const options = { passive: true };
86
+ this.props.target.addEventListener("mousedown", this, options);
87
+ this.props.target.addEventListener("touchstart", this, options);
88
+ this.props.target.addEventListener("dragstart", this, { capture: true });
89
+ this.props.target.addEventListener("click", this, { capture: true });
90
+ const pointer = (0, import_pointer.default)();
91
+ pointer.add(this.id, this.pointerHandler);
92
+ return this;
93
+ }
94
+ kill() {
95
+ this.props.target.removeEventListener("mousedown", this);
96
+ this.props.target.removeEventListener("touchstart", this);
97
+ this.props.target.removeEventListener("dragstart", this);
98
+ this.props.target.removeEventListener("click", this);
99
+ const pointer = (0, import_pointer.default)();
100
+ pointer.remove(this.id);
101
+ return this;
102
+ }
103
+ handleEvent(event) {
104
+ if (event.type === "dragstart") {
105
+ event.preventDefault();
106
+ return;
107
+ }
108
+ if (event.type === "click") {
109
+ if (this.__shouldPreventClick) {
110
+ event.stopImmediatePropagation();
111
+ event.stopPropagation();
112
+ event.preventDefault();
113
+ }
114
+ return;
115
+ }
116
+ const x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
117
+ const y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
118
+ this.start(x, y);
119
+ }
120
+ start(x, y) {
121
+ this.props.x = x;
122
+ this.props.y = y;
123
+ this.props.origin.x = x;
124
+ this.props.origin.y = y;
125
+ this.props.delta.x = 0;
126
+ this.props.delta.y = 0;
127
+ this.props.distance.x = 0;
128
+ this.props.distance.y = 0;
129
+ this.props.final.x = x;
130
+ this.props.final.y = y;
131
+ this.props.mode = MODES.START;
132
+ this.props.isGrabbing = true;
133
+ this.trigger(this.props);
134
+ }
135
+ drop() {
136
+ this.props.isGrabbing = false;
137
+ this.props.mode = MODES.DROP;
138
+ this.props.hasInertia = true;
139
+ this.props.final.x = (0, import_inertiaFinalValue.default)(this.props.x, this.props.delta.x, this.options.dampFactor);
140
+ this.props.final.y = (0, import_inertiaFinalValue.default)(this.props.y, this.props.delta.y, this.options.dampFactor);
141
+ this.trigger(this.props);
142
+ setTimeout(() => {
143
+ const raf = (0, import_raf.default)();
144
+ raf.remove(this.id);
145
+ raf.add(this.id, this.rafHandler);
146
+ }, 0);
147
+ }
148
+ stop() {
149
+ (0, import_raf.default)().remove(this.id);
150
+ this.props.hasInertia = false;
151
+ this.props.mode = MODES.STOP;
152
+ this.trigger(this.props);
153
+ }
154
+ rafHandler() {
155
+ if (!this.props.isGrabbing) {
156
+ this.props.x += this.props.delta.x;
157
+ this.props.y += this.props.delta.y;
158
+ this.props.distance.x = this.props.x - this.props.origin.x;
159
+ this.props.distance.y = this.props.y - this.props.origin.y;
160
+ this.props.delta.x *= this.options.dampFactor;
161
+ this.props.delta.y *= this.options.dampFactor;
162
+ if (this.props.mode !== MODES.INERTIA) {
163
+ this.props.mode = MODES.INERTIA;
164
+ }
165
+ this.trigger(this.props);
166
+ if (Math.abs(this.props.delta.x) < 0.1 && Math.abs(this.props.delta.y) < 0.1) {
167
+ this.stop();
168
+ }
169
+ }
170
+ }
171
+ pointerHandler(props) {
172
+ if (this.props.isGrabbing) {
173
+ this.props.x = props.x;
174
+ this.props.y = props.y;
175
+ this.props.delta.x = props.delta.x;
176
+ this.props.delta.y = props.delta.y;
177
+ this.props.final.x = props.x;
178
+ this.props.final.y = props.y;
179
+ this.props.distance.x = this.props.x - this.props.origin.x;
180
+ this.props.distance.y = this.props.y - this.props.origin.y;
181
+ if (this.props.mode !== MODES.DRAG) {
182
+ this.props.mode = MODES.DRAG;
183
+ }
184
+ this.trigger(this.props);
185
+ if (!props.isDown) {
186
+ this.drop();
187
+ }
188
+ }
189
+ }
190
+ updateProps() {
191
+ return this.props;
192
+ }
193
+ get __shouldPreventClick() {
194
+ return Math.abs(this.props.distance.x) > this.options.dragTreshold || Math.abs(this.props.distance.y) > this.options.dragTreshold;
195
+ }
196
+ };
197
+ var instances = /* @__PURE__ */ new WeakMap();
198
+ function useDrag(target, options) {
199
+ let drag = instances.get(target);
200
+ if (!drag) {
201
+ const instance = new Drag(target, options);
202
+ drag = {
203
+ add: instance.add.bind(instance),
204
+ remove: instance.remove.bind(instance),
205
+ has: instance.has.bind(instance),
206
+ props: instance.updateProps.bind(instance)
207
+ };
208
+ instances.set(target, drag);
209
+ }
210
+ return drag;
211
+ }
212
+ if (module.exports.default) module.exports = module.exports.default;
@@ -12,7 +12,7 @@
12
12
  *
13
13
  * @param {HTMLElement} target
14
14
  * @param {DragServiceOptions} options
15
- * @return {DragService}
15
+ * @returns {DragService}
16
16
  */
17
17
  export default function useDrag(target: HTMLElement, options: DragServiceOptions): DragService;
18
18
  export type DragService = import('./index').ServiceInterface<DragServiceProps>;
@@ -73,5 +73,6 @@ export type DragServiceProps = {
73
73
  };
74
74
  };
75
75
  export type DragServiceOptions = {
76
- dampFactor?: number | undefined;
76
+ dampFactor?: number;
77
+ dragTreshold?: number;
77
78
  };