@studiometa/js-toolkit 2.12.1 → 3.0.0-alpha.10
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/Base.d.ts +8 -8
- package/Base/Base.js +75 -78
- package/Base/Base.js.map +2 -2
- package/Base/features.d.ts +15 -2
- package/Base/features.js +31 -1
- package/Base/features.js.map +2 -2
- package/Base/index.d.ts +1 -0
- package/Base/index.js +4 -0
- package/Base/index.js.map +2 -2
- package/Base/managers/AbstractManager.d.ts +1 -1
- package/Base/managers/ChildrenManager.d.ts +6 -6
- package/Base/managers/ChildrenManager.js +34 -20
- package/Base/managers/ChildrenManager.js.map +2 -2
- package/Base/managers/EventsManager.d.ts +6 -28
- package/Base/managers/EventsManager.js +54 -64
- package/Base/managers/EventsManager.js.map +2 -2
- package/Base/managers/OptionsManager.d.ts +6 -6
- package/Base/managers/OptionsManager.js +29 -23
- package/Base/managers/OptionsManager.js.map +3 -3
- package/Base/managers/RefsManager.d.ts +1 -1
- package/Base/managers/RefsManager.js +23 -34
- package/Base/managers/RefsManager.js.map +3 -3
- package/Base/managers/ResponsiveOptionsManager.js +7 -4
- package/Base/managers/ResponsiveOptionsManager.js.map +2 -2
- package/Base/managers/ServicesManager.d.ts +2 -4
- package/Base/managers/ServicesManager.js +2 -3
- package/Base/managers/ServicesManager.js.map +2 -2
- package/Base/utils.d.ts +10 -5
- package/Base/utils.js +37 -23
- package/Base/utils.js.map +2 -2
- package/README.md +107 -6
- package/decorators/index.d.ts +1 -1
- package/decorators/index.js +1 -1
- package/decorators/index.js.map +1 -1
- package/decorators/withBreakpointManager.js +6 -4
- package/decorators/withBreakpointManager.js.map +2 -2
- package/decorators/withBreakpointObserver.d.ts +0 -1
- package/decorators/withBreakpointObserver.js +5 -3
- package/decorators/withBreakpointObserver.js.map +2 -2
- package/decorators/withIntersectionObserver.js +3 -5
- package/decorators/withIntersectionObserver.js.map +2 -2
- package/decorators/withMountOnMediaQuery.js +3 -3
- package/decorators/withMountOnMediaQuery.js.map +2 -2
- package/decorators/withMountWhenInView.js +3 -3
- package/decorators/withMountWhenInView.js.map +2 -2
- package/decorators/withName.d.ts +6 -0
- package/decorators/withName.js +12 -0
- package/decorators/withName.js.map +7 -0
- package/decorators/withScrolledInView/utils.d.ts +12 -1
- package/decorators/withScrolledInView/utils.js.map +2 -2
- package/decorators/withScrolledInView/withScrolledInView.js +7 -62
- package/decorators/withScrolledInView/withScrolledInView.js.map +2 -2
- package/helpers/createApp.d.ts +3 -4
- package/helpers/createApp.js +21 -11
- package/helpers/createApp.js.map +2 -2
- package/helpers/getDirectChildren.js +10 -5
- package/helpers/getDirectChildren.js.map +2 -2
- package/helpers/importWhenVisible.js +1 -2
- package/helpers/importWhenVisible.js.map +1 -1
- package/index.d.ts +2 -3
- package/index.js +2 -4
- package/index.js.map +2 -2
- package/package.json +2 -10
- package/services/index.d.ts +1 -1
- package/services/index.js.map +2 -2
- package/services/pointer.d.ts +1 -1
- package/services/pointer.js +3 -3
- package/services/pointer.js.map +2 -2
- package/services/raf.js +2 -4
- package/services/raf.js.map +2 -2
- package/services/resize.d.ts +7 -6
- package/services/resize.js +18 -26
- package/services/resize.js.map +2 -2
- package/services/scroll.d.ts +14 -0
- package/services/scroll.js +39 -21
- package/services/scroll.js.map +2 -2
- package/utils/SmartQueue.d.ts +1 -1
- package/utils/SmartQueue.js +1 -1
- package/utils/SmartQueue.js.map +2 -2
- package/utils/css/animate.d.ts +33 -15
- package/utils/css/animate.js +67 -26
- package/utils/css/animate.js.map +2 -2
- package/utils/css/getOffsetSizes.js +2 -2
- package/utils/css/getOffsetSizes.js.map +1 -1
- package/utils/css/index.d.ts +2 -1
- package/utils/css/index.js +4 -4
- package/utils/css/index.js.map +2 -2
- package/utils/css/matrix.d.ts +1 -1
- package/utils/css/matrix.js.map +2 -2
- package/utils/css/transform.d.ts +1 -1
- package/utils/css/transform.js +1 -1
- package/utils/css/transform.js.map +2 -2
- package/utils/css/utils.d.ts +1 -1
- package/utils/css/utils.js +1 -1
- package/utils/css/utils.js.map +2 -2
- package/utils/index.d.ts +6 -4
- package/utils/index.js +17 -16
- package/utils/index.js.map +2 -2
- package/utils/is.d.ts +12 -0
- package/utils/is.js +17 -0
- package/utils/is.js.map +2 -2
- package/utils/math/createRange.d.ts +4 -0
- package/utils/math/createRange.js +13 -0
- package/utils/math/createRange.js.map +7 -0
- package/utils/math/ease.d.ts +1 -0
- package/utils/math/ease.js +2 -0
- package/utils/math/ease.js.map +2 -2
- package/utils/math/index.d.ts +5 -3
- package/utils/math/index.js +7 -3
- package/utils/math/index.js.map +2 -2
- package/utils/math/mean.d.ts +1 -0
- package/utils/math/mean.js +17 -0
- package/utils/math/mean.js.map +7 -0
- package/utils/memo.d.ts +5 -0
- package/utils/memo.js +14 -0
- package/utils/memo.js.map +7 -0
- package/utils/nextFrame.d.ts +4 -22
- package/utils/nextFrame.js +3 -10
- package/utils/nextFrame.js.map +2 -2
- package/utils/nextMicrotask.d.ts +4 -1
- package/utils/nextMicrotask.js +3 -3
- package/utils/nextMicrotask.js.map +2 -2
- package/utils/nextTick.d.ts +4 -1
- package/utils/nextTick.js +3 -3
- package/utils/nextTick.js.map +2 -2
- package/utils/random.d.ts +15 -0
- package/utils/random.js +15 -0
- package/utils/random.js.map +7 -0
- package/utils/scrollTo.d.ts +30 -6
- package/utils/scrollTo.js +97 -43
- package/utils/scrollTo.js.map +2 -2
- package/utils/string/changeCase.d.ts +24 -0
- package/utils/string/changeCase.js +47 -0
- package/utils/string/changeCase.js.map +7 -0
- package/utils/string/index.d.ts +1 -0
- package/utils/string/index.js +1 -0
- package/utils/string/index.js.map +2 -2
- package/utils/{focusTrap.d.ts → trapFocus.d.ts} +2 -10
- package/utils/{focusTrap.js → trapFocus.js} +5 -9
- package/utils/trapFocus.js.map +7 -0
- package/utils/tween.d.ts +59 -10
- package/utils/tween.js +24 -18
- package/utils/tween.js.map +2 -2
- package/Base/Base.cjs +0 -420
- package/Base/Base.cjs.map +0 -7
- package/Base/features.cjs +0 -27
- package/Base/features.cjs.map +0 -7
- package/Base/index.cjs +0 -21
- package/Base/index.cjs.map +0 -7
- package/Base/managers/AbstractManager.cjs +0 -78
- package/Base/managers/AbstractManager.cjs.map +0 -7
- package/Base/managers/ChildrenManager.cjs +0 -156
- package/Base/managers/ChildrenManager.cjs.map +0 -7
- package/Base/managers/EventsManager.cjs +0 -305
- package/Base/managers/EventsManager.cjs.map +0 -7
- package/Base/managers/OptionsManager.cjs +0 -198
- package/Base/managers/OptionsManager.cjs.map +0 -7
- package/Base/managers/RefsManager.cjs +0 -118
- package/Base/managers/RefsManager.cjs.map +0 -7
- package/Base/managers/ResponsiveOptionsManager.cjs +0 -79
- package/Base/managers/ResponsiveOptionsManager.cjs.map +0 -7
- package/Base/managers/ServicesManager.cjs +0 -161
- package/Base/managers/ServicesManager.cjs.map +0 -7
- package/Base/managers/index.cjs +0 -39
- package/Base/managers/index.cjs.map +0 -7
- package/Base/types.cjs +0 -19
- package/Base/types.cjs.map +0 -7
- package/Base/utils.cjs +0 -95
- package/Base/utils.cjs.map +0 -7
- package/decorators/index.cjs +0 -33
- package/decorators/index.cjs.map +0 -7
- package/decorators/withBreakpointManager.cjs +0 -102
- package/decorators/withBreakpointManager.cjs.map +0 -7
- package/decorators/withBreakpointObserver.cjs +0 -128
- package/decorators/withBreakpointObserver.cjs.map +0 -7
- package/decorators/withDrag.cjs +0 -56
- package/decorators/withDrag.cjs.map +0 -7
- package/decorators/withExtraConfig.cjs +0 -50
- package/decorators/withExtraConfig.cjs.map +0 -7
- package/decorators/withFreezedOptions.cjs +0 -50
- package/decorators/withFreezedOptions.cjs.map +0 -7
- package/decorators/withIntersectionObserver.cjs +0 -71
- package/decorators/withIntersectionObserver.cjs.map +0 -7
- package/decorators/withMountOnMediaQuery.cjs +0 -86
- package/decorators/withMountOnMediaQuery.cjs.map +0 -7
- package/decorators/withMountWhenInView.cjs +0 -92
- package/decorators/withMountWhenInView.cjs.map +0 -7
- package/decorators/withMountWhenPrefersMotion.cjs +0 -30
- package/decorators/withMountWhenPrefersMotion.cjs.map +0 -7
- package/decorators/withRelativePointer.cjs +0 -56
- package/decorators/withRelativePointer.cjs.map +0 -7
- package/decorators/withResponsiveOptions.cjs +0 -41
- package/decorators/withResponsiveOptions.cjs.map +0 -7
- package/decorators/withScrolledInView/index.cjs +0 -22
- package/decorators/withScrolledInView/index.cjs.map +0 -7
- package/decorators/withScrolledInView/types.cjs +0 -19
- package/decorators/withScrolledInView/types.cjs.map +0 -7
- package/decorators/withScrolledInView/utils.cjs +0 -99
- package/decorators/withScrolledInView/utils.cjs.map +0 -7
- package/decorators/withScrolledInView/withScrolledInView.cjs +0 -249
- package/decorators/withScrolledInView/withScrolledInView.cjs.map +0 -7
- package/decorators/withVue2.cjs +0 -68
- package/decorators/withVue2.cjs.map +0 -7
- package/decorators/withVue2.d.ts +0 -16
- package/decorators/withVue2.js +0 -46
- package/decorators/withVue2.js.map +0 -7
- package/helpers/createApp.cjs +0 -58
- package/helpers/createApp.cjs.map +0 -7
- package/helpers/getClosestParent.cjs +0 -45
- package/helpers/getClosestParent.cjs.map +0 -7
- package/helpers/getDirectChildren.cjs +0 -47
- package/helpers/getDirectChildren.cjs.map +0 -7
- package/helpers/getInstanceFromElement.cjs +0 -32
- package/helpers/getInstanceFromElement.cjs.map +0 -7
- package/helpers/importOnInteraction.cjs +0 -48
- package/helpers/importOnInteraction.cjs.map +0 -7
- package/helpers/importOnMediaQuery.cjs +0 -44
- package/helpers/importOnMediaQuery.cjs.map +0 -7
- package/helpers/importWhenIdle.cjs +0 -46
- package/helpers/importWhenIdle.cjs.map +0 -7
- package/helpers/importWhenPrefersMotion.cjs +0 -40
- package/helpers/importWhenPrefersMotion.cjs.map +0 -7
- package/helpers/importWhenVisible.cjs +0 -44
- package/helpers/importWhenVisible.cjs.map +0 -7
- package/helpers/index.cjs +0 -53
- package/helpers/index.cjs.map +0 -7
- package/helpers/utils.cjs +0 -37
- package/helpers/utils.cjs.map +0 -7
- package/index.cjs +0 -31
- package/index.cjs.map +0 -7
- package/services/drag.cjs +0 -231
- package/services/drag.cjs.map +0 -7
- package/services/index.cjs +0 -51
- package/services/index.cjs.map +0 -7
- package/services/key.cjs +0 -97
- package/services/key.cjs.map +0 -7
- package/services/load.cjs +0 -60
- package/services/load.cjs.map +0 -7
- package/services/pointer.cjs +0 -145
- package/services/pointer.cjs.map +0 -7
- package/services/raf.cjs +0 -80
- package/services/raf.cjs.map +0 -7
- package/services/resize.cjs +0 -107
- package/services/resize.cjs.map +0 -7
- package/services/scroll.cjs +0 -119
- package/services/scroll.cjs.map +0 -7
- package/services/service.cjs +0 -69
- package/services/service.cjs.map +0 -7
- package/utils/Queue.cjs +0 -93
- package/utils/Queue.cjs.map +0 -7
- package/utils/SmartQueue.cjs +0 -73
- package/utils/SmartQueue.cjs.map +0 -7
- package/utils/collide/boundingRectToCircle.cjs +0 -36
- package/utils/collide/boundingRectToCircle.cjs.map +0 -7
- package/utils/collide/collideCircleCircle.cjs +0 -35
- package/utils/collide/collideCircleCircle.cjs.map +0 -7
- package/utils/collide/collideCircleRect.cjs +0 -47
- package/utils/collide/collideCircleRect.cjs.map +0 -7
- package/utils/collide/collidePointCircle.cjs +0 -32
- package/utils/collide/collidePointCircle.cjs.map +0 -7
- package/utils/collide/collidePointRect.cjs +0 -32
- package/utils/collide/collidePointRect.cjs.map +0 -7
- package/utils/collide/collideRectRect.cjs +0 -32
- package/utils/collide/collideRectRect.cjs.map +0 -7
- package/utils/collide/index.cjs +0 -47
- package/utils/collide/index.cjs.map +0 -7
- package/utils/css/animate.cjs +0 -202
- package/utils/css/animate.cjs.map +0 -7
- package/utils/css/classes.cjs +0 -51
- package/utils/css/classes.cjs.map +0 -7
- package/utils/css/getOffsetSizes.cjs +0 -48
- package/utils/css/getOffsetSizes.cjs.map +0 -7
- package/utils/css/index.cjs +0 -52
- package/utils/css/index.cjs.map +0 -7
- package/utils/css/matrix.cjs +0 -30
- package/utils/css/matrix.cjs.map +0 -7
- package/utils/css/styles.cjs +0 -45
- package/utils/css/styles.cjs.map +0 -7
- package/utils/css/transform.cjs +0 -91
- package/utils/css/transform.cjs.map +0 -7
- package/utils/css/transition.cjs +0 -145
- package/utils/css/transition.cjs.map +0 -7
- package/utils/css/utils.cjs +0 -32
- package/utils/css/utils.cjs.map +0 -7
- package/utils/debounce.cjs +0 -35
- package/utils/debounce.cjs.map +0 -7
- package/utils/dom/ancestors.cjs +0 -37
- package/utils/dom/ancestors.cjs.map +0 -7
- package/utils/dom/index.cjs +0 -21
- package/utils/dom/index.cjs.map +0 -7
- package/utils/focusTrap.cjs +0 -93
- package/utils/focusTrap.cjs.map +0 -7
- package/utils/focusTrap.js.map +0 -7
- package/utils/getComponentResolver.cjs +0 -38
- package/utils/getComponentResolver.cjs.map +0 -7
- package/utils/has.cjs +0 -29
- package/utils/has.cjs.map +0 -7
- package/utils/history.cjs +0 -92
- package/utils/history.cjs.map +0 -7
- package/utils/index.cjs +0 -76
- package/utils/index.cjs.map +0 -7
- package/utils/is.cjs +0 -41
- package/utils/is.cjs.map +0 -7
- package/utils/keyCodes.cjs +0 -36
- package/utils/keyCodes.cjs.map +0 -7
- package/utils/math/clamp.cjs +0 -29
- package/utils/math/clamp.cjs.map +0 -7
- package/utils/math/clamp01.cjs +0 -40
- package/utils/math/clamp01.cjs.map +0 -7
- package/utils/math/createEases.cjs +0 -33
- package/utils/math/createEases.cjs.map +0 -7
- package/utils/math/damp.cjs +0 -29
- package/utils/math/damp.cjs.map +0 -7
- package/utils/math/ease.cjs +0 -82
- package/utils/math/ease.cjs.map +0 -7
- package/utils/math/index.cjs +0 -54
- package/utils/math/index.cjs.map +0 -7
- package/utils/math/inertiaFinalValue.cjs +0 -47
- package/utils/math/inertiaFinalValue.cjs.map +0 -7
- package/utils/math/lerp.cjs +0 -29
- package/utils/math/lerp.cjs.map +0 -7
- package/utils/math/map.cjs +0 -29
- package/utils/math/map.cjs.map +0 -7
- package/utils/math/round.cjs +0 -29
- package/utils/math/round.cjs.map +0 -7
- package/utils/memoize.cjs +0 -48
- package/utils/memoize.cjs.map +0 -7
- package/utils/nextFrame.cjs +0 -41
- package/utils/nextFrame.cjs.map +0 -7
- package/utils/nextMicrotask.cjs +0 -30
- package/utils/nextMicrotask.cjs.map +0 -7
- package/utils/nextTick.cjs +0 -31
- package/utils/nextTick.cjs.map +0 -7
- package/utils/noop.cjs +0 -32
- package/utils/noop.cjs.map +0 -7
- package/utils/object/autoBind.cjs +0 -60
- package/utils/object/autoBind.cjs.map +0 -7
- package/utils/object/autoBind.d.ts +0 -14
- package/utils/object/autoBind.js +0 -28
- package/utils/object/autoBind.js.map +0 -7
- package/utils/object/getAllProperties.cjs +0 -39
- package/utils/object/getAllProperties.cjs.map +0 -7
- package/utils/object/index.cjs +0 -39
- package/utils/object/index.cjs.map +0 -7
- package/utils/object/index.d.ts +0 -2
- package/utils/object/index.js +0 -7
- package/utils/object/index.js.map +0 -7
- package/utils/scheduler.cjs +0 -75
- package/utils/scheduler.cjs.map +0 -7
- package/utils/scrollTo.cjs +0 -88
- package/utils/scrollTo.cjs.map +0 -7
- package/utils/string/endsWith.cjs +0 -35
- package/utils/string/endsWith.cjs.map +0 -7
- package/utils/string/index.cjs +0 -58
- package/utils/string/index.cjs.map +0 -7
- package/utils/string/startsWith.cjs +0 -35
- package/utils/string/startsWith.cjs.map +0 -7
- package/utils/string/withLeadingCharacters.cjs +0 -40
- package/utils/string/withLeadingCharacters.cjs.map +0 -7
- package/utils/string/withLeadingSlash.cjs +0 -40
- package/utils/string/withLeadingSlash.cjs.map +0 -7
- package/utils/string/withTrailingCharacters.cjs +0 -40
- package/utils/string/withTrailingCharacters.cjs.map +0 -7
- package/utils/string/withTrailingSlash.cjs +0 -40
- package/utils/string/withTrailingSlash.cjs.map +0 -7
- package/utils/string/withoutLeadingCharacters.cjs +0 -29
- package/utils/string/withoutLeadingCharacters.cjs.map +0 -7
- package/utils/string/withoutLeadingCharactersRecursive.cjs +0 -45
- package/utils/string/withoutLeadingCharactersRecursive.cjs.map +0 -7
- package/utils/string/withoutLeadingSlash.cjs +0 -40
- package/utils/string/withoutLeadingSlash.cjs.map +0 -7
- package/utils/string/withoutTrailingCharacters.cjs +0 -29
- package/utils/string/withoutTrailingCharacters.cjs.map +0 -7
- package/utils/string/withoutTrailingCharactersRecursive.cjs +0 -45
- package/utils/string/withoutTrailingCharactersRecursive.cjs.map +0 -7
- package/utils/string/withoutTrailingSlash.cjs +0 -40
- package/utils/string/withoutTrailingSlash.cjs.map +0 -7
- package/utils/throttle.cjs +0 -37
- package/utils/throttle.cjs.map +0 -7
- package/utils/tween.cjs +0 -122
- package/utils/tween.cjs.map +0 -7
- package/utils/wait.cjs +0 -31
- package/utils/wait.cjs.map +0 -7
package/helpers/createApp.js
CHANGED
|
@@ -1,20 +1,30 @@
|
|
|
1
1
|
import { features } from "../Base/features.js";
|
|
2
|
-
import {
|
|
3
|
-
function createApp(App,
|
|
2
|
+
import { isBoolean, isObject } from "../utils/index.js";
|
|
3
|
+
function createApp(App, options = {}) {
|
|
4
4
|
let app;
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
const {
|
|
6
|
+
root = document.body,
|
|
7
|
+
breakpoints = null,
|
|
8
|
+
blocking = null,
|
|
9
|
+
attributes = null
|
|
10
|
+
} = options instanceof HTMLElement ? { root: options } : options;
|
|
11
|
+
if (isObject(breakpoints)) {
|
|
12
|
+
features.set("breakpoints", breakpoints);
|
|
10
13
|
}
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
if (isBoolean(blocking)) {
|
|
15
|
+
features.set("blocking", blocking);
|
|
16
|
+
}
|
|
17
|
+
if (isObject(attributes)) {
|
|
18
|
+
features.set("attributes", attributes);
|
|
19
|
+
}
|
|
20
|
+
async function init() {
|
|
21
|
+
app = new App(root);
|
|
22
|
+
await app.$mount();
|
|
13
23
|
return app;
|
|
14
24
|
}
|
|
15
25
|
let p;
|
|
16
|
-
if (features.get("
|
|
17
|
-
p =
|
|
26
|
+
if (features.get("blocking")) {
|
|
27
|
+
p = init();
|
|
18
28
|
} else {
|
|
19
29
|
p = new Promise((resolve) => {
|
|
20
30
|
if (document.readyState === "complete") {
|
package/helpers/createApp.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/helpers/createApp.ts"],
|
|
4
|
-
"sourcesContent": ["import type { Base, BaseConstructor, BaseProps } from '../Base/index.js';\nimport type { Features } from '../Base/features.js';\nimport { features } from '../Base/features.js';\nimport {
|
|
5
|
-
"mappings": "AAEA,SAAS,gBAAgB;AACzB,SAAS,
|
|
4
|
+
"sourcesContent": ["import type { Base, BaseConstructor, BaseProps } from '../Base/index.js';\nimport type { Features } from '../Base/features.js';\nimport { features } from '../Base/features.js';\nimport { isBoolean, isObject } from '../utils/index.js';\n\nexport type CreateAppOptions = Partial<Features> & {\n root?: HTMLElement;\n};\n\n/**\n * Instantiate and mount the given component on the given root element when the page has been loaded\n * and return a function to use the app instance when it is ready.\n */\nexport default function createApp<S extends BaseConstructor<Base>, T extends BaseProps = BaseProps>(\n App: S,\n options: HTMLElement | CreateAppOptions = {},\n): () => Promise<S & Base<T>> {\n let app: S & Base<T>;\n const {\n root = document.body,\n breakpoints = null,\n blocking = null,\n attributes = null,\n } = options instanceof HTMLElement ? { root: options } : options;\n\n if (isObject(breakpoints)) {\n features.set('breakpoints', breakpoints);\n }\n\n if (isBoolean(blocking)) {\n features.set('blocking', blocking);\n }\n\n if (isObject(attributes)) {\n features.set('attributes', attributes);\n }\n\n async function init() {\n app = (new App(root) as S & Base<T>)\n await app.$mount();\n return app;\n }\n\n let p: Promise<S & Base<T>>;\n\n if (features.get('blocking')) {\n p = init();\n } else {\n p = new Promise<S & Base<T>>((resolve) => {\n if (document.readyState === 'complete') {\n resolve(init());\n } else {\n document.addEventListener('readystatechange', () => {\n if (document.readyState === 'complete') {\n resolve(init());\n }\n });\n }\n });\n }\n\n return () => p;\n}\n"],
|
|
5
|
+
"mappings": "AAEA,SAAS,gBAAgB;AACzB,SAAS,WAAW,gBAAgB;AAUrB,SAAR,UACL,KACA,UAA0C,CAAC,GACf;AAC5B,MAAI;AACJ,QAAM;AAAA,IACJ,OAAO,SAAS;AAAA,IAChB,cAAc;AAAA,IACd,WAAW;AAAA,IACX,aAAa;AAAA,EACf,IAAI,mBAAmB,cAAc,EAAE,MAAM,QAAQ,IAAI;AAEzD,MAAI,SAAS,WAAW,GAAG;AACzB,aAAS,IAAI,eAAe,WAAW;AAAA,EACzC;AAEA,MAAI,UAAU,QAAQ,GAAG;AACvB,aAAS,IAAI,YAAY,QAAQ;AAAA,EACnC;AAEA,MAAI,SAAS,UAAU,GAAG;AACxB,aAAS,IAAI,cAAc,UAAU;AAAA,EACvC;AAEA,iBAAe,OAAO;AACpB,UAAO,IAAI,IAAI,IAAI;AACnB,UAAM,IAAI,OAAO;AACjB,WAAO;AAAA,EACT;AAEA,MAAI;AAEJ,MAAI,SAAS,IAAI,UAAU,GAAG;AAC5B,QAAI,KAAK;AAAA,EACX,OAAO;AACL,QAAI,IAAI,QAAqB,CAAC,YAAY;AACxC,UAAI,SAAS,eAAe,YAAY;AACtC,gBAAQ,KAAK,CAAC;AAAA,MAChB,OAAO;AACL,iBAAS,iBAAiB,oBAAoB,MAAM;AAClD,cAAI,SAAS,eAAe,YAAY;AACtC,oBAAQ,KAAK,CAAC;AAAA,UAChB;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,MAAM;AACf;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -8,12 +8,17 @@ function getDirectChildren(parentInstance, parentName, childrenName) {
|
|
|
8
8
|
if (!isArray(nestedParents) || nestedParents.length <= 0) {
|
|
9
9
|
return children;
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const directChildren = [];
|
|
12
|
+
for (const child of children) {
|
|
13
|
+
for (const nestedParent of nestedParents) {
|
|
13
14
|
const nestedChildren = nestedParent.$children[childrenName];
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
if (isArray(nestedChildren) && nestedChildren.includes(child)) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
directChildren.push(child);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return directChildren;
|
|
17
22
|
}
|
|
18
23
|
function isDirectChild(parentInstance, parentName, childrenName, childInstance) {
|
|
19
24
|
return getDirectChildren(parentInstance, parentName, childrenName).includes(childInstance);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/helpers/getDirectChildren.ts"],
|
|
4
|
-
"sourcesContent": ["import { Base, BaseProps } from '../Base/index.js';\nimport { isArray } from '../utils/index.js';\n\n/**\n * Get direct children from a parent when working with nested components.\n *\n * @template {Base} T\n * @param {Base} parentInstance\n * @param {string} parentName\n * @param {string} childrenName\n * @returns {T[]}\n */\nexport function getDirectChildren<T extends Base = Base>(\n parentInstance: Base,\n parentName: string,\n childrenName: string,\n): T[] {\n const children = parentInstance.$children[childrenName] as Base<BaseProps>[];\n const nestedParents = parentInstance.$children[parentName] as Base<BaseProps>[];\n\n if (!isArray(children)) {\n return [];\n }\n\n if (!isArray(nestedParents) || nestedParents.length <= 0) {\n return children as T[];\n }\n\n
|
|
5
|
-
"mappings": "AACA,SAAS,eAAe;AAWjB,SAAS,kBACd,gBACA,YACA,cACK;AACL,QAAM,WAAW,eAAe,UAAU,YAAY;AACtD,QAAM,gBAAgB,eAAe,UAAU,UAAU;AAEzD,MAAI,CAAC,QAAQ,QAAQ,GAAG;AACtB,WAAO,CAAC;AAAA,EACV;AAEA,MAAI,CAAC,QAAQ,aAAa,KAAK,cAAc,UAAU,GAAG;AACxD,WAAO;AAAA,EACT;AAEA,
|
|
4
|
+
"sourcesContent": ["import { Base, BaseProps } from '../Base/index.js';\nimport { isArray } from '../utils/index.js';\n\n/**\n * Get direct children from a parent when working with nested components.\n *\n * @template {Base} T\n * @param {Base} parentInstance\n * @param {string} parentName\n * @param {string} childrenName\n * @returns {T[]}\n */\nexport function getDirectChildren<T extends Base = Base>(\n parentInstance: Base,\n parentName: string,\n childrenName: string,\n): T[] {\n const children = parentInstance.$children[childrenName] as Base<BaseProps>[];\n const nestedParents = parentInstance.$children[parentName] as Base<BaseProps>[];\n\n if (!isArray(children)) {\n return [];\n }\n\n if (!isArray(nestedParents) || nestedParents.length <= 0) {\n return children as T[];\n }\n\n const directChildren = [];\n\n for (const child of children) {\n for (const nestedParent of nestedParents) {\n const nestedChildren = nestedParent.$children[childrenName] as Base<BaseProps>[];\n\n if (isArray(nestedChildren) && nestedChildren.includes(child)) {\n continue;\n }\n\n directChildren.push(child);\n }\n }\n\n return directChildren;\n}\n\n/**\n * Test if a component instance is a direct child from the given component.\n *\n * @param {Base} parentInstance\n * @param {string} parentName\n * @param {string} childrenName\n * @param {Base} childInstance\n * @returns {boolean}\n */\nexport function isDirectChild(parentInstance, parentName, childrenName, childInstance) {\n return getDirectChildren(parentInstance, parentName, childrenName).includes(childInstance);\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,eAAe;AAWjB,SAAS,kBACd,gBACA,YACA,cACK;AACL,QAAM,WAAW,eAAe,UAAU,YAAY;AACtD,QAAM,gBAAgB,eAAe,UAAU,UAAU;AAEzD,MAAI,CAAC,QAAQ,QAAQ,GAAG;AACtB,WAAO,CAAC;AAAA,EACV;AAEA,MAAI,CAAC,QAAQ,aAAa,KAAK,cAAc,UAAU,GAAG;AACxD,WAAO;AAAA,EACT;AAEA,QAAM,iBAAiB,CAAC;AAExB,aAAW,SAAS,UAAU;AAC5B,eAAW,gBAAgB,eAAe;AACxC,YAAM,iBAAiB,aAAa,UAAU,YAAY;AAE1D,UAAI,QAAQ,cAAc,KAAK,eAAe,SAAS,KAAK,GAAG;AAC7D;AAAA,MACF;AAEA,qBAAe,KAAK,KAAK;AAAA,IAC3B;AAAA,EACF;AAEA,SAAO;AACT;AAWO,SAAS,cAAc,gBAAgB,YAAY,cAAc,eAAe;AACrF,SAAO,kBAAkB,gBAAgB,YAAY,YAAY,EAAE,SAAS,aAAa;AAC3F;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -12,8 +12,7 @@ function importWhenVisible(fn, nameOrSelectorOrElement, parent, observerOptions
|
|
|
12
12
|
}
|
|
13
13
|
}, observerOptions);
|
|
14
14
|
const elements = getTargetElements(nameOrSelectorOrElement, parent?.$el);
|
|
15
|
-
for (const element of elements)
|
|
16
|
-
observer.observe(element);
|
|
15
|
+
for (const element of elements) observer.observe(element);
|
|
17
16
|
});
|
|
18
17
|
}
|
|
19
18
|
export {
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/helpers/importWhenVisible.ts"],
|
|
4
4
|
"sourcesContent": ["import type { Base, BaseConstructor } from '../Base/index.js';\nimport { getTargetElements } from './utils.js';\nimport { getComponentResolver } from '../utils/index.js';\n\n/**\n * Import a component when it is visible.\n *\n * @template {BaseConstructor} T\n * @param {() => Promise<T|{default:T}>} fn\n * The import function.\n * @param {string|HTMLElement|HTMLElement[]} nameOrSelectorOrElement\n * The name or selector for the component.\n * @param {Base} [parent]\n * The parent component.\n * @param {IntersectionObserverInit} [observerOptions]\n * Options for the `IntersectionObserver` instance.\n * @returns {Promise<T>}\n */\nexport default function importWhenVisible<T extends BaseConstructor = BaseConstructor>(\n fn: () => Promise<T | { default: T }>,\n nameOrSelectorOrElement: string | HTMLElement | HTMLElement[],\n parent?: Base,\n observerOptions: IntersectionObserverInit = {},\n): Promise<T> {\n const resolver = getComponentResolver(fn);\n\n return new Promise((resolve) => {\n const observer = new IntersectionObserver((entries) => {\n const someEntriesAreVisible = entries.some((entry) => entry.isIntersecting);\n if (someEntriesAreVisible) {\n setTimeout(() => {\n resolver(resolve, () => observer.disconnect());\n }, 0);\n }\n }, observerOptions);\n\n const elements = getTargetElements(nameOrSelectorOrElement, parent?.$el);\n for (const element of elements) observer.observe(element);\n });\n}\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,yBAAyB;AAClC,SAAS,4BAA4B;AAgBtB,SAAR,kBACL,IACA,yBACA,QACA,kBAA4C,CAAC,GACjC;AACZ,QAAM,WAAW,qBAAqB,EAAE;AAExC,SAAO,IAAI,QAAQ,CAAC,YAAY;AAC9B,UAAM,WAAW,IAAI,qBAAqB,CAAC,YAAY;AACrD,YAAM,wBAAwB,QAAQ,KAAK,CAAC,UAAU,MAAM,cAAc;AAC1E,UAAI,uBAAuB;AACzB,mBAAW,MAAM;AACf,mBAAS,SAAS,MAAM,SAAS,WAAW,CAAC;AAAA,QAC/C,GAAG,CAAC;AAAA,MACN;AAAA,IACF,GAAG,eAAe;AAElB,UAAM,WAAW,kBAAkB,yBAAyB,QAAQ,GAAG;AACvE,eAAW,WAAW
|
|
5
|
+
"mappings": "AACA,SAAS,yBAAyB;AAClC,SAAS,4BAA4B;AAgBtB,SAAR,kBACL,IACA,yBACA,QACA,kBAA4C,CAAC,GACjC;AACZ,QAAM,WAAW,qBAAqB,EAAE;AAExC,SAAO,IAAI,QAAQ,CAAC,YAAY;AAC9B,UAAM,WAAW,IAAI,qBAAqB,CAAC,YAAY;AACrD,YAAM,wBAAwB,QAAQ,KAAK,CAAC,UAAU,MAAM,cAAc;AAC1E,UAAI,uBAAuB;AACzB,mBAAW,MAAM;AACf,mBAAS,SAAS,MAAM,SAAS,WAAW,CAAC;AAAA,QAC/C,GAAG,CAAC;AAAA,MACN;AAAA,IACF,GAAG,eAAe;AAElB,UAAM,WAAW,kBAAkB,yBAAyB,QAAQ,GAAG;AACvE,eAAW,WAAW,SAAU,UAAS,QAAQ,OAAO;AAAA,EAC1D,CAAC;AACH;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './Base/index.js';
|
|
2
|
+
export * from './Base/types.js';
|
|
2
3
|
export * from './decorators/index.js';
|
|
3
4
|
export * from './helpers/index.js';
|
|
4
5
|
export * from './services/index.js';
|
|
5
|
-
export type { BaseProps, BaseConstructor, BaseAsyncConstructor, BaseConfig } from './Base/index.js';
|
|
6
|
-
export type { BaseInterface, BaseDecorator } from './Base/types.js';
|
package/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
export * from "./Base/index.js";
|
|
2
|
+
export * from "./Base/types.js";
|
|
2
3
|
export * from "./decorators/index.js";
|
|
3
4
|
export * from "./helpers/index.js";
|
|
4
5
|
export * from "./services/index.js";
|
|
5
|
-
export {
|
|
6
|
-
Base
|
|
7
|
-
};
|
|
8
6
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../packages/js-toolkit/index.ts"],
|
|
4
|
-
"sourcesContent": ["export
|
|
5
|
-
"mappings": "AAAA,
|
|
4
|
+
"sourcesContent": ["export * from './Base/index.js';\nexport * from './Base/types.js';\nexport * from './decorators/index.js';\nexport * from './helpers/index.js';\nexport * from './services/index.js';\n"],
|
|
5
|
+
"mappings": "AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@studiometa/js-toolkit",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.10",
|
|
4
4
|
"description": "A set of useful little bits of JavaScript to boost your project! 🚀",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -23,13 +23,11 @@
|
|
|
23
23
|
"exports": {
|
|
24
24
|
".": {
|
|
25
25
|
"import": "./index.js",
|
|
26
|
-
"require": "./index.cjs",
|
|
27
26
|
"types": "./index.d.ts",
|
|
28
27
|
"default": "./index.js"
|
|
29
28
|
},
|
|
30
29
|
"./utils": {
|
|
31
30
|
"import": "./utils/index.js",
|
|
32
|
-
"require": "./utils/index.cjs",
|
|
33
31
|
"types": "./utils/index.d.ts",
|
|
34
32
|
"default": "./utils/index.js"
|
|
35
33
|
}
|
|
@@ -41,13 +39,7 @@
|
|
|
41
39
|
"toolkit"
|
|
42
40
|
],
|
|
43
41
|
"dependencies": {
|
|
44
|
-
"@motionone/easing": "^10.
|
|
42
|
+
"@motionone/easing": "^10.18.0",
|
|
45
43
|
"deepmerge": "^4.3.1"
|
|
46
|
-
},
|
|
47
|
-
"devDependencies": {
|
|
48
|
-
"vue": "^2.6.0"
|
|
49
|
-
},
|
|
50
|
-
"peerDependencies": {
|
|
51
|
-
"vue": "^2.6.0 || ^3.0"
|
|
52
44
|
}
|
|
53
45
|
}
|
package/services/index.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ export type KeyServiceInterface = ServiceInterface<KeyServiceProps>;
|
|
|
28
28
|
export type LoadServiceInterface = ServiceInterface<LoadServiceProps>;
|
|
29
29
|
export type PointerServiceInterface = ServiceInterface<PointerServiceProps>;
|
|
30
30
|
export type RafServiceInterface = ServiceInterface<RafServiceProps>;
|
|
31
|
-
export type ResizeServiceInterface = ServiceInterface<ResizeServiceProps
|
|
31
|
+
export type ResizeServiceInterface<T extends Record<string, string> = Record<string, string>> = ServiceInterface<ResizeServiceProps<T>>;
|
|
32
32
|
export type ScrollServiceInterface = ServiceInterface<ScrollServiceProps>;
|
|
33
33
|
export type { DragServiceProps, KeyServiceProps, LoadServiceProps, PointerServiceProps, RafServiceProps, ResizeServiceProps, ScrollServiceProps, };
|
|
34
34
|
export { default as useDrag } from './drag.js';
|
package/services/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/services/index.ts"],
|
|
4
|
-
"sourcesContent": ["import type { DragServiceProps } from './drag.js';\nimport type { KeyServiceProps } from './key.js';\nimport type { LoadServiceProps } from './load.js';\nimport type { PointerServiceProps } from './pointer.js';\nimport type { RafServiceProps } from './raf.js';\nimport type { ResizeServiceProps } from './resize.js';\nimport type { ScrollServiceProps } from './scroll.js';\n\nexport interface ServiceInterface<T> {\n /**\n * Remove a function from the resize service by its key.\n */\n remove(key: string): void;\n /**\n * Add a callback to the service. The callback will receive the current service props as parameter.\n */\n add(key: string, callback: (props: T) => void): void;\n /**\n * Test if the service has alreaydy a callback for the given key.\n */\n has(key: string): boolean;\n /**\n * Get the service's props.\n */\n props(): T;\n}\n\nexport type DragServiceInterface = ServiceInterface<DragServiceProps>;\nexport type KeyServiceInterface = ServiceInterface<KeyServiceProps>;\nexport type LoadServiceInterface = ServiceInterface<LoadServiceProps>;\nexport type PointerServiceInterface = ServiceInterface<PointerServiceProps>;\nexport type RafServiceInterface = ServiceInterface<RafServiceProps>;\nexport type ResizeServiceInterface = ServiceInterface<ResizeServiceProps
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import type { DragServiceProps } from './drag.js';\nimport type { KeyServiceProps } from './key.js';\nimport type { LoadServiceProps } from './load.js';\nimport type { PointerServiceProps } from './pointer.js';\nimport type { RafServiceProps } from './raf.js';\nimport type { ResizeServiceProps } from './resize.js';\nimport type { ScrollServiceProps } from './scroll.js';\n\nexport interface ServiceInterface<T> {\n /**\n * Remove a function from the resize service by its key.\n */\n remove(key: string): void;\n /**\n * Add a callback to the service. The callback will receive the current service props as parameter.\n */\n add(key: string, callback: (props: T) => void): void;\n /**\n * Test if the service has alreaydy a callback for the given key.\n */\n has(key: string): boolean;\n /**\n * Get the service's props.\n */\n props(): T;\n}\n\nexport type DragServiceInterface = ServiceInterface<DragServiceProps>;\nexport type KeyServiceInterface = ServiceInterface<KeyServiceProps>;\nexport type LoadServiceInterface = ServiceInterface<LoadServiceProps>;\nexport type PointerServiceInterface = ServiceInterface<PointerServiceProps>;\nexport type RafServiceInterface = ServiceInterface<RafServiceProps>;\nexport type ResizeServiceInterface<T extends Record<string, string> = Record<string, string>> =\n ServiceInterface<ResizeServiceProps<T>>;\nexport type ScrollServiceInterface = ServiceInterface<ScrollServiceProps>;\n\nexport type {\n DragServiceProps,\n KeyServiceProps,\n LoadServiceProps,\n PointerServiceProps,\n RafServiceProps,\n ResizeServiceProps,\n ScrollServiceProps,\n};\n\nexport { default as useDrag } from './drag.js';\nexport { default as useKey } from './key.js';\nexport { default as useLoad } from './load.js';\nexport { default as usePointer } from './pointer.js';\nexport { default as useRaf } from './raf.js';\nexport { default as useResize } from './resize.js';\nexport { default as useScroll } from './scroll.js';\nexport { useService } from './service.js';\n"],
|
|
5
|
+
"mappings": "AA8CA,SAAoB,WAAXA,gBAA0B;AACnC,SAAoB,WAAXA,gBAAyB;AAClC,SAAoB,WAAXA,gBAA0B;AACnC,SAAoB,WAAXA,gBAA6B;AACtC,SAAoB,WAAXA,gBAAyB;AAClC,SAAoB,WAAXA,gBAA4B;AACrC,SAAoB,WAAXA,gBAA4B;AACrC,SAAS,kBAAkB;",
|
|
6
6
|
"names": ["default"]
|
|
7
7
|
}
|
package/services/pointer.d.ts
CHANGED
package/services/pointer.js
CHANGED
|
@@ -112,10 +112,10 @@ function createPointerService(target) {
|
|
|
112
112
|
}
|
|
113
113
|
const instances = /* @__PURE__ */ new Map();
|
|
114
114
|
function usePointer(target) {
|
|
115
|
-
if (!instances.has(target)) {
|
|
116
|
-
instances.set(target, createPointerService(target));
|
|
115
|
+
if (!instances.has(target ?? window)) {
|
|
116
|
+
instances.set(target ?? window, createPointerService(target));
|
|
117
117
|
}
|
|
118
|
-
return instances.get(target);
|
|
118
|
+
return instances.get(target ?? window);
|
|
119
119
|
}
|
|
120
120
|
export {
|
|
121
121
|
usePointer as default
|
package/services/pointer.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/services/pointer.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport type { ServiceInterface } from './index.js';\nimport { isDefined } from '../utils/index.js';\n\nexport type PointerService = ServiceInterface<PointerServiceProps>;\n\nexport interface PointerServiceProps {\n event: MouseEvent | TouchEvent;\n isDown: boolean;\n x: number;\n y: number;\n changed: { x: boolean; y: boolean };\n last: { x: number; y: number };\n delta: { x: number; y: number };\n progress: { x: number; y: number };\n max: { x: number; y: number };\n}\n\n/**\n * Test if an event is an instance of TouchEvent.\n */\nfunction isTouchEvent(event: TouchEvent | MouseEvent): boolean {\n return typeof TouchEvent !== 'undefined' && event instanceof TouchEvent;\n}\n\nconst events = ['mousemove', 'touchmove', 'mousedown', 'touchstart', 'mouseup', 'touchend'];\n\n/**\n * Get pointer service.\n */\nfunction createPointerService(target: HTMLElement | undefined): PointerService {\n function getTargetSize() {\n return isDefined(target)\n ? target.getBoundingClientRect()\n : {\n width: window.innerWidth,\n height: window.innerHeight,\n x: 0,\n y: 0,\n };\n }\n\n /**\n * Update the pointer positions.\n */\n function updateProps(event: MouseEvent | TouchEvent): PointerServiceProps {\n props.event = event;\n const yLast = props.y;\n const xLast = props.x;\n\n const targetSize = getTargetSize();\n\n // Check pointer Y\n // We either get data from a touch event `event.touches[0].clientY` or from\n // a mouse event `event.clientY`.\n let y = isTouchEvent(event)\n ? (event as TouchEvent).touches[0]?.clientY\n : (event as MouseEvent).clientY;\n y -= targetSize.y;\n if (y !== props.y) {\n props.y = y;\n }\n\n // Check pointer X\n // We either get data from a touch event `event.touches[0].clientX` or from\n // a mouse event `event.clientX`.\n let x = isTouchEvent(event)\n ? (event as TouchEvent).touches[0]?.clientX\n : (event as MouseEvent).clientX;\n x -= targetSize.x;\n if (x !== props.x) {\n props.x = x;\n }\n\n props.changed.x = props.x !== xLast;\n props.changed.y = props.y !== yLast;\n\n props.last.x = xLast;\n props.last.y = yLast;\n\n props.delta.x = props.x - xLast;\n props.delta.y = props.y - yLast;\n\n props.max.x = targetSize.width;\n props.max.y = targetSize.height;\n\n props.progress.x = props.x / props.max.x;\n props.progress.y = props.y / props.max.y;\n\n return props;\n }\n\n /**\n * Handle events.\n *\n * @todo handle scroll as well\n */\n function handleEvent(event: MouseEvent | TouchEvent) {\n // eslint-disable-next-line default-case\n switch (event.type) {\n case 'mouseenter':\n case 'mousemove':\n case 'touchmove':\n trigger(updateProps(event));\n break;\n case 'mousedown':\n case 'touchstart':\n props.isDown = true;\n trigger(props);\n break;\n case 'mouseup':\n case 'touchend':\n props.isDown = false;\n trigger(props);\n break;\n }\n }\n\n const targetSize = getTargetSize();\n const { add, remove, has, trigger, props } = useService({\n props: {\n event: null,\n isDown: false,\n x: targetSize.x / 2,\n y: targetSize.y / 2,\n changed: {\n x: false,\n y: false,\n },\n last: {\n x: targetSize.x / 2,\n y: targetSize.y / 2,\n },\n delta: {\n x: 0,\n y: 0,\n },\n progress: {\n x: 0.5,\n y: 0.5,\n },\n max: {\n x: targetSize.width,\n y: targetSize.height,\n },\n } as PointerServiceProps,\n init() {\n document.documentElement.addEventListener('mouseenter', handleEvent, {\n once: true,\n capture: true,\n });\n\n const options = { passive: true, capture: true };\n for (const event of events) {\n document.addEventListener(event, handleEvent, options);\n }\n },\n kill() {\n for (const event of events) {\n document.removeEventListener(event, handleEvent);\n }\n },\n });\n\n return {\n add,\n remove,\n has,\n props: () => props,\n };\n}\n\nconst instances = new Map<HTMLElement | Window, PointerService>();\n\n/**\n * Use the pointer service.\n */\nexport default function usePointer(target
|
|
5
|
-
"mappings": "AACA,SAAS,kBAAkB;AAE3B,SAAS,iBAAiB;AAmB1B,SAAS,aAAa,OAAyC;AAC7D,SAAO,OAAO,eAAe,eAAe,iBAAiB;AAC/D;AAEA,MAAM,SAAS,CAAC,aAAa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define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport type { ServiceInterface } from './index.js';\nimport { isDefined } from '../utils/index.js';\n\nexport type PointerService = ServiceInterface<PointerServiceProps>;\n\nexport interface PointerServiceProps {\n event: MouseEvent | TouchEvent;\n isDown: boolean;\n x: number;\n y: number;\n changed: { x: boolean; y: boolean };\n last: { x: number; y: number };\n delta: { x: number; y: number };\n progress: { x: number; y: number };\n max: { x: number; y: number };\n}\n\n/**\n * Test if an event is an instance of TouchEvent.\n */\nfunction isTouchEvent(event: TouchEvent | MouseEvent): boolean {\n return typeof TouchEvent !== 'undefined' && event instanceof TouchEvent;\n}\n\nconst events = ['mousemove', 'touchmove', 'mousedown', 'touchstart', 'mouseup', 'touchend'];\n\n/**\n * Get pointer service.\n */\nfunction createPointerService(target: HTMLElement | undefined): PointerService {\n function getTargetSize() {\n return isDefined(target)\n ? target.getBoundingClientRect()\n : {\n width: window.innerWidth,\n height: window.innerHeight,\n x: 0,\n y: 0,\n };\n }\n\n /**\n * Update the pointer positions.\n */\n function updateProps(event: MouseEvent | TouchEvent): PointerServiceProps {\n props.event = event;\n const yLast = props.y;\n const xLast = props.x;\n\n const targetSize = getTargetSize();\n\n // Check pointer Y\n // We either get data from a touch event `event.touches[0].clientY` or from\n // a mouse event `event.clientY`.\n let y = isTouchEvent(event)\n ? (event as TouchEvent).touches[0]?.clientY\n : (event as MouseEvent).clientY;\n y -= targetSize.y;\n if (y !== props.y) {\n props.y = y;\n }\n\n // Check pointer X\n // We either get data from a touch event `event.touches[0].clientX` or from\n // a mouse event `event.clientX`.\n let x = isTouchEvent(event)\n ? (event as TouchEvent).touches[0]?.clientX\n : (event as MouseEvent).clientX;\n x -= targetSize.x;\n if (x !== props.x) {\n props.x = x;\n }\n\n props.changed.x = props.x !== xLast;\n props.changed.y = props.y !== yLast;\n\n props.last.x = xLast;\n props.last.y = yLast;\n\n props.delta.x = props.x - xLast;\n props.delta.y = props.y - yLast;\n\n props.max.x = targetSize.width;\n props.max.y = targetSize.height;\n\n props.progress.x = props.x / props.max.x;\n props.progress.y = props.y / props.max.y;\n\n return props;\n }\n\n /**\n * Handle events.\n *\n * @todo handle scroll as well\n */\n function handleEvent(event: MouseEvent | TouchEvent) {\n // eslint-disable-next-line default-case\n switch (event.type) {\n case 'mouseenter':\n case 'mousemove':\n case 'touchmove':\n trigger(updateProps(event));\n break;\n case 'mousedown':\n case 'touchstart':\n props.isDown = true;\n trigger(props);\n break;\n case 'mouseup':\n case 'touchend':\n props.isDown = false;\n trigger(props);\n break;\n }\n }\n\n const targetSize = getTargetSize();\n const { add, remove, has, trigger, props } = useService({\n props: {\n event: null,\n isDown: false,\n x: targetSize.x / 2,\n y: targetSize.y / 2,\n changed: {\n x: false,\n y: false,\n },\n last: {\n x: targetSize.x / 2,\n y: targetSize.y / 2,\n },\n delta: {\n x: 0,\n y: 0,\n },\n progress: {\n x: 0.5,\n y: 0.5,\n },\n max: {\n x: targetSize.width,\n y: targetSize.height,\n },\n } as PointerServiceProps,\n init() {\n document.documentElement.addEventListener('mouseenter', handleEvent, {\n once: true,\n capture: true,\n });\n\n const options = { passive: true, capture: true };\n for (const event of events) {\n document.addEventListener(event, handleEvent, options);\n }\n },\n kill() {\n for (const event of events) {\n document.removeEventListener(event, handleEvent);\n }\n },\n });\n\n return {\n add,\n remove,\n has,\n props: () => props,\n };\n}\n\nconst instances = new Map<HTMLElement | Window, PointerService>();\n\n/**\n * Use the pointer service.\n */\nexport default function usePointer(target?: HTMLElement): PointerService {\n if (!instances.has(target ?? window)) {\n instances.set(target ?? window, createPointerService(target));\n }\n return instances.get(target ?? window) as PointerService;\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,kBAAkB;AAE3B,SAAS,iBAAiB;AAmB1B,SAAS,aAAa,OAAyC;AAC7D,SAAO,OAAO,eAAe,eAAe,iBAAiB;AAC/D;AAEA,MAAM,SAAS,CAAC,aAAa,aAAa,aAAa,cAAc,WAAW,UAAU;AAK1F,SAAS,qBAAq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B,MAAM,CAAC;AAAA,EAC9D;AACA,SAAO,UAAU,IAAI,UAAU,MAAM;AACvC;",
|
|
6
6
|
"names": ["targetSize"]
|
|
7
7
|
}
|
package/services/raf.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { useService } from "./service.js";
|
|
2
|
-
import { getRaf as getRequestAnimationFrame } from "../utils/nextFrame.js";
|
|
3
2
|
import { useScheduler } from "../utils/scheduler.js";
|
|
4
3
|
import { isFunction } from "../utils/is.js";
|
|
5
4
|
const scheduler = useScheduler(["update", "render"]);
|
|
6
5
|
function createRafService() {
|
|
7
6
|
let isTicking = false;
|
|
8
|
-
const RAF = getRequestAnimationFrame();
|
|
9
7
|
function trigger(props2) {
|
|
10
8
|
for (const callback of callbacks.values()) {
|
|
11
9
|
scheduler.update(() => {
|
|
@@ -24,7 +22,7 @@ function createRafService() {
|
|
|
24
22
|
if (!isTicking) {
|
|
25
23
|
return;
|
|
26
24
|
}
|
|
27
|
-
|
|
25
|
+
requestAnimationFrame(loop);
|
|
28
26
|
}
|
|
29
27
|
const { add, remove, has, props, callbacks } = useService({
|
|
30
28
|
props: {
|
|
@@ -32,7 +30,7 @@ function createRafService() {
|
|
|
32
30
|
},
|
|
33
31
|
init() {
|
|
34
32
|
isTicking = true;
|
|
35
|
-
|
|
33
|
+
requestAnimationFrame(loop);
|
|
36
34
|
},
|
|
37
35
|
kill() {
|
|
38
36
|
isTicking = false;
|
package/services/raf.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/services/raf.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport {
|
|
5
|
-
"mappings": "AACA,SAAS,kBAAkB;AAC3B,SAAS,
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport { useScheduler } from '../utils/scheduler.js';\nimport { isFunction } from '../utils/is.js';\nimport type { ServiceInterface } from './index.js';\n\nconst scheduler = useScheduler(['update', 'render']);\n\nexport type RafService = ServiceInterface<RafServiceProps>;\n\nexport interface RafServiceProps {\n time: DOMHighResTimeStamp;\n}\n\n/**\n * Create raf service.\n */\nfunction createRafService(): RafService {\n let isTicking = false;\n\n /**\n * Trigger callbacks.\n */\n function trigger(props: RafServiceProps) {\n for (const callback of callbacks.values()) {\n scheduler.update(() => {\n const render = callback(props) as (() => unknown) | void;\n if (isFunction(render)) {\n scheduler.render(() => {\n // @ts-ignore\n render(props);\n });\n }\n });\n }\n }\n\n /**\n * Loop.\n */\n function loop() {\n // eslint-disable-next-line no-use-before-define\n props.time = performance.now();\n trigger(props);\n\n if (!isTicking) {\n return;\n }\n\n requestAnimationFrame(loop);\n }\n\n const { add, remove, has, props, callbacks } = useService({\n props: {\n time: performance.now(),\n } as RafServiceProps,\n init() {\n isTicking = true;\n requestAnimationFrame(loop);\n },\n kill() {\n isTicking = false;\n },\n });\n\n return {\n add,\n remove,\n has,\n props: () => props,\n };\n}\n\nlet raf;\n\n/**\n * Use the RequestAnimationFrame (raf) service.\n */\nexport default function useRaf(): RafService {\n if (!raf) {\n raf = createRafService();\n }\n\n return raf;\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,kBAAkB;AAC3B,SAAS,oBAAoB;AAC7B,SAAS,kBAAkB;AAG3B,MAAM,YAAY,aAAa,CAAC,UAAU,QAAQ,CAAC;AAWnD,SAAS,mBAA+B;AACtC,MAAI,YAAY;AAKhB,WAAS,QAAQA,QAAwB;AACvC,eAAW,YAAY,UAAU,OAAO,GAAG;AACzC,gBAAU,OAAO,MAAM;AACrB,cAAM,SAAS,SAASA,MAAK;AAC7B,YAAI,WAAW,MAAM,GAAG;AACtB,oBAAU,OAAO,MAAM;AAErB,mBAAOA,MAAK;AAAA,UACd,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAKA,WAAS,OAAO;AAEd,UAAM,OAAO,YAAY,IAAI;AAC7B,YAAQ,KAAK;AAEb,QAAI,CAAC,WAAW;AACd;AAAA,IACF;AAEA,0BAAsB,IAAI;AAAA,EAC5B;AAEA,QAAM,EAAE,KAAK,QAAQ,KAAK,OAAO,UAAU,IAAI,WAAW;AAAA,IACxD,OAAO;AAAA,MACL,MAAM,YAAY,IAAI;AAAA,IACxB;AAAA,IACA,OAAO;AACL,kBAAY;AACZ,4BAAsB,IAAI;AAAA,IAC5B;AAAA,IACA,OAAO;AACL,kBAAY;AAAA,IACd;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,OAAO,MAAM;AAAA,EACf;AACF;AAEA,IAAI;AAKW,SAAR,SAAsC;AAC3C,MAAI,CAAC,KAAK;AACR,UAAM,iBAAiB;AAAA,EACzB;AAEA,SAAO;AACT;",
|
|
6
6
|
"names": ["props"]
|
|
7
7
|
}
|
package/services/resize.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export interface ResizeServiceProps {
|
|
1
|
+
import type { Features } from '../Base/features.js';
|
|
2
|
+
import type { ResizeServiceInterface } from './index.js';
|
|
3
|
+
export interface ResizeServiceProps<U extends Features['breakpoints'] = Features['breakpoints']> {
|
|
4
4
|
width: number;
|
|
5
5
|
height: number;
|
|
6
6
|
ratio: number;
|
|
7
7
|
orientation: 'square' | 'landscape' | 'portrait';
|
|
8
|
-
breakpoint
|
|
9
|
-
breakpoints
|
|
8
|
+
breakpoint: string;
|
|
9
|
+
breakpoints: string[];
|
|
10
|
+
activeBreakpoints: Record<keyof U, boolean>;
|
|
10
11
|
}
|
|
11
12
|
/**
|
|
12
13
|
* Use the resize service.
|
|
13
14
|
*/
|
|
14
|
-
export default function useResize():
|
|
15
|
+
export default function useResize<T extends Features['breakpoints'] = Features['breakpoints']>(breakpoints?: T): ResizeServiceInterface<T>;
|
package/services/resize.js
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
1
|
import { useService } from "./service.js";
|
|
2
|
+
import { features } from "../Base/features.js";
|
|
2
3
|
import debounce from "../utils/debounce.js";
|
|
3
|
-
|
|
4
|
-
let breakpoints = [];
|
|
5
|
-
function getBreakpointElement() {
|
|
6
|
-
if (!breakpointElement) {
|
|
7
|
-
breakpointElement = document.querySelector("[data-breakpoint]");
|
|
8
|
-
}
|
|
9
|
-
return breakpointElement;
|
|
10
|
-
}
|
|
11
|
-
function getBreakpoint() {
|
|
12
|
-
return getBreakpointElement() ? window.getComputedStyle(getBreakpointElement(), "::before").getPropertyValue("content").replaceAll('"', "") : void 0;
|
|
13
|
-
}
|
|
14
|
-
function getBreakpoints() {
|
|
15
|
-
if (!getBreakpointElement() || breakpoints.length) {
|
|
16
|
-
return breakpoints;
|
|
17
|
-
}
|
|
18
|
-
breakpoints = window.getComputedStyle(getBreakpointElement(), "::after").getPropertyValue("content").replaceAll('"', "").split(",");
|
|
19
|
-
return breakpoints;
|
|
20
|
-
}
|
|
21
|
-
function createResizeService() {
|
|
4
|
+
function createResizeService(breakpoints) {
|
|
22
5
|
function updateProps() {
|
|
23
6
|
props.width = window.innerWidth;
|
|
24
7
|
props.height = window.innerHeight;
|
|
@@ -42,10 +25,18 @@ function createResizeService() {
|
|
|
42
25
|
ratio: window.innerWidth / window.innerHeight,
|
|
43
26
|
orientation: "square",
|
|
44
27
|
get breakpoint() {
|
|
45
|
-
return
|
|
28
|
+
return Object.keys(this.activeBreakpoints).toReversed().find((name) => this.activeBreakpoints[name]);
|
|
46
29
|
},
|
|
47
30
|
get breakpoints() {
|
|
48
|
-
return
|
|
31
|
+
return Object.keys(this.activeBreakpoints);
|
|
32
|
+
},
|
|
33
|
+
get activeBreakpoints() {
|
|
34
|
+
return Object.fromEntries(
|
|
35
|
+
Object.entries(breakpoints ?? features.get("breakpoints")).map(([name, breakpoint]) => [
|
|
36
|
+
name,
|
|
37
|
+
window.matchMedia(`(min-width: ${breakpoint})`).matches
|
|
38
|
+
])
|
|
39
|
+
);
|
|
49
40
|
}
|
|
50
41
|
},
|
|
51
42
|
init() {
|
|
@@ -62,12 +53,13 @@ function createResizeService() {
|
|
|
62
53
|
props: () => props
|
|
63
54
|
};
|
|
64
55
|
}
|
|
65
|
-
|
|
66
|
-
function useResize() {
|
|
67
|
-
|
|
68
|
-
|
|
56
|
+
const instances = /* @__PURE__ */ new Map();
|
|
57
|
+
function useResize(breakpoints) {
|
|
58
|
+
const key = JSON.stringify(breakpoints);
|
|
59
|
+
if (!instances.has(key)) {
|
|
60
|
+
instances.set(key, createResizeService(breakpoints));
|
|
69
61
|
}
|
|
70
|
-
return
|
|
62
|
+
return instances.get(key);
|
|
71
63
|
}
|
|
72
64
|
export {
|
|
73
65
|
useResize as default
|
package/services/resize.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/services/resize.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport debounce from '../utils/debounce.js';\nimport type {
|
|
5
|
-
"mappings": "AACA,SAAS,kBAAkB;AAC3B,
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport { features } from '../Base/features.js';\nimport type { Features } from '../Base/features.js';\nimport debounce from '../utils/debounce.js';\nimport type { ResizeServiceInterface } from './index.js';\n\nexport interface ResizeServiceProps<U extends Features['breakpoints'] = Features['breakpoints']> {\n width: number;\n height: number;\n ratio: number;\n orientation: 'square' | 'landscape' | 'portrait';\n breakpoint: string;\n breakpoints: string[];\n activeBreakpoints: Record<keyof U, boolean>;\n}\n\n/**\n * Get resize service.\n */\nfunction createResizeService<T extends Features['breakpoints'] = Features['breakpoints']>(\n breakpoints?: T,\n): ResizeServiceInterface<T> {\n /**\n * Update props.\n */\n function updateProps(): ResizeServiceProps<T> {\n props.width = window.innerWidth;\n props.height = window.innerHeight;\n props.ratio = window.innerWidth / window.innerHeight;\n props.orientation = 'square';\n\n if (props.ratio > 1) {\n props.orientation = 'landscape';\n }\n\n if (props.ratio < 1) {\n props.orientation = 'portrait';\n }\n\n return props;\n }\n\n const onResize = debounce(() => {\n trigger(updateProps());\n });\n\n const { add, remove, has, trigger, props } = useService({\n props: {\n width: window.innerWidth,\n height: window.innerHeight,\n ratio: window.innerWidth / window.innerHeight,\n orientation: 'square',\n get breakpoint() {\n return Object.keys(this.activeBreakpoints)\n .toReversed()\n .find((name) => this.activeBreakpoints[name]);\n },\n get breakpoints() {\n return Object.keys(this.activeBreakpoints);\n },\n get activeBreakpoints() {\n return Object.fromEntries(\n Object.entries(breakpoints ?? features.get('breakpoints')).map(([name, breakpoint]) => [\n name,\n window.matchMedia(`(min-width: ${breakpoint})`).matches,\n ]),\n ) as Record<keyof T, boolean>;\n },\n } as ResizeServiceProps<T>,\n init() {\n window.addEventListener('resize', onResize);\n },\n kill() {\n window.removeEventListener('resize', onResize);\n },\n });\n\n return {\n add,\n remove,\n has,\n props: () => props,\n };\n}\n\nconst instances: Map<string, ResizeServiceInterface | undefined> = new Map();\n\n/**\n * Use the resize service.\n */\nexport default function useResize<T extends Features['breakpoints'] = Features['breakpoints']>(\n breakpoints?: T,\n): ResizeServiceInterface<T> {\n const key = JSON.stringify(breakpoints);\n\n if (!instances.has(key)) {\n instances.set(key, createResizeService(breakpoints));\n }\n\n return (instances as Map<string, ResizeServiceInterface<T>>).get(key);\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB;AAEzB,OAAO,cAAc;AAgBrB,SAAS,oBACP,aAC2B;AAI3B,WAAS,cAAqC;AAC5C,UAAM,QAAQ,OAAO;AACrB,UAAM,SAAS,OAAO;AACtB,UAAM,QAAQ,OAAO,aAAa,OAAO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,GAAG;AACvB,cAAU,IAAI,KAAK,oBAAoB,WAAW,CAAC;AAAA,EACrD;AAEA,SAAQ,UAAqD,IAAI,GAAG;AACtE;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/services/scroll.d.ts
CHANGED
|
@@ -7,26 +7,40 @@ export interface ScrollServiceProps {
|
|
|
7
7
|
x: boolean;
|
|
8
8
|
y: boolean;
|
|
9
9
|
};
|
|
10
|
+
changedX: boolean;
|
|
11
|
+
changedY: boolean;
|
|
10
12
|
last: {
|
|
11
13
|
x: number;
|
|
12
14
|
y: number;
|
|
13
15
|
};
|
|
16
|
+
lastX: number;
|
|
17
|
+
lastY: number;
|
|
14
18
|
delta: {
|
|
15
19
|
x: number;
|
|
16
20
|
y: number;
|
|
17
21
|
};
|
|
22
|
+
deltaX: number;
|
|
23
|
+
deltaY: number;
|
|
18
24
|
progress: {
|
|
19
25
|
x: number;
|
|
20
26
|
y: number;
|
|
21
27
|
};
|
|
28
|
+
progressX: number;
|
|
29
|
+
progressY: number;
|
|
22
30
|
max: {
|
|
23
31
|
x: number;
|
|
24
32
|
y: number;
|
|
25
33
|
};
|
|
34
|
+
maxX: number;
|
|
35
|
+
maxY: number;
|
|
26
36
|
direction: {
|
|
27
37
|
x: 'LEFT' | 'RIGHT' | 'NONE';
|
|
28
38
|
y: 'UP' | 'DOWN' | 'NONE';
|
|
29
39
|
};
|
|
40
|
+
isUp: boolean;
|
|
41
|
+
isRight: boolean;
|
|
42
|
+
isDown: boolean;
|
|
43
|
+
isLeft: boolean;
|
|
30
44
|
}
|
|
31
45
|
/**
|
|
32
46
|
* Use the scroll service.
|
package/services/scroll.js
CHANGED
|
@@ -4,24 +4,28 @@ function createScrollService() {
|
|
|
4
4
|
function updateProps() {
|
|
5
5
|
const yLast = props.y;
|
|
6
6
|
const xLast = props.x;
|
|
7
|
-
if (window.
|
|
8
|
-
props.y = window.
|
|
7
|
+
if (window.scrollY !== props.y) {
|
|
8
|
+
props.y = window.scrollY;
|
|
9
9
|
}
|
|
10
|
-
if (window.
|
|
11
|
-
props.x = window.
|
|
10
|
+
if (window.scrollX !== props.x) {
|
|
11
|
+
props.x = window.scrollX;
|
|
12
12
|
}
|
|
13
|
-
props.changed.x = props.x !== xLast;
|
|
14
|
-
props.changed.y = props.y !== yLast;
|
|
15
|
-
props.last.x = xLast;
|
|
16
|
-
props.last.y = yLast;
|
|
17
|
-
props.delta.x = props.x - xLast;
|
|
18
|
-
props.delta.y = props.y - yLast;
|
|
19
|
-
props.max.x = (document.scrollingElement || document.body).scrollWidth - window.innerWidth;
|
|
20
|
-
props.max.y = (document.scrollingElement || document.body).scrollHeight - window.innerHeight;
|
|
21
|
-
props.progress.x = props.max.x === 0 ? 1 : props.x / props.max.x;
|
|
22
|
-
props.progress.y = props.max.y === 0 ? 1 : props.y / props.max.y;
|
|
23
|
-
props.
|
|
24
|
-
props.
|
|
13
|
+
props.changed.x = props.changedX = props.x !== xLast;
|
|
14
|
+
props.changed.y = props.changedY = props.y !== yLast;
|
|
15
|
+
props.last.x = props.lastX = xLast;
|
|
16
|
+
props.last.y = props.lastY = yLast;
|
|
17
|
+
props.delta.x = props.deltaX = props.x - xLast;
|
|
18
|
+
props.delta.y = props.deltaY = props.y - yLast;
|
|
19
|
+
props.max.x = props.maxX = (document.scrollingElement || document.body).scrollWidth - window.innerWidth;
|
|
20
|
+
props.max.y = props.maxY = (document.scrollingElement || document.body).scrollHeight - window.innerHeight;
|
|
21
|
+
props.progress.x = props.progressX = props.max.x === 0 ? 1 : props.x / props.max.x;
|
|
22
|
+
props.progress.y = props.progressY = props.max.y === 0 ? 1 : props.y / props.max.y;
|
|
23
|
+
props.isUp = props.y < yLast;
|
|
24
|
+
props.isRight = props.x > xLast;
|
|
25
|
+
props.isDown = props.y > yLast;
|
|
26
|
+
props.isLeft = props.x < xLast;
|
|
27
|
+
props.direction.x = props.isRight ? "RIGHT" : props.isLeft ? "LEFT" : "NONE";
|
|
28
|
+
props.direction.y = props.isDown ? "DOWN" : props.isUp ? "UP" : "NONE";
|
|
25
29
|
return props;
|
|
26
30
|
}
|
|
27
31
|
const onScrollDebounced = debounce(() => {
|
|
@@ -33,32 +37,46 @@ function createScrollService() {
|
|
|
33
37
|
}
|
|
34
38
|
const { add, remove, has, props, trigger } = useService({
|
|
35
39
|
props: {
|
|
36
|
-
x: window.
|
|
37
|
-
y: window.
|
|
40
|
+
x: window.scrollX,
|
|
41
|
+
y: window.scrollY,
|
|
38
42
|
changed: {
|
|
39
43
|
x: false,
|
|
40
44
|
y: false
|
|
41
45
|
},
|
|
46
|
+
changedX: false,
|
|
47
|
+
changedY: false,
|
|
42
48
|
last: {
|
|
43
|
-
x: window.
|
|
44
|
-
y: window.
|
|
49
|
+
x: window.scrollX,
|
|
50
|
+
y: window.scrollY
|
|
45
51
|
},
|
|
52
|
+
lastX: window.scrollX,
|
|
53
|
+
lastY: window.scrollY,
|
|
46
54
|
delta: {
|
|
47
55
|
x: 0,
|
|
48
56
|
y: 0
|
|
49
57
|
},
|
|
58
|
+
deltaX: 0,
|
|
59
|
+
deltaY: 0,
|
|
50
60
|
max: {
|
|
51
61
|
x: (document.scrollingElement || document.body).scrollWidth - window.innerWidth,
|
|
52
62
|
y: (document.scrollingElement || document.body).scrollHeight - window.innerHeight
|
|
53
63
|
},
|
|
64
|
+
maxX: (document.scrollingElement || document.body).scrollWidth - window.innerWidth,
|
|
65
|
+
maxY: (document.scrollingElement || document.body).scrollHeight - window.innerHeight,
|
|
54
66
|
progress: {
|
|
55
67
|
x: 0,
|
|
56
68
|
y: 0
|
|
57
69
|
},
|
|
70
|
+
progressX: 0,
|
|
71
|
+
progressY: 0,
|
|
58
72
|
direction: {
|
|
59
73
|
x: "NONE",
|
|
60
74
|
y: "NONE"
|
|
61
|
-
}
|
|
75
|
+
},
|
|
76
|
+
isUp: false,
|
|
77
|
+
isRight: false,
|
|
78
|
+
isDown: false,
|
|
79
|
+
isLeft: false
|
|
62
80
|
},
|
|
63
81
|
init() {
|
|
64
82
|
document.addEventListener("scroll", onScroll, { passive: true, capture: true });
|