@vnejs/plugins.core.text 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.
- package/const/const.js +6 -0
- package/const/events.js +10 -0
- package/const/settings.js +3 -0
- package/index.js +19 -0
- package/modules/cycle.js +56 -0
- package/modules/interact.js +34 -0
- package/modules/meet.js +31 -0
- package/modules/speaker.js +28 -0
- package/modules/text.js +78 -0
- package/modules/utils.js +37 -0
- package/package.json +18 -0
package/const/const.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const DEFAULT_SPEAKER = { names: { default: [""] }, color: "#ffffff" };
|
|
2
|
+
export const DEFAULT_SPEAKER_NAME = "default";
|
|
3
|
+
export const LINE_REG = /^((?<speaker>[a-zA-z_0-9]+) )?["](?<text>.+)["]( (?<hash>.*))?$/;
|
|
4
|
+
export const INTERFACE_VIEWS = { ADV: "adv", NVL: "nvl" };
|
|
5
|
+
export const SIZES = { XS: "xs", S: "s", M: "m", L: "l", XL: "xl" };
|
|
6
|
+
export const INTERACT_STATES = { EMPTY: "empty", RENDERING: "rendering", SHOWED: "showed" };
|
package/const/events.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const SUBSCRIBE_EVENTS = {
|
|
2
|
+
EMIT: "vne:text:emit",
|
|
3
|
+
EMIT_END: "vne:text:emit_end",
|
|
4
|
+
EMIT_BEFORE: "vne:text:emit_before",
|
|
5
|
+
CICLE: "vne:text:cicle",
|
|
6
|
+
INTERACT: "vne:text:interact",
|
|
7
|
+
SPEAKER_REG: "vne:text:speaker_reg",
|
|
8
|
+
VISIT: "vne:text:visit",
|
|
9
|
+
MEET: "vne:text:meet",
|
|
10
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { regPlugin } from "@vnejs/shared";
|
|
2
|
+
|
|
3
|
+
import * as constants from "./const/const";
|
|
4
|
+
import { SUBSCRIBE_EVENTS } from "./const/events";
|
|
5
|
+
import { SETTINGS_KEYS } from "./const/settings";
|
|
6
|
+
|
|
7
|
+
import { TextCycle } from "./modules/cycle";
|
|
8
|
+
import { TextInteract } from "./modules/interact";
|
|
9
|
+
import { TextMeet } from "./modules/meet";
|
|
10
|
+
import { TextSpeaker } from "./modules/speaker";
|
|
11
|
+
import { Text } from "./modules/text";
|
|
12
|
+
|
|
13
|
+
regPlugin("TEXT", { constants, events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS }, [
|
|
14
|
+
TextCycle,
|
|
15
|
+
TextInteract,
|
|
16
|
+
TextMeet,
|
|
17
|
+
TextSpeaker,
|
|
18
|
+
Text,
|
|
19
|
+
]);
|
package/modules/cycle.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
let shouldCancelAnimation = false;
|
|
4
|
+
let updateCounter = 0;
|
|
5
|
+
|
|
6
|
+
export class TextCycle extends Module {
|
|
7
|
+
name = "text.cycle";
|
|
8
|
+
|
|
9
|
+
subscribe = () => {
|
|
10
|
+
this.on(this.EVENTS.TEXT.CICLE, this.onCicle);
|
|
11
|
+
this.on(this.EVENTS.TEXT.EMIT_END, this.onTextEmitEnd);
|
|
12
|
+
};
|
|
13
|
+
onCicle = ({ text = "" } = {}) => this.startUpdateCycle(text);
|
|
14
|
+
startUpdateCycle = async (oldOriginalText) => {
|
|
15
|
+
let lastTs = new Date().getTime();
|
|
16
|
+
|
|
17
|
+
const textSpeed = this.shared.settings[this.SETTINGS.TEXT.SPEED];
|
|
18
|
+
const updateCounterCur = ++updateCounter;
|
|
19
|
+
|
|
20
|
+
while (updateCounterCur === updateCounter) {
|
|
21
|
+
await this.waitRender();
|
|
22
|
+
if (updateCounterCur !== updateCounter || shouldCancelAnimation) {
|
|
23
|
+
shouldCancelAnimation = false;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const ts = new Date().getTime();
|
|
27
|
+
const diff = ts - lastTs;
|
|
28
|
+
if (diff > textSpeed) {
|
|
29
|
+
lastTs = ts - (diff % textSpeed);
|
|
30
|
+
this.globalState.text.charsVisible += Math.floor(diff / textSpeed);
|
|
31
|
+
} else {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const { text, charsVisible, originalText } = this.globalState.text;
|
|
35
|
+
|
|
36
|
+
if (charsVisible > text.text.length) {
|
|
37
|
+
updateCounter++;
|
|
38
|
+
await this.emit(this.EVENTS.TEXT.EMIT_END, { isForced: false });
|
|
39
|
+
} else {
|
|
40
|
+
if (originalText === oldOriginalText) {
|
|
41
|
+
await this.emit(this.EVENTS.INTERFACE.TEXT, { text, charsVisible });
|
|
42
|
+
} else updateCounter++;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
onTextEmitEnd = () => {
|
|
50
|
+
updateCounter++;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
onStateClear = () => {
|
|
54
|
+
shouldCancelAnimation = true;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export class TextInteract extends Module {
|
|
4
|
+
name = "text.interact";
|
|
5
|
+
|
|
6
|
+
subscribe = () => {
|
|
7
|
+
this.on(this.EVENTS.TEXT.INTERACT, this.onTextInteract);
|
|
8
|
+
|
|
9
|
+
this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
|
|
10
|
+
this.on(this.EVENTS.STATE.SET, this.onStateSet);
|
|
11
|
+
};
|
|
12
|
+
onTextInteract = async ({ state = "" } = {}) => {
|
|
13
|
+
this.state.state = state;
|
|
14
|
+
|
|
15
|
+
state === this.CONST.TEXT.INTERACT_STATES.RENDERING
|
|
16
|
+
? await this.emit(this.EVENTS.INTERACT.PUSH, { key: `${this.name}.rendering`, handler: this.handlerRendering })
|
|
17
|
+
: await this.emit(this.EVENTS.INTERACT.POP, { key: `${this.name}.rendering` });
|
|
18
|
+
|
|
19
|
+
state === this.CONST.TEXT.INTERACT_STATES.SHOWED
|
|
20
|
+
? await this.emit(this.EVENTS.INTERACT.PUSH, { key: `${this.name}.showed`, handler: this.handlerShowed })
|
|
21
|
+
: await this.emit(this.EVENTS.INTERACT.POP, { key: `${this.name}.showed` });
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
onStateSet = ({ [this.name]: state }) => this.emit(this.EVENTS.TEXT.INTERACT, state);
|
|
25
|
+
onStateClear = () => this.emit(this.EVENTS.TEXT.INTERACT, this.getDefaultState());
|
|
26
|
+
|
|
27
|
+
handlerShowed = async () => {
|
|
28
|
+
await this.emit(this.EVENTS.TEXT.INTERACT, { state: this.CONST.TEXT.INTERACT_STATES.EMPTY });
|
|
29
|
+
this.emitNext();
|
|
30
|
+
};
|
|
31
|
+
handlerRendering = () => this.emit(this.EVENTS.TEXT.EMIT_END, { isForced: true });
|
|
32
|
+
|
|
33
|
+
getDefaultState = () => ({ state: this.CONST.TEXT.INTERACT_STATES.EMPTY });
|
|
34
|
+
}
|
package/modules/meet.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
const MEET_REG = /^(?<char>[a-zA-Z0-9_\-]+)( (?<value>-?\d+))?$/;
|
|
4
|
+
|
|
5
|
+
export class TextMeet extends Module {
|
|
6
|
+
name = "meet";
|
|
7
|
+
|
|
8
|
+
subscribe = () => {
|
|
9
|
+
this.on(this.EVENTS.TEXT.MEET, this.onMeet);
|
|
10
|
+
|
|
11
|
+
this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
|
|
12
|
+
this.on(this.EVENTS.STATE.SET, this.setStateFromGlobalState);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
init = () => this.emit(this.EVENTS.SCENARIO.EXEC_REG, { module: this.name, handler: this.execHandler });
|
|
16
|
+
|
|
17
|
+
execHandler = async (line, args) => {
|
|
18
|
+
const { char, value } = line.match(MEET_REG).groups;
|
|
19
|
+
|
|
20
|
+
await this.emit(this.EVENTS.TEXT.MEET, { char, value, ...args });
|
|
21
|
+
this.emitNext();
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
onMeet = ({ char, value = 1 }) => {
|
|
25
|
+
const realValue = Number(value);
|
|
26
|
+
this.log({ char, value });
|
|
27
|
+
this.state[char] = this.state[char] ? this.state[char] + realValue : realValue;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
getDefaultState = () => ({});
|
|
31
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
window.VNESpeakers = {};
|
|
4
|
+
|
|
5
|
+
export class TextSpeaker extends Module {
|
|
6
|
+
name = "text.speaker";
|
|
7
|
+
|
|
8
|
+
subscribe = () => {
|
|
9
|
+
this.on(this.EVENTS.TEXT.SPEAKER_REG, this.onSpeakerReg);
|
|
10
|
+
};
|
|
11
|
+
init = async () => {
|
|
12
|
+
const defaultSpeakerName = this.CONST.TEXT.DEFAULT_SPEAKER_NAME;
|
|
13
|
+
const defaultSpeaker = this.CONST.TEXT.DEFAULT_SPEAKER;
|
|
14
|
+
window.VNESpeakers = { [defaultSpeakerName]: defaultSpeaker, ...window.VNESpeakers };
|
|
15
|
+
|
|
16
|
+
this.shared.speakers = {};
|
|
17
|
+
|
|
18
|
+
await Promise.all(Object.entries(window.VNESpeakers).map(this.mapSpeakers));
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
mapSpeakers = ([name, speaker]) => this.emit(this.EVENTS.TEXT.SPEAKER_REG, { name, speaker });
|
|
22
|
+
|
|
23
|
+
onSpeakerReg = ({ name, speaker } = {}) => {
|
|
24
|
+
this.log({ action: "reg", name, speaker });
|
|
25
|
+
|
|
26
|
+
this.shared.speakers[name] = speaker;
|
|
27
|
+
};
|
|
28
|
+
}
|
package/modules/text.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
import { prepareText } from "./utils";
|
|
4
|
+
|
|
5
|
+
const BIG_VALUE = 99999999;
|
|
6
|
+
|
|
7
|
+
export class Text extends Module {
|
|
8
|
+
name = "text";
|
|
9
|
+
|
|
10
|
+
subscribe = () => {
|
|
11
|
+
this.on(this.EVENTS.TEXT.EMIT, this.onTextEmit);
|
|
12
|
+
this.on(this.EVENTS.TEXT.EMIT_END, this.onTextEmitEnd);
|
|
13
|
+
|
|
14
|
+
this.on(this.EVENTS.LOCS.LANG_CHANGED, this.changeTextLang);
|
|
15
|
+
|
|
16
|
+
this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
|
|
17
|
+
this.on(this.EVENTS.STATE.SET, this.onStateSet);
|
|
18
|
+
};
|
|
19
|
+
init = async () => {
|
|
20
|
+
await this.emit(this.EVENTS.SCENARIO.EXEC_REG, { module: this.name, handler: this.onLineHandler });
|
|
21
|
+
await this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.TEXT.SPEED, value: 15 });
|
|
22
|
+
|
|
23
|
+
this.shared.textNoAnimationSources = [];
|
|
24
|
+
};
|
|
25
|
+
onLineHandler = async (line, args) => {
|
|
26
|
+
const { speaker, text, hash } = line.match(this.CONST.TEXT.LINE_REG).groups;
|
|
27
|
+
|
|
28
|
+
await this.emit(this.EVENTS.INTERFACE.SHOW);
|
|
29
|
+
await this.emit(this.EVENTS.TEXT.EMIT, { speaker, text, hash, ...args });
|
|
30
|
+
};
|
|
31
|
+
onTextEmit = async ({ text: originalText, speaker = this.CONST.TEXT.DEFAULT_SPEAKER_NAME, ...otherArgs }) => {
|
|
32
|
+
const [meet, textSpeed] = [this.globalState.meet[speaker], this.shared.settings[this.SETTINGS.TEXT.SPEED]];
|
|
33
|
+
const [uid, text] = [
|
|
34
|
+
await this.emitOne(this.EVENTS.VENDORS.UID),
|
|
35
|
+
prepareText(await this.getRealText(originalText)),
|
|
36
|
+
];
|
|
37
|
+
const charsVisible = textSpeed && !this.shared.textNoAnimationSources.length ? 0 : BIG_VALUE;
|
|
38
|
+
|
|
39
|
+
this.updateState({ originalText, charsVisible, text: { ...otherArgs, uid, text, speaker, meet } });
|
|
40
|
+
|
|
41
|
+
await this.setInterfaceText();
|
|
42
|
+
|
|
43
|
+
if (!textSpeed || this.shared.textNoAnimationSources.length) return setImmediate(this.emitAnimationEnd);
|
|
44
|
+
|
|
45
|
+
this.shared.isTextAnimationInProcess = true;
|
|
46
|
+
|
|
47
|
+
await this.emit(this.EVENTS.TEXT.INTERACT, { state: this.CONST.TEXT.INTERACT_STATES.RENDERING });
|
|
48
|
+
await this.waitRender();
|
|
49
|
+
await this.emit(this.EVENTS.TEXT.CICLE, { text: originalText });
|
|
50
|
+
};
|
|
51
|
+
onTextEmitEnd = async ({ isForced = true } = {}) => {
|
|
52
|
+
await this.emit(this.EVENTS.TEXT.INTERACT, { state: this.CONST.TEXT.INTERACT_STATES.SHOWED });
|
|
53
|
+
this.state.charsVisible = isForced ? BIG_VALUE : (await this.getRealText(this.state.originalText)).length;
|
|
54
|
+
|
|
55
|
+
if (!this.shared.textNoAnimationSources.length) await this.setInterfaceText();
|
|
56
|
+
this.shared.isTextAnimationInProcess = false;
|
|
57
|
+
};
|
|
58
|
+
onStateSet = ({ [this.name]: state }) => this.setNewState(state);
|
|
59
|
+
onStateClear = () => this.setNewState(this.getDefaultState());
|
|
60
|
+
|
|
61
|
+
setNewState = (state) => {
|
|
62
|
+
this.setState(state);
|
|
63
|
+
this.shared.isTextAnimationInProcess = false;
|
|
64
|
+
this.state.charsVisible = BIG_VALUE;
|
|
65
|
+
|
|
66
|
+
return this.changeTextLang();
|
|
67
|
+
};
|
|
68
|
+
setInterfaceText = () => this.emit(this.EVENTS.INTERFACE.TEXT, this.state);
|
|
69
|
+
changeTextLang = async () => {
|
|
70
|
+
this.state.text = { ...this.state.text, text: prepareText(await this.getRealText(this.state.originalText)) };
|
|
71
|
+
return this.setInterfaceText();
|
|
72
|
+
};
|
|
73
|
+
getRealText = (text) => this.getLocTextByKey(this.globalState.scenario.label, text);
|
|
74
|
+
emitAnimationEnd = () => this.emit(this.EVENTS.TEXT.EMIT_END, { isForced: true });
|
|
75
|
+
|
|
76
|
+
getDefaultState = () => ({ originalText: "", charsVisible: 0, text: this.getDefaultText() });
|
|
77
|
+
getDefaultText = () => ({ text: [], speaker: this.CONST.TEXT.DEFAULT_SPEAKER_NAME, meet: 0 });
|
|
78
|
+
}
|
package/modules/utils.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const prepareText = (str) => {
|
|
2
|
+
const marks = {};
|
|
3
|
+
const result = [];
|
|
4
|
+
|
|
5
|
+
for (let i = 0; i < str.length; ) {
|
|
6
|
+
const curChar = str[i];
|
|
7
|
+
|
|
8
|
+
if (curChar === "{") {
|
|
9
|
+
let j = 0;
|
|
10
|
+
while (true) {
|
|
11
|
+
j++;
|
|
12
|
+
if (str[i + j] === "}") {
|
|
13
|
+
if (str[i + 1] === "/") {
|
|
14
|
+
let mark = str.slice(i + 2, i + j);
|
|
15
|
+
if (mark.includes(":")) {
|
|
16
|
+
mark = mark.split(":")[0];
|
|
17
|
+
}
|
|
18
|
+
delete marks[mark];
|
|
19
|
+
} else {
|
|
20
|
+
let [mark, value] = [str.slice(i + 1, i + j), true];
|
|
21
|
+
if (mark.includes(":")) {
|
|
22
|
+
[mark, value] = mark.split(":");
|
|
23
|
+
}
|
|
24
|
+
marks[mark] = value;
|
|
25
|
+
}
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
i += j + 1;
|
|
30
|
+
} else {
|
|
31
|
+
i++;
|
|
32
|
+
result.push({ char: curChar, marks: { ...marks } });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return result;
|
|
37
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/plugins.core.text",
|
|
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
|
+
}
|