@vnejs/plugins.text.locs 0.1.4

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,5 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ LOAD: "vne:locs:load",
3
+ GET: "vne:locs:get",
4
+ CHANGED: "vne:locs:changed",
5
+ };
@@ -0,0 +1 @@
1
+ export const DEFAULT_LANG = "ru";
@@ -0,0 +1 @@
1
+ export const SETTINGS_KEYS = { LANG: "vne:locs:lang" };
package/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+
3
+ import { SUBSCRIBE_EVENTS } from "./const/events";
4
+ import * as params from "./const/params";
5
+ import { SETTINGS_KEYS } from "./const/settings";
6
+
7
+ import { TextLocs } from "./modules/locs";
8
+
9
+ regPlugin("LOCS", { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params }, [TextLocs]);
@@ -0,0 +1,96 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ const toJson = (i) => i.json();
4
+
5
+ export class TextLocs extends Module {
6
+ name = "text.locs";
7
+
8
+ loading = {};
9
+
10
+ subscribe = () => {
11
+ this.on(this.EVENTS.LOCS.LOAD, this.onLocsLoad);
12
+ this.on(this.EVENTS.LOCS.GET, this.onLocsGet);
13
+ this.on(this.EVENTS.LOCS.CHANGED, this.onLocsChanged);
14
+
15
+ this.on(this.EVENTS.SETTINGS.CHANGED, this.onSettingChange);
16
+ };
17
+
18
+ init = async () => {
19
+ await Promise.all([
20
+ this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.LOCS.LANG, value: this.PARAMS.LOCS.DEFAULT_LANG }),
21
+ this.emit(this.EVENTS.MEMORY.REG, { module: this.name }),
22
+ this.emit(this.EVENTS.COMPONENTS.INJECT_REG, { handler: this.componentsInjectHandler }),
23
+ this.emit(this.EVENTS.TEXT.REPLACE_REG, { priority: -999999, handler: this.textReplaceHandler }),
24
+ ]);
25
+
26
+ return this.setLang(this.shared.settings[this.SETTINGS.LOCS.LANG]);
27
+ };
28
+ componentsInjectHandler = async (component) => {
29
+ const result = { field: "locs", value: {} };
30
+
31
+ if (component.locLabel) result.value = await this.emitOne(this.EVENTS.LOCS.GET, { label: component.locLabel });
32
+
33
+ return result;
34
+ };
35
+ textReplaceHandler = async (text = "", label = "") => {
36
+ const locs = await this.emitOne(this.EVENTS.LOCS.GET, { label });
37
+
38
+ return locs?.[text] || text;
39
+ };
40
+
41
+ onLocsLoad = async ({ label, lang } = {}) => {
42
+ await this.waitRerenderCheck(this.checkLang);
43
+
44
+ const mediaId = `${lang || this.shared.lang}:${label}`;
45
+ const memoryLocs = await this.emitOne(this.EVENTS.MEMORY.GET, { module: this.name, key: mediaId });
46
+
47
+ if (memoryLocs) return;
48
+ if (this.loading[mediaId]) return this.loading[mediaId];
49
+
50
+ return this.loadLoc(label);
51
+ };
52
+ onLocsChanged = async () => {
53
+ const { label } = this.globalState.scenario;
54
+
55
+ const loadedLocs = await this.getLoadedLocs(label);
56
+ if (loadedLocs) return loadedLocs;
57
+
58
+ await this.emit(this.EVENTS.LOCS.LOAD, { label });
59
+ await this.emit(this.EVENTS.TEXT.RECALC);
60
+ };
61
+ onLocsGet = async ({ label = this.PARAMS.SCENARIO.INIT_LABEL_NAME, lang = null } = {}) => {
62
+ await this.waitRerenderCheck(this.checkLang);
63
+
64
+ const loadedLocs = await this.getLoadedLocs(label, lang);
65
+
66
+ if (loadedLocs) return loadedLocs;
67
+
68
+ await this.emit(this.EVENTS.LOCS.LOAD, { label, lang });
69
+
70
+ return this.getLoadedLocs(label, lang);
71
+ };
72
+ onSettingChange = ({ name, value } = {}) => name === this.SETTINGS.LOCS.LANG && this.setLang(value);
73
+
74
+ getLoadedLocs = (label, lang) => this.emitOne(this.EVENTS.MEMORY.GET, { module: this.name, key: `${lang || this.shared.lang}:${label}` });
75
+ loadLoc = (label) => {
76
+ const [loadingId, url] = [`${this.shared.lang}:${label}`, this.media.locs[`${this.shared.lang}/${label}`]];
77
+
78
+ const promiseFunc = async () => {
79
+ const value = url ? await fetch(`${this.CONST.SYSTEM.HOST}/${url}`).then(toJson) : {};
80
+
81
+ await this.emit(this.EVENTS.MEMORY.SET, { module: this.name, key: loadingId, value });
82
+
83
+ delete this.loading[loadingId];
84
+ };
85
+
86
+ this.loading[loadingId] = promiseFunc();
87
+
88
+ return this.loading[loadingId];
89
+ };
90
+ setLang = (lang) => {
91
+ this.shared.lang = lang;
92
+
93
+ return this.emit(this.EVENTS.LOCS.CHANGED);
94
+ };
95
+ checkLang = () => this.shared.lang;
96
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.text.locs",
3
+ "version": "0.1.4",
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
+ }