@studiometa/js-toolkit 2.2.0 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Base/index.cjs +3 -4
- package/Base/index.js +1 -2
- package/Base/managers/ServicesManager.cjs +4 -5
- package/Base/managers/ServicesManager.js +4 -5
- package/Base/utils.cjs +1 -5
- package/Base/utils.d.ts +0 -8
- package/Base/utils.js +1 -5
- package/package.json +1 -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 +73 -103
- package/services/pointer.d.ts +1 -9
- package/services/pointer.js +72 -100
- package/services/raf.cjs +36 -53
- package/services/raf.d.ts +0 -8
- package/services/raf.js +36 -54
- 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 +4 -8
- package/utils/css/animate.d.ts +16 -15
- package/utils/css/animate.js +3 -7
- package/utils/index.cjs +1 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/math/createEases.cjs +32 -0
- package/utils/math/createEases.d.ts +18 -0
- package/utils/math/createEases.js +10 -0
- package/utils/math/ease.cjs +15 -22
- package/utils/math/ease.d.ts +16 -33
- package/utils/math/ease.js +1 -8
- package/utils/math/index.cjs +1 -0
- package/utils/math/index.d.ts +1 -0
- package/utils/math/index.js +1 -0
- package/utils/noop.cjs +31 -0
- package/utils/noop.d.ts +12 -0
- package/utils/noop.js +9 -0
- package/services/Service.cjs +0 -76
- package/services/Service.d.ts +0 -76
- package/services/Service.js +0 -55
package/Base/index.cjs
CHANGED
|
@@ -36,10 +36,9 @@ var import_RefsManager = __toESM(require("./managers/RefsManager.cjs"), 1);
|
|
|
36
36
|
var import_ServicesManager = __toESM(require("./managers/ServicesManager.cjs"), 1);
|
|
37
37
|
var import_EventsManager = __toESM(require("./managers/EventsManager.cjs"), 1);
|
|
38
38
|
var import_OptionsManager = __toESM(require("./managers/OptionsManager.cjs"), 1);
|
|
39
|
+
var import_noop = require("../utils/noop.cjs");
|
|
39
40
|
var id = 0;
|
|
40
41
|
var isDev = typeof __DEV__ !== "undefined" && __DEV__;
|
|
41
|
-
function noop() {
|
|
42
|
-
}
|
|
43
42
|
function createAndTestManagers(instance) {
|
|
44
43
|
[
|
|
45
44
|
{
|
|
@@ -152,10 +151,10 @@ var Base = class extends EventTarget {
|
|
|
152
151
|
return this.__children;
|
|
153
152
|
}
|
|
154
153
|
get $log() {
|
|
155
|
-
return this.__options.log ? window.console.log.bind(window, `[${this.__config.name}]`) : noop;
|
|
154
|
+
return this.__options.log ? window.console.log.bind(window, `[${this.__config.name}]`) : import_noop.noop;
|
|
156
155
|
}
|
|
157
156
|
get __debug() {
|
|
158
|
-
return isDev && this.__options.debug ? window.console.log.bind(window, `[debug] [${this.$id}]`) : noop;
|
|
157
|
+
return isDev && this.__options.debug ? window.console.log.bind(window, `[debug] [${this.$id}]`) : import_noop.noop;
|
|
159
158
|
}
|
|
160
159
|
get __managers() {
|
|
161
160
|
return {
|
package/Base/index.js
CHANGED
|
@@ -10,10 +10,9 @@ import RefsManager from "./managers/RefsManager.js";
|
|
|
10
10
|
import ServicesManager from "./managers/ServicesManager.js";
|
|
11
11
|
import EventsManager from "./managers/EventsManager.js";
|
|
12
12
|
import OptionsManager from "./managers/OptionsManager.js";
|
|
13
|
+
import { noop } from "../utils/noop.js";
|
|
13
14
|
let id = 0;
|
|
14
15
|
const isDev = typeof __DEV__ !== "undefined" && __DEV__;
|
|
15
|
-
function noop() {
|
|
16
|
-
}
|
|
17
16
|
function createAndTestManagers(instance) {
|
|
18
17
|
[
|
|
19
18
|
{
|
|
@@ -37,7 +37,7 @@ var import_scroll = __toESM(require("../../services/scroll.cjs"), 1);
|
|
|
37
37
|
var import_key = __toESM(require("../../services/key.cjs"), 1);
|
|
38
38
|
var import_load = __toESM(require("../../services/load.cjs"), 1);
|
|
39
39
|
var import_AbstractManager = __toESM(require("./AbstractManager.cjs"), 1);
|
|
40
|
-
var import_utils = require("
|
|
40
|
+
var import_utils = require("../../utils/index.cjs");
|
|
41
41
|
var SERVICES_MAP = {
|
|
42
42
|
scrolled: import_scroll.default,
|
|
43
43
|
resized: import_resize.default,
|
|
@@ -59,7 +59,7 @@ var ServicesManager = class extends import_AbstractManager.default {
|
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
61
|
has(service) {
|
|
62
|
-
if (!(((0, import_utils.
|
|
62
|
+
if (!(((0, import_utils.isFunction)(this.__base[service]) || this.__base.__hasEvent(service)) && this.__services[service])) {
|
|
63
63
|
return false;
|
|
64
64
|
}
|
|
65
65
|
const { has } = this.__services[service]();
|
|
@@ -72,9 +72,8 @@ var ServicesManager = class extends import_AbstractManager.default {
|
|
|
72
72
|
if (this.has(service)) {
|
|
73
73
|
return this.disable.bind(this, service);
|
|
74
74
|
}
|
|
75
|
-
if (!((0, import_utils.
|
|
76
|
-
return
|
|
77
|
-
};
|
|
75
|
+
if (!((0, import_utils.isFunction)(this.__base[service]) || this.__base.__hasEvent(service)) || !this.__services[service]) {
|
|
76
|
+
return import_utils.noop;
|
|
78
77
|
}
|
|
79
78
|
const { add } = this.__services[service]();
|
|
80
79
|
const self = this;
|
|
@@ -11,7 +11,7 @@ import useScroll from "../../services/scroll.js";
|
|
|
11
11
|
import useKey from "../../services/key.js";
|
|
12
12
|
import useLoad from "../../services/load.js";
|
|
13
13
|
import AbstractManager from "./AbstractManager.js";
|
|
14
|
-
import {
|
|
14
|
+
import { noop, isFunction } from "../../utils/index.js";
|
|
15
15
|
const SERVICES_MAP = {
|
|
16
16
|
scrolled: useScroll,
|
|
17
17
|
resized: useResize,
|
|
@@ -33,7 +33,7 @@ class ServicesManager extends AbstractManager {
|
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
has(service) {
|
|
36
|
-
if (!((
|
|
36
|
+
if (!((isFunction(this.__base[service]) || this.__base.__hasEvent(service)) && this.__services[service])) {
|
|
37
37
|
return false;
|
|
38
38
|
}
|
|
39
39
|
const { has } = this.__services[service]();
|
|
@@ -46,9 +46,8 @@ class ServicesManager extends AbstractManager {
|
|
|
46
46
|
if (this.has(service)) {
|
|
47
47
|
return this.disable.bind(this, service);
|
|
48
48
|
}
|
|
49
|
-
if (!(
|
|
50
|
-
return
|
|
51
|
-
};
|
|
49
|
+
if (!(isFunction(this.__base[service]) || this.__base.__hasEvent(service)) || !this.__services[service]) {
|
|
50
|
+
return noop;
|
|
52
51
|
}
|
|
53
52
|
const { add } = this.__services[service]();
|
|
54
53
|
const self = this;
|
package/Base/utils.cjs
CHANGED
|
@@ -19,13 +19,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// packages/js-toolkit/Base/utils.js
|
|
20
20
|
var utils_exports = {};
|
|
21
21
|
__export(utils_exports, {
|
|
22
|
-
getComponentElements: () => getComponentElements
|
|
23
|
-
hasMethod: () => hasMethod
|
|
22
|
+
getComponentElements: () => getComponentElements
|
|
24
23
|
});
|
|
25
24
|
module.exports = __toCommonJS(utils_exports);
|
|
26
|
-
function hasMethod(obj, name) {
|
|
27
|
-
return typeof obj[name] === "function";
|
|
28
|
-
}
|
|
29
25
|
function getComponentElements(nameOrSelector, element = document) {
|
|
30
26
|
const selector = `[data-component="${nameOrSelector}"]`;
|
|
31
27
|
let elements = [];
|
package/Base/utils.d.ts
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @typedef {import('./index.js').default} Base
|
|
3
3
|
*/
|
|
4
|
-
/**
|
|
5
|
-
* Test if an object has a method.
|
|
6
|
-
*
|
|
7
|
-
* @param {Object} obj The object to test
|
|
8
|
-
* @param {string} name The method's name
|
|
9
|
-
* @returns {boolean}
|
|
10
|
-
*/
|
|
11
|
-
export function hasMethod(obj: any, name: string): boolean;
|
|
12
4
|
/**
|
|
13
5
|
* Get a list of elements based on the name of a component.
|
|
14
6
|
*
|
package/Base/utils.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
function hasMethod(obj, name) {
|
|
2
|
-
return typeof obj[name] === "function";
|
|
3
|
-
}
|
|
4
1
|
function getComponentElements(nameOrSelector, element = document) {
|
|
5
2
|
const selector = `[data-component="${nameOrSelector}"]`;
|
|
6
3
|
let elements = [];
|
|
@@ -14,6 +11,5 @@ function getComponentElements(nameOrSelector, element = document) {
|
|
|
14
11
|
return elements;
|
|
15
12
|
}
|
|
16
13
|
export {
|
|
17
|
-
getComponentElements
|
|
18
|
-
hasMethod
|
|
14
|
+
getComponentElements
|
|
19
15
|
};
|
package/package.json
CHANGED
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;
|