@vnejs/plugins.feature.choose 0.0.1 → 0.0.2

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/const/events.js CHANGED
@@ -4,4 +4,6 @@ export const SUBSCRIBE_EVENTS = {
4
4
  SHOW: "vne:choose:show",
5
5
  HIDE: "vne:choose:hide",
6
6
  UPDATE: "vne:choose:update",
7
+ EXEC_REG: "vne:choose:exec_reg",
8
+ EXEC_GET: "vne:choose:exec_get",
7
9
  };
package/index.js CHANGED
@@ -3,5 +3,7 @@ import { regPlugin } from "@vnejs/shared";
3
3
  import { SUBSCRIBE_EVENTS } from "./const/events";
4
4
 
5
5
  import { Choose } from "./modules/choose";
6
+ import { ChooseExec } from "./modules/exec";
7
+ import { ChooseScenario } from "./modules/scenario";
6
8
 
7
- regPlugin("CHOOSE", { events: SUBSCRIBE_EVENTS }, [Choose]);
9
+ regPlugin("CHOOSE", { events: SUBSCRIBE_EVENTS }, [Choose, ChooseExec, ChooseScenario]);
package/modules/choose.js CHANGED
@@ -11,29 +11,6 @@ export class Choose extends Module {
11
11
  this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
12
12
  };
13
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
14
  onChooseEmit = async ({ options, args } = {}) => {
38
15
  this.log({ action: "emit", options, args });
39
16
  this.updateState({ isShow: true, options: await this.getRealOptions(options), args });
@@ -57,16 +34,30 @@ export class Choose extends Module {
57
34
  onStateClear = () => this.emit(this.EVENTS.CHOOSE.HIDE, { force: true });
58
35
 
59
36
  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
- });
37
+ mapOptions = async ({ optionLine, index } = {}) => {
38
+ const text = await this.getOptionText(optionLine.line);
39
+ const result = { index, text, coords: this.getOptionCoords(optionLine.line), ...optionLine.args };
40
+
41
+ await Promise.all(
42
+ Object.keys(optionLine.args).map(async (key) => {
43
+ if (typeof optionLine.args[key] !== "string" || !optionLine.args[key].startsWith("$")) return;
44
+ const [dollar, module, ...other] = optionLine.args[key].split(" ");
45
+ result[key] = await this.emitOne(this.EVENTS.CHOOSE.EXEC_GET, { module, line: other.join(" ") });
46
+ })
47
+ );
48
+
49
+ return result;
50
+ };
65
51
  getOptionText = async (line) => {
66
- const quotedText = line.slice("% option ".length);
67
- const text = quotedText.slice(1, quotedText.length - 1);
52
+ const quotedText = line.slice(line.indexOf('"'), line.lastIndexOf('"') + 1);
53
+
54
+ return this.getLocTextByKey(this.globalState.scenario.label, quotedText.slice(1, quotedText.length - 1));
55
+ };
56
+ getOptionCoords = (line) => {
57
+ const lineText = line.slice("% option ".length);
58
+ const numbers = lineText.slice(lineText.lastIndexOf('"') + 2);
68
59
 
69
- return this.getLocTextByKey(this.globalState.scenario.label, text);
60
+ return numbers.split(" ").map(Number);
70
61
  };
71
62
 
72
63
  getDefaultState = () => ({ options: [], args: {}, isShow: false });
@@ -0,0 +1,19 @@
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 } = {}) =>
18
+ typeof this.handlers[module] === "function" ? this.handlers[module](line) : "";
19
+ }
@@ -0,0 +1,30 @@
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.EXEC_REG, { block: this.execName, handler: this.execHandler });
9
+
10
+ execHandler = async (line, args) => {
11
+ const { curLine = 0, label } = this.globalState.scenario;
12
+ const [lines] = await this.emit(this.EVENTS.SCENARIO.LINES_GET, { label });
13
+ const optionsArr = [];
14
+
15
+ let indexShift = 1;
16
+ let curLineObj = lines[curLine + indexShift];
17
+
18
+ while (curLineObj && curLineObj.indent >= lines[curLine].indent) {
19
+ curLineObj.line.startsWith("% option") &&
20
+ curLineObj.indent === lines[curLine].indent + 2 &&
21
+ optionsArr.push(curLine + indexShift);
22
+ indexShift++;
23
+ curLineObj = lines[curLine + indexShift];
24
+ }
25
+
26
+ const options = optionsArr.map((index) => ({ index, optionLine: lines[index] }));
27
+
28
+ await this.emit(this.EVENTS.CHOOSE.EMIT, { args, options });
29
+ };
30
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.feature.choose",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",