@studiometa/js-toolkit 3.0.0-alpha.9 → 3.0.0-beta.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/Base.js +2 -2
- package/Base/Base.js.map +2 -2
- package/Base/features.d.ts +1 -0
- package/Base/features.js +1 -0
- package/Base/features.js.map +2 -2
- package/Base/managers/EventsManager.js +2 -4
- package/Base/managers/EventsManager.js.map +2 -2
- package/Base/managers/OptionsManager.d.ts +2 -2
- package/Base/managers/OptionsManager.js +8 -8
- package/Base/managers/OptionsManager.js.map +2 -2
- package/Base/managers/RefsManager.js.map +2 -2
- package/Base/utils.d.ts +4 -0
- package/Base/utils.js +7 -3
- package/Base/utils.js.map +2 -2
- package/decorators/withBreakpointObserver.js +14 -9
- package/decorators/withBreakpointObserver.js.map +2 -2
- package/decorators/withDrag.d.ts +1 -1
- package/decorators/withDrag.js.map +1 -1
- package/decorators/withRelativePointer.d.ts +1 -1
- package/decorators/withRelativePointer.js.map +1 -1
- package/decorators/withResponsiveOptions.d.ts +3 -1
- package/decorators/withResponsiveOptions.js +22 -2
- package/decorators/withResponsiveOptions.js.map +2 -2
- package/helpers/createApp.js +5 -1
- package/helpers/createApp.js.map +2 -2
- package/package.json +1 -1
- package/services/AbstractService.d.ts +98 -0
- package/services/AbstractService.js +115 -0
- package/services/AbstractService.js.map +7 -0
- package/services/DragService.d.ts +124 -0
- package/services/DragService.js +217 -0
- package/services/DragService.js.map +7 -0
- package/services/KeyService.d.ts +29 -0
- package/services/KeyService.js +48 -0
- package/services/KeyService.js.map +7 -0
- package/services/LoadService.d.ts +15 -0
- package/services/LoadService.js +19 -0
- package/services/LoadService.js.map +7 -0
- package/services/PointerService.d.ts +56 -0
- package/services/PointerService.js +129 -0
- package/services/PointerService.js.map +7 -0
- package/services/RafService.d.ts +19 -0
- package/services/RafService.js +45 -0
- package/services/RafService.js.map +7 -0
- package/services/ResizeService.d.ts +29 -0
- package/services/ResizeService.js +66 -0
- package/services/ResizeService.js.map +7 -0
- package/services/{scroll.d.ts → ScrollService.d.ts} +14 -3
- package/services/ScrollService.js +90 -0
- package/services/ScrollService.js.map +7 -0
- package/services/index.d.ts +8 -41
- package/services/index.js +8 -18
- package/services/index.js.map +3 -3
- package/services/utils.d.ts +14 -0
- package/services/utils.js +11 -0
- package/services/utils.js.map +7 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/index.js.map +2 -2
- package/utils/loadElement.d.ts +42 -0
- package/utils/loadElement.js +33 -0
- package/utils/loadElement.js.map +7 -0
- package/utils/tween.js +1 -1
- package/utils/tween.js.map +2 -2
- package/services/drag.d.ts +0 -75
- package/services/drag.js +0 -199
- package/services/drag.js.map +0 -7
- package/services/key.d.ts +0 -21
- package/services/key.js +0 -65
- package/services/key.js.map +0 -7
- package/services/load.d.ts +0 -11
- package/services/load.js +0 -38
- package/services/load.js.map +0 -7
- package/services/pointer.d.ts +0 -32
- package/services/pointer.js +0 -123
- package/services/pointer.js.map +0 -7
- package/services/raf.d.ts +0 -9
- package/services/raf.js +0 -56
- package/services/raf.js.map +0 -7
- package/services/resize.d.ts +0 -15
- package/services/resize.js +0 -67
- package/services/resize.js.map +0 -7
- package/services/scroll.js +0 -105
- package/services/scroll.js.map +0 -7
- package/services/service.d.ts +0 -16
- package/services/service.js +0 -47
- package/services/service.js.map +0 -7
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/decorators/withDrag.ts"],
|
|
4
|
-
"sourcesContent": ["import type { BaseDecorator, BaseInterface } from '../Base/types.js';\nimport type { Base, BaseProps, BaseConfig } from '../Base/index.js';\nimport type { DragServiceOptions, DragServiceProps } from '../services/
|
|
4
|
+
"sourcesContent": ["import type { BaseDecorator, BaseInterface } from '../Base/types.js';\nimport type { Base, BaseProps, BaseConfig } from '../Base/index.js';\nimport type { DragServiceOptions, DragServiceProps } from '../services/index.js';\nimport { useDrag } from '../services/index.js';\n\nexport type DragDecoratorOptions = DragServiceOptions & {\n target?: (this: Base, instance: Base) => HTMLElement;\n};\n\nexport interface WithDragInterface extends BaseInterface {\n dragged?(props: DragServiceProps): void;\n}\n\n/**\n * Add dragging capabilities to a component.\n */\nexport function withDrag<S extends Base>(\n BaseClass: typeof Base,\n { target = (instance) => instance.$el, ...options }: DragDecoratorOptions = {},\n): BaseDecorator<BaseInterface, S> {\n /**\n * Class.\n */\n class WithDrag<T extends BaseProps = BaseProps> extends BaseClass<T & WithDragInterface> {\n /**\n * Config.\n */\n static config: BaseConfig = {\n name: `${BaseClass.config.name}WithDrag`,\n emits: ['dragged'],\n };\n\n /**\n * Class constructor.\n */\n constructor(element: HTMLElement) {\n super(element);\n\n this.$on('mounted', () => {\n this.$services.register(\n 'dragged',\n useDrag.bind(undefined, target.call(this, this), options),\n );\n this.$services.enable('dragged');\n });\n\n this.$on('destroyed', () => {\n this.$services.disable('dragged');\n this.$services.unregister('dragged');\n });\n }\n }\n\n // @ts-ignore\n return WithDrag;\n}\n"],
|
|
5
5
|
"mappings": "AAGA,SAAS,eAAe;AAajB,SAAS,SACd,WACA,EAAE,SAAS,CAAC,aAAa,SAAS,KAAK,GAAG,QAAQ,IAA0B,CAAC,GAC5C;AAAA,EAIjC,MAAM,iBAAkD,UAAiC;AAAA;AAAA;AAAA;AAAA,IAIvF,OAAO,SAAqB;AAAA,MAC1B,MAAM,GAAG,UAAU,OAAO,IAAI;AAAA,MAC9B,OAAO,CAAC,SAAS;AAAA,IACnB;AAAA;AAAA;AAAA;AAAA,IAKA,YAAY,SAAsB;AAChC,YAAM,OAAO;AAEb,WAAK,IAAI,WAAW,MAAM;AACxB,aAAK,UAAU;AAAA,UACb;AAAA,UACA,QAAQ,KAAK,QAAW,OAAO,KAAK,MAAM,IAAI,GAAG,OAAO;AAAA,QAC1D;AACA,aAAK,UAAU,OAAO,SAAS;AAAA,MACjC,CAAC;AAED,WAAK,IAAI,aAAa,MAAM;AAC1B,aAAK,UAAU,QAAQ,SAAS;AAChC,aAAK,UAAU,WAAW,SAAS;AAAA,MACrC,CAAC;AAAA,IACH;AAAA,EACF;AAGA,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { BaseDecorator, BaseInterface } from '../Base/types.js';
|
|
2
2
|
import type { Base } from '../Base/index.js';
|
|
3
|
-
import type { PointerServiceProps } from '../services/
|
|
3
|
+
import type { PointerServiceProps } from '../services/index.js';
|
|
4
4
|
export type RelativePointerDecoratorOptions = {
|
|
5
5
|
target?: (this: Base, instance: Base) => HTMLElement;
|
|
6
6
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/decorators/withRelativePointer.ts"],
|
|
4
|
-
"sourcesContent": ["import type { BaseDecorator, BaseInterface } from '../Base/types.js';\nimport type { Base, BaseProps, BaseConfig } from '../Base/index.js';\nimport type { PointerServiceProps } from '../services/
|
|
4
|
+
"sourcesContent": ["import type { BaseDecorator, BaseInterface } from '../Base/types.js';\nimport type { Base, BaseProps, BaseConfig } from '../Base/index.js';\nimport type { PointerServiceProps } from '../services/index.js';\nimport { usePointer } from '../services/index.js';\n\nexport type RelativePointerDecoratorOptions = {\n target?: (this: Base, instance: Base) => HTMLElement;\n};\n\nexport interface RelativePointerInterface extends BaseInterface {\n movedrelative?(props: PointerServiceProps): void;\n}\n\n/**\n * Add dragging capabilities to a component.\n */\nexport function withRelativePointer<S extends Base>(\n BaseClass: typeof Base,\n { target = (instance) => instance.$el }: RelativePointerDecoratorOptions = {},\n): BaseDecorator<RelativePointerInterface, S> {\n /**\n * Class.\n */\n class WithRelativePointer<T extends BaseProps = BaseProps> extends BaseClass<T> {\n /**\n * Config.\n */\n static config: BaseConfig = {\n name: `${BaseClass.config.name}WithRelativePointer`,\n emits: ['movedrelative'],\n };\n\n /**\n * Class constructor.\n */\n constructor(element: HTMLElement) {\n super(element);\n\n this.$on('mounted', () => {\n this.$services.register(\n 'movedrelative',\n usePointer.bind(undefined, target.call(this, this)),\n );\n this.$services.enable('movedrelative');\n });\n\n this.$on('destroyed', () => {\n this.$services.disable('movedrelative');\n this.$services.unregister('movedrelative');\n });\n }\n }\n\n // @ts-ignore\n return WithRelativePointer;\n}\n"],
|
|
5
5
|
"mappings": "AAGA,SAAS,kBAAkB;AAapB,SAAS,oBACd,WACA,EAAE,SAAS,CAAC,aAAa,SAAS,IAAI,IAAqC,CAAC,GAChC;AAAA,EAI5C,MAAM,4BAA6D,UAAa;AAAA;AAAA;AAAA;AAAA,IAI9E,OAAO,SAAqB;AAAA,MAC1B,MAAM,GAAG,UAAU,OAAO,IAAI;AAAA,MAC9B,OAAO,CAAC,eAAe;AAAA,IACzB;AAAA;AAAA;AAAA;AAAA,IAKA,YAAY,SAAsB;AAChC,YAAM,OAAO;AAEb,WAAK,IAAI,WAAW,MAAM;AACxB,aAAK,UAAU;AAAA,UACb;AAAA,UACA,WAAW,KAAK,QAAW,OAAO,KAAK,MAAM,IAAI,CAAC;AAAA,QACpD;AACA,aAAK,UAAU,OAAO,eAAe;AAAA,MACvC,CAAC;AAED,WAAK,IAAI,aAAa,MAAM;AAC1B,aAAK,UAAU,QAAQ,eAAe;AACtC,aAAK,UAAU,WAAW,eAAe;AAAA,MAC3C,CAAC;AAAA,IACH;AAAA,EACF;AAGA,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -3,4 +3,6 @@ import type { Base } from '../Base/index.js';
|
|
|
3
3
|
/**
|
|
4
4
|
* Extends the configuration of an existing class.
|
|
5
5
|
*/
|
|
6
|
-
export declare function withResponsiveOptions<S extends Base>(BaseClass: typeof Base
|
|
6
|
+
export declare function withResponsiveOptions<S extends Base>(BaseClass: typeof Base, { responsiveOptions }?: {
|
|
7
|
+
responsiveOptions?: any[];
|
|
8
|
+
}): BaseDecorator<BaseInterface, S>;
|
|
@@ -1,6 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
ResponsiveOptionsManager
|
|
3
|
+
} from "../Base/managers/ResponsiveOptionsManager.js";
|
|
4
|
+
import { isDefined, isObject } from "../utils/index.js";
|
|
5
|
+
function withResponsiveOptions(BaseClass, { responsiveOptions = [] } = {}) {
|
|
3
6
|
class WithResponsiveOptions extends BaseClass {
|
|
7
|
+
/**
|
|
8
|
+
* Configure responsive options.
|
|
9
|
+
*/
|
|
10
|
+
get __config() {
|
|
11
|
+
const config = super.__config;
|
|
12
|
+
const options = config.options;
|
|
13
|
+
for (const option of responsiveOptions) {
|
|
14
|
+
if (isDefined(options[option])) {
|
|
15
|
+
const oldOption = options[option];
|
|
16
|
+
options[option] = {
|
|
17
|
+
type: isObject(oldOption) ? oldOption.type : oldOption,
|
|
18
|
+
responsive: true
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return config;
|
|
23
|
+
}
|
|
4
24
|
/**
|
|
5
25
|
* Get managers.
|
|
6
26
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/decorators/withResponsiveOptions.ts"],
|
|
4
|
-
"sourcesContent": ["import type { BaseInterface, BaseDecorator } from '../Base/types.js';\nimport type { Base, BaseProps, Managers } from '../Base/index.js';\nimport {
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import type { BaseInterface, BaseDecorator } from '../Base/types.js';\nimport type { Base, BaseProps, Managers } from '../Base/index.js';\nimport type { OptionObject, OptionType } from '../Base/managers/OptionsManager.js';\nimport {\n ResponsiveOptionsManager,\n ResponsiveOptionObject,\n} from '../Base/managers/ResponsiveOptionsManager.js';\nimport { isDefined, isObject } from '../utils/index.js';\n\n/**\n * Extends the configuration of an existing class.\n */\nexport function withResponsiveOptions<S extends Base>(\n BaseClass: typeof Base,\n { responsiveOptions = [] } = {},\n): BaseDecorator<BaseInterface, S> {\n /**\n * Class.\n */\n class WithResponsiveOptions<T extends BaseProps = BaseProps> extends BaseClass<T> {\n /**\n * Configure responsive options.\n */\n get __config() {\n const config = super.__config;\n const options = config.options;\n\n for (const option of responsiveOptions) {\n if (isDefined(options[option])) {\n const oldOption = options[option];\n options[option] = {\n type: isObject(oldOption)\n ? (oldOption as OptionObject).type\n : (oldOption as OptionType),\n responsive: true,\n } as ResponsiveOptionObject;\n }\n }\n\n return config;\n }\n\n /**\n * Get managers.\n */\n get __managers(): Managers & { OptionsManager: typeof ResponsiveOptionsManager } {\n return {\n ...super.__managers,\n OptionsManager: ResponsiveOptionsManager,\n };\n }\n }\n\n // @ts-ignore\n return WithResponsiveOptions;\n}\n"],
|
|
5
|
+
"mappings": "AAGA;AAAA,EACE;AAAA,OAEK;AACP,SAAS,WAAW,gBAAgB;AAK7B,SAAS,sBACd,WACA,EAAE,oBAAoB,CAAC,EAAE,IAAI,CAAC,GACG;AAAA,EAIjC,MAAM,8BAA+D,UAAa;AAAA;AAAA;AAAA;AAAA,IAIhF,IAAI,WAAW;AACb,YAAM,SAAS,MAAM;AACrB,YAAM,UAAU,OAAO;AAEvB,iBAAW,UAAU,mBAAmB;AACtC,YAAI,UAAU,QAAQ,MAAM,CAAC,GAAG;AAC9B,gBAAM,YAAY,QAAQ,MAAM;AAChC,kBAAQ,MAAM,IAAI;AAAA,YAChB,MAAM,SAAS,SAAS,IACnB,UAA2B,OAC3B;AAAA,YACL,YAAY;AAAA,UACd;AAAA,QACF;AAAA,MACF;AAEA,aAAO;AAAA,IACT;AAAA;AAAA;AAAA;AAAA,IAKA,IAAI,aAA6E;AAC/E,aAAO;AAAA,QACL,GAAG,MAAM;AAAA,QACT,gBAAgB;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AAGA,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/helpers/createApp.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { features } from "../Base/features.js";
|
|
2
|
-
import { isBoolean, isObject } from "../utils/index.js";
|
|
2
|
+
import { isBoolean, isObject, isString } from "../utils/index.js";
|
|
3
3
|
function createApp(App, options = {}) {
|
|
4
4
|
let app;
|
|
5
5
|
const {
|
|
6
6
|
root = document.body,
|
|
7
7
|
breakpoints = null,
|
|
8
8
|
blocking = null,
|
|
9
|
+
prefix = null,
|
|
9
10
|
attributes = null
|
|
10
11
|
} = options instanceof HTMLElement ? { root: options } : options;
|
|
11
12
|
if (isObject(breakpoints)) {
|
|
@@ -14,6 +15,9 @@ function createApp(App, options = {}) {
|
|
|
14
15
|
if (isBoolean(blocking)) {
|
|
15
16
|
features.set("blocking", blocking);
|
|
16
17
|
}
|
|
18
|
+
if (isString(prefix)) {
|
|
19
|
+
features.set("prefix", prefix);
|
|
20
|
+
}
|
|
17
21
|
if (isObject(attributes)) {
|
|
18
22
|
features.set("attributes", attributes);
|
|
19
23
|
}
|
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 { 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 =
|
|
5
|
-
"mappings": "AAEA,SAAS,gBAAgB;AACzB,SAAS,WAAW,gBAAgB;
|
|
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, isString } 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 prefix = 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 (isString(prefix)) {\n features.set('prefix', prefix);\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,UAAU,gBAAgB;AAU/B,SAAR,UACL,KACA,UAA0C,CAAC,GACf;AAC5B,MAAI;AACJ,QAAM;AAAA,IACJ,OAAO,SAAS;AAAA,IAChB,cAAc;AAAA,IACd,WAAW;AAAA,IACX,SAAS;AAAA,IACT,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,MAAM,GAAG;AACpB,aAAS,IAAI,UAAU,MAAM;AAAA,EAC/B;AAEA,MAAI,SAAS,UAAU,GAAG;AACxB,aAAS,IAAI,cAAc,UAAU;AAAA,EACvC;AAEA,iBAAe,OAAO;AACpB,UAAM,IAAI,IAAI,IAAI;AAClB,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
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export interface ServiceInterface<T> {
|
|
2
|
+
/**
|
|
3
|
+
* Remove a function from the resize service by its key.
|
|
4
|
+
*/
|
|
5
|
+
remove(key: string): void;
|
|
6
|
+
/**
|
|
7
|
+
* Add a callback to the service. The callback will receive the current service props as parameter.
|
|
8
|
+
*/
|
|
9
|
+
add(key: string, callback: (props: T) => void): void;
|
|
10
|
+
/**
|
|
11
|
+
* Test if the service has alreaydy a callback for the given key.
|
|
12
|
+
*/
|
|
13
|
+
has(key: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Get the service's props.
|
|
16
|
+
*/
|
|
17
|
+
props(): T;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Service configuration of events to be attached to targets.
|
|
21
|
+
*/
|
|
22
|
+
export type ServiceConfig = [
|
|
23
|
+
EventTarget | ((instance: AbstractService) => EventTarget),
|
|
24
|
+
[
|
|
25
|
+
string,
|
|
26
|
+
AddEventListenerOptions?
|
|
27
|
+
][]
|
|
28
|
+
][];
|
|
29
|
+
/**
|
|
30
|
+
* AbstractService class.
|
|
31
|
+
*/
|
|
32
|
+
export declare class AbstractService<PropsType = any> {
|
|
33
|
+
/**
|
|
34
|
+
* Used to type `this.constructor` correctly
|
|
35
|
+
* @see https://github.com/microsoft/TypeScript/issues/3841#issuecomment-2381594311
|
|
36
|
+
*/
|
|
37
|
+
['constructor']: typeof AbstractService;
|
|
38
|
+
/**
|
|
39
|
+
* Service configuration.
|
|
40
|
+
*/
|
|
41
|
+
static config: ServiceConfig;
|
|
42
|
+
/**
|
|
43
|
+
* Cache for the created instances.
|
|
44
|
+
*/
|
|
45
|
+
static __instances: Map<any, any>;
|
|
46
|
+
/**
|
|
47
|
+
* Get a service instance as a singleton based on the given key.
|
|
48
|
+
*/
|
|
49
|
+
static getInstance<T extends ServiceInterface<any>>(key?: any, ...args: any[]): any;
|
|
50
|
+
/**
|
|
51
|
+
* Is the service active or not?
|
|
52
|
+
*/
|
|
53
|
+
__isInit: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Props for the service.
|
|
56
|
+
*/
|
|
57
|
+
props: {};
|
|
58
|
+
/**
|
|
59
|
+
* Holds all the callbacks that will be triggered.
|
|
60
|
+
*/
|
|
61
|
+
callbacks: Map<string, (props: PropsType) => unknown>;
|
|
62
|
+
/**
|
|
63
|
+
* Does the service has the given key?
|
|
64
|
+
*/
|
|
65
|
+
has(key: string): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Get a service callback by its key.
|
|
68
|
+
*/
|
|
69
|
+
get(key: string): (props: PropsType) => unknown;
|
|
70
|
+
/**
|
|
71
|
+
* Add a callback to the service.
|
|
72
|
+
*/
|
|
73
|
+
add(key: string, callback: (props: PropsType) => unknown): void;
|
|
74
|
+
/**
|
|
75
|
+
* Remove a callback from the service by its key.
|
|
76
|
+
*/
|
|
77
|
+
remove(key: string): void;
|
|
78
|
+
/**
|
|
79
|
+
* Trigger all service callbacks with service props.
|
|
80
|
+
*/
|
|
81
|
+
trigger(props: PropsType): void;
|
|
82
|
+
/**
|
|
83
|
+
* Implements the EventListenerObject interface.
|
|
84
|
+
*/
|
|
85
|
+
handleEvent(event: Event): void;
|
|
86
|
+
/**
|
|
87
|
+
* Add or remove event listeners based on the static `config` property.
|
|
88
|
+
*/
|
|
89
|
+
__manageEvents(mode: 'add' | 'remove'): void;
|
|
90
|
+
/**
|
|
91
|
+
* Triggered when the service is initialized.
|
|
92
|
+
*/
|
|
93
|
+
init(): void;
|
|
94
|
+
/**
|
|
95
|
+
* Triggered when the service is killed.
|
|
96
|
+
*/
|
|
97
|
+
kill(): void;
|
|
98
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { isFunction } from "../utils/index.js";
|
|
2
|
+
class AbstractService {
|
|
3
|
+
/**
|
|
4
|
+
* Service configuration.
|
|
5
|
+
*/
|
|
6
|
+
static config = [];
|
|
7
|
+
/**
|
|
8
|
+
* Cache for the created instances.
|
|
9
|
+
*/
|
|
10
|
+
static __instances = /* @__PURE__ */ new Map();
|
|
11
|
+
/**
|
|
12
|
+
* Get a service instance as a singleton based on the given key.
|
|
13
|
+
*/
|
|
14
|
+
static getInstance(key = this, ...args) {
|
|
15
|
+
if (!this.__instances.has(key)) {
|
|
16
|
+
const instance = new this(...args);
|
|
17
|
+
this.__instances.set(key, {
|
|
18
|
+
add: (key2, callback) => instance.add(key2, callback),
|
|
19
|
+
remove: (key2) => instance.remove(key2),
|
|
20
|
+
has: (key2) => instance.has(key2),
|
|
21
|
+
props: () => instance.props
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return this.__instances.get(key);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Is the service active or not?
|
|
28
|
+
*/
|
|
29
|
+
__isInit = false;
|
|
30
|
+
/**
|
|
31
|
+
* Props for the service.
|
|
32
|
+
*/
|
|
33
|
+
props = {};
|
|
34
|
+
/**
|
|
35
|
+
* Holds all the callbacks that will be triggered.
|
|
36
|
+
*/
|
|
37
|
+
callbacks = /* @__PURE__ */ new Map();
|
|
38
|
+
/**
|
|
39
|
+
* Does the service has the given key?
|
|
40
|
+
*/
|
|
41
|
+
has(key) {
|
|
42
|
+
return this.callbacks.has(key);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get a service callback by its key.
|
|
46
|
+
*/
|
|
47
|
+
get(key) {
|
|
48
|
+
return this.callbacks.get(key);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Add a callback to the service.
|
|
52
|
+
*/
|
|
53
|
+
add(key, callback) {
|
|
54
|
+
if (this.has(key)) {
|
|
55
|
+
console.warn(`The key \`${key}\` has already been added.`);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (this.callbacks.size === 0 && !this.__isInit) {
|
|
59
|
+
this.init();
|
|
60
|
+
this.__isInit = true;
|
|
61
|
+
}
|
|
62
|
+
this.callbacks.set(key, callback);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Remove a callback from the service by its key.
|
|
66
|
+
*/
|
|
67
|
+
remove(key) {
|
|
68
|
+
this.callbacks.delete(key);
|
|
69
|
+
if (this.callbacks.size === 0 && this.__isInit) {
|
|
70
|
+
this.kill();
|
|
71
|
+
this.__isInit = false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Trigger all service callbacks with service props.
|
|
76
|
+
*/
|
|
77
|
+
trigger(props) {
|
|
78
|
+
for (const callback of this.callbacks.values()) {
|
|
79
|
+
callback(props);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/* v8 ignore next 6 */
|
|
83
|
+
/**
|
|
84
|
+
* Implements the EventListenerObject interface.
|
|
85
|
+
*/
|
|
86
|
+
handleEvent(event) {
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Add or remove event listeners based on the static `config` property.
|
|
90
|
+
*/
|
|
91
|
+
__manageEvents(mode) {
|
|
92
|
+
for (const [target, events] of this.constructor.config) {
|
|
93
|
+
const resolvedTarget = isFunction(target) ? target(this) : target;
|
|
94
|
+
for (const [type, options] of events) {
|
|
95
|
+
resolvedTarget[`${mode}EventListener`](type, this, options);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Triggered when the service is initialized.
|
|
101
|
+
*/
|
|
102
|
+
init() {
|
|
103
|
+
this.__manageEvents("add");
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Triggered when the service is killed.
|
|
107
|
+
*/
|
|
108
|
+
kill() {
|
|
109
|
+
this.__manageEvents("remove");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export {
|
|
113
|
+
AbstractService
|
|
114
|
+
};
|
|
115
|
+
//# sourceMappingURL=AbstractService.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../packages/js-toolkit/services/AbstractService.ts"],
|
|
4
|
+
"sourcesContent": ["import { isFunction } from '../utils/index.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\n/**\n * Service configuration of events to be attached to targets.\n */\nexport type ServiceConfig = [\n EventTarget | ((instance: AbstractService) => EventTarget),\n [string, AddEventListenerOptions?][],\n][];\n\n/**\n * AbstractService class.\n */\nexport class AbstractService<PropsType = any> {\n /**\n * Used to type `this.constructor` correctly\n * @see https://github.com/microsoft/TypeScript/issues/3841#issuecomment-2381594311\n */\n declare ['constructor']: typeof AbstractService;\n\n /**\n * Service configuration.\n */\n static config: ServiceConfig = [];\n\n /**\n * Cache for the created instances.\n */\n static __instances = new Map();\n\n /**\n * Get a service instance as a singleton based on the given key.\n */\n static getInstance<T extends ServiceInterface<any>>(key: any = this, ...args: any[]) {\n if (!this.__instances.has(key)) {\n // @ts-ignore\n const instance = new this(...args);\n this.__instances.set(key, {\n add: (key, callback) => instance.add(key, callback),\n remove: (key) => instance.remove(key),\n has: (key) => instance.has(key),\n props: () => instance.props,\n } as T);\n }\n\n return this.__instances.get(key);\n }\n\n /**\n * Is the service active or not?\n */\n __isInit = false;\n\n /**\n * Props for the service.\n */\n props = {};\n\n /**\n * Holds all the callbacks that will be triggered.\n */\n callbacks: Map<string, (props: PropsType) => unknown> = new Map();\n\n /**\n * Does the service has the given key?\n */\n has(key: string): boolean {\n return this.callbacks.has(key);\n }\n\n /**\n * Get a service callback by its key.\n */\n get(key: string): (props: PropsType) => unknown {\n return this.callbacks.get(key);\n }\n\n /**\n * Add a callback to the service.\n */\n add(key: string, callback: (props: PropsType) => unknown) {\n if (this.has(key)) {\n console.warn(`The key \\`${key}\\` has already been added.`);\n return;\n }\n\n // Initialize the service when we add the first callback\n if (this.callbacks.size === 0 && !this.__isInit) {\n this.init();\n this.__isInit = true;\n }\n\n this.callbacks.set(key, callback);\n }\n\n /**\n * Remove a callback from the service by its key.\n */\n remove(key: string) {\n this.callbacks.delete(key);\n\n // Kill the service when we remove the last callback\n if (this.callbacks.size === 0 && this.__isInit) {\n this.kill();\n this.__isInit = false;\n }\n }\n\n /**\n * Trigger all service callbacks with service props.\n */\n trigger(props: PropsType) {\n for (const callback of this.callbacks.values()) {\n callback(props);\n }\n }\n\n /* v8 ignore next 6 */\n /**\n * Implements the EventListenerObject interface.\n */\n handleEvent(event: Event) {\n // Should be implemented.\n }\n\n /**\n * Add or remove event listeners based on the static `config` property.\n */\n __manageEvents(mode: 'add' | 'remove') {\n for (const [target, events] of this.constructor.config) {\n const resolvedTarget = isFunction(target) ? target(this) : target;\n for (const [type, options] of events) {\n resolvedTarget[`${mode}EventListener`](type, this, options);\n }\n }\n }\n\n /**\n * Triggered when the service is initialized.\n */\n init() {\n this.__manageEvents('add');\n }\n\n /**\n * Triggered when the service is killed.\n */\n kill() {\n this.__manageEvents('remove');\n }\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,kBAAkB;AAgCpB,MAAM,gBAAiC;AAAA;AAAA;AAAA;AAAA,EAU5C,OAAO,SAAwB,CAAC;AAAA;AAAA;AAAA;AAAA,EAKhC,OAAO,cAAc,oBAAI,IAAI;AAAA;AAAA;AAAA;AAAA,EAK7B,OAAO,YAA6C,MAAW,SAAS,MAAa;AACnF,QAAI,CAAC,KAAK,YAAY,IAAI,GAAG,GAAG;AAE9B,YAAM,WAAW,IAAI,KAAK,GAAG,IAAI;AACjC,WAAK,YAAY,IAAI,KAAK;AAAA,QACxB,KAAK,CAACA,MAAK,aAAa,SAAS,IAAIA,MAAK,QAAQ;AAAA,QAClD,QAAQ,CAACA,SAAQ,SAAS,OAAOA,IAAG;AAAA,QACpC,KAAK,CAACA,SAAQ,SAAS,IAAIA,IAAG;AAAA,QAC9B,OAAO,MAAM,SAAS;AAAA,MACxB,CAAM;AAAA,IACR;AAEA,WAAO,KAAK,YAAY,IAAI,GAAG;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA,EAKA,WAAW;AAAA;AAAA;AAAA;AAAA,EAKX,QAAQ,CAAC;AAAA;AAAA;AAAA;AAAA,EAKT,YAAwD,oBAAI,IAAI;AAAA;AAAA;AAAA;AAAA,EAKhE,IAAI,KAAsB;AACxB,WAAO,KAAK,UAAU,IAAI,GAAG;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,KAA4C;AAC9C,WAAO,KAAK,UAAU,IAAI,GAAG;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,KAAa,UAAyC;AACxD,QAAI,KAAK,IAAI,GAAG,GAAG;AACjB,cAAQ,KAAK,aAAa,GAAG,4BAA4B;AACzD;AAAA,IACF;AAGA,QAAI,KAAK,UAAU,SAAS,KAAK,CAAC,KAAK,UAAU;AAC/C,WAAK,KAAK;AACV,WAAK,WAAW;AAAA,IAClB;AAEA,SAAK,UAAU,IAAI,KAAK,QAAQ;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,KAAa;AAClB,SAAK,UAAU,OAAO,GAAG;AAGzB,QAAI,KAAK,UAAU,SAAS,KAAK,KAAK,UAAU;AAC9C,WAAK,KAAK;AACV,WAAK,WAAW;AAAA,IAClB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,QAAQ,OAAkB;AACxB,eAAW,YAAY,KAAK,UAAU,OAAO,GAAG;AAC9C,eAAS,KAAK;AAAA,IAChB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAAY,OAAc;AAAA,EAE1B;AAAA;AAAA;AAAA;AAAA,EAKA,eAAe,MAAwB;AACrC,eAAW,CAAC,QAAQ,MAAM,KAAK,KAAK,YAAY,QAAQ;AACtD,YAAM,iBAAiB,WAAW,MAAM,IAAI,OAAO,IAAI,IAAI;AAC3D,iBAAW,CAAC,MAAM,OAAO,KAAK,QAAQ;AACpC,uBAAe,GAAG,IAAI,eAAe,EAAE,MAAM,MAAM,OAAO;AAAA,MAC5D;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO;AACL,SAAK,eAAe,KAAK;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO;AACL,SAAK,eAAe,QAAQ;AAAA,EAC9B;AACF;",
|
|
6
|
+
"names": ["key"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { ServiceConfig, ServiceInterface } from './AbstractService.js';
|
|
2
|
+
import { AbstractService } from './AbstractService.js';
|
|
3
|
+
type DragLifecycle = 'start' | 'drag' | 'drop' | 'inertia' | 'stop';
|
|
4
|
+
export interface DragServiceProps {
|
|
5
|
+
/**
|
|
6
|
+
* The current mode of the dragging logic.
|
|
7
|
+
*/
|
|
8
|
+
mode: DragLifecycle;
|
|
9
|
+
/**
|
|
10
|
+
* The target element of the drag.
|
|
11
|
+
*/
|
|
12
|
+
target: HTMLElement;
|
|
13
|
+
/**
|
|
14
|
+
* Whether the user is currently grabbing the targeted element or not.
|
|
15
|
+
*/
|
|
16
|
+
isGrabbing: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Whether the drag currently has inertia or not.
|
|
19
|
+
*/
|
|
20
|
+
hasInertia: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* The current horizontal position in the viewport.
|
|
23
|
+
*/
|
|
24
|
+
x: number;
|
|
25
|
+
/**
|
|
26
|
+
* The current vertical position in the viewport.
|
|
27
|
+
*/
|
|
28
|
+
y: number;
|
|
29
|
+
/**
|
|
30
|
+
* The delta between the current position and the last.
|
|
31
|
+
*/
|
|
32
|
+
delta: {
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* The initial position where the dragging started.
|
|
38
|
+
*/
|
|
39
|
+
origin: {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* The distance from the current position to the origin.
|
|
45
|
+
*/
|
|
46
|
+
distance: {
|
|
47
|
+
x: number;
|
|
48
|
+
y: number;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The final position that will be reached at the end of the inertia.
|
|
52
|
+
*/
|
|
53
|
+
final: {
|
|
54
|
+
x: number;
|
|
55
|
+
y: number;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export type DragServiceInterface = ServiceInterface<DragServiceProps>;
|
|
59
|
+
export interface DragServiceOptions {
|
|
60
|
+
dampFactor?: number;
|
|
61
|
+
dragTreshold?: number;
|
|
62
|
+
}
|
|
63
|
+
export declare class DragService extends AbstractService<DragServiceProps> {
|
|
64
|
+
static MODES: Record<Uppercase<DragLifecycle>, DragLifecycle>;
|
|
65
|
+
static config: ServiceConfig;
|
|
66
|
+
id: string;
|
|
67
|
+
dampFactor: number;
|
|
68
|
+
dragTreshold: number;
|
|
69
|
+
previousEvent: TouchEvent | MouseEvent;
|
|
70
|
+
props: DragServiceProps;
|
|
71
|
+
/**
|
|
72
|
+
* Test if we should allow click on links and buttons.
|
|
73
|
+
*/
|
|
74
|
+
get shouldPreventClick(): boolean;
|
|
75
|
+
constructor(target: HTMLElement, { dampFactor, dragTreshold }?: DragServiceOptions);
|
|
76
|
+
/**
|
|
77
|
+
* Get the client value for the given axis.
|
|
78
|
+
*/
|
|
79
|
+
getEventPosition(event: MouseEvent | TouchEvent): {
|
|
80
|
+
x: number;
|
|
81
|
+
y: number;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Start the drag.
|
|
85
|
+
*
|
|
86
|
+
* @param {number} x The initial horizontal position.
|
|
87
|
+
* @param {number} y The initial vertical position.
|
|
88
|
+
*/
|
|
89
|
+
start(x: number, y: number): void;
|
|
90
|
+
/**
|
|
91
|
+
* Stop the drag, or drop.
|
|
92
|
+
*/
|
|
93
|
+
drop(): void;
|
|
94
|
+
/**
|
|
95
|
+
* Stop the drag.
|
|
96
|
+
*/
|
|
97
|
+
stop(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Raf service handler.
|
|
100
|
+
*/
|
|
101
|
+
rafHandler(): void;
|
|
102
|
+
/**
|
|
103
|
+
* Pointer service handler.
|
|
104
|
+
*/
|
|
105
|
+
drag(event: TouchEvent | MouseEvent): void;
|
|
106
|
+
/**
|
|
107
|
+
* Handle any event.
|
|
108
|
+
*/
|
|
109
|
+
handleEvent(event: MouseEvent | PointerEvent | DragEvent): void;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Use the drag service.
|
|
113
|
+
*
|
|
114
|
+
* ```js
|
|
115
|
+
* import { useDrag } from '@studiometa/js-toolkit/services';
|
|
116
|
+
* const element = document.querySelector('.draggable');
|
|
117
|
+
* const { add, remove, props } = useDrag(element, { dampFactor: 0.5 });
|
|
118
|
+
* add('key', (props) => console.log(props));
|
|
119
|
+
* remove('key');
|
|
120
|
+
* props();
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export declare function useDrag(target: HTMLElement, options: DragServiceOptions): DragServiceInterface;
|
|
124
|
+
export {};
|