@sdata-plugin-adapter/utils 0.0.0-alpha.1 → 0.0.0-alpha.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.
@@ -1,5 +1,6 @@
1
- import { AdapterProps } from '@sdata-plugin-adapter/types';
1
+ import { AdapterProps } from "@sdata-plugin-adapter/types";
2
2
 
3
+ //#region export/enum.d.ts
3
4
  /**
4
5
  * 泛型定义 :
5
6
  * L: label 标签
@@ -8,52 +9,56 @@ import { AdapterProps } from '@sdata-plugin-adapter/types';
8
9
  * test: build update version
9
10
  */
10
11
  interface EnumItem {
11
- 0: string;
12
- 1: string | number;
13
- 2: string;
14
- length: 3;
12
+ 0: string;
13
+ 1: string | number;
14
+ 2: string;
15
+ length: 3;
15
16
  }
16
17
  interface LabelsMap {
17
- [key: string | number]: string;
18
+ [key: string | number]: string;
18
19
  }
19
20
  interface CodesMap {
20
- [code: string]: string | number;
21
+ [code: string]: string | number;
21
22
  }
22
23
  interface DescMap {
23
- [code: string]: string;
24
+ [code: string]: string;
24
25
  }
25
26
  interface GetCodesFromValueMap {
26
- [value: string | number]: string;
27
+ [value: string | number]: string;
27
28
  }
28
29
  interface OptionItem {
29
- [key: string]: string | number;
30
+ [key: string]: string | number;
30
31
  }
31
32
  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[];
33
+ originalEnum: EnumItem[];
34
+ codes: CodesMap;
35
+ desc: DescMap;
36
+ labels: LabelsMap;
37
+ getCodesFromValue: GetCodesFromValueMap;
38
+ get length(): number | undefined;
39
+ get arrLabels(): (string | undefined)[];
40
+ get values(): (string | number | undefined)[];
41
+ get keys(): (string | undefined)[];
42
+ constructor(parameters: EnumItem[]);
43
+ options(label?: string, value?: string, key?: string): OptionItem[];
43
44
  }
44
-
45
+ //#endregion
46
+ //#region export/loadSource.d.ts
45
47
  type LoadScriptPromise = Promise<void>;
46
48
  declare function loadScript(path: string, loadFlag: string, mountedName: string, maxIntervalCount?: number): LoadScriptPromise;
47
49
  declare function loadStyle(path: string, loadFlag: string): void;
48
-
50
+ //#endregion
51
+ //#region export/locales.d.ts
49
52
  declare const LANG_EN = "en-us";
50
53
  declare const LANG_ZH = "zh-cn";
51
54
  declare const lang_type: string;
52
55
  declare const setLocalePrefix: (prefix: string) => void;
53
56
  declare const setLang: (path: string) => Promise<void>;
54
57
  declare const intlGetKey: (key: string, ...rest: any[]) => any;
55
-
58
+ //#endregion
59
+ //#region index.d.ts
56
60
  declare const generateUUID: () => string;
57
61
  declare const setDomAttributes: (dom: Element, props: AdapterProps, adapter: (props: AdapterProps) => AdapterProps) => Promise<void>;
58
-
62
+ //#endregion
59
63
  export { Enum, LANG_EN, LANG_ZH, generateUUID, intlGetKey, lang_type, loadScript, loadStyle, setDomAttributes, setLang, setLocalePrefix };
64
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../export/enum.ts","../export/loadSource.ts","../export/locales.ts","../index.ts"],"mappings":";;;;;;;;;;UAYU,QAAA;EACR,CAAA;EACA,CAAA;EACA,CAAA;EACA,MAAA;AAAA;AAAA,UAGQ,SAAA;EAAA,CACP,GAAA;AAAA;AAAA,UAGO,QAAA;EAAA,CACP,IAAA;AAAA;AAAA,UAGO,OAAA;EAAA,CACP,IAAA;AAAA;AAAA,UAGO,oBAAA;EAAA,CACP,KAAA;AAAA;AAAA,UAGO,UAAA;EAAA,CACP,GAAA;AAAA;AAAA,cAGU,IAAA;EACX,YAAA,EAAc,QAAA;EAEd,KAAA,EAAO,QAAA;EAEP,IAAA,EAAM,OAAA;EAEN,MAAA,EAAQ,SAAA;EAER,iBAAA,EAAmB,oBAAA;EAAA,IAEf,MAAA,CAAA;EAAA,IAIA,SAAA,CAAA;EAAA,IAIA,MAAA,CAAA;EAAA,IAIA,IAAA,CAAA;cAIQ,UAAA,EAAY,QAAA;EAuBxB,OAAA,CACE,KAAA,WACA,KAAA,WACA,GAAA,YACC,UAAA;AAAA;;;KCjFO,iBAAA,GAAoB,OAAA;AAAA,iBA0BhB,UAAA,CACd,IAAA,UACA,QAAA,UACA,WAAA,UACA,gBAAA,YACC,iBAAA;AAAA,iBAgDa,SAAA,CAAU,IAAA,UAAc,QAAA;;;cC7E3B,OAAA;AAAA,cACA,OAAA;AAAA,cAeA,SAAA;AAAA,cAIA,eAAA,GAAmB,MAAA;AAAA,cAkBnB,OAAA,GAAiB,IAAA,aAAY,OAAA;AAAA,cAa7B,UAAA,GAAc,GAAA,aAAgB,IAAA;;;cChD9B,YAAA;AAAA,cASA,gBAAA,GACX,GAAA,EAAK,OAAA,EACL,KAAA,EAAO,YAAA,EACP,OAAA,GAAU,KAAA,EAAO,YAAA,KAAiB,YAAA,KAAY,OAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,174 @@
1
+ import configJson from "configJson";
2
+
3
+ //#region export/enum.ts
4
+ var Enum = class {
5
+ get length() {
6
+ return this.originalEnum?.length;
7
+ }
8
+ get arrLabels() {
9
+ return this.originalEnum?.map?.((item) => item[0]) ?? [];
10
+ }
11
+ get values() {
12
+ return this.originalEnum?.map?.((item) => item[1]) ?? [];
13
+ }
14
+ get keys() {
15
+ return this.originalEnum?.map?.((item) => item[2]) ?? [];
16
+ }
17
+ constructor(parameters) {
18
+ this.originalEnum = parameters;
19
+ let labels = {};
20
+ let codes = {};
21
+ let desc = {};
22
+ let getCodesFromValue = {};
23
+ parameters.forEach((item) => {
24
+ let label = item[0];
25
+ let value = item[1];
26
+ let code = item[2];
27
+ codes[code] = value;
28
+ desc[code] = label;
29
+ labels[value] = label;
30
+ getCodesFromValue[value] = code;
31
+ });
32
+ this.labels = labels;
33
+ this.desc = desc;
34
+ this.codes = codes;
35
+ this.getCodesFromValue = getCodesFromValue;
36
+ }
37
+ options(label = "label", value = "value", key = "key") {
38
+ return this.originalEnum?.map?.((item) => {
39
+ let outPut = {};
40
+ outPut[label] = item[0];
41
+ outPut[value] = item[1];
42
+ outPut[key] = item[2];
43
+ return outPut;
44
+ }) ?? [];
45
+ }
46
+ };
47
+
48
+ //#endregion
49
+ //#region export/loadSource.ts
50
+ const isEnvProduction$1 = !import.meta.env.DEV;
51
+ const host$1 = configJson["online-development-mode"]?.host ?? "http://localhost";
52
+ const port$1 = configJson["online-development-mode"]?.port ?? 3e3;
53
+ let productionPrefix$1 = getRuningScriptPath().split("/").slice(0, -2).join("/") + "/assets/";
54
+ const prefix$1 = isEnvProduction$1 ? productionPrefix$1 : `${host$1}:${port$1}/`;
55
+ function resolveFun(resolve, maxIntervalCount, mountedName) {
56
+ let count = 0;
57
+ const mountInterval = setInterval(() => {
58
+ if (count >= maxIntervalCount || (mountedName ? window[mountedName] : true)) {
59
+ clearInterval(mountInterval);
60
+ resolve();
61
+ }
62
+ count++;
63
+ }, 20);
64
+ }
65
+ function loadScript(path, loadFlag, mountedName, maxIntervalCount = 20) {
66
+ if (window?.[mountedName]) return new Promise((resolve) => {
67
+ resolve();
68
+ });
69
+ else if (window?.[loadFlag]) return window[loadFlag];
70
+ const mountedFn = new Promise((resolve) => {
71
+ const script = document.createElement("script");
72
+ script.type = "text/javascript";
73
+ if (path.indexOf("http") > -1 || path.startsWith("//")) script.src = path;
74
+ else script.src = `${prefix$1}${path}`;
75
+ document.head.appendChild(script);
76
+ if (script?.readyState) script.onreadystatechange = function() {
77
+ if (script.readyState == "loaded" || script.readyState == "complete") {
78
+ script.onreadystatechange = null;
79
+ resolveFun(resolve, maxIntervalCount, mountedName);
80
+ }
81
+ };
82
+ else script.onload = function() {
83
+ resolveFun(resolve, maxIntervalCount, mountedName);
84
+ };
85
+ });
86
+ return window[loadFlag] = mountedFn;
87
+ }
88
+ function getRuningScriptPath() {
89
+ const currentScript = document.currentScript;
90
+ if (currentScript && currentScript.src !== "") return currentScript.src;
91
+ return ((/* @__PURE__ */ new Error()).stack?.match(/(https?:[^:]*).*\.js/))?.[0] ?? "";
92
+ }
93
+ function loadStyle(path, loadFlag) {
94
+ if (window[loadFlag]) return;
95
+ const style = document.createElement("link");
96
+ if (path.indexOf("http") > -1 || path.startsWith("//")) style.href = path;
97
+ else style.href = `${prefix$1}${path}`;
98
+ style.type = "text/css";
99
+ style.rel = "stylesheet";
100
+ document.head.appendChild(style);
101
+ window[loadFlag] = style;
102
+ }
103
+
104
+ //#endregion
105
+ //#region export/locales.ts
106
+ const { isValidElement } = window.React ?? {};
107
+ const isEnvProduction = !import.meta.env.DEV;
108
+ const host = configJson["online-development-mode"]?.host ?? "http://localhost";
109
+ const port = configJson["online-development-mode"]?.port ?? 3e3;
110
+ let productionPrefix = getRuningScriptPath().split("/").slice(0, -2).join("/") + "/assets/";
111
+ const prefix = isEnvProduction ? productionPrefix : `${host}:${port}/`;
112
+ const LANG_EN = "en-us";
113
+ const LANG_ZH = "zh-cn";
114
+ const localStorageLocaleKey = "lang_type";
115
+ const getLanguage = () => {
116
+ if ((window?.appSdk?.intl?.determineLocale?.({ localStorageLocaleKey }))?.toLowerCase()?.startsWith?.("zh")) return LANG_ZH;
117
+ else return LANG_EN;
118
+ };
119
+ const lang_type = getLanguage();
120
+ let locale_prefix = `custom_plugin_locales_${configJson.id}`;
121
+ const setLocalePrefix = (prefix) => {
122
+ locale_prefix = prefix;
123
+ };
124
+ const loadJSON = (path) => {
125
+ return new Promise((resolve, reject) => {
126
+ const url = path.startsWith("http") ? path : `${prefix}${path}`;
127
+ fetch(url).then((res) => res.json()).then((data) => {
128
+ resolve(data);
129
+ }).catch(() => reject("获取国际化JSON失败,请检查文件路径是否正确!"));
130
+ });
131
+ };
132
+ const setLang = async (path) => {
133
+ try {
134
+ const data = await loadJSON(path);
135
+ const locales = { [lang_type]: () => Promise.resolve({ default: data }) };
136
+ await window.appSdk.loadIntl(locales, locale_prefix);
137
+ } catch (error) {
138
+ window.console.error(error);
139
+ }
140
+ };
141
+ const intlGetKey = (key, ...rest) => {
142
+ const regex = /* @__PURE__ */ new RegExp("(.[一-龥]+)|([一-龥]+.)", "g");
143
+ if (isValidElement?.(key) || regex.test(key) || typeof key === "number" || typeof key === "object") return key;
144
+ if (!key) return;
145
+ const res = window.appSdk.intl.get(`${locale_prefix}.${key}` || "common.empty", ...rest);
146
+ return typeof res === "object" ? key : res.d(key);
147
+ };
148
+
149
+ //#endregion
150
+ //#region index.ts
151
+ const isEnvProd = import.meta.env.PROD;
152
+ const generateUUID = () => {
153
+ let d = (/* @__PURE__ */ new Date()).getTime();
154
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
155
+ let r = (d + Math.random() * 16) % 16 | 0;
156
+ d = Math.floor(d / 16);
157
+ return (c == "x" ? r : r & 3 | 8).toString(16);
158
+ });
159
+ };
160
+ const setDomAttributes = async (dom, props, adapter) => {
161
+ const pluginType = configJson.type;
162
+ let { customConfig = {}, customOptions, type = "" } = adapter(props);
163
+ if (pluginType === "bigscreen" || pluginType === "analyzer") customConfig = customOptions;
164
+ const requirementNum = configJson["requirement-number"] ? `${configJson["requirement-number"]}-` : "";
165
+ const domSymbol = customConfig?.id ? `${pluginType}-${type}-secondary-${requirementNum}${customConfig.id}` : `${pluginType}-${type}-secondary-${requirementNum}${generateUUID()}`;
166
+ let className = dom.className;
167
+ className = className ? `${className} ${domSymbol}` : domSymbol;
168
+ dom.setAttribute("class", className);
169
+ if (isEnvProd && type != "designConfiguration") console.info(`需求编号:${configJson["requirement-number"]}`, dom);
170
+ };
171
+
172
+ //#endregion
173
+ export { Enum, LANG_EN, LANG_ZH, generateUUID, intlGetKey, lang_type, loadScript, loadStyle, setDomAttributes, setLang, setLocalePrefix };
174
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["isEnvProduction","host","port","productionPrefix","prefix"],"sources":["../export/enum.ts","../export/loadSource.ts","../export/locales.ts","../index.ts"],"sourcesContent":["/*\r\n * @Author: Xie Jun\r\n * @Description: TsEnum 支持 类型推导 和 IDE智能提示(type inference & intellisense)\r\n */\r\n/**\r\n * 泛型定义 :\r\n * L: label 标签\r\n * V: value 值\r\n * C: code 代码\r\n * test: build update version\r\n */\r\n\r\ninterface EnumItem {\r\n 0: string;\r\n 1: string | number;\r\n 2: string;\r\n length: 3;\r\n}\r\n\r\ninterface LabelsMap {\r\n [key: string | number]: string;\r\n}\r\n\r\ninterface CodesMap {\r\n [code: string]: string | number;\r\n}\r\n\r\ninterface DescMap {\r\n [code: string]: string;\r\n}\r\n\r\ninterface GetCodesFromValueMap {\r\n [value: string | number]: string;\r\n}\r\n\r\ninterface OptionItem {\r\n [key: string]: string | number;\r\n}\r\n\r\nexport class Enum {\r\n originalEnum: EnumItem[];\r\n\r\n codes: CodesMap;\r\n\r\n desc: DescMap;\r\n\r\n labels: LabelsMap;\r\n\r\n getCodesFromValue: GetCodesFromValueMap;\r\n\r\n get length(): number | undefined {\r\n return this.originalEnum?.length;\r\n }\r\n\r\n get arrLabels(): (string | undefined)[] {\r\n return this.originalEnum?.map?.((item) => item[0]) ?? [];\r\n }\r\n\r\n get values(): (string | number | undefined)[] {\r\n return this.originalEnum?.map?.((item) => item[1]) ?? [];\r\n }\r\n\r\n get keys(): (string | undefined)[] {\r\n return this.originalEnum?.map?.((item) => item[2]) ?? [];\r\n }\r\n\r\n constructor(parameters: EnumItem[]) {\r\n this.originalEnum = parameters;\r\n let labels: LabelsMap = {};\r\n let codes: CodesMap = {};\r\n let desc: DescMap = {};\r\n let getCodesFromValue: GetCodesFromValueMap = {};\r\n parameters.forEach((item) => {\r\n let label = item[0];\r\n let value = item[1];\r\n let code = item[2];\r\n\r\n codes[code] = value;\r\n desc[code] = label;\r\n labels[value] = label;\r\n getCodesFromValue[value] = code;\r\n });\r\n\r\n this.labels = labels;\r\n this.desc = desc;\r\n this.codes = codes;\r\n this.getCodesFromValue = getCodesFromValue;\r\n }\r\n\r\n options(\r\n label: string = \"label\",\r\n value: string = \"value\",\r\n key: string = \"key\",\r\n ): OptionItem[] {\r\n return (\r\n this.originalEnum?.map?.((item) => {\r\n let outPut: OptionItem = {};\r\n outPut[label] = item[0];\r\n outPut[value] = item[1];\r\n outPut[key] = item[2];\r\n return outPut;\r\n }) ?? []\r\n );\r\n }\r\n}\r\n","import configJson from \"configJson\";\r\n\r\nconst isEnvProduction = !import.meta.env.DEV;\r\nconst host: string =\r\n configJson[\"online-development-mode\"]?.host ?? \"http://localhost\";\r\nconst port = configJson[\"online-development-mode\"]?.port ?? 3000;\r\n\r\nlet currentScriptPath = getRuningScriptPath();\r\nlet productionPrefix =\r\n currentScriptPath.split(\"/\").slice(0, -2).join(\"/\") + \"/assets/\";\r\nconst prefix = isEnvProduction ? productionPrefix : `${host}:${port}/`;\r\n\r\nexport type LoadScriptPromise = Promise<void>;\r\nexport type ResolveFn = () => void;\r\n\r\ntype IEScript = HTMLScriptElement & {\r\n readyState: string;\r\n onreadystatechange: () => void;\r\n};\r\n\r\nfunction resolveFun(\r\n resolve: ResolveFn,\r\n maxIntervalCount: number,\r\n mountedName: string,\r\n) {\r\n let count = 0;\r\n const mountInterval = setInterval(() => {\r\n if (\r\n count >= maxIntervalCount ||\r\n (mountedName ? window[mountedName] : true)\r\n ) {\r\n clearInterval(mountInterval);\r\n resolve();\r\n }\r\n count++;\r\n }, 20);\r\n}\r\n\r\nexport function loadScript(\r\n path: string,\r\n loadFlag: string,\r\n mountedName: string,\r\n maxIntervalCount: number = 20,\r\n): LoadScriptPromise {\r\n if (window?.[mountedName]) {\r\n return new Promise<void>((resolve: ResolveFn) => {\r\n resolve();\r\n });\r\n } else if (window?.[loadFlag]) {\r\n return window[loadFlag];\r\n }\r\n const mountedFn = new Promise<void>((resolve: ResolveFn) => {\r\n const script: IEScript = document.createElement(\"script\") as IEScript;\r\n script.type = \"text/javascript\";\r\n if (path.indexOf(\"http\") > -1 || path.startsWith(\"//\")) {\r\n script.src = path;\r\n } else {\r\n script.src = `${prefix}${path}`;\r\n }\r\n document.head.appendChild(script);\r\n if (script?.readyState) {\r\n //IE\r\n script.onreadystatechange = function () {\r\n if (\r\n (script as any).readyState == \"loaded\" ||\r\n (script as any).readyState == \"complete\"\r\n ) {\r\n (script as any).onreadystatechange = null;\r\n resolveFun(resolve, maxIntervalCount, mountedName);\r\n }\r\n };\r\n } else {\r\n //Others\r\n script.onload = function () {\r\n resolveFun(resolve, maxIntervalCount, mountedName);\r\n };\r\n }\r\n });\r\n return (window[loadFlag] = mountedFn);\r\n}\r\n\r\nexport function getRuningScriptPath() {\r\n const currentScript = document.currentScript as HTMLScriptElement;\r\n if (currentScript && currentScript.src !== \"\") {\r\n return currentScript.src;\r\n }\r\n const match = new Error().stack?.match(/(https?:[^:]*).*\\.js/);\r\n return match?.[0] ?? \"\";\r\n}\r\n\r\n// 动态加载样式\r\nexport function loadStyle(path: string, loadFlag: string) {\r\n if (window[loadFlag]) {\r\n return;\r\n }\r\n const style = document.createElement(\"link\");\r\n if (path.indexOf(\"http\") > -1 || path.startsWith(\"//\")) {\r\n style.href = path;\r\n } else {\r\n style.href = `${prefix}${path}`;\r\n }\r\n style.type = \"text/css\";\r\n style.rel = \"stylesheet\";\r\n document.head.appendChild(style);\r\n window[loadFlag] = style;\r\n}\r\n","import configJson from \"configJson\";\r\nimport { getRuningScriptPath } from \"./loadSource\";\r\nconst { isValidElement } = window.React ?? {};\r\n\r\nconst isEnvProduction = !import.meta.env.DEV;\r\nconst host: string =\r\n configJson[\"online-development-mode\"]?.host ?? \"http://localhost\";\r\nconst port = configJson[\"online-development-mode\"]?.port ?? 3000;\r\n\r\nlet currentScriptPath = getRuningScriptPath();\r\nlet productionPrefix =\r\n currentScriptPath.split(\"/\").slice(0, -2).join(\"/\") + \"/assets/\";\r\nconst prefix = isEnvProduction ? productionPrefix : `${host}:${port}/`;\r\n\r\nexport const LANG_EN = \"en-us\";\r\nexport const LANG_ZH = \"zh-cn\";\r\n\r\nconst localStorageLocaleKey = \"lang_type\";\r\n// 获取语言(先找缓存再找浏览器内置语言)\r\nconst getLanguage = () => {\r\n const language = window?.appSdk?.intl?.determineLocale?.({\r\n localStorageLocaleKey,\r\n });\r\n if (language?.toLowerCase()?.startsWith?.(\"zh\")) {\r\n return LANG_ZH;\r\n } else {\r\n return LANG_EN;\r\n }\r\n};\r\n\r\nexport const lang_type = getLanguage();\r\n\r\nlet locale_prefix = `custom_plugin_locales_${configJson.id}`;\r\n\r\nexport const setLocalePrefix = (prefix: string) => {\r\n locale_prefix = prefix;\r\n};\r\n\r\n// 加载国际化JSON\r\nconst loadJSON = (path: string) => {\r\n return new Promise((resolve, reject) => {\r\n const url = path.startsWith(\"http\") ? path : `${prefix}${path}`;\r\n fetch(url)\r\n .then((res) => res.json())\r\n .then((data) => {\r\n resolve(data);\r\n })\r\n .catch(() => reject(\"获取国际化JSON失败,请检查文件路径是否正确!\"));\r\n });\r\n};\r\n\r\n// 国际化加载方法\r\nexport const setLang = async (path: string) => {\r\n try {\r\n const data = await loadJSON(path);\r\n const locales = {\r\n [lang_type]: () => Promise.resolve({ default: data }),\r\n };\r\n await window.appSdk.loadIntl(locales, locale_prefix);\r\n } catch (error) {\r\n window.console.error(error);\r\n }\r\n};\r\n\r\n// 国际化翻译方法\r\nexport const intlGetKey = (key: string, ...rest: any[]) => {\r\n const regex = new RegExp(\"(.[\\u4E00-\\u9FA5]+)|([\\u4E00-\\u9FA5]+.)\", \"g\");\r\n if (\r\n isValidElement?.(key) ||\r\n regex.test(key) ||\r\n typeof key === \"number\" ||\r\n typeof key === \"object\"\r\n )\r\n return key;\r\n if (!key) return;\r\n\r\n const res = window.appSdk.intl.get(\r\n `${locale_prefix}.${key}` || \"common.empty\",\r\n ...rest,\r\n );\r\n return typeof res === \"object\" ? key : res.d(key);\r\n};\r\n","import configJson from \"configJson\";\r\nimport type { AdapterProps, CustomConfig } from \"@sdata-plugin-adapter/types\";\r\n\r\nexport {\r\n Enum,\r\n setLang,\r\n intlGetKey,\r\n setLocalePrefix,\r\n lang_type,\r\n LANG_EN,\r\n LANG_ZH,\r\n loadScript,\r\n loadStyle,\r\n} from \"./export\";\r\n\r\nconst isEnvProd = import.meta.env.PROD;\r\n\r\nexport const generateUUID = (): string => {\r\n let d = new Date().getTime();\r\n return \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\".replace(/[xy]/g, function (c) {\r\n let r = ((d + Math.random() * 16) % 16) | 0;\r\n d = Math.floor(d / 16);\r\n return (c == \"x\" ? r : (r & 0x3) | 0x8).toString(16);\r\n });\r\n};\r\n\r\nexport const setDomAttributes = async (\r\n dom: Element,\r\n props: AdapterProps,\r\n adapter: (props: AdapterProps) => AdapterProps,\r\n) => {\r\n const pluginType = configJson.type;\r\n const adapterProps = adapter(props);\r\n let {\r\n customConfig = {} as CustomConfig,\r\n customOptions,\r\n type = \"\",\r\n } = adapterProps;\r\n if (pluginType === \"bigscreen\" || pluginType === \"analyzer\") {\r\n customConfig = customOptions;\r\n }\r\n const requirementNum = configJson[\"requirement-number\"]\r\n ? `${configJson[\"requirement-number\"]}-`\r\n : \"\";\r\n const domSymbol = customConfig?.id\r\n ? `${pluginType}-${type}-secondary-${requirementNum}${customConfig.id}`\r\n : `${pluginType}-${type}-secondary-${requirementNum}${generateUUID()}`;\r\n let className = dom.className;\r\n className = className ? `${className} ${domSymbol}` : domSymbol;\r\n dom.setAttribute(\"class\", className);\r\n if (isEnvProd && type != \"designConfiguration\") {\r\n // eslint-disable-next-line\r\n console.info(`需求编号:${configJson[\"requirement-number\"]}`, dom);\r\n }\r\n};\r\n"],"mappings":";;;AAuCA,IAAa,OAAb,MAAkB;CAWhB,IAAI,SAA6B;AAC/B,SAAO,KAAK,cAAc;;CAG5B,IAAI,YAAoC;AACtC,SAAO,KAAK,cAAc,OAAO,SAAS,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CAEV,MAAM,MAAM,OAAO,OAAO,KAAK,IAC7B,GAAG,cAAc,GAAG,SAAS,gBAC7B,GAAG,KACJ;AACD,QAAO,OAAO,QAAQ,WAAW,MAAM,IAAI,EAAE,IAAI;;;;;ACjEnD,MAAM,YAAY,OAAO,KAAK,IAAI;AAElC,MAAa,qBAA6B;CACxC,IAAI,qBAAI,IAAI,MAAM,EAAC,SAAS;AAC5B,QAAO,uCAAuC,QAAQ,SAAS,SAAU,GAAG;EAC1E,IAAI,KAAM,IAAI,KAAK,QAAQ,GAAG,MAAM,KAAM;AAC1C,MAAI,KAAK,MAAM,IAAI,GAAG;AACtB,UAAQ,KAAK,MAAM,IAAK,IAAI,IAAO,GAAK,SAAS,GAAG;GACpD;;AAGJ,MAAa,mBAAmB,OAC9B,KACA,OACA,YACG;CACH,MAAM,aAAa,WAAW;CAE9B,IAAI,EACF,eAAe,EAAE,EACjB,eACA,OAAO,OAJY,QAAQ,MAAM;AAMnC,KAAI,eAAe,eAAe,eAAe,WAC/C,gBAAe;CAEjB,MAAM,iBAAiB,WAAW,wBAC9B,GAAG,WAAW,sBAAsB,KACpC;CACJ,MAAM,YAAY,cAAc,KAC5B,GAAG,WAAW,GAAG,KAAK,aAAa,iBAAiB,aAAa,OACjE,GAAG,WAAW,GAAG,KAAK,aAAa,iBAAiB,cAAc;CACtE,IAAI,YAAY,IAAI;AACpB,aAAY,YAAY,GAAG,UAAU,GAAG,cAAc;AACtD,KAAI,aAAa,SAAS,UAAU;AACpC,KAAI,aAAa,QAAQ,sBAEvB,SAAQ,KAAK,QAAQ,WAAW,yBAAyB,IAAI"}
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@sdata-plugin-adapter/utils",
3
- "version": "0.0.0-alpha.1",
3
+ "version": "0.0.0-alpha.2",
4
4
  "description": "toolset of smdata plugin",
5
5
  "type": "module",
6
- "module": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.mts",
8
8
  "peerDependencies": {
9
- "@sdata-plugin-adapter/types": "0.0.0-alpha.1"
9
+ "@sdata-plugin-adapter/types": "0.0.0-alpha.2"
10
10
  },
11
11
  "exports": {
12
12
  ".": {
13
- "types": "./dist/index.d.ts",
14
- "import": "./dist/index.js"
13
+ "types": "./dist/index.d.mts",
14
+ "import": "./dist/index.mjs"
15
15
  }
16
16
  },
17
17
  "files": [
@@ -24,6 +24,6 @@
24
24
  },
25
25
  "scripts": {
26
26
  "test": "echo \"Error: no test specified\" && exit 1",
27
- "build": "tsup"
27
+ "build": "tsdown"
28
28
  }
29
29
  }
package/dist/index.js DELETED
@@ -1,223 +0,0 @@
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
- };