@vnejs/plugins.views.scenario.choose 0.1.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.
@@ -0,0 +1,9 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ EMIT: "vne:choose:emit",
3
+ OPTION: "vne:choose:option",
4
+ SHOW: "vne:choose:show",
5
+ HIDE: "vne:choose:hide",
6
+ UPDATE: "vne:choose:update",
7
+ EXEC_REG: "vne:choose:exec_reg",
8
+ EXEC_GET: "vne:choose:exec_get",
9
+ };
@@ -0,0 +1,11 @@
1
+ export const TRANSITION = 300;
2
+ export const ZINDEX = 1200;
3
+
4
+ export const VIEW_PROPS = {
5
+ screen: { withBlur: true, withDark: true, zIndex: ZINDEX, transition: TRANSITION },
6
+ position: { isCentered: true },
7
+ controls: { flexDirection: "column", flexGap: 60, textSize: 96, textAlign: "center" },
8
+ text: { size: 96 },
9
+ };
10
+
11
+ export const CONTROLS_EXTENSIONS = {};
package/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+
3
+ import { SUBSCRIBE_EVENTS } from "./const/events";
4
+ import * as params from "./const/params";
5
+
6
+ import { Choose } from "./modules/choose";
7
+ import { ChooseController } from "./modules/controller";
8
+ import { ChooseExec } from "./modules/exec";
9
+ import { ChooseScenario } from "./modules/scenario";
10
+ import { ChooseView } from "./modules/view";
11
+
12
+ regPlugin("CHOOSE", { events: SUBSCRIBE_EVENTS, params }, [Choose, ChooseController, ChooseExec, ChooseScenario, ChooseView]);
@@ -0,0 +1,75 @@
1
+ import { splitLineKeywords } from "@vnejs/helpers";
2
+ import { Module } from "@vnejs/module";
3
+
4
+ export class Choose extends Module {
5
+ name = "choose";
6
+
7
+ subscribe = () => {
8
+ this.on(this.EVENTS.CHOOSE.EMIT, this.onChooseEmit);
9
+ this.on(this.EVENTS.CHOOSE.OPTION, this.onChooseOption);
10
+
11
+ this.on(this.EVENTS.STATE.SET, this.onStateSet);
12
+ this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
13
+ };
14
+
15
+ onChooseEmit = async ({ options, args } = {}) => {
16
+ this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "emit", options, args } });
17
+ this.updateState({ isShow: true, options: await this.getRealOptions(options), args });
18
+ await this.emit(this.EVENTS.CHOOSE.SHOW);
19
+ };
20
+ onChooseOption = async ({ index = 0 } = {}) => {
21
+ this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "choose", index } });
22
+ this.globalState.scenario.curLine = this.state.options[index].index + 1;
23
+ await this.emit(this.EVENTS.CHOOSE.HIDE);
24
+ this.setDefaultState();
25
+ this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
26
+ };
27
+
28
+ onStateSet = async ({ [this.name]: state } = {}) => {
29
+ const clonedState = await this.emitOne(this.EVENTS.VENDORS.CLONE, state);
30
+
31
+ if (this.state.isShow === clonedState.isShow) return;
32
+
33
+ this.state.args = clonedState.args;
34
+ this.state.options = clonedState.options;
35
+
36
+ return this.emit(state.isShow ? this.EVENTS.CHOOSE.SHOW : this.EVENTS.CHOOSE.HIDE, { isForce: true });
37
+ };
38
+ onStateClear = () => {
39
+ this.setDefaultState();
40
+
41
+ return this.emit(this.EVENTS.CHOOSE.HIDE, { isForce: true });
42
+ };
43
+
44
+ getRealOptions = async (options = []) => Promise.all(options.map(this.mapOptions));
45
+ mapOptions = async ({ optionLine, index } = {}) => {
46
+ const text = await this.getOptionText(optionLine.line, optionLine.args);
47
+ const result = { index, text, coords: this.getOptionCoords(optionLine.line), ...optionLine.args };
48
+
49
+ await Promise.all(
50
+ Object.keys(optionLine.args).map(async (key) => {
51
+ if (typeof optionLine.args[key] !== "string" || !optionLine.args[key].startsWith(this.CONST.SCENARIO.LINE_PREFIXES.MODULE)) return;
52
+
53
+ const { line = "", module = "", keywords = [] } = splitLineKeywords(optionLine.args[key]);
54
+
55
+ result[key] = await this.emitOne(this.EVENTS.CHOOSE.EXEC_GET, { line, module, keywords });
56
+ }),
57
+ );
58
+
59
+ return result;
60
+ };
61
+ getOptionText = (line, args) => {
62
+ const quotedText = line.slice(line.indexOf('"'), line.lastIndexOf('"') + 1);
63
+ const text = quotedText.slice(1, quotedText.length - 1);
64
+
65
+ return this.emitOne(this.EVENTS.TEXT.REPLACE, { text, args, label: this.globalState.scenario.label, state: this.globalState });
66
+ };
67
+ getOptionCoords = (line) => {
68
+ const lineText = line.slice("% option ".length);
69
+ const numbers = lineText.slice(lineText.lastIndexOf('"') + 2);
70
+
71
+ return numbers === "" ? undefined : numbers.split(" ").map(Number);
72
+ };
73
+
74
+ getDefaultState = () => ({ options: [], args: {}, isShow: false });
75
+ }
@@ -0,0 +1,38 @@
1
+ import { ModuleController } from "@vnejs/module.components";
2
+
3
+ export class ChooseController extends ModuleController {
4
+ name = "choose.controller";
5
+
6
+ maxCurrentItem = 0;
7
+ updateEvent = this.EVENTS.CHOOSE.UPDATE;
8
+
9
+ controls = {
10
+ [this.CONST.CONTROLS.BUTTONS.ARROW_UP]: () => this.onArrow(this.decCurrentItem),
11
+ [this.CONST.CONTROLS.BUTTONS.ARROW_BOTTOM]: () => this.onArrow(this.incCurrentItem),
12
+ [this.CONST.CONTROLS.BUTTONS.ACCEPT]: () => this.currentItem !== null && this.emit(this.EVENTS.CHOOSE.OPTION, { index: this.currentItem }),
13
+ };
14
+ controlsIndex = this.PARAMS.CHOOSE.ZINDEX;
15
+
16
+ subscribe = () => {
17
+ this.on(this.EVENTS.CHOOSE.SHOW, this.onShow);
18
+ this.on(this.EVENTS.CHOOSE.HIDE, this.onHide);
19
+ };
20
+ init = () => {
21
+ Object.assign(this.controls, this.PARAMS.CHOOSE.CONTROLS_EXTENSIONS);
22
+ };
23
+
24
+ beforeShow = async () => {
25
+ const state = await this.emitOne(this.EVENTS.VENDORS.CLONE, this.globalState.choose);
26
+
27
+ this.updateState(state);
28
+ this.maxCurrentItem = this.state.options.length - 1;
29
+
30
+ return this.emit(this.EVENTS.INTERFACE.HIDE);
31
+ };
32
+ afterHide = () => this.setDefaultState();
33
+
34
+ onArrow = (cb) => {
35
+ cb();
36
+ while (this.state.options?.[this.currentItem].hided) cb();
37
+ };
38
+ }
@@ -0,0 +1,21 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class ChooseExec extends Module {
4
+ name = "choose.exec";
5
+
6
+ handlers = {};
7
+
8
+ subscribe = () => {
9
+ this.on(this.EVENTS.CHOOSE.EXEC_REG, this.onChooseExecReg);
10
+ this.on(this.EVENTS.CHOOSE.EXEC_GET, this.onChooseExecGet);
11
+ };
12
+
13
+ onChooseExecReg = ({ handler, module } = {}) => {
14
+ this.handlers[module] = handler;
15
+ };
16
+
17
+ onChooseExecGet = ({ module, line = "", keywords = [] } = {}) => {
18
+ if (typeof this.handlers[module] === "function") return this.handlers[module]({ line, keywords });
19
+ return "";
20
+ };
21
+ }
@@ -0,0 +1,26 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class ChooseScenario extends Module {
4
+ name = "choose.scenario";
5
+
6
+ execName = "choose";
7
+
8
+ init = () => this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: this.execName, handler: this.onLineExec });
9
+
10
+ onLineExec = async ({ args = {} } = {}) => {
11
+ const [{ curLine = 0, label }, optionsArr] = [this.globalState.scenario, []];
12
+ const lines = await this.emitOne(this.EVENTS.SCENARIO.LINES_GET, { label });
13
+
14
+ let [indexShift, curLineObj] = [1, lines[curLine + 1]];
15
+
16
+ while (curLineObj && curLineObj.indent >= lines[curLine].indent) {
17
+ curLineObj.line.startsWith("% option") && curLineObj.indent === lines[curLine].indent + 2 && optionsArr.push(curLine + indexShift);
18
+ indexShift++;
19
+ curLineObj = lines[curLine + indexShift];
20
+ }
21
+
22
+ const options = optionsArr.map((index) => ({ index, optionLine: lines[index] }));
23
+
24
+ await this.emit(this.EVENTS.CHOOSE.EMIT, { args, options });
25
+ };
26
+ }
@@ -0,0 +1,13 @@
1
+ import { ModuleView } from "@vnejs/module.components";
2
+
3
+ import { render } from "../view";
4
+
5
+ export class ChooseView extends ModuleView {
6
+ name = "choose.view";
7
+
8
+ animationTime = this.PARAMS.CHOOSE.TRANSITION;
9
+ updateEvent = this.EVENTS.CHOOSE.UPDATE;
10
+
11
+ renderFunc = render;
12
+ updateHandler = this.onUpdateStoreComponent;
13
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.views.scenario.choose",
3
+ "version": "0.1.10",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "publish:major:plugin": "npm run publish:major",
8
+ "publish:minor:plugin": "npm run publish:minor",
9
+ "publish:patch:plugin": "npm run publish:patch",
10
+ "publish:major": "npm version major && npm publish --access public",
11
+ "publish:minor": "npm version minor && npm publish --access public",
12
+ "publish:patch": "npm version patch && npm publish --access public"
13
+ },
14
+ "author": "",
15
+ "license": "ISC",
16
+ "description": ""
17
+ }
package/view/index.jsx ADDED
@@ -0,0 +1,66 @@
1
+ import { useCallback, useEffect, useMemo, useState } from "react";
2
+ import { createRoot } from "react-dom/client";
3
+
4
+ import { PositionBox, Screen, Text, TextControls, useIsForceHook } from "@vnejs/uis.react";
5
+
6
+ const EMPTY_ARRAY = [];
7
+
8
+ const renderOptionWithCoords = ({ text, isDisabled, isNotRender, value, coords: [x, y], props, currentItem, onClick } = {}) => {
9
+ const positionProps = { left: `${x}%`, top: `${y}%`, translateX: "-50%", translateY: "-50%", isNotRender };
10
+ const propsText = { ...props.PARAMS.CHOOSE.VIEW_PROPS.text, isDisabled, onClick, text, value, isSelected: currentItem === value };
11
+
12
+ return (
13
+ <PositionBox
14
+ {...positionProps}
15
+ key={value}
16
+ >
17
+ <Text {...propsText} />
18
+ </PositionBox>
19
+ );
20
+ };
21
+
22
+ const Choose = ({ store, onMount, ...props } = {}) => {
23
+ const [{ isShow = false, isForce = false, options = EMPTY_ARRAY, currentItem = null }, setState] = useState({});
24
+
25
+ useEffect(() => store.subscribe(setState), []);
26
+ useEffect(() => void onMount(), []);
27
+
28
+ const onClick = useCallback((index) => props.emit(props.EVENTS.CHOOSE.OPTION, { index }), []);
29
+
30
+ const isRealForce = useIsForceHook(isForce);
31
+
32
+ const optionsMapped = useMemo(
33
+ () => options.map(({ text, hided, coords, disabled }, i) => ({ text, i, coords, isNotRender: Boolean(hided), isDisabled: Boolean(disabled) })),
34
+ [options],
35
+ );
36
+ const textsInfo = useMemo(
37
+ () => optionsMapped.map(({ i, ...obj } = {}) => ({ value: i, isSelected: currentItem === i, ...obj })),
38
+ [optionsMapped, currentItem],
39
+ );
40
+ const optionsRegular = useMemo(() => textsInfo.filter((e) => !e.coords), [textsInfo]);
41
+ const optionsWithCoords = useMemo(() => textsInfo.filter((e) => e.coords), [textsInfo]);
42
+
43
+ const propsView = props.PARAMS.CHOOSE.VIEW_PROPS;
44
+
45
+ const propsScreen = useMemo(
46
+ () => ({ ...propsView.screen, isDisableAutoread: true, isIgnoreOnScreenshot: false, isShow, isForce: isRealForce }),
47
+ [isShow, isRealForce],
48
+ );
49
+ const propsControls = useMemo(() => ({ ...propsView.controls, texts: optionsRegular, onClick }), [optionsRegular]);
50
+
51
+ const optionsWithCoordsWithProps = useMemo(
52
+ () => optionsWithCoords.map((option) => ({ ...option, currentItem, onClick, props })),
53
+ [optionsWithCoords, currentItem, onClick],
54
+ );
55
+
56
+ return (
57
+ <Screen {...propsScreen}>
58
+ {optionsWithCoordsWithProps.map(renderOptionWithCoords)}
59
+ <PositionBox {...propsView.position}>
60
+ <TextControls {...propsControls} />
61
+ </PositionBox>
62
+ </Screen>
63
+ );
64
+ };
65
+
66
+ export const render = (props, root) => createRoot(root).render(<Choose {...props} />);