@vnejs/plugins.feature.choose 0.0.1

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,7 @@
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
+ };
package/index.js ADDED
@@ -0,0 +1,7 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+
3
+ import { SUBSCRIBE_EVENTS } from "./const/events";
4
+
5
+ import { Choose } from "./modules/choose";
6
+
7
+ regPlugin("CHOOSE", { events: SUBSCRIBE_EVENTS }, [Choose]);
@@ -0,0 +1,73 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class Choose extends Module {
4
+ name = "choose";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.CHOOSE.EMIT, this.onChooseEmit);
8
+ this.on(this.EVENTS.CHOOSE.OPTION, this.onChooseOption);
9
+
10
+ this.on(this.EVENTS.STATE.SET, this.onStateSet);
11
+ this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
12
+ };
13
+
14
+ init = () => this.emit(this.EVENTS.SCENARIO.EXEC_REG, { block: this.name, handler: this.execHandler });
15
+
16
+ execHandler = async (line, args) => {
17
+ const { curLine = 0, label } = this.globalState.scenario;
18
+ const [lines] = await this.emit(this.EVENTS.SCENARIO.LINES_GET, { label });
19
+ const optionsArr = [];
20
+
21
+ let indexShift = 1;
22
+ let curLineObj = lines[curLine + indexShift];
23
+
24
+ while (curLineObj && curLineObj.indent >= lines[curLine].indent) {
25
+ curLineObj.line.startsWith("% option") &&
26
+ curLineObj.indent === lines[curLine].indent + 2 &&
27
+ optionsArr.push(curLine + indexShift);
28
+ indexShift++;
29
+ curLineObj = lines[curLine + indexShift];
30
+ }
31
+
32
+ const options = optionsArr.map((index) => ({ index, optionLine: lines[index] }));
33
+
34
+ await this.emit(this.EVENTS.CHOOSE.EMIT, { args, options });
35
+ };
36
+
37
+ onChooseEmit = async ({ options, args } = {}) => {
38
+ this.log({ action: "emit", options, args });
39
+ this.updateState({ isShow: true, options: await this.getRealOptions(options), args });
40
+ await this.emit(this.EVENTS.CHOOSE.SHOW);
41
+ };
42
+
43
+ onChooseOption = async ({ index = 0 } = {}) => {
44
+ this.log({ action: "choose", index });
45
+ this.globalState.scenario.curLine = this.state.options[index].index + 1;
46
+ await this.emit(this.EVENTS.CHOOSE.HIDE);
47
+ this.setDefaultState();
48
+ this.emitNext();
49
+ };
50
+ onStateSet = async ({ [this.name]: state } = {}) => {
51
+ if (this.state.isShow === state.isShow) return;
52
+
53
+ this.setState(state);
54
+
55
+ return this.emit(state.isShow ? this.EVENTS.CHOOSE.SHOW : this.EVENTS.CHOOSE.HIDE, { force: true });
56
+ };
57
+ onStateClear = () => this.emit(this.EVENTS.CHOOSE.HIDE, { force: true });
58
+
59
+ getRealOptions = async (options = []) => Promise.all(options.map(this.mapOptions));
60
+ mapOptions = async ({ optionLine, index } = {}) => ({
61
+ index,
62
+ text: await this.getOptionText(optionLine.line),
63
+ ...optionLine.args,
64
+ });
65
+ getOptionText = async (line) => {
66
+ const quotedText = line.slice("% option ".length);
67
+ const text = quotedText.slice(1, quotedText.length - 1);
68
+
69
+ return this.getLocTextByKey(this.globalState.scenario.label, text);
70
+ };
71
+
72
+ getDefaultState = () => ({ options: [], args: {}, isShow: false });
73
+ }
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@vnejs/plugins.feature.choose",
3
+ "version": "0.0.1",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "publish:major": "npm version major && npm publish --access public",
8
+ "publish:minor": "npm version minor && npm publish --access public",
9
+ "publish:patch": "npm version patch && npm publish --access public"
10
+ },
11
+ "author": "",
12
+ "license": "ISC",
13
+ "description": "",
14
+ "peerDependencies": {
15
+ "@vnejs/shared": "*",
16
+ "@vnejs/module": "*"
17
+ }
18
+ }