@vnejs/plugins.text.locs 0.1.6 → 0.1.8

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 @@
1
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+ import { PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/plugins.text.locs.contract";
3
+ import { TextLocs } from "./modules/locs.js";
4
+ regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS }, [TextLocs]);
@@ -0,0 +1,19 @@
1
+ import { Module } from "@vnejs/module";
2
+ import type { LocsPluginConstants, LocsPluginEvents, LocsPluginParams, LocsPluginSettings } from "../types.js";
3
+ import type { ComponentsInjectResult, LocsComponent, LocsGetPayload, LocsLoadPayload, SettingsChangedPayload, TextReplaceHandlerPayload } from "../utils/locs.js";
4
+ export declare class TextLocs extends Module<LocsPluginEvents, LocsPluginConstants, LocsPluginSettings, LocsPluginParams> {
5
+ name: string;
6
+ loading: Record<string, Promise<void> | undefined>;
7
+ subscribe: () => void;
8
+ init: () => Promise<unknown[] | undefined>;
9
+ componentsInjectHandler: (component: LocsComponent) => Promise<ComponentsInjectResult>;
10
+ textReplaceHandler: ({ text, label }?: TextReplaceHandlerPayload) => Promise<string>;
11
+ onLocsLoad: ({ label, lang }?: LocsLoadPayload) => Promise<void>;
12
+ onLocsChanged: () => Promise<void>;
13
+ onLocsGet: ({ label, lang }?: LocsGetPayload) => Promise<unknown>;
14
+ onSettingChange: ({ name, value }?: SettingsChangedPayload) => false | Promise<unknown[]> | undefined;
15
+ getLoadedLocs: (label?: string, lang?: string | null) => Promise<unknown> | undefined;
16
+ loadLoc: (label?: string) => Promise<void>;
17
+ setLang: (lang: string) => Promise<unknown[]> | undefined;
18
+ checkLang: () => boolean;
19
+ }
@@ -0,0 +1,73 @@
1
+ import { Module } from "@vnejs/module";
2
+ import { toJson } from "@vnejs/helpers";
3
+ export class TextLocs extends Module {
4
+ name = "text.locs";
5
+ loading = {};
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.LOCS.LOAD, this.onLocsLoad);
8
+ this.on(this.EVENTS.LOCS.GET, this.onLocsGet);
9
+ this.on(this.EVENTS.LOCS.CHANGED, this.onLocsChanged);
10
+ this.on(this.EVENTS.SETTINGS.CHANGED, this.onSettingChange);
11
+ };
12
+ init = async () => {
13
+ await Promise.all([
14
+ this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.LOCS.LANG, value: this.PARAMS.LOCS.DEFAULT_LANG }),
15
+ this.emit(this.EVENTS.MEMORY.REG, { module: this.name }),
16
+ this.emit(this.EVENTS.COMPONENTS.INJECT_REG, { handler: this.componentsInjectHandler }),
17
+ this.emit(this.EVENTS.TEXT.REPLACE_REG, { priority: -999999, handler: this.textReplaceHandler }),
18
+ ]);
19
+ return this.setLang(this.shared.settings[this.SETTINGS.LOCS.LANG]);
20
+ };
21
+ componentsInjectHandler = async (component) => {
22
+ const result = { field: "locs", value: {} };
23
+ if (component.locLabel)
24
+ result.value = (await this.emitOne(this.EVENTS.LOCS.GET, { label: component.locLabel }));
25
+ return result;
26
+ };
27
+ textReplaceHandler = async ({ text = "", label = "" } = {}) => {
28
+ const locs = (await this.emitOne(this.EVENTS.LOCS.GET, { label }));
29
+ return locs?.[text] || text;
30
+ };
31
+ onLocsLoad = async ({ label, lang } = {}) => {
32
+ await this.waitRerenderCheck(this.checkLang);
33
+ const mediaId = `${lang || this.shared.lang}:${label}`;
34
+ const memoryLocs = await this.emitOne(this.EVENTS.MEMORY.GET, { module: this.name, key: mediaId });
35
+ if (memoryLocs)
36
+ return;
37
+ if (this.loading[mediaId])
38
+ return this.loading[mediaId];
39
+ return this.loadLoc(label);
40
+ };
41
+ onLocsChanged = async () => {
42
+ const { label } = this.globalState.scenario;
43
+ const loadedLocs = await this.getLoadedLocs(label);
44
+ if (!loadedLocs)
45
+ await this.emit(this.EVENTS.LOCS.LOAD, { label });
46
+ await this.emit(this.EVENTS.TEXT.RECALC);
47
+ };
48
+ onLocsGet = async ({ label = this.PARAMS.SCENARIO.INIT_LABEL_NAME, lang = null } = {}) => {
49
+ await this.waitRerenderCheck(this.checkLang);
50
+ const loadedLocs = await this.getLoadedLocs(label, lang);
51
+ if (loadedLocs)
52
+ return loadedLocs;
53
+ await this.emit(this.EVENTS.LOCS.LOAD, { label, lang });
54
+ return this.getLoadedLocs(label, lang);
55
+ };
56
+ onSettingChange = ({ name, value } = {}) => name === this.SETTINGS.LOCS.LANG && this.setLang(value);
57
+ getLoadedLocs = (label, lang) => this.emitOne(this.EVENTS.MEMORY.GET, { module: this.name, key: `${lang || this.shared.lang}:${label}` });
58
+ loadLoc = (label) => {
59
+ const [loadingId, url] = [`${this.shared.lang}:${label}`, this.media.locs?.[`${this.shared.lang}/${label}`]];
60
+ const promiseFunc = async () => {
61
+ const value = url ? (await fetch(`${this.CONST.SYSTEM.HOST}/${url}`).then(toJson)) : {};
62
+ await this.emit(this.EVENTS.MEMORY.SET, { module: this.name, key: loadingId, value });
63
+ delete this.loading[loadingId];
64
+ };
65
+ this.loading[loadingId] = promiseFunc();
66
+ return this.loading[loadingId];
67
+ };
68
+ setLang = (lang) => {
69
+ this.shared.lang = lang;
70
+ return this.emit(this.EVENTS.LOCS.CHANGED);
71
+ };
72
+ checkLang = () => Boolean(this.shared.lang);
73
+ }
@@ -0,0 +1,12 @@
1
+ import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
2
+ import type { PluginName as ComponentsPluginName, SubscribeEvents as ComponentsSubscribeEvents } from "@vnejs/plugins.core.components.contract";
3
+ import type { PluginName as MemoryPluginName, SubscribeEvents as MemorySubscribeEvents } from "@vnejs/plugins.core.memory.contract";
4
+ import type { Params as ScenarioParams, PluginName as ScenarioPluginName, SubscribeEvents as ScenarioSubscribeEvents } from "@vnejs/plugins.core.scenario.contract";
5
+ import type { PluginName as SettingsPluginName, SubscribeEvents as SettingsSubscribeEvents } from "@vnejs/plugins.core.settings.contract";
6
+ import type { Constants as SystemConstants, PluginName as SystemPluginName, SubscribeEvents as SystemSubscribeEvents } from "@vnejs/plugins.core.system.contract";
7
+ import type { PluginName as TextPluginName, SubscribeEvents as TextSubscribeEvents } from "@vnejs/plugins.text.contract";
8
+ import type { Params, PluginName, SettingsKeys, SubscribeEvents } from "@vnejs/plugins.text.locs.contract";
9
+ export type LocsPluginEvents = VnePluginEventsMapRegistry & Record<PluginName, SubscribeEvents> & Record<ComponentsPluginName, ComponentsSubscribeEvents> & Record<MemoryPluginName, MemorySubscribeEvents> & Record<ScenarioPluginName, ScenarioSubscribeEvents> & Record<SettingsPluginName, SettingsSubscribeEvents> & Record<SystemPluginName, SystemSubscribeEvents> & Record<TextPluginName, TextSubscribeEvents>;
10
+ export type LocsPluginConstants = VnePluginConstantsMapRegistry & Record<SystemPluginName, SystemConstants>;
11
+ export type LocsPluginSettings = VnePluginSettingsMapRegistry & Record<PluginName, SettingsKeys>;
12
+ export type LocsPluginParams = VnePluginParamsMapRegistry & Record<PluginName, Params> & Record<ScenarioPluginName, ScenarioParams>;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ export type LocsRecord = Record<string, string>;
2
+ export type LocsLoadPayload = {
3
+ label?: string;
4
+ lang?: string | null;
5
+ };
6
+ export type LocsGetPayload = {
7
+ label?: string;
8
+ lang?: string | null;
9
+ };
10
+ export type LocsComponent = {
11
+ locLabel?: string;
12
+ };
13
+ export type ComponentsInjectResult = {
14
+ field: string;
15
+ value: LocsRecord;
16
+ };
17
+ export type SettingsChangedPayload = {
18
+ name?: string;
19
+ value?: unknown;
20
+ };
21
+ export type TextReplaceHandlerPayload = {
22
+ text?: string;
23
+ label?: string;
24
+ };
25
+ export type ComponentsInjectHandler = (component: LocsComponent) => ComponentsInjectResult | Promise<ComponentsInjectResult>;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,17 +1,49 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.text.locs",
3
- "version": "0.1.6",
4
- "main": "index.js",
3
+ "version": "0.1.8",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src",
18
+ "tsconfig.json"
19
+ ],
5
20
  "scripts": {
6
21
  "test": "echo \"Error: no test specified\" && exit 1",
22
+ "build": "npx @vnejs/monorepo package",
7
23
  "publish:major:plugin": "npm run publish:major",
8
24
  "publish:minor:plugin": "npm run publish:minor",
9
25
  "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"
26
+ "publish:major": "npx @vnejs/monorepo publish major --access public",
27
+ "publish:minor": "npx @vnejs/monorepo publish minor --access public",
28
+ "publish:patch": "npx @vnejs/monorepo publish patch --access public"
13
29
  },
14
30
  "author": "",
15
31
  "license": "ISC",
16
- "description": ""
32
+ "dependencies": {
33
+ "@vnejs/plugins.text.locs.contract": "~0.0.1"
34
+ },
35
+ "peerDependencies": {
36
+ "@vnejs/helpers": "~0.1.0",
37
+ "@vnejs/module": "~0.0.1",
38
+ "@vnejs/plugins.core.components.contract": "~0.0.1",
39
+ "@vnejs/plugins.core.memory.contract": "~0.0.1",
40
+ "@vnejs/plugins.core.scenario.contract": "~0.0.1",
41
+ "@vnejs/plugins.core.settings.contract": "~0.0.1",
42
+ "@vnejs/plugins.core.system.contract": "~0.0.1",
43
+ "@vnejs/plugins.text.contract": "~0.0.1",
44
+ "@vnejs/shared": "~0.0.9"
45
+ },
46
+ "devDependencies": {
47
+ "@vnejs/configs.ts-common": "~0.0.1"
48
+ }
17
49
  }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+ import { PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/plugins.text.locs.contract";
3
+
4
+ import { TextLocs } from "./modules/locs.js";
5
+
6
+ regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS }, [TextLocs]);
@@ -1,11 +1,21 @@
1
1
  import { Module } from "@vnejs/module";
2
-
3
- const toJson = (i) => i.json();
4
-
5
- export class TextLocs extends Module {
2
+ import { toJson } from "@vnejs/helpers";
3
+
4
+ import type { LocsPluginConstants, LocsPluginEvents, LocsPluginParams, LocsPluginSettings } from "../types.js";
5
+ import type {
6
+ ComponentsInjectResult,
7
+ LocsComponent,
8
+ LocsGetPayload,
9
+ LocsLoadPayload,
10
+ LocsRecord,
11
+ SettingsChangedPayload,
12
+ TextReplaceHandlerPayload,
13
+ } from "../utils/locs.js";
14
+
15
+ export class TextLocs extends Module<LocsPluginEvents, LocsPluginConstants, LocsPluginSettings, LocsPluginParams> {
6
16
  name = "text.locs";
7
17
 
8
- loading = {};
18
+ loading: Record<string, Promise<void> | undefined> = {};
9
19
 
10
20
  subscribe = () => {
11
21
  this.on(this.EVENTS.LOCS.LOAD, this.onLocsLoad);
@@ -25,20 +35,22 @@ export class TextLocs extends Module {
25
35
 
26
36
  return this.setLang(this.shared.settings[this.SETTINGS.LOCS.LANG]);
27
37
  };
28
- componentsInjectHandler = async (component) => {
29
- const result = { field: "locs", value: {} };
30
38
 
31
- if (component.locLabel) result.value = await this.emitOne(this.EVENTS.LOCS.GET, { label: component.locLabel });
39
+ componentsInjectHandler = async (component: LocsComponent): Promise<ComponentsInjectResult> => {
40
+ const result: ComponentsInjectResult = { field: "locs", value: {} };
41
+
42
+ if (component.locLabel) result.value = (await this.emitOne(this.EVENTS.LOCS.GET, { label: component.locLabel })) as LocsRecord;
32
43
 
33
44
  return result;
34
45
  };
35
- textReplaceHandler = async ({ text = "", label = "" } = {}) => {
36
- const locs = await this.emitOne(this.EVENTS.LOCS.GET, { label });
46
+
47
+ textReplaceHandler = async ({ text = "", label = "" }: TextReplaceHandlerPayload = {}) => {
48
+ const locs = (await this.emitOne(this.EVENTS.LOCS.GET, { label })) as LocsRecord | undefined;
37
49
 
38
50
  return locs?.[text] || text;
39
51
  };
40
52
 
41
- onLocsLoad = async ({ label, lang } = {}) => {
53
+ onLocsLoad = async ({ label, lang }: LocsLoadPayload = {}) => {
42
54
  await this.waitRerenderCheck(this.checkLang);
43
55
 
44
56
  const mediaId = `${lang || this.shared.lang}:${label}`;
@@ -49,6 +61,7 @@ export class TextLocs extends Module {
49
61
 
50
62
  return this.loadLoc(label);
51
63
  };
64
+
52
65
  onLocsChanged = async () => {
53
66
  const { label } = this.globalState.scenario;
54
67
 
@@ -58,7 +71,8 @@ export class TextLocs extends Module {
58
71
 
59
72
  await this.emit(this.EVENTS.TEXT.RECALC);
60
73
  };
61
- onLocsGet = async ({ label = this.PARAMS.SCENARIO.INIT_LABEL_NAME, lang = null } = {}) => {
74
+
75
+ onLocsGet = async ({ label = this.PARAMS.SCENARIO.INIT_LABEL_NAME, lang = null }: LocsGetPayload = {}) => {
62
76
  await this.waitRerenderCheck(this.checkLang);
63
77
 
64
78
  const loadedLocs = await this.getLoadedLocs(label, lang);
@@ -69,14 +83,17 @@ export class TextLocs extends Module {
69
83
 
70
84
  return this.getLoadedLocs(label, lang);
71
85
  };
72
- onSettingChange = ({ name, value } = {}) => name === this.SETTINGS.LOCS.LANG && this.setLang(value);
73
86
 
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}`]];
87
+ onSettingChange = ({ name, value }: SettingsChangedPayload = {}) => name === this.SETTINGS.LOCS.LANG && this.setLang(value as string);
88
+
89
+ getLoadedLocs = (label?: string, lang?: string | null) =>
90
+ this.emitOne(this.EVENTS.MEMORY.GET, { module: this.name, key: `${lang || this.shared.lang}:${label}` });
91
+
92
+ loadLoc = (label?: string) => {
93
+ const [loadingId, url] = [`${this.shared.lang}:${label}`, this.media.locs?.[`${this.shared.lang}/${label}`]];
77
94
 
78
95
  const promiseFunc = async () => {
79
- const value = url ? await fetch(`${this.CONST.SYSTEM.HOST}/${url}`).then(toJson) : {};
96
+ const value = url ? ((await fetch(`${this.CONST.SYSTEM.HOST}/${url}`).then(toJson)) as LocsRecord) : {};
80
97
 
81
98
  await this.emit(this.EVENTS.MEMORY.SET, { module: this.name, key: loadingId, value });
82
99
 
@@ -87,10 +104,12 @@ export class TextLocs extends Module {
87
104
 
88
105
  return this.loading[loadingId];
89
106
  };
90
- setLang = (lang) => {
107
+
108
+ setLang = (lang: string) => {
91
109
  this.shared.lang = lang;
92
110
 
93
111
  return this.emit(this.EVENTS.LOCS.CHANGED);
94
112
  };
95
- checkLang = () => this.shared.lang;
113
+
114
+ checkLang = (): boolean => Boolean(this.shared.lang);
96
115
  }
package/src/types.ts ADDED
@@ -0,0 +1,23 @@
1
+ import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
2
+ import type { PluginName as ComponentsPluginName, SubscribeEvents as ComponentsSubscribeEvents } from "@vnejs/plugins.core.components.contract";
3
+ import type { PluginName as MemoryPluginName, SubscribeEvents as MemorySubscribeEvents } from "@vnejs/plugins.core.memory.contract";
4
+ import type { Params as ScenarioParams, PluginName as ScenarioPluginName, SubscribeEvents as ScenarioSubscribeEvents } from "@vnejs/plugins.core.scenario.contract";
5
+ import type { PluginName as SettingsPluginName, SubscribeEvents as SettingsSubscribeEvents } from "@vnejs/plugins.core.settings.contract";
6
+ import type { Constants as SystemConstants, PluginName as SystemPluginName, SubscribeEvents as SystemSubscribeEvents } from "@vnejs/plugins.core.system.contract";
7
+ import type { PluginName as TextPluginName, SubscribeEvents as TextSubscribeEvents } from "@vnejs/plugins.text.contract";
8
+ import type { Params, PluginName, SettingsKeys, SubscribeEvents } from "@vnejs/plugins.text.locs.contract";
9
+
10
+ export type LocsPluginEvents = VnePluginEventsMapRegistry &
11
+ Record<PluginName, SubscribeEvents> &
12
+ Record<ComponentsPluginName, ComponentsSubscribeEvents> &
13
+ Record<MemoryPluginName, MemorySubscribeEvents> &
14
+ Record<ScenarioPluginName, ScenarioSubscribeEvents> &
15
+ Record<SettingsPluginName, SettingsSubscribeEvents> &
16
+ Record<SystemPluginName, SystemSubscribeEvents> &
17
+ Record<TextPluginName, TextSubscribeEvents>;
18
+
19
+ export type LocsPluginConstants = VnePluginConstantsMapRegistry & Record<SystemPluginName, SystemConstants>;
20
+
21
+ export type LocsPluginSettings = VnePluginSettingsMapRegistry & Record<PluginName, SettingsKeys>;
22
+
23
+ export type LocsPluginParams = VnePluginParamsMapRegistry & Record<PluginName, Params> & Record<ScenarioPluginName, ScenarioParams>;
@@ -0,0 +1,32 @@
1
+ export type LocsRecord = Record<string, string>;
2
+
3
+ export type LocsLoadPayload = {
4
+ label?: string;
5
+ lang?: string | null;
6
+ };
7
+
8
+ export type LocsGetPayload = {
9
+ label?: string;
10
+ lang?: string | null;
11
+ };
12
+
13
+ export type LocsComponent = {
14
+ locLabel?: string;
15
+ };
16
+
17
+ export type ComponentsInjectResult = {
18
+ field: string;
19
+ value: LocsRecord;
20
+ };
21
+
22
+ export type SettingsChangedPayload = {
23
+ name?: string;
24
+ value?: unknown;
25
+ };
26
+
27
+ export type TextReplaceHandlerPayload = {
28
+ text?: string;
29
+ label?: string;
30
+ };
31
+
32
+ export type ComponentsInjectHandler = (component: LocsComponent) => ComponentsInjectResult | Promise<ComponentsInjectResult>;
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@vnejs/configs.ts-common/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*.ts"],
8
+ "exclude": ["dist", "node_modules"]
9
+ }
package/const/events.js DELETED
@@ -1,5 +0,0 @@
1
- export const SUBSCRIBE_EVENTS = {
2
- LOAD: "vne:locs:load",
3
- GET: "vne:locs:get",
4
- CHANGED: "vne:locs:changed",
5
- };
package/const/params.js DELETED
@@ -1 +0,0 @@
1
- export const DEFAULT_LANG = "ru";
package/const/settings.js DELETED
@@ -1 +0,0 @@
1
- export const SETTINGS_KEYS = { LANG: "vne:locs:lang" };
package/index.js DELETED
@@ -1,9 +0,0 @@
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]);