@studiometa/js-toolkit 2.0.0-beta.8 → 2.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Base/index.cjs +285 -0
- package/Base/index.d.ts +11 -2
- package/Base/index.js +114 -197
- package/Base/managers/ChildrenManager.cjs +146 -0
- package/Base/managers/ChildrenManager.d.ts +9 -1
- package/Base/managers/ChildrenManager.js +60 -59
- package/Base/managers/EventsManager.cjs +183 -0
- package/Base/managers/EventsManager.js +72 -112
- package/Base/managers/OptionsManager.cjs +174 -0
- package/Base/managers/OptionsManager.d.ts +9 -5
- package/Base/managers/OptionsManager.js +60 -90
- package/Base/managers/RefsManager.cjs +99 -0
- package/Base/managers/RefsManager.js +30 -35
- package/Base/managers/ServicesManager.cjs +131 -0
- package/Base/managers/ServicesManager.js +39 -67
- package/Base/utils.cjs +41 -0
- package/Base/utils.js +12 -11
- package/README.md +13 -70
- package/decorators/index.cjs +45 -0
- package/decorators/index.d.ts +9 -9
- package/decorators/index.js +20 -9
- package/decorators/withBreakpointManager.cjs +85 -0
- package/decorators/withBreakpointManager.js +18 -39
- package/decorators/withBreakpointObserver.cjs +115 -0
- package/decorators/withBreakpointObserver.js +44 -86
- package/decorators/withDrag.cjs +53 -0
- package/decorators/withDrag.js +24 -26
- package/decorators/withExtraConfig.cjs +43 -0
- package/decorators/withExtraConfig.js +17 -12
- package/decorators/withFreezedOptions.cjs +37 -0
- package/decorators/withFreezedOptions.d.ts +14 -0
- package/decorators/withFreezedOptions.js +15 -0
- package/decorators/withIntersectionObserver.cjs +62 -0
- package/decorators/withIntersectionObserver.js +29 -32
- package/decorators/withMountWhenInView.cjs +69 -0
- package/decorators/withMountWhenInView.js +31 -52
- package/decorators/withScrolledInView.cjs +114 -0
- package/decorators/withScrolledInView.d.ts +2 -1
- package/decorators/withScrolledInView.js +35 -39
- package/decorators/withVue2.cjs +61 -0
- package/decorators/withVue2.js +27 -32
- package/helpers/createApp.cjs +52 -0
- package/helpers/createApp.d.ts +2 -2
- package/helpers/createApp.js +12 -12
- package/helpers/importOnInteraction.cjs +52 -0
- package/helpers/importOnInteraction.js +17 -18
- package/helpers/importWhenIdle.cjs +49 -0
- package/helpers/importWhenIdle.js +14 -17
- package/helpers/importWhenVisible.cjs +50 -0
- package/helpers/importWhenVisible.js +16 -18
- package/helpers/index.cjs +35 -0
- package/helpers/index.d.ts +4 -5
- package/helpers/index.js +10 -5
- package/helpers/utils.cjs +35 -0
- package/helpers/utils.js +7 -6
- package/index.cjs +33 -0
- package/index.d.ts +4 -21
- package/index.js +7 -5
- package/package.json +14 -4
- package/services/Service.cjs +76 -0
- package/services/Service.js +19 -32
- package/services/drag.cjs +213 -0
- package/services/drag.d.ts +1 -0
- package/services/drag.js +64 -68
- package/services/index.cjs +41 -0
- package/services/index.d.ts +7 -8
- package/services/index.js +16 -8
- package/services/key.cjs +104 -0
- package/services/key.js +28 -36
- package/services/load.cjs +72 -0
- package/services/load.js +17 -19
- package/services/pointer.cjs +162 -0
- package/services/pointer.js +43 -60
- package/services/raf.cjs +83 -0
- package/services/raf.js +20 -28
- package/services/resize.cjs +108 -0
- package/services/resize.js +33 -42
- package/services/scroll.cjs +131 -0
- package/services/scroll.js +26 -35
- package/utils/collide/boundingRectToCircle.cjs +34 -0
- package/utils/collide/boundingRectToCircle.d.ts +1 -1
- package/utils/collide/boundingRectToCircle.js +7 -10
- package/utils/collide/collideCircleCircle.cjs +34 -0
- package/utils/collide/collideCircleCircle.js +8 -7
- package/utils/collide/collideCircleRect.cjs +46 -0
- package/utils/collide/collideCircleRect.js +10 -12
- package/utils/collide/collidePointCircle.cjs +31 -0
- package/utils/collide/collidePointCircle.js +8 -5
- package/utils/collide/collidePointRect.cjs +28 -0
- package/utils/collide/collidePointRect.js +5 -2
- package/utils/collide/collideRectRect.cjs +28 -0
- package/utils/collide/collideRectRect.js +5 -2
- package/utils/collide/index.cjs +39 -0
- package/utils/collide/index.d.ts +6 -7
- package/utils/collide/index.js +14 -7
- package/utils/css/classes.cjs +47 -0
- package/utils/css/classes.d.ts +10 -9
- package/utils/css/classes.js +18 -14
- package/utils/css/index.cjs +38 -0
- package/utils/css/index.d.ts +4 -5
- package/utils/css/index.js +13 -5
- package/utils/css/matrix.cjs +30 -0
- package/utils/css/matrix.js +7 -5
- package/utils/css/styles.cjs +44 -0
- package/utils/css/styles.d.ts +6 -18
- package/utils/css/styles.js +12 -12
- package/utils/css/transition.cjs +120 -0
- package/utils/css/transition.d.ts +15 -9
- package/utils/css/transition.js +70 -119
- package/utils/debounce.cjs +34 -0
- package/utils/debounce.js +7 -9
- package/utils/focusTrap.cjs +82 -0
- package/utils/focusTrap.js +30 -21
- package/utils/history.cjs +89 -0
- package/utils/history.js +28 -57
- package/utils/index.cjs +54 -0
- package/utils/index.d.ts +14 -35
- package/utils/index.js +32 -17
- package/utils/keyCodes.cjs +35 -0
- package/utils/keyCodes.js +5 -2
- package/utils/math/clamp.cjs +28 -0
- package/utils/math/clamp.js +5 -2
- package/utils/math/clamp01.cjs +32 -0
- package/utils/math/clamp01.js +6 -3
- package/utils/math/damp.cjs +28 -0
- package/utils/math/damp.js +5 -4
- package/utils/math/ease.cjs +88 -0
- package/utils/math/ease.d.ts +78 -0
- package/utils/math/ease.js +66 -0
- package/utils/math/index.cjs +43 -0
- package/utils/math/index.d.ts +8 -8
- package/utils/math/index.js +17 -8
- package/utils/math/inertiaFinalValue.cjs +37 -0
- package/utils/math/inertiaFinalValue.js +8 -10
- package/utils/math/lerp.cjs +28 -0
- package/utils/math/lerp.js +5 -2
- package/utils/math/map.cjs +28 -0
- package/utils/math/map.js +5 -2
- package/utils/math/round.cjs +28 -0
- package/utils/math/round.js +5 -3
- package/utils/memoize.cjs +43 -0
- package/utils/memoize.js +10 -19
- package/utils/nextFrame.cjs +38 -0
- package/utils/nextFrame.d.ts +4 -3
- package/utils/nextFrame.js +13 -10
- package/utils/nextMicrotask.cjs +28 -0
- package/utils/nextMicrotask.d.ts +8 -0
- package/utils/nextMicrotask.js +6 -0
- package/utils/nextTick.cjs +30 -0
- package/utils/nextTick.d.ts +8 -0
- package/utils/nextTick.js +8 -0
- package/utils/object/autoBind.cjs +49 -0
- package/utils/object/autoBind.js +12 -22
- package/utils/object/getAllProperties.cjs +37 -0
- package/utils/object/getAllProperties.js +10 -13
- package/utils/object/index.cjs +33 -0
- package/utils/object/index.d.ts +3 -4
- package/utils/object/index.js +8 -4
- package/utils/object/isObject.cjs +28 -0
- package/utils/object/isObject.js +6 -3
- package/utils/scrollTo.cjs +80 -0
- package/utils/scrollTo.js +20 -40
- package/utils/string/index.cjs +47 -0
- package/utils/string/index.d.ts +10 -11
- package/utils/string/index.js +22 -11
- package/utils/string/withLeadingCharacters.cjs +32 -0
- package/utils/string/withLeadingCharacters.js +7 -4
- package/utils/string/withLeadingSlash.cjs +32 -0
- package/utils/string/withLeadingSlash.js +7 -4
- package/utils/string/withTrailingCharacters.cjs +32 -0
- package/utils/string/withTrailingCharacters.js +7 -4
- package/utils/string/withTrailingSlash.cjs +32 -0
- package/utils/string/withTrailingSlash.js +7 -4
- package/utils/string/withoutLeadingCharacters.cjs +28 -0
- package/utils/string/withoutLeadingCharacters.js +6 -3
- package/utils/string/withoutLeadingCharactersRecursive.cjs +37 -0
- package/utils/string/withoutLeadingCharactersRecursive.js +8 -7
- package/utils/string/withoutLeadingSlash.cjs +32 -0
- package/utils/string/withoutLeadingSlash.js +7 -4
- package/utils/string/withoutTrailingCharacters.cjs +28 -0
- package/utils/string/withoutTrailingCharacters.js +6 -3
- package/utils/string/withoutTrailingCharactersRecursive.cjs +37 -0
- package/utils/string/withoutTrailingCharactersRecursive.js +8 -7
- package/utils/string/withoutTrailingSlash.cjs +32 -0
- package/utils/string/withoutTrailingSlash.js +7 -4
- package/utils/throttle.cjs +36 -0
- package/utils/throttle.js +9 -9
- package/index.modern.js +0 -1
- package/index.umd.js +0 -1
package/index.d.ts
CHANGED
|
@@ -1,21 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import { importWhenVisible } from "./helpers/index.js";
|
|
6
|
-
import { useDrag } from "./services/index.js";
|
|
7
|
-
import { useKey } from "./services/index.js";
|
|
8
|
-
import { useLoad } from "./services/index.js";
|
|
9
|
-
import { usePointer } from "./services/index.js";
|
|
10
|
-
import { useRaf } from "./services/index.js";
|
|
11
|
-
import { useResize } from "./services/index.js";
|
|
12
|
-
import { useScroll } from "./services/index.js";
|
|
13
|
-
import { withBreakpointManager } from "./decorators/index.js";
|
|
14
|
-
import { withBreakpointObserver } from "./decorators/index.js";
|
|
15
|
-
import { withDrag } from "./decorators/index.js";
|
|
16
|
-
import { withExtraConfig } from "./decorators/index.js";
|
|
17
|
-
import { withIntersectionObserver } from "./decorators/index.js";
|
|
18
|
-
import { withMountWhenInView } from "./decorators/index.js";
|
|
19
|
-
import { withScrolledInView } from "./decorators/index.js";
|
|
20
|
-
import { withVue2 } from "./decorators/index.js";
|
|
21
|
-
export { Base, createApp, importOnInteraction, importWhenIdle, importWhenVisible, useDrag, useKey, useLoad, usePointer, useRaf, useResize, useScroll, withBreakpointManager, withBreakpointObserver, withDrag, withExtraConfig, withIntersectionObserver, withMountWhenInView, withScrolledInView, withVue2 };
|
|
1
|
+
export { default as Base } from "./Base/index.js";
|
|
2
|
+
export * from "./decorators/index.js";
|
|
3
|
+
export * from "./helpers/index.js";
|
|
4
|
+
export * from "./services/index.js";
|
package/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export {
|
|
1
|
+
import { default as default2 } from "./Base/index.js";
|
|
2
|
+
export * from "./decorators/index.js";
|
|
3
|
+
export * from "./helpers/index.js";
|
|
4
|
+
export * from "./services/index.js";
|
|
5
|
+
export {
|
|
6
|
+
default2 as Base
|
|
7
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@studiometa/js-toolkit",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-rc.2",
|
|
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;
|
package/services/Service.js
CHANGED
|
@@ -1,68 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
-
|
|
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(
|
|
15
|
+
throw new Error("The `updateProps` method must be implemented.");
|
|
17
16
|
}
|
|
18
|
-
|
|
19
17
|
init() {
|
|
20
|
-
throw new Error(
|
|
18
|
+
throw new Error("The `init` method must be implemented.");
|
|
21
19
|
}
|
|
22
|
-
|
|
23
20
|
kill() {
|
|
24
|
-
throw new Error(
|
|
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(
|
|
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;
|