@vnejs/plugins.text.inject 0.1.1 → 0.1.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/dist/index.d.ts +1 -0
- package/dist/index.js +4 -0
- package/dist/modules/inject.d.ts +11 -0
- package/dist/modules/inject.js +31 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.js +1 -0
- package/dist/utils/inject.d.ts +22 -0
- package/dist/utils/inject.js +1 -0
- package/package.json +33 -6
- package/src/index.ts +6 -0
- package/{modules/inject.js → src/modules/inject.ts} +9 -5
- package/src/types.ts +11 -0
- package/src/utils/inject.ts +27 -0
- package/tsconfig.json +9 -0
- package/const/events.js +0 -3
- package/index.js +0 -7
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
import type { TextInjectPluginConstants, TextInjectPluginEvents, TextInjectPluginParams, TextInjectPluginSettings } from "../types.js";
|
|
3
|
+
import type { TextInjectHandlerEntry, TextInjectRegPayload, TextInjectReplacePayload } from "../utils/inject.js";
|
|
4
|
+
export declare class TextInject extends Module<TextInjectPluginEvents, TextInjectPluginConstants, TextInjectPluginSettings, TextInjectPluginParams> {
|
|
5
|
+
name: string;
|
|
6
|
+
handlers: TextInjectHandlerEntry[];
|
|
7
|
+
subscribe: () => void;
|
|
8
|
+
init: () => Promise<unknown[]> | undefined;
|
|
9
|
+
textInjectHandler: ({ text, state, args }?: TextInjectReplacePayload) => Promise<string>;
|
|
10
|
+
onInjectReg: ({ module, handler }?: TextInjectRegPayload) => void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
import { splitLineKeywords } from "@vnejs/helpers";
|
|
3
|
+
export class TextInject extends Module {
|
|
4
|
+
name = "text.inject";
|
|
5
|
+
handlers = [];
|
|
6
|
+
subscribe = () => {
|
|
7
|
+
this.on(this.EVENTS.TEXT_INJECT.REG, this.onInjectReg);
|
|
8
|
+
};
|
|
9
|
+
init = () => this.emit(this.EVENTS.TEXT.REPLACE_REG, { handler: this.textInjectHandler });
|
|
10
|
+
textInjectHandler = async ({ text = "", state = this.globalState, args = {} } = {}) => {
|
|
11
|
+
let resultText = text;
|
|
12
|
+
if (args?.inject) {
|
|
13
|
+
const injectKeys = Object.keys(args.inject);
|
|
14
|
+
for (let i = 0; i < injectKeys.length; i++) {
|
|
15
|
+
const [injectKey, injectValue] = [injectKeys[i], args.inject[injectKeys[i]]];
|
|
16
|
+
if (resultText.includes(injectKey)) {
|
|
17
|
+
const { line = "", module = "", keywords = [] } = splitLineKeywords(injectValue);
|
|
18
|
+
const handlerObj = this.handlers.find((e) => e.module === module);
|
|
19
|
+
if (handlerObj)
|
|
20
|
+
resultText = resultText.replace(injectKey, String(await handlerObj.handler({ line, keywords, state })));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return resultText;
|
|
25
|
+
};
|
|
26
|
+
onInjectReg = ({ module = "", handler } = {}) => {
|
|
27
|
+
if (!handler)
|
|
28
|
+
return;
|
|
29
|
+
this.handlers.push({ module, handler });
|
|
30
|
+
};
|
|
31
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
|
|
2
|
+
import type { PluginName as TextPluginName, SubscribeEvents as TextSubscribeEvents } from "@vnejs/plugins.text.contract";
|
|
3
|
+
import type { PluginName, SubscribeEvents } from "@vnejs/plugins.text.inject.contract";
|
|
4
|
+
export type TextInjectPluginEvents = VnePluginEventsMapRegistry & Record<PluginName, SubscribeEvents> & Record<TextPluginName, TextSubscribeEvents>;
|
|
5
|
+
export type TextInjectPluginConstants = VnePluginConstantsMapRegistry;
|
|
6
|
+
export type TextInjectPluginSettings = VnePluginSettingsMapRegistry;
|
|
7
|
+
export type TextInjectPluginParams = VnePluginParamsMapRegistry;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ModuleGlobalStateRegistry } from "@vnejs/module";
|
|
2
|
+
export type TextInjectHandlerPayload = {
|
|
3
|
+
line?: string;
|
|
4
|
+
keywords?: string[];
|
|
5
|
+
state?: ModuleGlobalStateRegistry;
|
|
6
|
+
};
|
|
7
|
+
export type TextInjectHandler = (payload: TextInjectHandlerPayload) => string | Promise<string>;
|
|
8
|
+
export type TextInjectRegPayload = {
|
|
9
|
+
module?: string;
|
|
10
|
+
handler?: TextInjectHandler;
|
|
11
|
+
};
|
|
12
|
+
export type TextInjectReplacePayload = {
|
|
13
|
+
text?: string;
|
|
14
|
+
state?: ModuleGlobalStateRegistry;
|
|
15
|
+
args?: {
|
|
16
|
+
inject?: Record<string, string>;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export type TextInjectHandlerEntry = {
|
|
20
|
+
module: string;
|
|
21
|
+
handler: TextInjectHandler;
|
|
22
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,17 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/plugins.text.inject",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.1.2",
|
|
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": "
|
|
11
|
-
"publish:minor": "
|
|
12
|
-
"publish:patch": "
|
|
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
|
-
"
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@vnejs/plugins.text.inject.contract": "~0.0.1"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@vnejs/helpers": "~0.1.0",
|
|
37
|
+
"@vnejs/module": "~0.0.1",
|
|
38
|
+
"@vnejs/plugins.text.contract": "~0.0.1",
|
|
39
|
+
"@vnejs/shared": "~0.0.9"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@vnejs/configs.ts-common": "~0.0.1"
|
|
43
|
+
}
|
|
17
44
|
}
|
package/src/index.ts
ADDED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { Module } from "@vnejs/module";
|
|
2
|
-
|
|
3
2
|
import { splitLineKeywords } from "@vnejs/helpers";
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
import type { TextInjectPluginConstants, TextInjectPluginEvents, TextInjectPluginParams, TextInjectPluginSettings } from "../types.js";
|
|
5
|
+
import type { TextInjectHandlerEntry, TextInjectRegPayload, TextInjectReplacePayload } from "../utils/inject.js";
|
|
6
|
+
|
|
7
|
+
export class TextInject extends Module<TextInjectPluginEvents, TextInjectPluginConstants, TextInjectPluginSettings, TextInjectPluginParams> {
|
|
6
8
|
name = "text.inject";
|
|
7
9
|
|
|
8
|
-
handlers = [];
|
|
10
|
+
handlers: TextInjectHandlerEntry[] = [];
|
|
9
11
|
|
|
10
12
|
subscribe = () => {
|
|
11
13
|
this.on(this.EVENTS.TEXT_INJECT.REG, this.onInjectReg);
|
|
@@ -13,7 +15,7 @@ export class TextInject extends Module {
|
|
|
13
15
|
|
|
14
16
|
init = () => this.emit(this.EVENTS.TEXT.REPLACE_REG, { handler: this.textInjectHandler });
|
|
15
17
|
|
|
16
|
-
textInjectHandler = async ({ text = "", state =
|
|
18
|
+
textInjectHandler = async ({ text = "", state = this.globalState, args = {} }: TextInjectReplacePayload = {}) => {
|
|
17
19
|
let resultText = text;
|
|
18
20
|
|
|
19
21
|
if (args?.inject) {
|
|
@@ -35,7 +37,9 @@ export class TextInject extends Module {
|
|
|
35
37
|
return resultText;
|
|
36
38
|
};
|
|
37
39
|
|
|
38
|
-
onInjectReg = ({ module, handler } = {}) => {
|
|
40
|
+
onInjectReg = ({ module = "", handler }: TextInjectRegPayload = {}) => {
|
|
41
|
+
if (!handler) return;
|
|
42
|
+
|
|
39
43
|
this.handlers.push({ module, handler });
|
|
40
44
|
};
|
|
41
45
|
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
|
|
2
|
+
import type { PluginName as TextPluginName, SubscribeEvents as TextSubscribeEvents } from "@vnejs/plugins.text.contract";
|
|
3
|
+
import type { PluginName, SubscribeEvents } from "@vnejs/plugins.text.inject.contract";
|
|
4
|
+
|
|
5
|
+
export type TextInjectPluginEvents = VnePluginEventsMapRegistry & Record<PluginName, SubscribeEvents> & Record<TextPluginName, TextSubscribeEvents>;
|
|
6
|
+
|
|
7
|
+
export type TextInjectPluginConstants = VnePluginConstantsMapRegistry;
|
|
8
|
+
|
|
9
|
+
export type TextInjectPluginSettings = VnePluginSettingsMapRegistry;
|
|
10
|
+
|
|
11
|
+
export type TextInjectPluginParams = VnePluginParamsMapRegistry;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ModuleGlobalStateRegistry } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export type TextInjectHandlerPayload = {
|
|
4
|
+
line?: string;
|
|
5
|
+
keywords?: string[];
|
|
6
|
+
state?: ModuleGlobalStateRegistry;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type TextInjectHandler = (payload: TextInjectHandlerPayload) => string | Promise<string>;
|
|
10
|
+
|
|
11
|
+
export type TextInjectRegPayload = {
|
|
12
|
+
module?: string;
|
|
13
|
+
handler?: TextInjectHandler;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type TextInjectReplacePayload = {
|
|
17
|
+
text?: string;
|
|
18
|
+
state?: ModuleGlobalStateRegistry;
|
|
19
|
+
args?: {
|
|
20
|
+
inject?: Record<string, string>;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type TextInjectHandlerEntry = {
|
|
25
|
+
module: string;
|
|
26
|
+
handler: TextInjectHandler;
|
|
27
|
+
};
|
package/tsconfig.json
ADDED
package/const/events.js
DELETED