@vnejs/plugins.feature.choose 0.0.1 → 0.1.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/const/events.js +2 -0
- package/index.js +3 -1
- package/modules/choose.js +28 -37
- package/modules/exec.js +19 -0
- package/modules/scenario.js +30 -0
- package/package.json +5 -6
package/const/events.js
CHANGED
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,62 +11,53 @@ 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
|
-
this.
|
|
15
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "emit", options, args } });
|
|
39
16
|
this.updateState({ isShow: true, options: await this.getRealOptions(options), args });
|
|
40
17
|
await this.emit(this.EVENTS.CHOOSE.SHOW);
|
|
41
18
|
};
|
|
42
19
|
|
|
43
20
|
onChooseOption = async ({ index = 0 } = {}) => {
|
|
44
|
-
this.
|
|
21
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "choose", index } });
|
|
45
22
|
this.globalState.scenario.curLine = this.state.options[index].index + 1;
|
|
46
23
|
await this.emit(this.EVENTS.CHOOSE.HIDE);
|
|
47
24
|
this.setDefaultState();
|
|
48
|
-
this.
|
|
25
|
+
this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
|
|
49
26
|
};
|
|
50
27
|
onStateSet = async ({ [this.name]: state } = {}) => {
|
|
51
28
|
if (this.state.isShow === state.isShow) return;
|
|
52
29
|
|
|
53
30
|
this.setState(state);
|
|
54
31
|
|
|
55
|
-
return this.emit(state.isShow ? this.EVENTS.CHOOSE.SHOW : this.EVENTS.CHOOSE.HIDE, {
|
|
32
|
+
return this.emit(state.isShow ? this.EVENTS.CHOOSE.SHOW : this.EVENTS.CHOOSE.HIDE, { isForce: true });
|
|
56
33
|
};
|
|
57
|
-
onStateClear = () => this.emit(this.EVENTS.CHOOSE.HIDE, {
|
|
34
|
+
onStateClear = () => this.emit(this.EVENTS.CHOOSE.HIDE, { isForce: true });
|
|
58
35
|
|
|
59
36
|
getRealOptions = async (options = []) => Promise.all(options.map(this.mapOptions));
|
|
60
|
-
mapOptions = async ({ optionLine, index } = {}) =>
|
|
61
|
-
|
|
62
|
-
text:
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
+
};
|
|
51
|
+
getOptionText = (line) => {
|
|
52
|
+
const quotedText = line.slice(line.indexOf('"'), line.lastIndexOf('"') + 1);
|
|
53
|
+
|
|
54
|
+
return this.emitOne(this.EVENTS.LOCS.GET_TEXT, { key: quotedText.slice(1, quotedText.length - 1), label: this.globalState.scenario.label });
|
|
55
|
+
};
|
|
56
|
+
getOptionCoords = (line) => {
|
|
57
|
+
const lineText = line.slice("% option ".length);
|
|
58
|
+
const numbers = lineText.slice(lineText.lastIndexOf('"') + 2);
|
|
68
59
|
|
|
69
|
-
return
|
|
60
|
+
return numbers === "" ? undefined : numbers.split(" ").map(Number);
|
|
70
61
|
};
|
|
71
62
|
|
|
72
63
|
getDefaultState = () => ({ options: [], args: {}, isShow: false });
|
package/modules/exec.js
ADDED
|
@@ -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,18 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/plugins.feature.choose",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"scripts": {
|
|
6
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",
|
|
7
10
|
"publish:major": "npm version major && npm publish --access public",
|
|
8
11
|
"publish:minor": "npm version minor && npm publish --access public",
|
|
9
12
|
"publish:patch": "npm version patch && npm publish --access public"
|
|
10
13
|
},
|
|
11
14
|
"author": "",
|
|
12
15
|
"license": "ISC",
|
|
13
|
-
"description": ""
|
|
14
|
-
"peerDependencies": {
|
|
15
|
-
"@vnejs/shared": "*",
|
|
16
|
-
"@vnejs/module": "*"
|
|
17
|
-
}
|
|
16
|
+
"description": ""
|
|
18
17
|
}
|