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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. package/Base/index.cjs +285 -0
  2. package/Base/index.d.ts +11 -2
  3. package/Base/index.js +114 -197
  4. package/Base/managers/ChildrenManager.cjs +146 -0
  5. package/Base/managers/ChildrenManager.d.ts +9 -1
  6. package/Base/managers/ChildrenManager.js +60 -59
  7. package/Base/managers/EventsManager.cjs +183 -0
  8. package/Base/managers/EventsManager.js +72 -112
  9. package/Base/managers/OptionsManager.cjs +174 -0
  10. package/Base/managers/OptionsManager.d.ts +9 -5
  11. package/Base/managers/OptionsManager.js +60 -90
  12. package/Base/managers/RefsManager.cjs +99 -0
  13. package/Base/managers/RefsManager.js +28 -38
  14. package/Base/managers/ServicesManager.cjs +131 -0
  15. package/Base/managers/ServicesManager.js +39 -67
  16. package/Base/utils.cjs +41 -0
  17. package/Base/utils.js +12 -11
  18. package/decorators/index.cjs +45 -0
  19. package/decorators/index.d.ts +1 -0
  20. package/decorators/index.js +20 -8
  21. package/decorators/withBreakpointManager.cjs +85 -0
  22. package/decorators/withBreakpointManager.js +18 -39
  23. package/decorators/withBreakpointObserver.cjs +115 -0
  24. package/decorators/withBreakpointObserver.js +44 -86
  25. package/decorators/withDrag.cjs +53 -0
  26. package/decorators/withDrag.js +24 -26
  27. package/decorators/withExtraConfig.cjs +43 -0
  28. package/decorators/withExtraConfig.js +17 -12
  29. package/decorators/withFreezedOptions.cjs +36 -0
  30. package/decorators/withFreezedOptions.d.ts +14 -0
  31. package/decorators/withFreezedOptions.js +14 -0
  32. package/decorators/withIntersectionObserver.cjs +62 -0
  33. package/decorators/withIntersectionObserver.js +29 -32
  34. package/decorators/withMountWhenInView.cjs +69 -0
  35. package/decorators/withMountWhenInView.js +31 -52
  36. package/decorators/withScrolledInView.cjs +114 -0
  37. package/decorators/withScrolledInView.js +35 -39
  38. package/decorators/withVue2.cjs +60 -0
  39. package/decorators/withVue2.js +26 -32
  40. package/helpers/createApp.cjs +52 -0
  41. package/helpers/createApp.d.ts +2 -2
  42. package/helpers/createApp.js +12 -12
  43. package/helpers/importOnInteraction.cjs +52 -0
  44. package/helpers/importOnInteraction.js +17 -18
  45. package/helpers/importWhenIdle.cjs +49 -0
  46. package/helpers/importWhenIdle.js +14 -17
  47. package/helpers/importWhenVisible.cjs +50 -0
  48. package/helpers/importWhenVisible.js +16 -18
  49. package/helpers/index.cjs +35 -0
  50. package/helpers/index.js +10 -4
  51. package/helpers/utils.cjs +35 -0
  52. package/helpers/utils.js +7 -6
  53. package/index.cjs +33 -0
  54. package/index.js +7 -4
  55. package/package.json +14 -4
  56. package/services/Service.cjs +76 -0
  57. package/services/Service.js +19 -32
  58. package/services/drag.cjs +213 -0
  59. package/services/drag.d.ts +1 -0
  60. package/services/drag.js +64 -68
  61. package/services/index.cjs +41 -0
  62. package/services/index.js +16 -7
  63. package/services/key.cjs +104 -0
  64. package/services/key.js +28 -36
  65. package/services/load.cjs +72 -0
  66. package/services/load.js +17 -19
  67. package/services/pointer.cjs +162 -0
  68. package/services/pointer.js +43 -60
  69. package/services/raf.cjs +83 -0
  70. package/services/raf.js +20 -28
  71. package/services/resize.cjs +108 -0
  72. package/services/resize.js +33 -42
  73. package/services/scroll.cjs +131 -0
  74. package/services/scroll.js +26 -35
  75. package/utils/collide/boundingRectToCircle.cjs +34 -0
  76. package/utils/collide/boundingRectToCircle.d.ts +1 -1
  77. package/utils/collide/boundingRectToCircle.js +7 -10
  78. package/utils/collide/collideCircleCircle.cjs +34 -0
  79. package/utils/collide/collideCircleCircle.js +8 -7
  80. package/utils/collide/collideCircleRect.cjs +46 -0
  81. package/utils/collide/collideCircleRect.js +10 -12
  82. package/utils/collide/collidePointCircle.cjs +31 -0
  83. package/utils/collide/collidePointCircle.js +8 -5
  84. package/utils/collide/collidePointRect.cjs +28 -0
  85. package/utils/collide/collidePointRect.js +5 -2
  86. package/utils/collide/collideRectRect.cjs +28 -0
  87. package/utils/collide/collideRectRect.js +5 -2
  88. package/utils/collide/index.cjs +39 -0
  89. package/utils/collide/index.js +14 -6
  90. package/utils/css/classes.cjs +47 -0
  91. package/utils/css/classes.d.ts +10 -9
  92. package/utils/css/classes.js +18 -14
  93. package/utils/css/index.cjs +38 -0
  94. package/utils/css/index.d.ts +2 -2
  95. package/utils/css/index.js +13 -6
  96. package/utils/css/matrix.cjs +30 -0
  97. package/utils/css/matrix.js +7 -5
  98. package/utils/css/styles.cjs +44 -0
  99. package/utils/css/styles.d.ts +6 -18
  100. package/utils/css/styles.js +12 -12
  101. package/utils/css/transition.cjs +120 -0
  102. package/utils/css/transition.d.ts +15 -9
  103. package/utils/css/transition.js +70 -119
  104. package/utils/debounce.cjs +34 -0
  105. package/utils/debounce.js +7 -9
  106. package/utils/focusTrap.cjs +82 -0
  107. package/utils/focusTrap.js +30 -21
  108. package/utils/history.cjs +89 -0
  109. package/utils/history.js +28 -57
  110. package/utils/index.cjs +54 -0
  111. package/utils/index.d.ts +4 -1
  112. package/utils/index.js +32 -11
  113. package/utils/keyCodes.cjs +35 -0
  114. package/utils/keyCodes.js +5 -2
  115. package/utils/math/clamp.cjs +28 -0
  116. package/utils/math/clamp.js +5 -2
  117. package/utils/math/clamp01.cjs +32 -0
  118. package/utils/math/clamp01.js +6 -3
  119. package/utils/math/damp.cjs +28 -0
  120. package/utils/math/damp.js +5 -4
  121. package/utils/math/ease.cjs +88 -0
  122. package/utils/math/ease.js +50 -25
  123. package/utils/math/index.cjs +43 -0
  124. package/utils/math/index.js +17 -8
  125. package/utils/math/inertiaFinalValue.cjs +37 -0
  126. package/utils/math/inertiaFinalValue.js +8 -10
  127. package/utils/math/lerp.cjs +28 -0
  128. package/utils/math/lerp.js +5 -2
  129. package/utils/math/map.cjs +28 -0
  130. package/utils/math/map.js +5 -2
  131. package/utils/math/round.cjs +28 -0
  132. package/utils/math/round.js +5 -3
  133. package/utils/memoize.cjs +43 -0
  134. package/utils/memoize.js +10 -19
  135. package/utils/nextFrame.cjs +38 -0
  136. package/utils/nextFrame.d.ts +4 -3
  137. package/utils/nextFrame.js +13 -10
  138. package/utils/nextMicrotask.cjs +28 -0
  139. package/utils/nextMicrotask.d.ts +8 -0
  140. package/utils/nextMicrotask.js +6 -0
  141. package/utils/nextTick.cjs +30 -0
  142. package/utils/nextTick.d.ts +8 -0
  143. package/utils/nextTick.js +8 -0
  144. package/utils/object/autoBind.cjs +49 -0
  145. package/utils/object/autoBind.js +12 -22
  146. package/utils/object/getAllProperties.cjs +37 -0
  147. package/utils/object/getAllProperties.js +10 -13
  148. package/utils/object/index.cjs +33 -0
  149. package/utils/object/index.js +8 -3
  150. package/utils/object/isObject.cjs +28 -0
  151. package/utils/object/isObject.js +6 -3
  152. package/utils/scrollTo.cjs +80 -0
  153. package/utils/scrollTo.js +20 -40
  154. package/utils/string/index.cjs +47 -0
  155. package/utils/string/index.js +22 -10
  156. package/utils/string/withLeadingCharacters.cjs +32 -0
  157. package/utils/string/withLeadingCharacters.js +7 -4
  158. package/utils/string/withLeadingSlash.cjs +32 -0
  159. package/utils/string/withLeadingSlash.js +7 -4
  160. package/utils/string/withTrailingCharacters.cjs +32 -0
  161. package/utils/string/withTrailingCharacters.js +7 -4
  162. package/utils/string/withTrailingSlash.cjs +32 -0
  163. package/utils/string/withTrailingSlash.js +7 -4
  164. package/utils/string/withoutLeadingCharacters.cjs +28 -0
  165. package/utils/string/withoutLeadingCharacters.js +6 -3
  166. package/utils/string/withoutLeadingCharactersRecursive.cjs +37 -0
  167. package/utils/string/withoutLeadingCharactersRecursive.js +8 -7
  168. package/utils/string/withoutLeadingSlash.cjs +32 -0
  169. package/utils/string/withoutLeadingSlash.js +7 -4
  170. package/utils/string/withoutTrailingCharacters.cjs +28 -0
  171. package/utils/string/withoutTrailingCharacters.js +6 -3
  172. package/utils/string/withoutTrailingCharactersRecursive.cjs +37 -0
  173. package/utils/string/withoutTrailingCharactersRecursive.js +8 -7
  174. package/utils/string/withoutTrailingSlash.cjs +32 -0
  175. package/utils/string/withoutTrailingSlash.js +7 -4
  176. package/utils/throttle.cjs +36 -0
  177. package/utils/throttle.js +9 -9
  178. package/index.modern.js +0 -1
  179. package/index.umd.js +0 -1
@@ -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;
@@ -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,213 @@
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
+ return this;
84
+ }
85
+ init() {
86
+ const options = { passive: true };
87
+ this.props.target.addEventListener("mousedown", this, options);
88
+ this.props.target.addEventListener("touchstart", this, options);
89
+ this.props.target.addEventListener("dragstart", this, { capture: true });
90
+ this.props.target.addEventListener("click", this, { capture: true });
91
+ const pointer = (0, import_pointer.default)();
92
+ pointer.add(this.id, this.pointerHandler);
93
+ return this;
94
+ }
95
+ kill() {
96
+ this.props.target.removeEventListener("mousedown", this);
97
+ this.props.target.removeEventListener("touchstart", this);
98
+ this.props.target.removeEventListener("dragstart", this);
99
+ this.props.target.removeEventListener("click", this);
100
+ const pointer = (0, import_pointer.default)();
101
+ pointer.remove(this.id);
102
+ return this;
103
+ }
104
+ handleEvent(event) {
105
+ if (event.type === "dragstart") {
106
+ event.preventDefault();
107
+ return;
108
+ }
109
+ if (event.type === "click") {
110
+ if (this.__shouldPreventClick) {
111
+ event.stopImmediatePropagation();
112
+ event.stopPropagation();
113
+ event.preventDefault();
114
+ }
115
+ return;
116
+ }
117
+ const x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
118
+ const y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
119
+ this.start(x, y);
120
+ }
121
+ start(x, y) {
122
+ this.props.x = x;
123
+ this.props.y = y;
124
+ this.props.origin.x = x;
125
+ this.props.origin.y = y;
126
+ this.props.delta.x = 0;
127
+ this.props.delta.y = 0;
128
+ this.props.distance.x = 0;
129
+ this.props.distance.y = 0;
130
+ this.props.final.x = x;
131
+ this.props.final.y = y;
132
+ this.props.mode = MODES.START;
133
+ this.props.isGrabbing = true;
134
+ this.trigger(this.props);
135
+ }
136
+ drop() {
137
+ this.props.isGrabbing = false;
138
+ this.props.mode = MODES.DROP;
139
+ this.props.hasInertia = true;
140
+ this.props.final.x = (0, import_inertiaFinalValue.default)(this.props.x, this.props.delta.x, this.options.dampFactor);
141
+ this.props.final.y = (0, import_inertiaFinalValue.default)(this.props.y, this.props.delta.y, this.options.dampFactor);
142
+ this.trigger(this.props);
143
+ setTimeout(() => {
144
+ const raf = (0, import_raf.default)();
145
+ raf.remove(this.id);
146
+ raf.add(this.id, this.rafHandler);
147
+ }, 0);
148
+ }
149
+ stop() {
150
+ (0, import_raf.default)().remove(this.id);
151
+ this.props.hasInertia = false;
152
+ this.props.mode = MODES.STOP;
153
+ this.trigger(this.props);
154
+ }
155
+ rafHandler() {
156
+ if (!this.props.isGrabbing) {
157
+ this.props.x += this.props.delta.x;
158
+ this.props.y += this.props.delta.y;
159
+ this.props.distance.x = this.props.x - this.props.origin.x;
160
+ this.props.distance.y = this.props.y - this.props.origin.y;
161
+ this.props.delta.x *= this.options.dampFactor;
162
+ this.props.delta.y *= this.options.dampFactor;
163
+ if (this.props.mode !== MODES.INERTIA) {
164
+ this.props.mode = MODES.INERTIA;
165
+ }
166
+ this.trigger(this.props);
167
+ if (Math.abs(this.props.delta.x) < 0.1 && Math.abs(this.props.delta.y) < 0.1) {
168
+ this.stop();
169
+ }
170
+ }
171
+ }
172
+ pointerHandler(props) {
173
+ if (this.props.isGrabbing) {
174
+ this.props.x = props.x;
175
+ this.props.y = props.y;
176
+ this.props.delta.x = props.delta.x;
177
+ this.props.delta.y = props.delta.y;
178
+ this.props.final.x = props.x;
179
+ this.props.final.y = props.y;
180
+ this.props.distance.x = this.props.x - this.props.origin.x;
181
+ this.props.distance.y = this.props.y - this.props.origin.y;
182
+ if (this.props.mode !== MODES.DRAG) {
183
+ this.props.mode = MODES.DRAG;
184
+ }
185
+ this.trigger(this.props);
186
+ if (!props.isDown) {
187
+ this.drop();
188
+ }
189
+ }
190
+ }
191
+ updateProps() {
192
+ return this.props;
193
+ }
194
+ get __shouldPreventClick() {
195
+ return Math.abs(this.props.distance.x) > this.options.dragTreshold || Math.abs(this.props.distance.y) > this.options.dragTreshold;
196
+ }
197
+ };
198
+ var instances = /* @__PURE__ */ new WeakMap();
199
+ function useDrag(target, options) {
200
+ let drag = instances.get(target);
201
+ if (!drag) {
202
+ const instance = new Drag(target, options);
203
+ drag = {
204
+ add: instance.add.bind(instance),
205
+ remove: instance.remove.bind(instance),
206
+ has: instance.has.bind(instance),
207
+ props: instance.updateProps.bind(instance)
208
+ };
209
+ instances.set(target, drag);
210
+ }
211
+ return drag;
212
+ }
213
+ if (module.exports.default) module.exports = module.exports.default;
@@ -74,4 +74,5 @@ export type DragServiceProps = {
74
74
  };
75
75
  export type DragServiceOptions = {
76
76
  dampFactor?: number | undefined;
77
+ dragTreshold?: number | undefined;
77
78
  };
package/services/drag.js CHANGED
@@ -1,32 +1,28 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
-
3
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
-
5
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
-
7
- import Service from './Service.js';
8
- import usePointer from './pointer.js';
9
- import useRaf from './raf.js';
10
- import inertiaFinalValue from '../utils/math/inertiaFinalValue.js';
11
- var MODES = {
12
- START: 'start',
13
- DRAG: 'drag',
14
- DROP: 'drop',
15
- INERTIA: 'inertia',
16
- STOP: 'stop'
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;
17
6
  };
18
- var instanceIds = 0;
19
-
7
+ import Service from "./Service.js";
8
+ import usePointer from "./pointer.js";
9
+ import useRaf from "./raf.js";
10
+ import inertiaFinalValue from "../utils/math/inertiaFinalValue.js";
11
+ const MODES = {
12
+ START: "start",
13
+ DRAG: "drag",
14
+ DROP: "drop",
15
+ INERTIA: "inertia",
16
+ STOP: "stop"
17
+ };
18
+ let id = 0;
20
19
  class Drag extends Service {
21
- constructor(target) {
22
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
20
+ constructor(target, options = {}) {
23
21
  super();
24
-
25
- _defineProperty(this, "id", void 0);
26
-
27
- _defineProperty(this, "props", {
22
+ __publicField(this, "id");
23
+ __publicField(this, "props", {
28
24
  target: null,
29
- mode: undefined,
25
+ mode: void 0,
30
26
  isGrabbing: false,
31
27
  hasInertia: false,
32
28
  x: 0,
@@ -48,45 +44,54 @@ class Drag extends Service {
48
44
  y: 0
49
45
  }
50
46
  });
51
-
52
- _defineProperty(this, "options", {
53
- dampFactor: 0.85
47
+ __publicField(this, "options", {
48
+ dampFactor: 0.85,
49
+ dragTreshold: 10
54
50
  });
55
-
56
- instanceIds += 1;
57
- this.id = "drag-".concat(instanceIds);
51
+ id += 1;
52
+ this.id = `drag-${id}`;
58
53
  this.props.target = target;
59
- this.options = _objectSpread(_objectSpread({}, this.options), options);
54
+ this.options = { ...this.options, ...options };
60
55
  this.pointerHandler = this.pointerHandler.bind(this);
61
56
  this.rafHandler = this.rafHandler.bind(this);
62
57
  return this;
63
58
  }
64
-
65
59
  init() {
66
- var options = {
67
- passive: true
68
- };
69
- this.props.target.addEventListener('mousedown', this, options);
70
- this.props.target.addEventListener('touchstart', this, options);
71
- var pointer = usePointer();
60
+ const options = { passive: true };
61
+ this.props.target.addEventListener("mousedown", this, options);
62
+ this.props.target.addEventListener("touchstart", this, options);
63
+ this.props.target.addEventListener("dragstart", this, { capture: true });
64
+ this.props.target.addEventListener("click", this, { capture: true });
65
+ const pointer = usePointer();
72
66
  pointer.add(this.id, this.pointerHandler);
73
67
  return this;
74
68
  }
75
-
76
69
  kill() {
77
- this.props.target.removeEventListener('mousemove', this);
78
- this.props.target.removeEventListener('touchmove', this);
79
- var pointer = usePointer();
70
+ this.props.target.removeEventListener("mousedown", this);
71
+ this.props.target.removeEventListener("touchstart", this);
72
+ this.props.target.removeEventListener("dragstart", this);
73
+ this.props.target.removeEventListener("click", this);
74
+ const pointer = usePointer();
80
75
  pointer.remove(this.id);
81
76
  return this;
82
77
  }
83
-
84
78
  handleEvent(event) {
85
- var x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
86
- var y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
79
+ if (event.type === "dragstart") {
80
+ event.preventDefault();
81
+ return;
82
+ }
83
+ if (event.type === "click") {
84
+ if (this.__shouldPreventClick) {
85
+ event.stopImmediatePropagation();
86
+ event.stopPropagation();
87
+ event.preventDefault();
88
+ }
89
+ return;
90
+ }
91
+ const x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
92
+ const y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
87
93
  this.start(x, y);
88
94
  }
89
-
90
95
  start(x, y) {
91
96
  this.props.x = x;
92
97
  this.props.y = y;
@@ -102,7 +107,6 @@ class Drag extends Service {
102
107
  this.props.isGrabbing = true;
103
108
  this.trigger(this.props);
104
109
  }
105
-
106
110
  drop() {
107
111
  this.props.isGrabbing = false;
108
112
  this.props.mode = MODES.DROP;
@@ -111,19 +115,17 @@ class Drag extends Service {
111
115
  this.props.final.y = inertiaFinalValue(this.props.y, this.props.delta.y, this.options.dampFactor);
112
116
  this.trigger(this.props);
113
117
  setTimeout(() => {
114
- var raf = useRaf();
118
+ const raf = useRaf();
115
119
  raf.remove(this.id);
116
120
  raf.add(this.id, this.rafHandler);
117
121
  }, 0);
118
122
  }
119
-
120
123
  stop() {
121
124
  useRaf().remove(this.id);
122
125
  this.props.hasInertia = false;
123
126
  this.props.mode = MODES.STOP;
124
127
  this.trigger(this.props);
125
128
  }
126
-
127
129
  rafHandler() {
128
130
  if (!this.props.isGrabbing) {
129
131
  this.props.x += this.props.delta.x;
@@ -132,19 +134,15 @@ class Drag extends Service {
132
134
  this.props.distance.y = this.props.y - this.props.origin.y;
133
135
  this.props.delta.x *= this.options.dampFactor;
134
136
  this.props.delta.y *= this.options.dampFactor;
135
-
136
137
  if (this.props.mode !== MODES.INERTIA) {
137
138
  this.props.mode = MODES.INERTIA;
138
139
  }
139
-
140
140
  this.trigger(this.props);
141
-
142
141
  if (Math.abs(this.props.delta.x) < 0.1 && Math.abs(this.props.delta.y) < 0.1) {
143
142
  this.stop();
144
143
  }
145
144
  }
146
145
  }
147
-
148
146
  pointerHandler(props) {
149
147
  if (this.props.isGrabbing) {
150
148
  this.props.x = props.x;
@@ -155,31 +153,27 @@ class Drag extends Service {
155
153
  this.props.final.y = props.y;
156
154
  this.props.distance.x = this.props.x - this.props.origin.x;
157
155
  this.props.distance.y = this.props.y - this.props.origin.y;
158
-
159
156
  if (this.props.mode !== MODES.DRAG) {
160
157
  this.props.mode = MODES.DRAG;
161
158
  }
162
-
163
159
  this.trigger(this.props);
164
-
165
160
  if (!props.isDown) {
166
161
  this.drop();
167
162
  }
168
163
  }
169
164
  }
170
-
171
165
  updateProps() {
172
166
  return this.props;
173
167
  }
174
-
168
+ get __shouldPreventClick() {
169
+ return Math.abs(this.props.distance.x) > this.options.dragTreshold || Math.abs(this.props.distance.y) > this.options.dragTreshold;
170
+ }
175
171
  }
176
-
177
- var instances = new WeakMap();
178
- export default function useDrag(target, options) {
179
- var drag = instances.get(target);
180
-
172
+ const instances = /* @__PURE__ */ new WeakMap();
173
+ function useDrag(target, options) {
174
+ let drag = instances.get(target);
181
175
  if (!drag) {
182
- var instance = new Drag(target, options);
176
+ const instance = new Drag(target, options);
183
177
  drag = {
184
178
  add: instance.add.bind(instance),
185
179
  remove: instance.remove.bind(instance),
@@ -188,6 +182,8 @@ export default function useDrag(target, options) {
188
182
  };
189
183
  instances.set(target, drag);
190
184
  }
191
-
192
185
  return drag;
193
- }
186
+ }
187
+ export {
188
+ useDrag as default
189
+ };
@@ -0,0 +1,41 @@
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 __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ 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));
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+
22
+ // packages/js-toolkit/services/index.js
23
+ var services_exports = {};
24
+ __export(services_exports, {
25
+ useDrag: () => import_drag.default,
26
+ useKey: () => import_key.default,
27
+ useLoad: () => import_load.default,
28
+ usePointer: () => import_pointer.default,
29
+ useRaf: () => import_raf.default,
30
+ useResize: () => import_resize.default,
31
+ useScroll: () => import_scroll.default
32
+ });
33
+ module.exports = __toCommonJS(services_exports);
34
+ var import_drag = __toESM(require("./drag.cjs"), 1);
35
+ var import_key = __toESM(require("./key.cjs"), 1);
36
+ var import_load = __toESM(require("./load.cjs"), 1);
37
+ var import_pointer = __toESM(require("./pointer.cjs"), 1);
38
+ var import_raf = __toESM(require("./raf.cjs"), 1);
39
+ var import_resize = __toESM(require("./resize.cjs"), 1);
40
+ var import_scroll = __toESM(require("./scroll.cjs"), 1);
41
+ if (module.exports.default) module.exports = module.exports.default;
package/services/index.js CHANGED
@@ -1,7 +1,16 @@
1
- export { default as useDrag } from './drag.js';
2
- export { default as useKey } from './key.js';
3
- export { default as useLoad } from './load.js';
4
- export { default as usePointer } from './pointer.js';
5
- export { default as useRaf } from './raf.js';
6
- export { default as useResize } from './resize.js';
7
- export { default as useScroll } from './scroll.js';
1
+ import { default as default2 } from "./drag.js";
2
+ import { default as default3 } from "./key.js";
3
+ import { default as default4 } from "./load.js";
4
+ import { default as default5 } from "./pointer.js";
5
+ import { default as default6 } from "./raf.js";
6
+ import { default as default7 } from "./resize.js";
7
+ import { default as default8 } from "./scroll.js";
8
+ export {
9
+ default2 as useDrag,
10
+ default3 as useKey,
11
+ default4 as useLoad,
12
+ default5 as usePointer,
13
+ default6 as useRaf,
14
+ default7 as useResize,
15
+ default8 as useScroll
16
+ };