@studiometa/js-toolkit 2.2.1 → 2.3.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.
- package/Base/index.cjs +60 -69
- package/Base/index.d.ts +0 -8
- package/Base/index.js +67 -82
- package/Base/managers/AbstractManager.cjs +5 -10
- package/Base/managers/AbstractManager.js +5 -11
- package/Base/managers/ChildrenManager.cjs +2 -11
- package/Base/managers/ChildrenManager.js +2 -12
- package/Base/managers/EventsManager.cjs +61 -67
- package/Base/managers/EventsManager.js +61 -68
- package/Base/managers/OptionsManager.cjs +26 -27
- package/Base/managers/OptionsManager.js +26 -28
- package/Base/managers/RefsManager.cjs +9 -7
- package/Base/managers/RefsManager.js +9 -7
- package/Base/managers/ResponsiveOptionsManager.cjs +4 -1
- package/Base/managers/ResponsiveOptionsManager.js +4 -1
- package/Base/managers/ServicesManager.cjs +10 -16
- package/Base/managers/ServicesManager.js +10 -17
- package/Base/utils.cjs +22 -4
- package/Base/utils.d.ts +25 -8
- package/Base/utils.js +22 -4
- package/decorators/withBreakpointManager.cjs +15 -5
- package/decorators/withBreakpointManager.js +15 -5
- package/decorators/withBreakpointObserver.cjs +19 -18
- package/decorators/withBreakpointObserver.js +19 -19
- package/decorators/withDrag.cjs +6 -11
- package/decorators/withDrag.js +6 -12
- package/decorators/withExtraConfig.cjs +3 -8
- package/decorators/withExtraConfig.js +3 -9
- package/decorators/withFreezedOptions.cjs +1 -9
- package/decorators/withFreezedOptions.js +1 -10
- package/decorators/withIntersectionObserver.cjs +11 -16
- package/decorators/withIntersectionObserver.js +11 -17
- package/decorators/withMountWhenInView.cjs +12 -17
- package/decorators/withMountWhenInView.js +12 -18
- package/decorators/withScrolledInView.cjs +30 -35
- package/decorators/withScrolledInView.js +30 -36
- package/decorators/withVue2.cjs +20 -18
- package/decorators/withVue2.js +20 -19
- package/helpers/getInstanceFromElement.cjs +1 -2
- package/helpers/getInstanceFromElement.js +1 -2
- package/helpers/importOnInteraction.cjs +3 -2
- package/helpers/importOnInteraction.js +3 -2
- package/helpers/importWhenIdle.cjs +2 -3
- package/helpers/importWhenIdle.js +2 -3
- package/helpers/importWhenVisible.cjs +3 -2
- package/helpers/importWhenVisible.js +3 -2
- package/helpers/utils.cjs +3 -2
- package/helpers/utils.js +3 -2
- package/package.json +4 -1
- package/services/drag.cjs +126 -148
- package/services/drag.d.ts +10 -10
- package/services/drag.js +125 -148
- package/services/index.cjs +3 -1
- package/services/index.d.ts +1 -0
- package/services/index.js +3 -1
- package/services/key.cjs +48 -63
- package/services/key.d.ts +0 -8
- package/services/key.js +47 -63
- package/services/load.cjs +24 -40
- package/services/load.d.ts +0 -8
- package/services/load.js +23 -37
- package/services/pointer.cjs +72 -103
- package/services/pointer.d.ts +1 -9
- package/services/pointer.js +71 -100
- package/services/raf.cjs +38 -58
- package/services/raf.d.ts +0 -8
- package/services/raf.js +37 -55
- package/services/resize.cjs +59 -68
- package/services/resize.d.ts +0 -7
- package/services/resize.js +58 -68
- package/services/scroll.cjs +49 -71
- package/services/scroll.js +48 -71
- package/services/service.cjs +68 -0
- package/services/service.d.ts +18 -0
- package/services/service.js +46 -0
- package/utils/css/animate.cjs +33 -102
- package/utils/css/animate.d.ts +4 -13
- package/utils/css/animate.js +31 -100
- package/utils/css/classes.cjs +2 -1
- package/utils/css/classes.js +2 -1
- package/utils/css/matrix.cjs +1 -2
- package/utils/css/matrix.js +1 -2
- package/utils/css/styles.cjs +2 -5
- package/utils/css/styles.js +1 -1
- package/utils/css/transform.cjs +14 -18
- package/utils/css/transform.js +2 -3
- package/utils/css/transition.cjs +7 -10
- package/utils/css/transition.js +7 -11
- package/utils/focusTrap.cjs +2 -1
- package/utils/focusTrap.js +2 -1
- package/utils/{isFunction.cjs → has.cjs} +7 -7
- package/utils/has.d.ts +6 -0
- package/utils/has.js +6 -0
- package/utils/history.cjs +5 -7
- package/utils/history.js +4 -3
- package/utils/index.cjs +6 -5
- package/utils/index.d.ts +4 -2
- package/utils/index.js +6 -5
- package/utils/{object/isObject.cjs → is.cjs} +20 -8
- package/utils/is.d.ts +8 -0
- package/utils/is.js +18 -0
- package/utils/math/inertiaFinalValue.cjs +5 -3
- package/utils/math/inertiaFinalValue.js +2 -3
- package/utils/nextFrame.cjs +5 -3
- package/utils/nextFrame.js +5 -3
- package/utils/nextMicrotask.cjs +2 -1
- package/utils/nextMicrotask.js +2 -1
- package/utils/nextTick.cjs +2 -1
- package/utils/nextTick.js +2 -1
- package/utils/{isDefined.cjs → noop.cjs} +10 -7
- package/utils/noop.d.ts +12 -0
- package/utils/noop.js +9 -0
- package/utils/object/autoBind.cjs +3 -2
- package/utils/object/autoBind.js +3 -2
- package/utils/object/getAllProperties.cjs +2 -1
- package/utils/object/getAllProperties.js +2 -1
- package/utils/object/index.cjs +1 -3
- package/utils/object/index.d.ts +0 -1
- package/utils/object/index.js +1 -3
- package/utils/tween.cjs +113 -0
- package/utils/tween.d.ts +25 -0
- package/utils/tween.js +88 -0
- package/services/Service.cjs +0 -76
- package/services/Service.d.ts +0 -76
- package/services/Service.js +0 -55
- package/utils/isDefined.d.ts +0 -7
- package/utils/isDefined.js +0 -6
- package/utils/isFunction.d.ts +0 -7
- package/utils/isFunction.js +0 -6
- package/utils/object/isObject.d.ts +0 -7
- package/utils/object/isObject.js +0 -6
package/decorators/withVue2.js
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
|
-
|
|
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
|
-
};
|
|
1
|
+
import { isDev, isFunction } from "../utils/index.js";
|
|
7
2
|
var withVue2_default = (BaseClass, Vue) => {
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
return class WithVue2 extends BaseClass {
|
|
4
|
+
static config = {
|
|
5
|
+
...BaseClass.config,
|
|
6
|
+
name: `${BaseClass.config.name}WithVue`,
|
|
7
|
+
refs: [...BaseClass.config?.refs ?? [], "vue"]
|
|
8
|
+
};
|
|
9
|
+
$vue;
|
|
10
|
+
static vueConfig;
|
|
10
11
|
constructor(element) {
|
|
11
|
-
var _a2;
|
|
12
12
|
super(element);
|
|
13
|
-
|
|
14
|
-
const vueConfig = (_a2 = this.vueConfig) != null ? _a2 : this.constructor.vueConfig;
|
|
13
|
+
const vueConfig = this.vueConfig ?? this.constructor.vueConfig;
|
|
15
14
|
if (!vueConfig) {
|
|
16
|
-
|
|
15
|
+
if (isDev) {
|
|
16
|
+
throw new Error("[withVue] You must define a `vueConfig` object.");
|
|
17
|
+
}
|
|
18
|
+
return;
|
|
17
19
|
}
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
+
if (!isFunction(vueConfig.render)) {
|
|
21
|
+
if (isDev) {
|
|
22
|
+
throw new Error("[withVue] You must define a `render` function in vueConfig.");
|
|
23
|
+
}
|
|
24
|
+
return;
|
|
20
25
|
}
|
|
21
26
|
this.$vue = new Vue(vueConfig);
|
|
22
27
|
this.$on("mounted", () => {
|
|
@@ -29,11 +34,7 @@ var withVue2_default = (BaseClass, Vue) => {
|
|
|
29
34
|
this.$vue.$destroy();
|
|
30
35
|
});
|
|
31
36
|
}
|
|
32
|
-
}
|
|
33
|
-
...BaseClass.config,
|
|
34
|
-
name: `${BaseClass.config.name}WithVue`,
|
|
35
|
-
refs: [...(_b = (_a = BaseClass.config) == null ? void 0 : _a.refs) != null ? _b : [], "vue"]
|
|
36
|
-
}), __publicField(_c, "vueConfig"), _c;
|
|
37
|
+
};
|
|
37
38
|
};
|
|
38
39
|
export {
|
|
39
40
|
withVue2_default as default
|
|
@@ -23,10 +23,9 @@ __export(getInstanceFromElement_exports, {
|
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(getInstanceFromElement_exports);
|
|
25
25
|
function getInstanceFromElement(element, Constructor) {
|
|
26
|
-
var _a;
|
|
27
26
|
if (!element.__base__) {
|
|
28
27
|
return null;
|
|
29
28
|
}
|
|
30
|
-
return
|
|
29
|
+
return element.__base__.get(Constructor) ?? null;
|
|
31
30
|
}
|
|
32
31
|
if (module.exports.default) module.exports = module.exports.default;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
function getInstanceFromElement(element, Constructor) {
|
|
2
|
-
var _a;
|
|
3
2
|
if (!element.__base__) {
|
|
4
3
|
return null;
|
|
5
4
|
}
|
|
6
|
-
return
|
|
5
|
+
return element.__base__.get(Constructor) ?? null;
|
|
7
6
|
}
|
|
8
7
|
export {
|
|
9
8
|
getInstanceFromElement as default
|
|
@@ -23,8 +23,9 @@ __export(importOnInteraction_exports, {
|
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(importOnInteraction_exports);
|
|
25
25
|
var import_utils = require("./utils.cjs");
|
|
26
|
+
var import_utils2 = require("../utils/index.cjs");
|
|
26
27
|
function importOnInteraction(fn, nameOrSelectorOrElement, events, parent) {
|
|
27
|
-
const normalizedEvents =
|
|
28
|
+
const normalizedEvents = (0, import_utils2.isString)(events) ? [events] : events;
|
|
28
29
|
let ResolvedClass;
|
|
29
30
|
const resolver = (resolve) => {
|
|
30
31
|
fn().then((module2) => {
|
|
@@ -33,7 +34,7 @@ function importOnInteraction(fn, nameOrSelectorOrElement, events, parent) {
|
|
|
33
34
|
});
|
|
34
35
|
};
|
|
35
36
|
return new Promise((resolve) => {
|
|
36
|
-
const elements = (0, import_utils.getTargetElements)(nameOrSelectorOrElement, parent
|
|
37
|
+
const elements = (0, import_utils.getTargetElements)(nameOrSelectorOrElement, parent?.$el);
|
|
37
38
|
const eventListenerOptions = {
|
|
38
39
|
capture: true,
|
|
39
40
|
passive: true,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getTargetElements } from "./utils.js";
|
|
2
|
+
import { isString } from "../utils/index.js";
|
|
2
3
|
function importOnInteraction(fn, nameOrSelectorOrElement, events, parent) {
|
|
3
|
-
const normalizedEvents =
|
|
4
|
+
const normalizedEvents = isString(events) ? [events] : events;
|
|
4
5
|
let ResolvedClass;
|
|
5
6
|
const resolver = (resolve) => {
|
|
6
7
|
fn().then((module) => {
|
|
@@ -9,7 +10,7 @@ function importOnInteraction(fn, nameOrSelectorOrElement, events, parent) {
|
|
|
9
10
|
});
|
|
10
11
|
};
|
|
11
12
|
return new Promise((resolve) => {
|
|
12
|
-
const elements = getTargetElements(nameOrSelectorOrElement, parent
|
|
13
|
+
const elements = getTargetElements(nameOrSelectorOrElement, parent?.$el);
|
|
13
14
|
const eventListenerOptions = {
|
|
14
15
|
capture: true,
|
|
15
16
|
passive: true,
|
|
@@ -31,8 +31,7 @@ function importWhenIdle(fn, options) {
|
|
|
31
31
|
});
|
|
32
32
|
};
|
|
33
33
|
return new Promise((resolve) => {
|
|
34
|
-
|
|
35
|
-
const timeout = (_a = options == null ? void 0 : options.timeout) != null ? _a : 1;
|
|
34
|
+
const timeout = options?.timeout ?? 1;
|
|
36
35
|
if (!("requestIdleCallback" in window)) {
|
|
37
36
|
setTimeout(() => {
|
|
38
37
|
resolver(resolve);
|
|
@@ -42,7 +41,7 @@ function importWhenIdle(fn, options) {
|
|
|
42
41
|
setTimeout(() => {
|
|
43
42
|
resolver(resolve);
|
|
44
43
|
}, 0);
|
|
45
|
-
}, { timeout: options
|
|
44
|
+
}, { timeout: options?.timeout });
|
|
46
45
|
}
|
|
47
46
|
});
|
|
48
47
|
}
|
|
@@ -7,8 +7,7 @@ function importWhenIdle(fn, options) {
|
|
|
7
7
|
});
|
|
8
8
|
};
|
|
9
9
|
return new Promise((resolve) => {
|
|
10
|
-
|
|
11
|
-
const timeout = (_a = options == null ? void 0 : options.timeout) != null ? _a : 1;
|
|
10
|
+
const timeout = options?.timeout ?? 1;
|
|
12
11
|
if (!("requestIdleCallback" in window)) {
|
|
13
12
|
setTimeout(() => {
|
|
14
13
|
resolver(resolve);
|
|
@@ -18,7 +17,7 @@ function importWhenIdle(fn, options) {
|
|
|
18
17
|
setTimeout(() => {
|
|
19
18
|
resolver(resolve);
|
|
20
19
|
}, 0);
|
|
21
|
-
}, { timeout: options
|
|
20
|
+
}, { timeout: options?.timeout });
|
|
22
21
|
}
|
|
23
22
|
});
|
|
24
23
|
}
|
|
@@ -23,13 +23,14 @@ __export(importWhenVisible_exports, {
|
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(importWhenVisible_exports);
|
|
25
25
|
var import_utils = require("./utils.cjs");
|
|
26
|
+
var import_utils2 = require("../utils/index.cjs");
|
|
26
27
|
function importWhenVisible(fn, nameOrSelectorOrElement, parent, observerOptions = {}) {
|
|
27
28
|
let ResolvedClass;
|
|
28
29
|
const resolver = (resolve, cb) => {
|
|
29
30
|
fn().then((module2) => {
|
|
30
31
|
ResolvedClass = "default" in module2 ? module2.default : module2;
|
|
31
32
|
resolve(ResolvedClass);
|
|
32
|
-
if (
|
|
33
|
+
if ((0, import_utils2.isFunction)(cb)) {
|
|
33
34
|
cb();
|
|
34
35
|
}
|
|
35
36
|
});
|
|
@@ -43,7 +44,7 @@ function importWhenVisible(fn, nameOrSelectorOrElement, parent, observerOptions
|
|
|
43
44
|
}, 0);
|
|
44
45
|
}
|
|
45
46
|
}, observerOptions);
|
|
46
|
-
const elements = (0, import_utils.getTargetElements)(nameOrSelectorOrElement, parent
|
|
47
|
+
const elements = (0, import_utils.getTargetElements)(nameOrSelectorOrElement, parent?.$el);
|
|
47
48
|
elements.forEach((element) => observer.observe(element));
|
|
48
49
|
});
|
|
49
50
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { getTargetElements } from "./utils.js";
|
|
2
|
+
import { isFunction } from "../utils/index.js";
|
|
2
3
|
function importWhenVisible(fn, nameOrSelectorOrElement, parent, observerOptions = {}) {
|
|
3
4
|
let ResolvedClass;
|
|
4
5
|
const resolver = (resolve, cb) => {
|
|
5
6
|
fn().then((module) => {
|
|
6
7
|
ResolvedClass = "default" in module ? module.default : module;
|
|
7
8
|
resolve(ResolvedClass);
|
|
8
|
-
if (
|
|
9
|
+
if (isFunction(cb)) {
|
|
9
10
|
cb();
|
|
10
11
|
}
|
|
11
12
|
});
|
|
@@ -19,7 +20,7 @@ function importWhenVisible(fn, nameOrSelectorOrElement, parent, observerOptions
|
|
|
19
20
|
}, 0);
|
|
20
21
|
}
|
|
21
22
|
}, observerOptions);
|
|
22
|
-
const elements = getTargetElements(nameOrSelectorOrElement, parent
|
|
23
|
+
const elements = getTargetElements(nameOrSelectorOrElement, parent?.$el);
|
|
23
24
|
elements.forEach((element) => observer.observe(element));
|
|
24
25
|
});
|
|
25
26
|
}
|
package/helpers/utils.cjs
CHANGED
|
@@ -23,11 +23,12 @@ __export(utils_exports, {
|
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(utils_exports);
|
|
25
25
|
var import_utils = require("../Base/utils.cjs");
|
|
26
|
+
var import_utils2 = require("../utils/index.cjs");
|
|
26
27
|
function getTargetElements(nameOrSelectorOrElement, context) {
|
|
27
|
-
if (
|
|
28
|
+
if ((0, import_utils2.isString)(nameOrSelectorOrElement)) {
|
|
28
29
|
return (0, import_utils.getComponentElements)(nameOrSelectorOrElement, context);
|
|
29
30
|
}
|
|
30
|
-
if (!
|
|
31
|
+
if (!(0, import_utils2.isArray)(nameOrSelectorOrElement)) {
|
|
31
32
|
return [nameOrSelectorOrElement];
|
|
32
33
|
}
|
|
33
34
|
return nameOrSelectorOrElement;
|
package/helpers/utils.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { getComponentElements } from "../Base/utils.js";
|
|
2
|
+
import { isString, isArray } from "../utils/index.js";
|
|
2
3
|
function getTargetElements(nameOrSelectorOrElement, context) {
|
|
3
|
-
if (
|
|
4
|
+
if (isString(nameOrSelectorOrElement)) {
|
|
4
5
|
return getComponentElements(nameOrSelectorOrElement, context);
|
|
5
6
|
}
|
|
6
|
-
if (!
|
|
7
|
+
if (!isArray(nameOrSelectorOrElement)) {
|
|
7
8
|
return [nameOrSelectorOrElement];
|
|
8
9
|
}
|
|
9
10
|
return nameOrSelectorOrElement;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@studiometa/js-toolkit",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "A set of useful little bits of JavaScript to boost your project! 🚀",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -41,5 +41,8 @@
|
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@motionone/easing": "^10.9.0",
|
|
43
43
|
"deepmerge": "^4.2.2"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"vue": "2.6"
|
|
44
47
|
}
|
|
45
48
|
}
|
package/services/drag.cjs
CHANGED
|
@@ -4,7 +4,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
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
7
|
var __export = (target, all) => {
|
|
9
8
|
for (var name in all)
|
|
10
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -19,10 +18,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
19
18
|
};
|
|
20
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));
|
|
21
20
|
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
21
|
|
|
27
22
|
// packages/js-toolkit/services/drag.js
|
|
28
23
|
var drag_exports = {};
|
|
@@ -30,7 +25,7 @@ __export(drag_exports, {
|
|
|
30
25
|
default: () => useDrag
|
|
31
26
|
});
|
|
32
27
|
module.exports = __toCommonJS(drag_exports);
|
|
33
|
-
var
|
|
28
|
+
var import_service = require("./service.cjs");
|
|
34
29
|
var import_pointer = __toESM(require("./pointer.cjs"), 1);
|
|
35
30
|
var import_raf = __toESM(require("./raf.cjs"), 1);
|
|
36
31
|
var import_inertiaFinalValue = __toESM(require("../utils/math/inertiaFinalValue.cjs"), 1);
|
|
@@ -41,13 +36,103 @@ var MODES = {
|
|
|
41
36
|
INERTIA: "inertia",
|
|
42
37
|
STOP: "stop"
|
|
43
38
|
};
|
|
44
|
-
var
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
39
|
+
var count = 0;
|
|
40
|
+
function createDragService(target, { dampFactor = 0.85, dragTreshold = 10 } = {}) {
|
|
41
|
+
count += 1;
|
|
42
|
+
const id = `drag-${count}`;
|
|
43
|
+
function shouldPreventClick() {
|
|
44
|
+
return Math.abs(props.distance.x) > dragTreshold || Math.abs(props.distance.y) > dragTreshold;
|
|
45
|
+
}
|
|
46
|
+
function start(x, y) {
|
|
47
|
+
props.x = x;
|
|
48
|
+
props.y = y;
|
|
49
|
+
props.origin.x = x;
|
|
50
|
+
props.origin.y = y;
|
|
51
|
+
props.delta.x = 0;
|
|
52
|
+
props.delta.y = 0;
|
|
53
|
+
props.distance.x = 0;
|
|
54
|
+
props.distance.y = 0;
|
|
55
|
+
props.final.x = x;
|
|
56
|
+
props.final.y = y;
|
|
57
|
+
props.mode = MODES.START;
|
|
58
|
+
props.isGrabbing = true;
|
|
59
|
+
trigger(props);
|
|
60
|
+
}
|
|
61
|
+
function drop() {
|
|
62
|
+
props.isGrabbing = false;
|
|
63
|
+
props.mode = MODES.DROP;
|
|
64
|
+
props.hasInertia = true;
|
|
65
|
+
props.final.x = (0, import_inertiaFinalValue.default)(props.x, props.delta.x, dampFactor);
|
|
66
|
+
props.final.y = (0, import_inertiaFinalValue.default)(props.y, props.delta.y, dampFactor);
|
|
67
|
+
trigger(props);
|
|
68
|
+
setTimeout(() => {
|
|
69
|
+
const raf = (0, import_raf.default)();
|
|
70
|
+
raf.remove(id);
|
|
71
|
+
raf.add(id, rafHandler);
|
|
72
|
+
}, 0);
|
|
73
|
+
}
|
|
74
|
+
function stop() {
|
|
75
|
+
(0, import_raf.default)().remove(id);
|
|
76
|
+
props.hasInertia = false;
|
|
77
|
+
props.mode = MODES.STOP;
|
|
78
|
+
trigger(props);
|
|
79
|
+
}
|
|
80
|
+
function rafHandler() {
|
|
81
|
+
if (!props.isGrabbing) {
|
|
82
|
+
props.x += props.delta.x;
|
|
83
|
+
props.y += props.delta.y;
|
|
84
|
+
props.distance.x = props.x - props.origin.x;
|
|
85
|
+
props.distance.y = props.y - props.origin.y;
|
|
86
|
+
props.delta.x *= dampFactor;
|
|
87
|
+
props.delta.y *= dampFactor;
|
|
88
|
+
if (props.mode !== MODES.INERTIA) {
|
|
89
|
+
props.mode = MODES.INERTIA;
|
|
90
|
+
}
|
|
91
|
+
trigger(props);
|
|
92
|
+
if (Math.abs(props.delta.x) < 0.1 && Math.abs(props.delta.y) < 0.1) {
|
|
93
|
+
stop();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function pointerHandler(pointerProps) {
|
|
98
|
+
if (props.isGrabbing) {
|
|
99
|
+
props.x = pointerProps.x;
|
|
100
|
+
props.y = pointerProps.y;
|
|
101
|
+
props.delta.x = pointerProps.delta.x;
|
|
102
|
+
props.delta.y = pointerProps.delta.y;
|
|
103
|
+
props.final.x = pointerProps.x;
|
|
104
|
+
props.final.y = pointerProps.y;
|
|
105
|
+
props.distance.x = props.x - props.origin.x;
|
|
106
|
+
props.distance.y = props.y - props.origin.y;
|
|
107
|
+
if (props.mode !== MODES.DRAG) {
|
|
108
|
+
props.mode = MODES.DRAG;
|
|
109
|
+
}
|
|
110
|
+
trigger(props);
|
|
111
|
+
if (!pointerProps.isDown) {
|
|
112
|
+
drop();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function handleEvent(event) {
|
|
117
|
+
if (event.type === "dragstart") {
|
|
118
|
+
event.preventDefault();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (event.type === "click") {
|
|
122
|
+
if (shouldPreventClick()) {
|
|
123
|
+
event.stopImmediatePropagation();
|
|
124
|
+
event.stopPropagation();
|
|
125
|
+
event.preventDefault();
|
|
126
|
+
}
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
|
|
130
|
+
const y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
|
|
131
|
+
start(x, y);
|
|
132
|
+
}
|
|
133
|
+
const { add, remove, has, trigger, props } = (0, import_service.useService)({
|
|
134
|
+
props: {
|
|
135
|
+
target,
|
|
51
136
|
mode: void 0,
|
|
52
137
|
isGrabbing: false,
|
|
53
138
|
hasInertia: false,
|
|
@@ -69,144 +154,37 @@ var Drag = class extends import_Service.default {
|
|
|
69
154
|
x: 0,
|
|
70
155
|
y: 0
|
|
71
156
|
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
}
|
|
157
|
+
},
|
|
158
|
+
init() {
|
|
159
|
+
const options = { passive: true };
|
|
160
|
+
props.target.addEventListener("mousedown", handleEvent, options);
|
|
161
|
+
props.target.addEventListener("touchstart", handleEvent, options);
|
|
162
|
+
props.target.addEventListener("dragstart", handleEvent, { capture: true });
|
|
163
|
+
props.target.addEventListener("click", handleEvent, { capture: true });
|
|
164
|
+
const pointer = (0, import_pointer.default)();
|
|
165
|
+
pointer.add(id, pointerHandler);
|
|
166
|
+
},
|
|
167
|
+
kill() {
|
|
168
|
+
props.target.removeEventListener("mousedown", handleEvent);
|
|
169
|
+
props.target.removeEventListener("touchstart", handleEvent);
|
|
170
|
+
props.target.removeEventListener("dragstart", handleEvent);
|
|
171
|
+
props.target.removeEventListener("click", handleEvent);
|
|
172
|
+
const pointer = (0, import_pointer.default)();
|
|
173
|
+
pointer.remove(id);
|
|
188
174
|
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
}
|
|
175
|
+
});
|
|
176
|
+
return {
|
|
177
|
+
add,
|
|
178
|
+
remove,
|
|
179
|
+
has,
|
|
180
|
+
props: () => props
|
|
181
|
+
};
|
|
182
|
+
}
|
|
197
183
|
var instances = /* @__PURE__ */ new WeakMap();
|
|
198
184
|
function useDrag(target, options) {
|
|
199
|
-
|
|
200
|
-
|
|
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);
|
|
185
|
+
if (!instances.has(target)) {
|
|
186
|
+
instances.set(target, createDragService(target, options));
|
|
209
187
|
}
|
|
210
|
-
return
|
|
188
|
+
return instances.get(target);
|
|
211
189
|
}
|
|
212
190
|
if (module.exports.default) module.exports = module.exports.default;
|
package/services/drag.d.ts
CHANGED
|
@@ -20,52 +20,52 @@ export type PointerServiceProps = import('./pointer.js').PointerServiceProps;
|
|
|
20
20
|
export type DragLifecycle = 'start' | 'drag' | 'drop' | 'inertia' | 'stop';
|
|
21
21
|
export type DragServiceProps = {
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* The current mode of the dragging logic.
|
|
24
24
|
*/
|
|
25
25
|
mode: DragLifecycle;
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* The target element of the drag.
|
|
28
28
|
*/
|
|
29
29
|
target: HTMLElement;
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
31
|
+
* Whether the user is currently grabbing the targeted element or not.
|
|
32
32
|
*/
|
|
33
33
|
isGrabbing: boolean;
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
35
|
+
* Whether the drag currently has inertia or not.
|
|
36
36
|
*/
|
|
37
37
|
hasInertia: boolean;
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
39
|
+
* The current horizontal position in the viewport.
|
|
40
40
|
*/
|
|
41
41
|
x: number;
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
43
|
+
* The current vertical position in the viewport.
|
|
44
44
|
*/
|
|
45
45
|
y: number;
|
|
46
46
|
/**
|
|
47
|
-
*
|
|
47
|
+
* The delta between the current position and the last.
|
|
48
48
|
*/
|
|
49
49
|
delta: {
|
|
50
50
|
x: number;
|
|
51
51
|
y: number;
|
|
52
52
|
};
|
|
53
53
|
/**
|
|
54
|
-
*
|
|
54
|
+
* The initial position where the dragging started.
|
|
55
55
|
*/
|
|
56
56
|
origin: {
|
|
57
57
|
x: number;
|
|
58
58
|
y: number;
|
|
59
59
|
};
|
|
60
60
|
/**
|
|
61
|
-
*
|
|
61
|
+
* The distance from the current position to the origin.
|
|
62
62
|
*/
|
|
63
63
|
distance: {
|
|
64
64
|
x: number;
|
|
65
65
|
y: number;
|
|
66
66
|
};
|
|
67
67
|
/**
|
|
68
|
-
*
|
|
68
|
+
* The final position that will be reached at the end of the inertia.
|
|
69
69
|
*/
|
|
70
70
|
final: {
|
|
71
71
|
x: number;
|