@tmagic/utils 1.7.7 → 1.7.8-beta.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/LICENSE +235 -164
- package/dist/es/dom.js +114 -0
- package/dist/es/index.js +360 -0
- package/dist/tmagic-utils.umd.cjs +2738 -2959
- package/package.json +5 -4
- package/types/index.d.ts +27 -22
- package/dist/tmagic-utils.js +0 -525
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.7.
|
|
2
|
+
"version": "1.7.8-beta.2",
|
|
3
3
|
"name": "@tmagic/utils",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
5
6
|
"main": "dist/tmagic-utils.umd.cjs",
|
|
6
|
-
"module": "dist/
|
|
7
|
+
"module": "dist/es/index.js",
|
|
7
8
|
"types": "types/index.d.ts",
|
|
8
9
|
"exports": {
|
|
9
10
|
".": {
|
|
10
11
|
"types": "./types/index.d.ts",
|
|
11
|
-
"import": "./dist/
|
|
12
|
+
"import": "./dist/es/index.js",
|
|
12
13
|
"require": "./dist/tmagic-utils.umd.cjs"
|
|
13
14
|
}
|
|
14
15
|
},
|
|
@@ -34,7 +35,7 @@
|
|
|
34
35
|
},
|
|
35
36
|
"peerDependencies": {
|
|
36
37
|
"typescript": "^5.9.3",
|
|
37
|
-
"@tmagic/schema": "1.7.
|
|
38
|
+
"@tmagic/schema": "1.7.8-beta.2"
|
|
38
39
|
},
|
|
39
40
|
"peerDependenciesMeta": {
|
|
40
41
|
"typescript": {
|
package/types/index.d.ts
CHANGED
|
@@ -1,34 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { DataSchema, DataSourceDeps, Id, MApp, MComponent, MNode, MNodeInstance } from "@tmagic/schema";
|
|
2
|
+
import { MContainer, MNode as MNode$1, MPage, MPageFragment } from "@tmagic/core";
|
|
3
3
|
|
|
4
|
+
//#region temp/packages/editor/src/type.d.ts
|
|
4
5
|
interface EditorNodeInfo {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
node: MNode$1 | null;
|
|
7
|
+
parent: MContainer | null;
|
|
8
|
+
page: MPage | MPageFragment | null;
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region temp/packages/utils/src/dom.d.ts
|
|
10
12
|
declare const asyncLoadJs: (url: string, crossOrigin?: string, document?: Document) => any;
|
|
11
13
|
declare const asyncLoadCss: (url: string, document?: Document) => any;
|
|
12
14
|
declare const addClassName: (el: Element, doc: Document, className: string) => void;
|
|
13
15
|
declare const removeClassName: (el: Element, ...className: string[]) => void;
|
|
14
16
|
declare const removeClassNameByClassName: (doc: Document, className: string) => HTMLElement | null;
|
|
15
17
|
declare const injectStyle: (doc: Document, style: string) => HTMLStyleElement;
|
|
16
|
-
declare const createDiv: ({
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
declare const createDiv: ({
|
|
19
|
+
className,
|
|
20
|
+
cssText
|
|
21
|
+
}: {
|
|
22
|
+
className: string;
|
|
23
|
+
cssText: string;
|
|
19
24
|
}) => HTMLDivElement;
|
|
20
25
|
declare const getDocument: () => Document;
|
|
21
26
|
declare const calcValueByFontsize: (doc: Document | undefined, value: number) => number;
|
|
22
27
|
declare const dslDomRelateConfig: {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
getIdFromEl: (el?: HTMLElement | SVGElement | null) => string | undefined;
|
|
29
|
+
getElById: (doc?: Document, id?: string | number) => HTMLElement;
|
|
30
|
+
setIdToEl: (el: HTMLElement | SVGElement, id: string | number) => void;
|
|
26
31
|
};
|
|
27
32
|
declare const setDslDomRelateConfig: <K extends keyof typeof dslDomRelateConfig, T extends (typeof dslDomRelateConfig)[K]>(name: K, value: T) => void;
|
|
28
33
|
declare const getIdFromEl: () => (el?: HTMLElement | SVGElement | null) => string | undefined;
|
|
29
34
|
declare const getElById: () => (doc?: Document, id?: string | number) => HTMLElement;
|
|
30
35
|
declare const setIdToEl: () => (el: HTMLElement | SVGElement, id: string | number) => void;
|
|
31
|
-
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region temp/packages/utils/src/index.d.ts
|
|
32
38
|
declare const getGlobalThis: () => any;
|
|
33
39
|
declare const sleep: (ms: number) => Promise<void>;
|
|
34
40
|
declare const toLine: (name?: string) => string;
|
|
@@ -40,7 +46,7 @@ declare const emptyFn: () => any;
|
|
|
40
46
|
* @param {Array} data 要查找的根容器节点
|
|
41
47
|
* @return {Array} 组件在data中的子孙路径
|
|
42
48
|
*/
|
|
43
|
-
declare const getNodePath: (id: Id, data?: MNode
|
|
49
|
+
declare const getNodePath: (id: Id, data?: MNode[]) => MNode[];
|
|
44
50
|
declare const getNodeInfo: (id: Id, root: Pick<MApp, "id" | "items"> | null) => EditorNodeInfo;
|
|
45
51
|
declare const filterXSS: (str: string) => string;
|
|
46
52
|
declare const getUrlParam: (param: string, url?: string) => string;
|
|
@@ -74,7 +80,7 @@ declare const guid: (digit?: number) => string;
|
|
|
74
80
|
declare const getKeysArray: (keys: string | number) => string[];
|
|
75
81
|
declare const getValueByKeyPath: (keys?: number | string | string[], data?: Record<string | number, any>) => any;
|
|
76
82
|
declare const setValueByKeyPath: (keys: string | number, value: any, data?: Record<string | number, any>) => any;
|
|
77
|
-
declare const getNodes: (ids: Id[], data?: MNode
|
|
83
|
+
declare const getNodes: (ids: Id[], data?: MNode[]) => MNode[];
|
|
78
84
|
declare const getDepKeys: (dataSourceDeps: DataSourceDeps | undefined, nodeId: Id) => Id[];
|
|
79
85
|
declare const getDepNodeIds: (dataSourceDeps?: DataSourceDeps) => string[];
|
|
80
86
|
/**
|
|
@@ -83,11 +89,11 @@ declare const getDepNodeIds: (dataSourceDeps?: DataSourceDeps) => string[];
|
|
|
83
89
|
* @param data 需要修改的数据
|
|
84
90
|
* @param parentId 父节点 id
|
|
85
91
|
*/
|
|
86
|
-
declare const replaceChildNode: (newNode: MNode
|
|
92
|
+
declare const replaceChildNode: (newNode: MNode, data?: MNode[], parentId?: Id) => void;
|
|
87
93
|
declare const DSL_NODE_KEY_COPY_PREFIX = "__tmagic__";
|
|
88
94
|
declare const IS_DSL_NODE_KEY = "__tmagic__dslNode";
|
|
89
95
|
declare const PAGE_FRAGMENT_CONTAINER_ID_KEY = "tmagic-page-fragment-container-id";
|
|
90
|
-
declare const compiledNode: (compile: (value: any) => any, node: MNode
|
|
96
|
+
declare const compiledNode: (compile: (value: any) => any, node: MNode, dataSourceDeps?: DataSourceDeps, sourceId?: Id) => MNode;
|
|
91
97
|
declare const compiledCond: (op: string, fieldValue: any, inputValue: any, range?: number[]) => boolean;
|
|
92
98
|
declare const getDefaultValueFromFields: (fields: DataSchema[]) => Record<string, any>;
|
|
93
99
|
declare const DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = "ds-field::";
|
|
@@ -106,12 +112,11 @@ declare const addParamToUrl: (obj: Record<string, any>, global?: typeof globalTh
|
|
|
106
112
|
declare const dataSourceTemplateRegExp: RegExp;
|
|
107
113
|
declare const isDslNode: (config: MNodeInstance) => boolean;
|
|
108
114
|
interface NodeItem {
|
|
109
|
-
|
|
110
|
-
|
|
115
|
+
items?: NodeItem[];
|
|
116
|
+
[key: string]: any;
|
|
111
117
|
}
|
|
112
118
|
declare const traverseNode: <T extends NodeItem = NodeItem>(node: T, cb: (node: T, parents: T[]) => void, parents?: T[], evalCbAfter?: boolean) => void;
|
|
113
119
|
declare const isValueIncludeDataSource: (value: any) => boolean;
|
|
114
120
|
declare const removeDataSourceFieldPrefix: (id?: string) => string;
|
|
115
|
-
|
|
116
|
-
export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, PAGE_FRAGMENT_CONTAINER_ID_KEY, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, delQueStr, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getGlobalThis, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getSearchObj, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, removeDataSourceFieldPrefix, replaceChildNode, setDslDomRelateConfig, setIdToEl, setUrlParam, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
|
|
117
|
-
export type { NodeItem };
|
|
121
|
+
//#endregion
|
|
122
|
+
export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, NodeItem, PAGE_FRAGMENT_CONTAINER_ID_KEY, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, delQueStr, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getGlobalThis, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getSearchObj, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, removeDataSourceFieldPrefix, replaceChildNode, setDslDomRelateConfig, setIdToEl, setUrlParam, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
|
package/dist/tmagic-utils.js
DELETED
|
@@ -1,525 +0,0 @@
|
|
|
1
|
-
import { set, cloneDeep } from 'lodash-es';
|
|
2
|
-
import { NodeType } from '@tmagic/schema';
|
|
3
|
-
|
|
4
|
-
const asyncLoadJs = /* @__PURE__ */ (() => {
|
|
5
|
-
const documentMap = /* @__PURE__ */ new Map();
|
|
6
|
-
return (url, crossOrigin, document = globalThis.document) => {
|
|
7
|
-
let loaded = documentMap.get(document);
|
|
8
|
-
if (!loaded) {
|
|
9
|
-
loaded = /* @__PURE__ */ new Map();
|
|
10
|
-
documentMap.set(document, loaded);
|
|
11
|
-
}
|
|
12
|
-
if (loaded.get(url)) return loaded.get(url);
|
|
13
|
-
const load = new Promise((resolve, reject) => {
|
|
14
|
-
const script = document.createElement("script");
|
|
15
|
-
script.type = "text/javascript";
|
|
16
|
-
if (crossOrigin) {
|
|
17
|
-
script.crossOrigin = crossOrigin;
|
|
18
|
-
}
|
|
19
|
-
script.src = url;
|
|
20
|
-
document.body.appendChild(script);
|
|
21
|
-
script.onload = () => {
|
|
22
|
-
resolve();
|
|
23
|
-
};
|
|
24
|
-
script.onerror = () => {
|
|
25
|
-
reject(new Error("加载失败"));
|
|
26
|
-
};
|
|
27
|
-
setTimeout(() => {
|
|
28
|
-
reject(new Error("timeout"));
|
|
29
|
-
}, 60 * 1e3);
|
|
30
|
-
}).catch((err) => {
|
|
31
|
-
loaded.delete(url);
|
|
32
|
-
throw err;
|
|
33
|
-
});
|
|
34
|
-
loaded.set(url, load);
|
|
35
|
-
return loaded.get(url);
|
|
36
|
-
};
|
|
37
|
-
})();
|
|
38
|
-
const asyncLoadCss = /* @__PURE__ */ (() => {
|
|
39
|
-
const documentMap = /* @__PURE__ */ new Map();
|
|
40
|
-
return (url, document = globalThis.document) => {
|
|
41
|
-
let loaded = documentMap.get(document);
|
|
42
|
-
if (!loaded) {
|
|
43
|
-
loaded = /* @__PURE__ */ new Map();
|
|
44
|
-
documentMap.set(document, loaded);
|
|
45
|
-
}
|
|
46
|
-
if (loaded.get(url)) return loaded.get(url);
|
|
47
|
-
const load = new Promise((resolve, reject) => {
|
|
48
|
-
const node = document.createElement("link");
|
|
49
|
-
node.rel = "stylesheet";
|
|
50
|
-
node.href = url;
|
|
51
|
-
document.head.appendChild(node);
|
|
52
|
-
node.onload = () => {
|
|
53
|
-
resolve();
|
|
54
|
-
};
|
|
55
|
-
node.onerror = () => {
|
|
56
|
-
reject(new Error("加载失败"));
|
|
57
|
-
};
|
|
58
|
-
setTimeout(() => {
|
|
59
|
-
reject(new Error("timeout"));
|
|
60
|
-
}, 60 * 1e3);
|
|
61
|
-
}).catch((err) => {
|
|
62
|
-
loaded.delete(url);
|
|
63
|
-
throw err;
|
|
64
|
-
});
|
|
65
|
-
loaded.set(url, load);
|
|
66
|
-
return loaded.get(url);
|
|
67
|
-
};
|
|
68
|
-
})();
|
|
69
|
-
const addClassName = (el, doc, className) => {
|
|
70
|
-
const oldEl = doc.querySelector(`.${className}`);
|
|
71
|
-
if (oldEl && oldEl !== el) removeClassName(oldEl, className);
|
|
72
|
-
if (!el.classList.contains(className)) el.classList.add(className);
|
|
73
|
-
};
|
|
74
|
-
const removeClassName = (el, ...className) => {
|
|
75
|
-
el.classList.remove(...className);
|
|
76
|
-
};
|
|
77
|
-
const removeClassNameByClassName = (doc, className) => {
|
|
78
|
-
const el = doc.querySelector(`.${className}`);
|
|
79
|
-
el?.classList.remove(className);
|
|
80
|
-
return el;
|
|
81
|
-
};
|
|
82
|
-
const injectStyle = (doc, style) => {
|
|
83
|
-
const styleEl = doc.createElement("style");
|
|
84
|
-
styleEl.innerHTML = style;
|
|
85
|
-
doc.head.appendChild(styleEl);
|
|
86
|
-
return styleEl;
|
|
87
|
-
};
|
|
88
|
-
const createDiv = ({ className, cssText }) => {
|
|
89
|
-
const el = globalThis.document.createElement("div");
|
|
90
|
-
el.className = className;
|
|
91
|
-
el.style.cssText = cssText;
|
|
92
|
-
return el;
|
|
93
|
-
};
|
|
94
|
-
const getDocument = () => globalThis.document;
|
|
95
|
-
const calcValueByFontsize = (doc, value) => {
|
|
96
|
-
if (!doc) return value;
|
|
97
|
-
const { fontSize } = doc.documentElement.style;
|
|
98
|
-
if (fontSize) {
|
|
99
|
-
const times = globalThis.parseFloat(fontSize) / 100;
|
|
100
|
-
return Number((value / times).toFixed(2));
|
|
101
|
-
}
|
|
102
|
-
return value;
|
|
103
|
-
};
|
|
104
|
-
const dslDomRelateConfig = {
|
|
105
|
-
getIdFromEl: (el) => el?.dataset?.tmagicId,
|
|
106
|
-
getElById: (doc, id) => doc?.querySelector(`[data-tmagic-id="${id}"]`),
|
|
107
|
-
setIdToEl: (el, id) => {
|
|
108
|
-
el.dataset.tmagicId = `${id}`;
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
const setDslDomRelateConfig = (name, value) => {
|
|
112
|
-
dslDomRelateConfig[name] = value;
|
|
113
|
-
};
|
|
114
|
-
const getIdFromEl = () => dslDomRelateConfig.getIdFromEl;
|
|
115
|
-
const getElById = () => dslDomRelateConfig.getElById;
|
|
116
|
-
const setIdToEl = () => dslDomRelateConfig.setIdToEl;
|
|
117
|
-
|
|
118
|
-
let _globalThis;
|
|
119
|
-
const getGlobalThis = () => _globalThis || (_globalThis = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {});
|
|
120
|
-
const sleep = (ms) => new Promise((resolve) => {
|
|
121
|
-
const timer = setTimeout(() => {
|
|
122
|
-
clearTimeout(timer);
|
|
123
|
-
resolve();
|
|
124
|
-
}, ms);
|
|
125
|
-
});
|
|
126
|
-
const toLine = (name = "") => name.replace(/\B([A-Z])/g, "-$1").toLowerCase();
|
|
127
|
-
const toHump = (name = "") => name.replace(/-(\w)/g, (_all, letter) => letter.toUpperCase());
|
|
128
|
-
const emptyFn = () => void 0;
|
|
129
|
-
const getNodePath = (id, data = []) => {
|
|
130
|
-
const path = [];
|
|
131
|
-
const get = function(id2, data2) {
|
|
132
|
-
if (!Array.isArray(data2)) {
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
for (let i = 0, l = data2.length; i < l; i++) {
|
|
136
|
-
const item = data2[i];
|
|
137
|
-
path.push(item);
|
|
138
|
-
if (`${item.id}` === `${id2}`) {
|
|
139
|
-
return item;
|
|
140
|
-
}
|
|
141
|
-
if (item.items) {
|
|
142
|
-
const node = get(id2, item.items);
|
|
143
|
-
if (node) {
|
|
144
|
-
return node;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
path.pop();
|
|
148
|
-
}
|
|
149
|
-
return null;
|
|
150
|
-
};
|
|
151
|
-
get(id, data);
|
|
152
|
-
return path;
|
|
153
|
-
};
|
|
154
|
-
const getNodeInfo = (id, root) => {
|
|
155
|
-
const info = {
|
|
156
|
-
node: null,
|
|
157
|
-
parent: null,
|
|
158
|
-
page: null
|
|
159
|
-
};
|
|
160
|
-
if (!root) return info;
|
|
161
|
-
if (id === root.id) {
|
|
162
|
-
info.node = root;
|
|
163
|
-
return info;
|
|
164
|
-
}
|
|
165
|
-
const path = getNodePath(id, root.items);
|
|
166
|
-
if (!path.length) return info;
|
|
167
|
-
path.unshift(root);
|
|
168
|
-
info.node = path[path.length - 1];
|
|
169
|
-
info.parent = path[path.length - 2];
|
|
170
|
-
path.forEach((item) => {
|
|
171
|
-
if (isPage(item) || isPageFragment(item)) {
|
|
172
|
-
info.page = item;
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
return info;
|
|
177
|
-
};
|
|
178
|
-
const filterXSS = (str) => str.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
179
|
-
const getUrlParam = (param, url) => {
|
|
180
|
-
const u = url || location.href;
|
|
181
|
-
const reg = new RegExp(`[?&#]${param}=([^&#]+)`, "gi");
|
|
182
|
-
const matches = u.match(reg);
|
|
183
|
-
let strArr;
|
|
184
|
-
if (matches && matches.length > 0) {
|
|
185
|
-
strArr = matches[matches.length - 1].split("=");
|
|
186
|
-
if (strArr && strArr.length > 1) {
|
|
187
|
-
return filterXSS(strArr[1]);
|
|
188
|
-
}
|
|
189
|
-
return "";
|
|
190
|
-
}
|
|
191
|
-
return "";
|
|
192
|
-
};
|
|
193
|
-
const setUrlParam = (name, value, url = globalThis.location.href) => {
|
|
194
|
-
const reg = new RegExp(`[?&#]${name}=([^&#]*)`, "gi");
|
|
195
|
-
const matches = url.match(reg);
|
|
196
|
-
const key = `{key${(/* @__PURE__ */ new Date()).getTime()}}`;
|
|
197
|
-
let strArr;
|
|
198
|
-
if (matches && matches.length > 0) {
|
|
199
|
-
strArr = matches[matches.length - 1];
|
|
200
|
-
} else {
|
|
201
|
-
strArr = "";
|
|
202
|
-
}
|
|
203
|
-
const extra = `${name}=${value}`;
|
|
204
|
-
if (strArr) {
|
|
205
|
-
const first = strArr.charAt(0);
|
|
206
|
-
url = url.replace(strArr, key);
|
|
207
|
-
url = url.replace(key, value ? first + extra : "");
|
|
208
|
-
} else if (value) {
|
|
209
|
-
if (url.indexOf("?") > -1) {
|
|
210
|
-
url += `&${extra}`;
|
|
211
|
-
} else {
|
|
212
|
-
url += `?${extra}`;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return url;
|
|
216
|
-
};
|
|
217
|
-
const getSearchObj = (search = globalThis.location.search ? globalThis.location.search.substring(1) : "") => {
|
|
218
|
-
return search.split("&").reduce((obj, item) => {
|
|
219
|
-
const [a, b = ""] = item.split("=");
|
|
220
|
-
return { ...obj, [a]: b };
|
|
221
|
-
}, {});
|
|
222
|
-
};
|
|
223
|
-
const delQueStr = (url, ref) => {
|
|
224
|
-
let str = "";
|
|
225
|
-
if (url.indexOf("?") !== -1) {
|
|
226
|
-
str = url.substring(url.indexOf("?") + 1);
|
|
227
|
-
} else {
|
|
228
|
-
return url;
|
|
229
|
-
}
|
|
230
|
-
let arr = [];
|
|
231
|
-
let returnurl = "";
|
|
232
|
-
const isHit = Array.isArray(ref) ? function(v) {
|
|
233
|
-
return ~ref.indexOf(v);
|
|
234
|
-
} : function(v) {
|
|
235
|
-
return v === ref;
|
|
236
|
-
};
|
|
237
|
-
if (str.indexOf("&") !== -1) {
|
|
238
|
-
arr = str.split("&");
|
|
239
|
-
for (let i = 0, len = arr.length; i < len; i++) {
|
|
240
|
-
if (!isHit(arr[i].split("=")[0])) {
|
|
241
|
-
returnurl = `${returnurl + arr[i].split("=")[0]}=${arr[i].split("=")[1]}&`;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
return returnurl ? `${url.substr(0, url.indexOf("?"))}?${returnurl.substr(0, returnurl.length - 1)}` : url.substr(0, url.indexOf("?"));
|
|
245
|
-
}
|
|
246
|
-
arr = str.split("=");
|
|
247
|
-
if (isHit(arr[0])) {
|
|
248
|
-
return url.substr(0, url.indexOf("?"));
|
|
249
|
-
}
|
|
250
|
-
return url;
|
|
251
|
-
};
|
|
252
|
-
const isObject = (obj) => Object.prototype.toString.call(obj) === "[object Object]";
|
|
253
|
-
const isPop = (node) => Boolean(node?.type?.toLowerCase().endsWith("pop"));
|
|
254
|
-
const isPage = (node) => {
|
|
255
|
-
if (!node) return false;
|
|
256
|
-
return Boolean(node.type?.toLowerCase() === NodeType.PAGE);
|
|
257
|
-
};
|
|
258
|
-
const isPageFragment = (node) => {
|
|
259
|
-
if (!node) return false;
|
|
260
|
-
return Boolean(node.type?.toLowerCase() === NodeType.PAGE_FRAGMENT);
|
|
261
|
-
};
|
|
262
|
-
const isNumber = (value) => typeof value === "number" && !isNaN(value) || /^(-?\d+)(\.\d+)?$/.test(`${value}`);
|
|
263
|
-
const getHost = (targetUrl) => targetUrl.match(/\/\/([^/]+)/)?.[1];
|
|
264
|
-
const isSameDomain = (targetUrl = "", source = globalThis.location.host) => {
|
|
265
|
-
const isHttpUrl = /^(http[s]?:)?\/\//.test(targetUrl);
|
|
266
|
-
if (!isHttpUrl) return true;
|
|
267
|
-
return getHost(targetUrl) === source;
|
|
268
|
-
};
|
|
269
|
-
const guid = (digit = 8) => "x".repeat(digit).replace(/[xy]/g, (c) => {
|
|
270
|
-
const r = Math.random() * 16 | 0;
|
|
271
|
-
const v = c === "x" ? r : r & 3 | 8;
|
|
272
|
-
return v.toString(16);
|
|
273
|
-
});
|
|
274
|
-
const getKeysArray = (keys) => (
|
|
275
|
-
// 将 array[0] 转成 array.0
|
|
276
|
-
`${keys}`.replace(/\[(\d+)\]/g, ".$1").split(".")
|
|
277
|
-
);
|
|
278
|
-
const getValueByKeyPath = (keys = "", data = {}) => {
|
|
279
|
-
const keyArray = Array.isArray(keys) ? keys : getKeysArray(keys);
|
|
280
|
-
return keyArray.reduce((accumulator, currentValue) => {
|
|
281
|
-
if (isObject(accumulator)) {
|
|
282
|
-
return accumulator[currentValue];
|
|
283
|
-
}
|
|
284
|
-
if (Array.isArray(accumulator) && /^\d*$/.test(`${currentValue}`)) {
|
|
285
|
-
return accumulator[currentValue];
|
|
286
|
-
}
|
|
287
|
-
throw new Error(`${data}中不存在${keys}`);
|
|
288
|
-
}, data);
|
|
289
|
-
};
|
|
290
|
-
const setValueByKeyPath = (keys, value, data = {}) => set(data, keys, value);
|
|
291
|
-
const getNodes = (ids, data = []) => {
|
|
292
|
-
const nodes = [];
|
|
293
|
-
const get = function(ids2, data2) {
|
|
294
|
-
if (!Array.isArray(data2)) {
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
for (const item of data2) {
|
|
298
|
-
const index = ids2.findIndex((id) => `${id}` === `${item.id}`);
|
|
299
|
-
if (index > -1) {
|
|
300
|
-
ids2.splice(index, 1);
|
|
301
|
-
nodes.push(item);
|
|
302
|
-
}
|
|
303
|
-
if (item.items) {
|
|
304
|
-
get(ids2, item.items);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
};
|
|
308
|
-
get(ids, data);
|
|
309
|
-
return nodes;
|
|
310
|
-
};
|
|
311
|
-
const getDepKeys = (dataSourceDeps = {}, nodeId) => Array.from(
|
|
312
|
-
Object.values(dataSourceDeps).reduce((prev, cur) => {
|
|
313
|
-
(cur[nodeId]?.keys || []).forEach((key) => prev.add(key));
|
|
314
|
-
return prev;
|
|
315
|
-
}, /* @__PURE__ */ new Set())
|
|
316
|
-
);
|
|
317
|
-
const getDepNodeIds = (dataSourceDeps = {}) => Array.from(
|
|
318
|
-
Object.values(dataSourceDeps).reduce((prev, cur) => {
|
|
319
|
-
Object.keys(cur).forEach((id) => {
|
|
320
|
-
prev.add(id);
|
|
321
|
-
});
|
|
322
|
-
return prev;
|
|
323
|
-
}, /* @__PURE__ */ new Set())
|
|
324
|
-
);
|
|
325
|
-
const replaceChildNode = (newNode, data, parentId) => {
|
|
326
|
-
const path = getNodePath(newNode.id, data);
|
|
327
|
-
const node = path.pop();
|
|
328
|
-
let parent = path.pop();
|
|
329
|
-
if (parentId) {
|
|
330
|
-
parent = getNodePath(parentId, data).pop();
|
|
331
|
-
}
|
|
332
|
-
if (!node) {
|
|
333
|
-
console.warn(`未找到目标节点(${newNode.id})`);
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
if (!parent) {
|
|
337
|
-
console.warn(`未找到父节点(${newNode.id})`);
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
const index = parent.items?.findIndex((child) => child.id === node.id);
|
|
341
|
-
parent.items.splice(index, 1, newNode);
|
|
342
|
-
};
|
|
343
|
-
const DSL_NODE_KEY_COPY_PREFIX = "__tmagic__";
|
|
344
|
-
const IS_DSL_NODE_KEY = "__tmagic__dslNode";
|
|
345
|
-
const PAGE_FRAGMENT_CONTAINER_ID_KEY = "tmagic-page-fragment-container-id";
|
|
346
|
-
const compiledNode = (compile, node, dataSourceDeps = {}, sourceId) => {
|
|
347
|
-
let keys = [];
|
|
348
|
-
if (!sourceId) {
|
|
349
|
-
keys = getDepKeys(dataSourceDeps, node.id);
|
|
350
|
-
} else {
|
|
351
|
-
const dep = dataSourceDeps[sourceId];
|
|
352
|
-
keys = dep?.[node.id].keys || [];
|
|
353
|
-
}
|
|
354
|
-
keys.forEach((key) => {
|
|
355
|
-
const keys2 = getKeysArray(key);
|
|
356
|
-
const cacheKey = keys2.map((key2, index) => {
|
|
357
|
-
if (index < keys2.length - 1) {
|
|
358
|
-
return key2;
|
|
359
|
-
}
|
|
360
|
-
return `${DSL_NODE_KEY_COPY_PREFIX}${key2}`;
|
|
361
|
-
});
|
|
362
|
-
let templateValue = getValueByKeyPath(cacheKey, node);
|
|
363
|
-
if (typeof templateValue === "undefined") {
|
|
364
|
-
try {
|
|
365
|
-
const value = getValueByKeyPath(key, node);
|
|
366
|
-
setValueByKeyPath(cacheKey.join("."), value, node);
|
|
367
|
-
templateValue = value;
|
|
368
|
-
} catch (e) {
|
|
369
|
-
console.warn(e);
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
let newValue;
|
|
374
|
-
try {
|
|
375
|
-
newValue = compile(templateValue);
|
|
376
|
-
} catch (e) {
|
|
377
|
-
console.error(e);
|
|
378
|
-
newValue = "";
|
|
379
|
-
}
|
|
380
|
-
setValueByKeyPath(key, newValue, node);
|
|
381
|
-
});
|
|
382
|
-
return node;
|
|
383
|
-
};
|
|
384
|
-
const compiledCond = (op, fieldValue, inputValue, range = []) => {
|
|
385
|
-
if (typeof fieldValue === "string" && typeof inputValue === "undefined") {
|
|
386
|
-
inputValue = "";
|
|
387
|
-
}
|
|
388
|
-
switch (op) {
|
|
389
|
-
case "is":
|
|
390
|
-
return fieldValue === inputValue;
|
|
391
|
-
case "not":
|
|
392
|
-
return fieldValue !== inputValue;
|
|
393
|
-
case "=":
|
|
394
|
-
return fieldValue === inputValue;
|
|
395
|
-
case "!=":
|
|
396
|
-
return fieldValue !== inputValue;
|
|
397
|
-
case ">":
|
|
398
|
-
return fieldValue > inputValue;
|
|
399
|
-
case ">=":
|
|
400
|
-
return fieldValue >= inputValue;
|
|
401
|
-
case "<":
|
|
402
|
-
return fieldValue < inputValue;
|
|
403
|
-
case "<=":
|
|
404
|
-
return fieldValue <= inputValue;
|
|
405
|
-
case "between":
|
|
406
|
-
return range.length > 1 && fieldValue >= range[0] && fieldValue <= range[1];
|
|
407
|
-
case "not_between":
|
|
408
|
-
return range.length < 2 || fieldValue < range[0] || fieldValue > range[1];
|
|
409
|
-
case "include":
|
|
410
|
-
return fieldValue?.includes?.(inputValue);
|
|
411
|
-
case "not_include":
|
|
412
|
-
return typeof fieldValue === "undefined" || !fieldValue.includes?.(inputValue);
|
|
413
|
-
}
|
|
414
|
-
return false;
|
|
415
|
-
};
|
|
416
|
-
const getDefaultValueFromFields = (fields) => {
|
|
417
|
-
const data = {};
|
|
418
|
-
const defaultValue = {
|
|
419
|
-
string: void 0,
|
|
420
|
-
object: {},
|
|
421
|
-
array: [],
|
|
422
|
-
boolean: void 0,
|
|
423
|
-
number: void 0,
|
|
424
|
-
null: null,
|
|
425
|
-
any: void 0
|
|
426
|
-
};
|
|
427
|
-
fields.forEach((field) => {
|
|
428
|
-
if (typeof field.defaultValue !== "undefined") {
|
|
429
|
-
if (field.type === "array" && !Array.isArray(field.defaultValue)) {
|
|
430
|
-
data[field.name] = defaultValue.array;
|
|
431
|
-
return;
|
|
432
|
-
}
|
|
433
|
-
if (field.type === "object" && !isObject(field.defaultValue)) {
|
|
434
|
-
if (typeof field.defaultValue === "string") {
|
|
435
|
-
try {
|
|
436
|
-
data[field.name] = JSON.parse(field.defaultValue);
|
|
437
|
-
} catch (e) {
|
|
438
|
-
data[field.name] = defaultValue.object;
|
|
439
|
-
console.warn("defaultValue 解析失败", field.defaultValue, e);
|
|
440
|
-
}
|
|
441
|
-
return;
|
|
442
|
-
}
|
|
443
|
-
data[field.name] = defaultValue.object;
|
|
444
|
-
return;
|
|
445
|
-
}
|
|
446
|
-
data[field.name] = cloneDeep(field.defaultValue);
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
if (field.type === "object") {
|
|
450
|
-
data[field.name] = field.fields ? getDefaultValueFromFields(field.fields) : defaultValue.object;
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
if (field.type) {
|
|
454
|
-
data[field.name] = defaultValue[field.type];
|
|
455
|
-
return;
|
|
456
|
-
}
|
|
457
|
-
data[field.name] = void 0;
|
|
458
|
-
});
|
|
459
|
-
return data;
|
|
460
|
-
};
|
|
461
|
-
const DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = "ds-field::";
|
|
462
|
-
const DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX = "ds-field-changed";
|
|
463
|
-
const getKeys = Object.keys;
|
|
464
|
-
const calculatePercentage = (value, percentageStr) => {
|
|
465
|
-
const percentage = globalThis.parseFloat(percentageStr) / 100;
|
|
466
|
-
const result = value * percentage;
|
|
467
|
-
return result;
|
|
468
|
-
};
|
|
469
|
-
const isPercentage = (value) => /^(\d+)(\.\d+)?%$/.test(`${value}`);
|
|
470
|
-
const convertToNumber = (value, parentValue = 0) => {
|
|
471
|
-
if (typeof value === "number") {
|
|
472
|
-
return value;
|
|
473
|
-
}
|
|
474
|
-
if (typeof value === "string" && isPercentage(value)) {
|
|
475
|
-
return calculatePercentage(parentValue, value);
|
|
476
|
-
}
|
|
477
|
-
return parseFloat(value);
|
|
478
|
-
};
|
|
479
|
-
const addParamToUrl = (obj, global2 = globalThis, needReload = true) => {
|
|
480
|
-
const url = new URL(global2.location.href);
|
|
481
|
-
const { searchParams } = url;
|
|
482
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
483
|
-
searchParams.set(k, v);
|
|
484
|
-
}
|
|
485
|
-
const newUrl = url.toString();
|
|
486
|
-
if (needReload) {
|
|
487
|
-
global2.location.href = newUrl;
|
|
488
|
-
} else {
|
|
489
|
-
global2.history.pushState({}, "", url);
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
const dataSourceTemplateRegExp = /\$\{([\s\S]+?)\}/g;
|
|
493
|
-
const isDslNode = (config) => typeof config[IS_DSL_NODE_KEY] === "undefined" || config[IS_DSL_NODE_KEY] === true;
|
|
494
|
-
const traverseNode = (node, cb, parents = [], evalCbAfter = false) => {
|
|
495
|
-
if (!evalCbAfter) {
|
|
496
|
-
cb(node, parents);
|
|
497
|
-
}
|
|
498
|
-
if (Array.isArray(node.items) && node.items.length) {
|
|
499
|
-
parents.push(node);
|
|
500
|
-
node.items.forEach((item) => {
|
|
501
|
-
traverseNode(item, cb, [...parents], evalCbAfter);
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
if (evalCbAfter) {
|
|
505
|
-
cb(node, parents);
|
|
506
|
-
}
|
|
507
|
-
};
|
|
508
|
-
const isValueIncludeDataSource = (value) => {
|
|
509
|
-
if (typeof value === "string" && /\$\{([\s\S]+?)\}/.test(value)) {
|
|
510
|
-
return true;
|
|
511
|
-
}
|
|
512
|
-
if (Array.isArray(value) && `${value[0]}`.startsWith(DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX)) {
|
|
513
|
-
return true;
|
|
514
|
-
}
|
|
515
|
-
if (value?.isBindDataSource && value.dataSourceId) {
|
|
516
|
-
return true;
|
|
517
|
-
}
|
|
518
|
-
if (value?.isBindDataSourceField && value.dataSourceId) {
|
|
519
|
-
return true;
|
|
520
|
-
}
|
|
521
|
-
return false;
|
|
522
|
-
};
|
|
523
|
-
const removeDataSourceFieldPrefix = (id) => id?.replace(DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, "") || "";
|
|
524
|
-
|
|
525
|
-
export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DSL_NODE_KEY_COPY_PREFIX, IS_DSL_NODE_KEY, PAGE_FRAGMENT_CONTAINER_ID_KEY, addClassName, addParamToUrl, asyncLoadCss, asyncLoadJs, calcValueByFontsize, calculatePercentage, compiledCond, compiledNode, convertToNumber, createDiv, dataSourceTemplateRegExp, delQueStr, emptyFn, filterXSS, getDefaultValueFromFields, getDepKeys, getDepNodeIds, getDocument, getElById, getGlobalThis, getHost, getIdFromEl, getKeys, getKeysArray, getNodeInfo, getNodePath, getNodes, getSearchObj, getUrlParam, getValueByKeyPath, guid, injectStyle, isDslNode, isNumber, isObject, isPage, isPageFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, removeDataSourceFieldPrefix, replaceChildNode, setDslDomRelateConfig, setIdToEl, setUrlParam, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
|