@sdata-plugin-adapter/utils 0.0.0-alpha.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.
@@ -0,0 +1,59 @@
1
+ import { AdapterProps } from '@sdata-plugin-adapter/types';
2
+
3
+ /**
4
+ * 泛型定义 :
5
+ * L: label 标签
6
+ * V: value 值
7
+ * C: code 代码
8
+ * test: build update version
9
+ */
10
+ interface EnumItem {
11
+ 0: string;
12
+ 1: string | number;
13
+ 2: string;
14
+ length: 3;
15
+ }
16
+ interface LabelsMap {
17
+ [key: string | number]: string;
18
+ }
19
+ interface CodesMap {
20
+ [code: string]: string | number;
21
+ }
22
+ interface DescMap {
23
+ [code: string]: string;
24
+ }
25
+ interface GetCodesFromValueMap {
26
+ [value: string | number]: string;
27
+ }
28
+ interface OptionItem {
29
+ [key: string]: string | number;
30
+ }
31
+ declare class Enum {
32
+ originalEnum: EnumItem[];
33
+ codes: CodesMap;
34
+ desc: DescMap;
35
+ labels: LabelsMap;
36
+ getCodesFromValue: GetCodesFromValueMap;
37
+ get length(): number | undefined;
38
+ get arrLabels(): (string | undefined)[];
39
+ get values(): (string | number | undefined)[];
40
+ get keys(): (string | undefined)[];
41
+ constructor(parameters: EnumItem[]);
42
+ options(label?: string, value?: string, key?: string): OptionItem[];
43
+ }
44
+
45
+ type LoadScriptPromise = Promise<void>;
46
+ declare function loadScript(path: string, loadFlag: string, mountedName: string, maxIntervalCount?: number): LoadScriptPromise;
47
+ declare function loadStyle(path: string, loadFlag: string): void;
48
+
49
+ declare const LANG_EN = "en-us";
50
+ declare const LANG_ZH = "zh-cn";
51
+ declare const lang_type: string;
52
+ declare const setLocalePrefix: (prefix: string) => void;
53
+ declare const setLang: (path: string) => Promise<void>;
54
+ declare const intlGetKey: (key: string, ...rest: any[]) => any;
55
+
56
+ declare const generateUUID: () => string;
57
+ declare const setDomAttributes: (dom: Element, props: AdapterProps, adapter: (props: AdapterProps) => AdapterProps) => Promise<void>;
58
+
59
+ export { Enum, LANG_EN, LANG_ZH, generateUUID, intlGetKey, lang_type, loadScript, loadStyle, setDomAttributes, setLang, setLocalePrefix };
package/dist/index.js ADDED
@@ -0,0 +1,223 @@
1
+ // index.ts
2
+ import configJson3 from "configJson";
3
+
4
+ // export/enum.ts
5
+ var Enum = class {
6
+ get length() {
7
+ return this.originalEnum?.length;
8
+ }
9
+ get arrLabels() {
10
+ return this.originalEnum?.map?.((item) => item[0]) ?? [];
11
+ }
12
+ get values() {
13
+ return this.originalEnum?.map?.((item) => item[1]) ?? [];
14
+ }
15
+ get keys() {
16
+ return this.originalEnum?.map?.((item) => item[2]) ?? [];
17
+ }
18
+ constructor(parameters) {
19
+ this.originalEnum = parameters;
20
+ let labels = {};
21
+ let codes = {};
22
+ let desc = {};
23
+ let getCodesFromValue = {};
24
+ parameters.forEach((item) => {
25
+ let label = item[0];
26
+ let value = item[1];
27
+ let code = item[2];
28
+ codes[code] = value;
29
+ desc[code] = label;
30
+ labels[value] = label;
31
+ getCodesFromValue[value] = code;
32
+ });
33
+ this.labels = labels;
34
+ this.desc = desc;
35
+ this.codes = codes;
36
+ this.getCodesFromValue = getCodesFromValue;
37
+ }
38
+ options(label = "label", value = "value", key = "key") {
39
+ return this.originalEnum?.map?.((item) => {
40
+ let outPut = {};
41
+ outPut[label] = item[0];
42
+ outPut[value] = item[1];
43
+ outPut[key] = item[2];
44
+ return outPut;
45
+ }) ?? [];
46
+ }
47
+ };
48
+
49
+ // export/loadSource.ts
50
+ import configJson from "configJson";
51
+ var isEnvProduction = !import.meta.env.DEV;
52
+ var host = configJson["online-development-mode"]?.host ?? "http://localhost";
53
+ var port = configJson["online-development-mode"]?.port ?? 3e3;
54
+ var currentScriptPath = getRuningScriptPath();
55
+ var productionPrefix = currentScriptPath.split("/").slice(0, -2).join("/") + "/assets/";
56
+ var prefix = isEnvProduction ? productionPrefix : `${host}:${port}/`;
57
+ function resolveFun(resolve, maxIntervalCount, mountedName) {
58
+ let count = 0;
59
+ const mountInterval = setInterval(() => {
60
+ if (count >= maxIntervalCount || (mountedName ? window[mountedName] : true)) {
61
+ clearInterval(mountInterval);
62
+ resolve();
63
+ }
64
+ count++;
65
+ }, 20);
66
+ }
67
+ function loadScript(path, loadFlag, mountedName, maxIntervalCount = 20) {
68
+ if (window?.[mountedName]) {
69
+ return new Promise((resolve) => {
70
+ resolve();
71
+ });
72
+ } else if (window?.[loadFlag]) {
73
+ return window[loadFlag];
74
+ }
75
+ const mountedFn = new Promise((resolve) => {
76
+ const script = document.createElement("script");
77
+ script.type = "text/javascript";
78
+ if (path.indexOf("http") > -1 || path.startsWith("//")) {
79
+ script.src = path;
80
+ } else {
81
+ script.src = `${prefix}${path}`;
82
+ }
83
+ document.head.appendChild(script);
84
+ if (script?.readyState) {
85
+ script.onreadystatechange = function() {
86
+ if (script.readyState == "loaded" || script.readyState == "complete") {
87
+ script.onreadystatechange = null;
88
+ resolveFun(resolve, maxIntervalCount, mountedName);
89
+ }
90
+ };
91
+ } else {
92
+ script.onload = function() {
93
+ resolveFun(resolve, maxIntervalCount, mountedName);
94
+ };
95
+ }
96
+ });
97
+ return window[loadFlag] = mountedFn;
98
+ }
99
+ function getRuningScriptPath() {
100
+ const currentScript = document.currentScript;
101
+ if (currentScript && currentScript.src !== "") {
102
+ return currentScript.src;
103
+ }
104
+ const match = new Error().stack?.match(/(https?:[^:]*).*\.js/);
105
+ return match?.[0] ?? "";
106
+ }
107
+ function loadStyle(path, loadFlag) {
108
+ if (window[loadFlag]) {
109
+ return;
110
+ }
111
+ const style = document.createElement("link");
112
+ if (path.indexOf("http") > -1 || path.startsWith("//")) {
113
+ style.href = path;
114
+ } else {
115
+ style.href = `${prefix}${path}`;
116
+ }
117
+ style.type = "text/css";
118
+ style.rel = "stylesheet";
119
+ document.head.appendChild(style);
120
+ window[loadFlag] = style;
121
+ }
122
+
123
+ // export/locales.ts
124
+ import configJson2 from "configJson";
125
+ var { isValidElement } = window.React ?? {};
126
+ var isEnvProduction2 = !import.meta.env.DEV;
127
+ var host2 = configJson2["online-development-mode"]?.host ?? "http://localhost";
128
+ var port2 = configJson2["online-development-mode"]?.port ?? 3e3;
129
+ var currentScriptPath2 = getRuningScriptPath();
130
+ var productionPrefix2 = currentScriptPath2.split("/").slice(0, -2).join("/") + "/assets/";
131
+ var prefix2 = isEnvProduction2 ? productionPrefix2 : `${host2}:${port2}/`;
132
+ var LANG_EN = "en-us";
133
+ var LANG_ZH = "zh-cn";
134
+ var localStorageLocaleKey = "lang_type";
135
+ var getLanguage = () => {
136
+ const language = window?.appSdk?.intl?.determineLocale?.({
137
+ localStorageLocaleKey
138
+ });
139
+ if (language?.toLowerCase()?.startsWith?.("zh")) {
140
+ return LANG_ZH;
141
+ } else {
142
+ return LANG_EN;
143
+ }
144
+ };
145
+ var lang_type = getLanguage();
146
+ var locale_prefix = `custom_plugin_locales_${configJson2.id}`;
147
+ var setLocalePrefix = (prefix3) => {
148
+ locale_prefix = prefix3;
149
+ };
150
+ var loadJSON = (path) => {
151
+ return new Promise((resolve, reject) => {
152
+ const url = path.startsWith("http") ? path : `${prefix2}${path}`;
153
+ fetch(url).then((res) => res.json()).then((data) => {
154
+ resolve(data);
155
+ }).catch(() => reject("\u83B7\u53D6\u56FD\u9645\u5316JSON\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5\u6587\u4EF6\u8DEF\u5F84\u662F\u5426\u6B63\u786E\uFF01"));
156
+ });
157
+ };
158
+ var setLang = async (path) => {
159
+ try {
160
+ const data = await loadJSON(path);
161
+ const locales = {
162
+ [lang_type]: () => Promise.resolve({ default: data })
163
+ };
164
+ await window.appSdk.loadIntl(locales, locale_prefix);
165
+ } catch (error) {
166
+ window.console.error(error);
167
+ }
168
+ };
169
+ var intlGetKey = (key, ...rest) => {
170
+ const regex = new RegExp("(.[\u4E00-\u9FA5]+)|([\u4E00-\u9FA5]+.)", "g");
171
+ if (isValidElement?.(key) || regex.test(key) || typeof key === "number" || typeof key === "object")
172
+ return key;
173
+ if (!key) return;
174
+ const res = window.appSdk.intl.get(
175
+ `${locale_prefix}.${key}` || "common.empty",
176
+ ...rest
177
+ );
178
+ return typeof res === "object" ? key : res.d(key);
179
+ };
180
+
181
+ // index.ts
182
+ var isEnvProd = import.meta.env.PROD;
183
+ var generateUUID = () => {
184
+ let d = (/* @__PURE__ */ new Date()).getTime();
185
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
186
+ let r = (d + Math.random() * 16) % 16 | 0;
187
+ d = Math.floor(d / 16);
188
+ return (c == "x" ? r : r & 3 | 8).toString(16);
189
+ });
190
+ };
191
+ var setDomAttributes = async (dom, props, adapter) => {
192
+ const pluginType = configJson3.type;
193
+ const adapterProps = adapter(props);
194
+ let {
195
+ customConfig = {},
196
+ customOptions,
197
+ type = ""
198
+ } = adapterProps;
199
+ if (pluginType === "bigscreen" || pluginType === "analyzer") {
200
+ customConfig = customOptions;
201
+ }
202
+ const requirementNum = configJson3["requirement-number"] ? `${configJson3["requirement-number"]}-` : "";
203
+ const domSymbol = customConfig?.id ? `${pluginType}-${type}-secondary-${requirementNum}${customConfig.id}` : `${pluginType}-${type}-secondary-${requirementNum}${generateUUID()}`;
204
+ let className = dom.className;
205
+ className = className ? `${className} ${domSymbol}` : domSymbol;
206
+ dom.setAttribute("class", className);
207
+ if (isEnvProd && type != "designConfiguration") {
208
+ console.info(`\u9700\u6C42\u7F16\u53F7:${configJson3["requirement-number"]}`, dom);
209
+ }
210
+ };
211
+ export {
212
+ Enum,
213
+ LANG_EN,
214
+ LANG_ZH,
215
+ generateUUID,
216
+ intlGetKey,
217
+ lang_type,
218
+ loadScript,
219
+ loadStyle,
220
+ setDomAttributes,
221
+ setLang,
222
+ setLocalePrefix
223
+ };
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@sdata-plugin-adapter/utils",
3
+ "version": "0.0.0-alpha.1",
4
+ "description": "toolset of smdata plugin",
5
+ "type": "module",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "peerDependencies": {
9
+ "@sdata-plugin-adapter/types": "0.0.0-alpha.1"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "author": "",
21
+ "license": "ISC",
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "scripts": {
26
+ "test": "echo \"Error: no test specified\" && exit 1",
27
+ "build": "tsup"
28
+ }
29
+ }