@vnejs/module.components 0.0.12 → 0.0.13
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/dist/index.d.ts +5 -0
- package/dist/index.js +2 -0
- package/dist/modules/controller.d.ts +43 -0
- package/dist/modules/controller.js +92 -0
- package/dist/modules/view.d.ts +16 -0
- package/dist/modules/view.js +38 -0
- package/dist/types.d.ts +22 -0
- package/dist/types.js +11 -0
- package/dist/utils/components.d.ts +38 -0
- package/dist/utils/components.js +1 -0
- package/package.json +32 -6
- package/src/index.ts +15 -0
- package/src/modules/controller.ts +122 -0
- package/src/modules/view.ts +57 -0
- package/src/types.ts +33 -0
- package/src/utils/components.ts +48 -0
- package/tsconfig.json +9 -0
- package/index.d.ts +0 -58
- package/index.js +0 -2
- package/modules/controller.js +0 -89
- package/modules/view.js +0 -43
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "./types.js";
|
|
2
|
+
export type { ModuleControllerState } from "./modules/controller.js";
|
|
3
|
+
export type { ComponentInjectResult, ControlsPopPayload, ControlsPushPayload, MediaLoadPayload, ViewActionPayload, ViewRenderFunc, ViewUpdatePayload, ViewUpdateState, } from "./utils/components.js";
|
|
4
|
+
export { ModuleController } from "./modules/controller.js";
|
|
5
|
+
export { ModuleView } from "./modules/view.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Module, type ModuleGlobalStateRegistry } from "@vnejs/module";
|
|
2
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "../types.js";
|
|
3
|
+
import type { ViewActionPayload } from "../utils/components.js";
|
|
4
|
+
export type ModuleControllerState = {
|
|
5
|
+
isShow: boolean;
|
|
6
|
+
isForce: boolean;
|
|
7
|
+
currentItem?: number | null;
|
|
8
|
+
};
|
|
9
|
+
export declare abstract class ModuleController<TEvents extends ModuleComponentsEvents = ModuleComponentsEvents, TConstants extends ModuleComponentsConstants = ModuleComponentsConstants, TSettings extends ModuleComponentsSettings = ModuleComponentsSettings, TParams extends ModuleComponentsParams = ModuleComponentsParams, TState extends ModuleControllerState = ModuleControllerState> extends Module<TEvents, TConstants, TSettings, TParams, TState> {
|
|
10
|
+
abstract name: string;
|
|
11
|
+
currentItem: number | null;
|
|
12
|
+
maxCurrentItem: number;
|
|
13
|
+
beforeShow: ((args: ViewActionPayload) => void | Promise<void>) | null;
|
|
14
|
+
afterShow: ((args: ViewActionPayload) => void | Promise<void>) | null;
|
|
15
|
+
beforeHide: ((args: ViewActionPayload) => void | Promise<void>) | null;
|
|
16
|
+
afterHide: ((args: ViewActionPayload) => void | Promise<void>) | null;
|
|
17
|
+
bgName: string | null;
|
|
18
|
+
updateEvent: string;
|
|
19
|
+
controls: Record<string, () => void | Promise<void>> | null;
|
|
20
|
+
controlsIndex: number;
|
|
21
|
+
controlsCheckNext: boolean;
|
|
22
|
+
isOnlyLocalState: boolean;
|
|
23
|
+
onClose?: () => void;
|
|
24
|
+
onHide: ({ isForce, ...args }?: ViewActionPayload) => Promise<void>;
|
|
25
|
+
onShow: ({ isForce, onClose, ...args }?: ViewActionPayload) => Promise<void>;
|
|
26
|
+
onHideForce: (args?: ViewActionPayload) => Promise<void>;
|
|
27
|
+
onShowForce: (args?: ViewActionPayload) => Promise<void>;
|
|
28
|
+
onUpdateStateFromGlobalState: ({ [this.name]: state }?: ModuleGlobalStateRegistry) => Promise<unknown[]> | undefined;
|
|
29
|
+
setShow: (value: boolean, isForce?: boolean) => Promise<unknown[]> | undefined;
|
|
30
|
+
loadImageMedia: (name: string, mediaType: string, quality: string | number) => Promise<unknown> | undefined;
|
|
31
|
+
loadBgMedia: () => Promise<unknown> | undefined;
|
|
32
|
+
updateView: (isForce?: boolean, isFast?: boolean) => Promise<unknown[]> | undefined;
|
|
33
|
+
updateViewForce: () => Promise<unknown[]> | undefined;
|
|
34
|
+
updateViewFast: () => Promise<unknown[]> | undefined;
|
|
35
|
+
updateStateAndView: (state: Partial<TState>, isForce?: boolean, isFast?: boolean) => Promise<unknown[]> | undefined;
|
|
36
|
+
updateStateAndViewForce: (state: Partial<TState>) => Promise<unknown[]> | undefined;
|
|
37
|
+
updateStateAndViewFast: (state: Partial<TState>) => Promise<unknown[]> | undefined;
|
|
38
|
+
incCurrentItem: () => Promise<void>;
|
|
39
|
+
decCurrentItem: () => Promise<void>;
|
|
40
|
+
setCurrentItem: (value: number, defaultValue: number) => Promise<void>;
|
|
41
|
+
unsetCurrentItem: () => void;
|
|
42
|
+
getDefaultState: () => TState;
|
|
43
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
export class ModuleController extends Module {
|
|
3
|
+
currentItem = null;
|
|
4
|
+
maxCurrentItem = 0;
|
|
5
|
+
beforeShow = null;
|
|
6
|
+
afterShow = null;
|
|
7
|
+
beforeHide = null;
|
|
8
|
+
afterHide = null;
|
|
9
|
+
bgName = null;
|
|
10
|
+
updateEvent;
|
|
11
|
+
controls = null;
|
|
12
|
+
controlsIndex = 0;
|
|
13
|
+
controlsCheckNext = false;
|
|
14
|
+
isOnlyLocalState = true;
|
|
15
|
+
onClose;
|
|
16
|
+
onHide = async ({ isForce = false, ...args } = {}) => {
|
|
17
|
+
if (!this.state.isShow)
|
|
18
|
+
return;
|
|
19
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "hide" } });
|
|
20
|
+
if (this.beforeHide)
|
|
21
|
+
await this.beforeHide(args);
|
|
22
|
+
if (this.controls)
|
|
23
|
+
await this.emit(this.EVENTS.CONTROLS.POP, { key: this.name });
|
|
24
|
+
await this.setShow(false, Boolean(isForce || this.shared.viewForceAnimationSources.length));
|
|
25
|
+
if (this.onClose) {
|
|
26
|
+
this.onClose();
|
|
27
|
+
delete this.onClose;
|
|
28
|
+
}
|
|
29
|
+
if (this.afterHide)
|
|
30
|
+
await this.afterHide(args);
|
|
31
|
+
this.unsetCurrentItem();
|
|
32
|
+
};
|
|
33
|
+
onShow = async ({ isForce = false, onClose, ...args } = {}) => {
|
|
34
|
+
if (this.state.isShow)
|
|
35
|
+
return;
|
|
36
|
+
this.currentItem = null;
|
|
37
|
+
this.state = { ...this.state, currentItem: null };
|
|
38
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "show" } });
|
|
39
|
+
if (onClose)
|
|
40
|
+
this.onClose = onClose;
|
|
41
|
+
if (this.beforeShow)
|
|
42
|
+
await this.beforeShow(args);
|
|
43
|
+
await this.setShow(true, Boolean(isForce || this.shared.viewForceAnimationSources.length));
|
|
44
|
+
if (this.controls)
|
|
45
|
+
await this.emit(this.EVENTS.CONTROLS.PUSH, {
|
|
46
|
+
key: this.name,
|
|
47
|
+
controls: this.controls,
|
|
48
|
+
index: this.controlsIndex,
|
|
49
|
+
checkNext: this.controlsCheckNext,
|
|
50
|
+
});
|
|
51
|
+
if (this.afterShow)
|
|
52
|
+
await this.afterShow(args);
|
|
53
|
+
};
|
|
54
|
+
onHideForce = (args = {}) => this.onHide({ ...args, isForce: true });
|
|
55
|
+
onShowForce = (args = {}) => this.onShow({ ...args, isForce: true });
|
|
56
|
+
onUpdateStateFromGlobalState = ({ [this.name]: state } = {}) => this.updateStateAndViewForce(state);
|
|
57
|
+
setShow = (value, isForce = false) => this.updateStateAndView({ isShow: value }, isForce);
|
|
58
|
+
loadImageMedia = (name, mediaType, quality) => {
|
|
59
|
+
const [type, priority] = [this.CONST.MEDIA.TYPES.IMAGE, this.CONST.MEDIA.PRIORITIES.HIGH];
|
|
60
|
+
return this.emitOne(this.EVENTS.MEDIA.LOAD, { type, priority, quality, name, mediaType });
|
|
61
|
+
};
|
|
62
|
+
loadBgMedia = () => this.loadImageMedia(`bg/${this.bgName}`, "view", this.shared.settings[this.SETTINGS.LAYER.QUALITY]);
|
|
63
|
+
updateView = (isForce = false, isFast = false) => this.emit(this.updateEvent, { ...this.state, isForce, isFast });
|
|
64
|
+
updateViewForce = () => this.emit(this.updateEvent, { ...this.state, isForce: true });
|
|
65
|
+
updateViewFast = () => this.emit(this.updateEvent, { ...this.state, isFast: true });
|
|
66
|
+
updateStateAndView = (state, isForce = false, isFast = false) => {
|
|
67
|
+
this.updateState(state);
|
|
68
|
+
return this.updateView(isForce, isFast);
|
|
69
|
+
};
|
|
70
|
+
updateStateAndViewForce = (state) => this.updateStateAndView(state, true, false);
|
|
71
|
+
updateStateAndViewFast = (state) => this.updateStateAndView(state, false, true);
|
|
72
|
+
incCurrentItem = () => this.setCurrentItem((this.currentItem ?? 0) + 1, 0);
|
|
73
|
+
decCurrentItem = () => this.setCurrentItem((this.currentItem ?? 0) - 1, this.maxCurrentItem);
|
|
74
|
+
setCurrentItem = async (value, defaultValue) => {
|
|
75
|
+
if (this.currentItem === null) {
|
|
76
|
+
this.currentItem = defaultValue;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
this.currentItem = value;
|
|
80
|
+
if (this.currentItem > this.maxCurrentItem)
|
|
81
|
+
this.currentItem = 0;
|
|
82
|
+
if (this.currentItem < 0)
|
|
83
|
+
this.currentItem = this.maxCurrentItem;
|
|
84
|
+
}
|
|
85
|
+
this.updateStateAndViewFast({ currentItem: this.currentItem });
|
|
86
|
+
};
|
|
87
|
+
unsetCurrentItem = () => {
|
|
88
|
+
this.currentItem = null;
|
|
89
|
+
this.updateStateAndViewFast({ currentItem: this.currentItem });
|
|
90
|
+
};
|
|
91
|
+
getDefaultState = () => ({ isShow: false, isForce: false });
|
|
92
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Module, type ModuleInitResult } from "@vnejs/module";
|
|
2
|
+
import { type StoreApi } from "zustand/vanilla";
|
|
3
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "../types.js";
|
|
4
|
+
import type { ViewRenderFunc, ViewUpdateState } from "../utils/components.js";
|
|
5
|
+
export declare abstract class ModuleView<TEvents extends ModuleComponentsEvents = ModuleComponentsEvents, TConstants extends ModuleComponentsConstants = ModuleComponentsConstants, TSettings extends ModuleComponentsSettings = ModuleComponentsSettings, TParams extends ModuleComponentsParams = ModuleComponentsParams, TState extends ViewUpdateState = ViewUpdateState> extends Module<TEvents, TConstants, TSettings, TParams, TState> {
|
|
6
|
+
abstract name: string;
|
|
7
|
+
animationTime: number;
|
|
8
|
+
renderFunc: ViewRenderFunc | null;
|
|
9
|
+
updateEvent: string;
|
|
10
|
+
updateHandler: ((state?: TState) => Promise<void>) | null;
|
|
11
|
+
locLabel: string | null;
|
|
12
|
+
store: StoreApi<TState>;
|
|
13
|
+
subscribe: () => void;
|
|
14
|
+
init: () => ModuleInitResult;
|
|
15
|
+
onUpdateStoreComponent: (state?: TState) => Promise<void>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
import { createStore } from "zustand/vanilla";
|
|
3
|
+
export class ModuleView extends Module {
|
|
4
|
+
animationTime = 0;
|
|
5
|
+
renderFunc = null;
|
|
6
|
+
updateEvent;
|
|
7
|
+
updateHandler = null;
|
|
8
|
+
locLabel = null;
|
|
9
|
+
store = createStore(() => ({}));
|
|
10
|
+
subscribe = () => {
|
|
11
|
+
if (this.updateEvent && this.updateHandler)
|
|
12
|
+
this.on(this.updateEvent, this.updateHandler);
|
|
13
|
+
};
|
|
14
|
+
init = () => {
|
|
15
|
+
if (!this.renderFunc)
|
|
16
|
+
return;
|
|
17
|
+
return this.emitOne(this.EVENTS.COMPONENTS.RENDER, { name: this.name, renderFunc: this.renderFunc, store: this.store });
|
|
18
|
+
};
|
|
19
|
+
onUpdateStoreComponent = async (state = {}) => {
|
|
20
|
+
if (!this.isReady)
|
|
21
|
+
await this.waitIsReady();
|
|
22
|
+
const injects = (await this.emitOne(this.EVENTS.COMPONENTS.INJECT, this));
|
|
23
|
+
const mutableState = state;
|
|
24
|
+
injects.forEach(({ field, value } = {}) => {
|
|
25
|
+
if (field)
|
|
26
|
+
mutableState[field] = value;
|
|
27
|
+
});
|
|
28
|
+
if (state.isForce) {
|
|
29
|
+
this.store.setState(state);
|
|
30
|
+
await this.waitTimeout(100);
|
|
31
|
+
this.store.setState({ ...this.store.getState(), isForce: false });
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.store.setState(state);
|
|
35
|
+
if (!state.isFast)
|
|
36
|
+
await this.waitTimeout(this.animationTime);
|
|
37
|
+
};
|
|
38
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import "@vnejs/plugins.core.components.contract";
|
|
2
|
+
import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
|
|
3
|
+
import type { Constants as ComponentsConstants, PluginName as ComponentsPluginName, SubscribeEvents as ComponentsSubscribeEvents } from "@vnejs/plugins.core.components.contract";
|
|
4
|
+
import type { PluginName as LogsPluginName, SubscribeEvents as LogsSubscribeEvents } from "@vnejs/plugins.core.logs.contract";
|
|
5
|
+
import type { Constants as MediaConstants, PluginName as MediaPluginName, SubscribeEvents as MediaSubscribeEvents } from "@vnejs/plugins.core.media.contract";
|
|
6
|
+
export declare const CONTROLS_EVENTS: {
|
|
7
|
+
readonly EMIT: "vne:controls:emit";
|
|
8
|
+
readonly PUSH: "vne:controls:push";
|
|
9
|
+
readonly POP: "vne:controls:pop";
|
|
10
|
+
};
|
|
11
|
+
export declare const LAYER_SETTINGS_KEYS: {
|
|
12
|
+
readonly WIDTH: "vne:canvas:width";
|
|
13
|
+
readonly HEIGHT: "vne:canvas:height";
|
|
14
|
+
readonly QUALITY: "vne:canvas:quality";
|
|
15
|
+
};
|
|
16
|
+
type ControlsPluginName = "CONTROLS";
|
|
17
|
+
type LayerPluginName = "LAYER";
|
|
18
|
+
export type ModuleComponentsEvents = VnePluginEventsMapRegistry & Record<ComponentsPluginName, ComponentsSubscribeEvents> & Record<LogsPluginName, LogsSubscribeEvents> & Record<MediaPluginName, MediaSubscribeEvents> & Record<ControlsPluginName, typeof CONTROLS_EVENTS>;
|
|
19
|
+
export type ModuleComponentsConstants = VnePluginConstantsMapRegistry & Record<ComponentsPluginName, ComponentsConstants> & Record<MediaPluginName, MediaConstants>;
|
|
20
|
+
export type ModuleComponentsSettings = VnePluginSettingsMapRegistry & Record<LayerPluginName, typeof LAYER_SETTINGS_KEYS>;
|
|
21
|
+
export type ModuleComponentsParams = VnePluginParamsMapRegistry;
|
|
22
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import "@vnejs/plugins.core.components.contract";
|
|
2
|
+
export const CONTROLS_EVENTS = {
|
|
3
|
+
EMIT: "vne:controls:emit",
|
|
4
|
+
PUSH: "vne:controls:push",
|
|
5
|
+
POP: "vne:controls:pop",
|
|
6
|
+
};
|
|
7
|
+
export const LAYER_SETTINGS_KEYS = {
|
|
8
|
+
WIDTH: "vne:canvas:width",
|
|
9
|
+
HEIGHT: "vne:canvas:height",
|
|
10
|
+
QUALITY: "vne:canvas:quality",
|
|
11
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Module } from "@vnejs/module";
|
|
2
|
+
export type ViewActionPayload = {
|
|
3
|
+
isForce?: boolean;
|
|
4
|
+
onClose?: () => void;
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
};
|
|
7
|
+
export type ViewUpdateState = {
|
|
8
|
+
isForce?: boolean;
|
|
9
|
+
isFast?: boolean;
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
};
|
|
12
|
+
export type ComponentInjectResult = {
|
|
13
|
+
field?: string;
|
|
14
|
+
value?: unknown;
|
|
15
|
+
};
|
|
16
|
+
export type ViewRenderProps = Record<string, unknown> & {
|
|
17
|
+
onMount?: () => void;
|
|
18
|
+
store?: unknown;
|
|
19
|
+
};
|
|
20
|
+
export type ViewRenderFunc = (props: ViewRenderProps, root: HTMLDivElement) => void;
|
|
21
|
+
export type MediaLoadPayload = {
|
|
22
|
+
type?: string;
|
|
23
|
+
priority?: number;
|
|
24
|
+
quality?: string | number;
|
|
25
|
+
name?: string;
|
|
26
|
+
mediaType?: string;
|
|
27
|
+
};
|
|
28
|
+
export type ControlsPushPayload = {
|
|
29
|
+
key?: string;
|
|
30
|
+
controls?: Record<string, () => void | Promise<void>>;
|
|
31
|
+
index?: number;
|
|
32
|
+
checkNext?: boolean;
|
|
33
|
+
};
|
|
34
|
+
export type ControlsPopPayload = {
|
|
35
|
+
key?: string;
|
|
36
|
+
};
|
|
37
|
+
export type ViewUpdatePayload = ViewUpdateState;
|
|
38
|
+
export type ComponentsInjectHandler = (component: Module) => ComponentInjectResult | Promise<ComponentInjectResult>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,18 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/module.components",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"tsconfig.json"
|
|
19
|
+
],
|
|
6
20
|
"scripts": {
|
|
7
21
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
-
"
|
|
9
|
-
"publish:
|
|
10
|
-
"publish:
|
|
22
|
+
"build": "npx @vnejs/monorepo package",
|
|
23
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
24
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
25
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
11
26
|
},
|
|
12
27
|
"author": "",
|
|
13
28
|
"license": "ISC",
|
|
14
29
|
"dependencies": {
|
|
15
|
-
"@vnejs/module": "~0.0.1",
|
|
16
30
|
"zustand": "5.0.10"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@vnejs/module": "~0.0.1",
|
|
34
|
+
"@vnejs/shared": "~0.0.9"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@vnejs/configs.ts-common": "~0.0.1",
|
|
38
|
+
"@vnejs/module": "~0.0.1",
|
|
39
|
+
"@vnejs/plugins.core.components.contract": "~0.0.1",
|
|
40
|
+
"@vnejs/plugins.core.logs.contract": "~0.0.1",
|
|
41
|
+
"@vnejs/plugins.core.media.contract": "~0.0.1",
|
|
42
|
+
"@vnejs/shared": "~0.0.9"
|
|
17
43
|
}
|
|
18
44
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "./types.js";
|
|
2
|
+
export type { ModuleControllerState } from "./modules/controller.js";
|
|
3
|
+
export type {
|
|
4
|
+
ComponentInjectResult,
|
|
5
|
+
ControlsPopPayload,
|
|
6
|
+
ControlsPushPayload,
|
|
7
|
+
MediaLoadPayload,
|
|
8
|
+
ViewActionPayload,
|
|
9
|
+
ViewRenderFunc,
|
|
10
|
+
ViewUpdatePayload,
|
|
11
|
+
ViewUpdateState,
|
|
12
|
+
} from "./utils/components.js";
|
|
13
|
+
|
|
14
|
+
export { ModuleController } from "./modules/controller.js";
|
|
15
|
+
export { ModuleView } from "./modules/view.js";
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Module, type ModuleGlobalStateRegistry } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "../types.js";
|
|
4
|
+
import type { ControlsPopPayload, ControlsPushPayload, MediaLoadPayload, ViewActionPayload, ViewUpdatePayload } from "../utils/components.js";
|
|
5
|
+
|
|
6
|
+
export type ModuleControllerState = {
|
|
7
|
+
isShow: boolean;
|
|
8
|
+
isForce: boolean;
|
|
9
|
+
currentItem?: number | null;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export abstract class ModuleController<
|
|
13
|
+
TEvents extends ModuleComponentsEvents = ModuleComponentsEvents,
|
|
14
|
+
TConstants extends ModuleComponentsConstants = ModuleComponentsConstants,
|
|
15
|
+
TSettings extends ModuleComponentsSettings = ModuleComponentsSettings,
|
|
16
|
+
TParams extends ModuleComponentsParams = ModuleComponentsParams,
|
|
17
|
+
TState extends ModuleControllerState = ModuleControllerState,
|
|
18
|
+
> extends Module<TEvents, TConstants, TSettings, TParams, TState> {
|
|
19
|
+
abstract override name: string;
|
|
20
|
+
|
|
21
|
+
currentItem: number | null = null;
|
|
22
|
+
maxCurrentItem = 0;
|
|
23
|
+
|
|
24
|
+
beforeShow: ((args: ViewActionPayload) => void | Promise<void>) | null = null;
|
|
25
|
+
afterShow: ((args: ViewActionPayload) => void | Promise<void>) | null = null;
|
|
26
|
+
beforeHide: ((args: ViewActionPayload) => void | Promise<void>) | null = null;
|
|
27
|
+
afterHide: ((args: ViewActionPayload) => void | Promise<void>) | null = null;
|
|
28
|
+
|
|
29
|
+
bgName: string | null = null;
|
|
30
|
+
updateEvent!: string;
|
|
31
|
+
|
|
32
|
+
controls: Record<string, () => void | Promise<void>> | null = null;
|
|
33
|
+
controlsIndex = 0;
|
|
34
|
+
controlsCheckNext = false;
|
|
35
|
+
|
|
36
|
+
override isOnlyLocalState = true;
|
|
37
|
+
|
|
38
|
+
onClose?: () => void;
|
|
39
|
+
|
|
40
|
+
onHide = async ({ isForce = false, ...args }: ViewActionPayload = {}) => {
|
|
41
|
+
if (!this.state.isShow) return;
|
|
42
|
+
|
|
43
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "hide" } });
|
|
44
|
+
if (this.beforeHide) await this.beforeHide(args);
|
|
45
|
+
if (this.controls) await this.emit(this.EVENTS.CONTROLS.POP, { key: this.name } satisfies ControlsPopPayload);
|
|
46
|
+
await this.setShow(false, Boolean(isForce || this.shared.viewForceAnimationSources.length));
|
|
47
|
+
if (this.onClose) {
|
|
48
|
+
this.onClose();
|
|
49
|
+
delete this.onClose;
|
|
50
|
+
}
|
|
51
|
+
if (this.afterHide) await this.afterHide(args);
|
|
52
|
+
this.unsetCurrentItem();
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
onShow = async ({ isForce = false, onClose, ...args }: ViewActionPayload = {}) => {
|
|
56
|
+
if (this.state.isShow) return;
|
|
57
|
+
|
|
58
|
+
this.currentItem = null;
|
|
59
|
+
this.state = { ...this.state, currentItem: null };
|
|
60
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "show" } });
|
|
61
|
+
if (onClose) this.onClose = onClose;
|
|
62
|
+
if (this.beforeShow) await this.beforeShow(args);
|
|
63
|
+
await this.setShow(true, Boolean(isForce || this.shared.viewForceAnimationSources.length));
|
|
64
|
+
if (this.controls)
|
|
65
|
+
await this.emit(this.EVENTS.CONTROLS.PUSH, {
|
|
66
|
+
key: this.name,
|
|
67
|
+
controls: this.controls,
|
|
68
|
+
index: this.controlsIndex,
|
|
69
|
+
checkNext: this.controlsCheckNext,
|
|
70
|
+
} satisfies ControlsPushPayload);
|
|
71
|
+
if (this.afterShow) await this.afterShow(args);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
onHideForce = (args: ViewActionPayload = {}) => this.onHide({ ...args, isForce: true });
|
|
75
|
+
onShowForce = (args: ViewActionPayload = {}) => this.onShow({ ...args, isForce: true });
|
|
76
|
+
onUpdateStateFromGlobalState = ({ [this.name]: state }: ModuleGlobalStateRegistry = {}) => this.updateStateAndViewForce(state as Partial<TState>);
|
|
77
|
+
|
|
78
|
+
setShow = (value: boolean, isForce = false) => this.updateStateAndView({ isShow: value } as Partial<TState>, isForce);
|
|
79
|
+
|
|
80
|
+
loadImageMedia = (name: string, mediaType: string, quality: string | number) => {
|
|
81
|
+
const [type, priority] = [this.CONST.MEDIA.TYPES.IMAGE, this.CONST.MEDIA.PRIORITIES.HIGH];
|
|
82
|
+
|
|
83
|
+
return this.emitOne(this.EVENTS.MEDIA.LOAD, { type, priority, quality, name, mediaType } satisfies MediaLoadPayload);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
loadBgMedia = () => this.loadImageMedia(`bg/${this.bgName}`, "view", this.shared.settings[this.SETTINGS.LAYER.QUALITY] as string | number);
|
|
87
|
+
|
|
88
|
+
updateView = (isForce = false, isFast = false) => this.emit(this.updateEvent, { ...this.state, isForce, isFast } satisfies ViewUpdatePayload);
|
|
89
|
+
updateViewForce = () => this.emit(this.updateEvent, { ...this.state, isForce: true } satisfies ViewUpdatePayload);
|
|
90
|
+
updateViewFast = () => this.emit(this.updateEvent, { ...this.state, isFast: true } satisfies ViewUpdatePayload);
|
|
91
|
+
|
|
92
|
+
updateStateAndView = (state: Partial<TState>, isForce = false, isFast = false) => {
|
|
93
|
+
this.updateState(state);
|
|
94
|
+
|
|
95
|
+
return this.updateView(isForce, isFast);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
updateStateAndViewForce = (state: Partial<TState>) => this.updateStateAndView(state, true, false);
|
|
99
|
+
updateStateAndViewFast = (state: Partial<TState>) => this.updateStateAndView(state, false, true);
|
|
100
|
+
|
|
101
|
+
incCurrentItem = () => this.setCurrentItem((this.currentItem ?? 0) + 1, 0);
|
|
102
|
+
decCurrentItem = () => this.setCurrentItem((this.currentItem ?? 0) - 1, this.maxCurrentItem);
|
|
103
|
+
|
|
104
|
+
setCurrentItem = async (value: number, defaultValue: number) => {
|
|
105
|
+
if (this.currentItem === null) {
|
|
106
|
+
this.currentItem = defaultValue;
|
|
107
|
+
} else {
|
|
108
|
+
this.currentItem = value;
|
|
109
|
+
if (this.currentItem > this.maxCurrentItem) this.currentItem = 0;
|
|
110
|
+
if (this.currentItem < 0) this.currentItem = this.maxCurrentItem;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
this.updateStateAndViewFast({ currentItem: this.currentItem } as Partial<TState>);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
unsetCurrentItem = () => {
|
|
117
|
+
this.currentItem = null;
|
|
118
|
+
this.updateStateAndViewFast({ currentItem: this.currentItem } as Partial<TState>);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
getDefaultState = (): TState => ({ isShow: false, isForce: false } as TState);
|
|
122
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Module, type ModuleInitResult } from "@vnejs/module";
|
|
2
|
+
import { createStore, type StoreApi } from "zustand/vanilla";
|
|
3
|
+
|
|
4
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "../types.js";
|
|
5
|
+
import type { ComponentInjectResult, ViewRenderFunc, ViewUpdateState } from "../utils/components.js";
|
|
6
|
+
|
|
7
|
+
export abstract class ModuleView<
|
|
8
|
+
TEvents extends ModuleComponentsEvents = ModuleComponentsEvents,
|
|
9
|
+
TConstants extends ModuleComponentsConstants = ModuleComponentsConstants,
|
|
10
|
+
TSettings extends ModuleComponentsSettings = ModuleComponentsSettings,
|
|
11
|
+
TParams extends ModuleComponentsParams = ModuleComponentsParams,
|
|
12
|
+
TState extends ViewUpdateState = ViewUpdateState,
|
|
13
|
+
> extends Module<TEvents, TConstants, TSettings, TParams, TState> {
|
|
14
|
+
abstract override name: string;
|
|
15
|
+
|
|
16
|
+
animationTime = 0;
|
|
17
|
+
renderFunc: ViewRenderFunc | null = null;
|
|
18
|
+
updateEvent!: string;
|
|
19
|
+
updateHandler: ((state?: TState) => Promise<void>) | null = null;
|
|
20
|
+
locLabel: string | null = null;
|
|
21
|
+
|
|
22
|
+
store: StoreApi<TState> = createStore<TState>(() => ({} as TState));
|
|
23
|
+
|
|
24
|
+
subscribe = () => {
|
|
25
|
+
if (this.updateEvent && this.updateHandler) this.on(this.updateEvent, this.updateHandler);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
init = (): ModuleInitResult => {
|
|
29
|
+
if (!this.renderFunc) return;
|
|
30
|
+
|
|
31
|
+
return this.emitOne(this.EVENTS.COMPONENTS.RENDER, { name: this.name, renderFunc: this.renderFunc, store: this.store });
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
onUpdateStoreComponent = async (state: TState = {} as TState) => {
|
|
35
|
+
if (!this.isReady) await this.waitIsReady();
|
|
36
|
+
|
|
37
|
+
const injects = (await this.emitOne(this.EVENTS.COMPONENTS.INJECT, this)) as ComponentInjectResult[];
|
|
38
|
+
|
|
39
|
+
const mutableState = state as ViewUpdateState;
|
|
40
|
+
|
|
41
|
+
injects.forEach(({ field, value } = {}) => {
|
|
42
|
+
if (field) mutableState[field] = value;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (state.isForce) {
|
|
46
|
+
this.store.setState(state);
|
|
47
|
+
await this.waitTimeout(100);
|
|
48
|
+
this.store.setState({ ...this.store.getState(), isForce: false } as TState);
|
|
49
|
+
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.store.setState(state);
|
|
54
|
+
|
|
55
|
+
if (!state.isFast) await this.waitTimeout(this.animationTime);
|
|
56
|
+
};
|
|
57
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import "@vnejs/plugins.core.components.contract";
|
|
2
|
+
|
|
3
|
+
import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
|
|
4
|
+
import type { Constants as ComponentsConstants, PluginName as ComponentsPluginName, SubscribeEvents as ComponentsSubscribeEvents } from "@vnejs/plugins.core.components.contract";
|
|
5
|
+
import type { Constants as LogsConstants, PluginName as LogsPluginName, SubscribeEvents as LogsSubscribeEvents } from "@vnejs/plugins.core.logs.contract";
|
|
6
|
+
import type { Constants as MediaConstants, PluginName as MediaPluginName, SubscribeEvents as MediaSubscribeEvents } from "@vnejs/plugins.core.media.contract";
|
|
7
|
+
|
|
8
|
+
export const CONTROLS_EVENTS = {
|
|
9
|
+
EMIT: "vne:controls:emit",
|
|
10
|
+
PUSH: "vne:controls:push",
|
|
11
|
+
POP: "vne:controls:pop",
|
|
12
|
+
} as const;
|
|
13
|
+
|
|
14
|
+
export const LAYER_SETTINGS_KEYS = {
|
|
15
|
+
WIDTH: "vne:canvas:width",
|
|
16
|
+
HEIGHT: "vne:canvas:height",
|
|
17
|
+
QUALITY: "vne:canvas:quality",
|
|
18
|
+
} as const;
|
|
19
|
+
|
|
20
|
+
type ControlsPluginName = "CONTROLS";
|
|
21
|
+
type LayerPluginName = "LAYER";
|
|
22
|
+
|
|
23
|
+
export type ModuleComponentsEvents = VnePluginEventsMapRegistry &
|
|
24
|
+
Record<ComponentsPluginName, ComponentsSubscribeEvents> &
|
|
25
|
+
Record<LogsPluginName, LogsSubscribeEvents> &
|
|
26
|
+
Record<MediaPluginName, MediaSubscribeEvents> &
|
|
27
|
+
Record<ControlsPluginName, typeof CONTROLS_EVENTS>;
|
|
28
|
+
|
|
29
|
+
export type ModuleComponentsConstants = VnePluginConstantsMapRegistry & Record<ComponentsPluginName, ComponentsConstants> & Record<MediaPluginName, MediaConstants>;
|
|
30
|
+
|
|
31
|
+
export type ModuleComponentsSettings = VnePluginSettingsMapRegistry & Record<LayerPluginName, typeof LAYER_SETTINGS_KEYS>;
|
|
32
|
+
|
|
33
|
+
export type ModuleComponentsParams = VnePluginParamsMapRegistry;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export type ViewActionPayload = {
|
|
4
|
+
isForce?: boolean;
|
|
5
|
+
onClose?: () => void;
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type ViewUpdateState = {
|
|
10
|
+
isForce?: boolean;
|
|
11
|
+
isFast?: boolean;
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type ComponentInjectResult = {
|
|
16
|
+
field?: string;
|
|
17
|
+
value?: unknown;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type ViewRenderProps = Record<string, unknown> & {
|
|
21
|
+
onMount?: () => void;
|
|
22
|
+
store?: unknown;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type ViewRenderFunc = (props: ViewRenderProps, root: HTMLDivElement) => void;
|
|
26
|
+
|
|
27
|
+
export type MediaLoadPayload = {
|
|
28
|
+
type?: string;
|
|
29
|
+
priority?: number;
|
|
30
|
+
quality?: string | number;
|
|
31
|
+
name?: string;
|
|
32
|
+
mediaType?: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type ControlsPushPayload = {
|
|
36
|
+
key?: string;
|
|
37
|
+
controls?: Record<string, () => void | Promise<void>>;
|
|
38
|
+
index?: number;
|
|
39
|
+
checkNext?: boolean;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type ControlsPopPayload = {
|
|
43
|
+
key?: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type ViewUpdatePayload = ViewUpdateState;
|
|
47
|
+
|
|
48
|
+
export type ComponentsInjectHandler = (component: Module) => ComponentInjectResult | Promise<ComponentInjectResult>;
|
package/tsconfig.json
ADDED
package/index.d.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { GlobalState, Module } from "@vnejs/module";
|
|
2
|
-
|
|
3
|
-
export class ModuleController extends Module {
|
|
4
|
-
currentItem: number | null;
|
|
5
|
-
maxCurrentItem: number;
|
|
6
|
-
|
|
7
|
-
beforeShow?: () => Promise<void>;
|
|
8
|
-
afterShow?: () => Promise<void>;
|
|
9
|
-
beforeHide?: () => Promise<void>;
|
|
10
|
-
afterHide?: () => Promise<void>;
|
|
11
|
-
|
|
12
|
-
bgName?: string;
|
|
13
|
-
updateEvent: string;
|
|
14
|
-
|
|
15
|
-
controls?: Record<string, any>;
|
|
16
|
-
controlsIndex?: number;
|
|
17
|
-
controlsCheckNext?: Boolean;
|
|
18
|
-
|
|
19
|
-
onHide: (...args: any[]) => Promise<void>;
|
|
20
|
-
onShow: (...args: any[]) => Promise<void>;
|
|
21
|
-
onHideForce: (...args: any[]) => Promise<void>;
|
|
22
|
-
onShowForce: (...args: any[]) => Promise<void>;
|
|
23
|
-
onUpdateStateFromGlobalState: (state: GlobalState) => Promise<void>;
|
|
24
|
-
|
|
25
|
-
setShow: (value: Boolean, isForce?: Boolean) => Promise<void>;
|
|
26
|
-
|
|
27
|
-
loadImageMedia: (name: string, mediaType: string, quality: string) => Promise<void>;
|
|
28
|
-
loadBgMedia: () => Promise<void>;
|
|
29
|
-
|
|
30
|
-
updateView: (isForce: Boolean, isFast: Boolean) => Promise<void>;
|
|
31
|
-
|
|
32
|
-
updateViewForce: () => Promise<void>;
|
|
33
|
-
updateViewFast: () => Promise<void>;
|
|
34
|
-
updateStateAndView: (state: any, isForce: Boolean, isFast: Boolean) => Promise<void>;
|
|
35
|
-
updateStateAndViewForce: (state: any) => Promise<void>;
|
|
36
|
-
updateStateAndViewFast: (state: any) => Promise<void>;
|
|
37
|
-
|
|
38
|
-
incCurrentItem: () => void;
|
|
39
|
-
decCurrentItem: () => void;
|
|
40
|
-
setCurrentItem: (value: number, defaultValue: number) => void;
|
|
41
|
-
unsetCurrentItem: () => void;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export class ModuleView extends Module {
|
|
45
|
-
animationTime: number;
|
|
46
|
-
renderFunc: () => void;
|
|
47
|
-
updateEvent: string;
|
|
48
|
-
updateHandler: () => Promise<void>;
|
|
49
|
-
canvasInfo: any;
|
|
50
|
-
locLabel?: string;
|
|
51
|
-
|
|
52
|
-
regCanvas: () => Promise<void>;
|
|
53
|
-
regComponent: () => Promise<void>;
|
|
54
|
-
|
|
55
|
-
onUpdateStoreComponent: (state: any) => Promise<void>;
|
|
56
|
-
|
|
57
|
-
insertExtraDataToState: (state: any) => Promise<any>;
|
|
58
|
-
}
|
package/index.js
DELETED
package/modules/controller.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { Module } from "@vnejs/module";
|
|
2
|
-
|
|
3
|
-
export class ModuleController extends Module {
|
|
4
|
-
currentItem = null;
|
|
5
|
-
maxCurrentItem = 0;
|
|
6
|
-
|
|
7
|
-
beforeShow = null;
|
|
8
|
-
afterShow = null;
|
|
9
|
-
beforeHide = null;
|
|
10
|
-
afterHide = null;
|
|
11
|
-
|
|
12
|
-
bgName = null;
|
|
13
|
-
updateEvent = null;
|
|
14
|
-
|
|
15
|
-
controls = null;
|
|
16
|
-
controlsIndex = 0;
|
|
17
|
-
controlsCheckNext = false;
|
|
18
|
-
|
|
19
|
-
isOnlyLocalState = true;
|
|
20
|
-
|
|
21
|
-
onHide = async ({ isForce = false, ...args } = {}) => {
|
|
22
|
-
if (!this.state.isShow) return;
|
|
23
|
-
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "hide" } });
|
|
24
|
-
if (this.beforeHide) await this.beforeHide(args);
|
|
25
|
-
if (this.controls) await this.emit(this.EVENTS.CONTROLS.POP, { key: this.name });
|
|
26
|
-
await this.setShow(false, Boolean(isForce || this.shared.viewForceAnimationSources.length));
|
|
27
|
-
if (this.onClose) {
|
|
28
|
-
this.onClose();
|
|
29
|
-
delete this.onClose;
|
|
30
|
-
}
|
|
31
|
-
if (this.afterHide) await this.afterHide(args);
|
|
32
|
-
this.unsetCurrentItem();
|
|
33
|
-
};
|
|
34
|
-
onShow = async ({ isForce = false, onClose, ...args } = {}) => {
|
|
35
|
-
if (this.state.isShow) return;
|
|
36
|
-
this.currentItem = null;
|
|
37
|
-
this.state = { ...this.state, currentItem: null };
|
|
38
|
-
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "show" } });
|
|
39
|
-
if (onClose) this.onClose = onClose;
|
|
40
|
-
if (this.beforeShow) await this.beforeShow(args);
|
|
41
|
-
await this.setShow(true, Boolean(isForce || this.shared.viewForceAnimationSources.length));
|
|
42
|
-
if (this.controls)
|
|
43
|
-
await this.emit(this.EVENTS.CONTROLS.PUSH, { key: this.name, controls: this.controls, index: this.controlsIndex, checkNext: this.controlsCheckNext });
|
|
44
|
-
if (this.afterShow) await this.afterShow(args);
|
|
45
|
-
};
|
|
46
|
-
onHideForce = (args) => this.onHide({ ...args, isForce: true });
|
|
47
|
-
onShowForce = (args) => this.onShow({ ...args, isForce: true });
|
|
48
|
-
onUpdateStateFromGlobalState = ({ [this.name]: state } = {}) => this.updateStateAndViewForce(state);
|
|
49
|
-
|
|
50
|
-
setShow = (value, isForce = false) => this.updateStateAndView({ isShow: value }, isForce);
|
|
51
|
-
|
|
52
|
-
loadImageMedia = (name, mediaType, quality) => {
|
|
53
|
-
const [type, priority] = [this.CONST.MEDIA.TYPES.IMAGE, this.CONST.MEDIA.PRIORITIES.HIGH];
|
|
54
|
-
|
|
55
|
-
return this.emitOne(this.EVENTS.MEDIA.LOAD, { type, priority, quality, name, mediaType });
|
|
56
|
-
};
|
|
57
|
-
loadBgMedia = () => this.loadImageMedia(`bg/${this.bgName}`, "view", this.shared.settings[this.SETTINGS.LAYER.QUALITY]);
|
|
58
|
-
|
|
59
|
-
updateView = (isForce = false, isFast = false) => this.emit(this.updateEvent, { ...this.state, isForce, isFast });
|
|
60
|
-
updateViewForce = () => this.emit(this.updateEvent, { ...this.state, isForce: true });
|
|
61
|
-
updateViewFast = () => this.emit(this.updateEvent, { ...this.state, isFast: true });
|
|
62
|
-
updateStateAndView = (state, isForce = false, isFast = false) => {
|
|
63
|
-
this.updateState(state);
|
|
64
|
-
|
|
65
|
-
return this.updateView(isForce, isFast);
|
|
66
|
-
};
|
|
67
|
-
updateStateAndViewForce = (state) => this.updateStateAndView(state, true, false);
|
|
68
|
-
updateStateAndViewFast = (state) => this.updateStateAndView(state, false, true);
|
|
69
|
-
|
|
70
|
-
incCurrentItem = () => this.setCurrentItem(this.currentItem + 1, 0);
|
|
71
|
-
decCurrentItem = () => this.setCurrentItem(this.currentItem - 1, this.maxCurrentItem);
|
|
72
|
-
setCurrentItem = async (value, defaultValue) => {
|
|
73
|
-
if (this.currentItem === null) {
|
|
74
|
-
this.currentItem = defaultValue;
|
|
75
|
-
} else {
|
|
76
|
-
this.currentItem = value;
|
|
77
|
-
if (this.currentItem > this.maxCurrentItem) this.currentItem = 0;
|
|
78
|
-
if (this.currentItem < 0) this.currentItem = this.maxCurrentItem;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
this.updateStateAndViewFast({ currentItem: this.currentItem });
|
|
82
|
-
};
|
|
83
|
-
unsetCurrentItem = () => {
|
|
84
|
-
this.currentItem = null;
|
|
85
|
-
this.updateStateAndViewFast({ currentItem: this.currentItem });
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
getDefaultState = () => ({ isShow: false, isForce: false });
|
|
89
|
-
}
|
package/modules/view.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { Module } from "@vnejs/module";
|
|
2
|
-
|
|
3
|
-
import { createStore } from "zustand/vanilla";
|
|
4
|
-
|
|
5
|
-
export class ModuleView extends Module {
|
|
6
|
-
animationTime = 0;
|
|
7
|
-
renderFunc = null;
|
|
8
|
-
updateEvent = null;
|
|
9
|
-
updateHandler = null;
|
|
10
|
-
locLabel = null;
|
|
11
|
-
|
|
12
|
-
store = createStore(() => ({}));
|
|
13
|
-
|
|
14
|
-
subscribe = () => {
|
|
15
|
-
if (this.updateEvent && this.updateHandler) this.on(this.updateEvent, this.updateHandler);
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
init = () => this.renderFunc && this.emitOne(this.EVENTS.COMPONENTS.RENDER, { name: this.name, renderFunc: this.renderFunc, store: this.store });
|
|
19
|
-
|
|
20
|
-
onUpdateStoreComponent = async (state = {}) => {
|
|
21
|
-
if (!this.isReady) await this.waitIsReady();
|
|
22
|
-
|
|
23
|
-
const injects = await this.emitOne(this.EVENTS.COMPONENTS.INJECT, this);
|
|
24
|
-
|
|
25
|
-
injects.forEach(({ field, value } = {}) => {
|
|
26
|
-
if (field) state[field] = value;
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
return new Promise(async (resolve) => {
|
|
30
|
-
if (state.isForce) {
|
|
31
|
-
this.store.setState(state);
|
|
32
|
-
// FIXME
|
|
33
|
-
await this.waitTimeout(100);
|
|
34
|
-
this.store.setState({ ...this.store.getState(), isForce: false });
|
|
35
|
-
return resolve();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
this.store.setState(state);
|
|
39
|
-
|
|
40
|
-
return state.isFast ? resolve() : setTimeout(resolve, this.animationTime);
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
}
|