@tmagic/utils 1.8.5 → 1.8.7
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/es/index.js +63 -48
- package/dist/tmagic-utils.umd.cjs +63 -47
- package/package.json +2 -2
- package/src/index.ts +81 -74
- package/types/index.d.ts +81 -59
package/dist/es/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DATA_SOURCE_SET_DATA_METHOD_NAME } from "./const.js";
|
|
2
|
-
import { addClassName, asyncLoadCss, asyncLoadJs, calcValueByFontsize, createDiv, getDocument, getElById, getIdFromEl, injectStyle, removeClassName, removeClassNameByClassName, setDslDomRelateConfig, setIdToEl } from "./dom.js";
|
|
3
1
|
import { cloneDeep, get, set } from "lodash-es";
|
|
4
2
|
import { NodeType } from "@tmagic/schema";
|
|
3
|
+
import { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DATA_SOURCE_SET_DATA_METHOD_NAME } from "./const.js";
|
|
4
|
+
import { addClassName, asyncLoadCss, asyncLoadJs, calcValueByFontsize, createDiv, getDocument, getElById, getIdFromEl, injectStyle, removeClassName, removeClassNameByClassName, setDslDomRelateConfig, setIdToEl } from "./dom.js";
|
|
5
5
|
// packages/utils/src/index.ts
|
|
6
6
|
var _globalThis;
|
|
7
7
|
var getGlobalThis = () => _globalThis || (_globalThis = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {});
|
|
@@ -65,17 +65,28 @@ var getNodeInfo = (id, root, skip) => {
|
|
|
65
65
|
return info;
|
|
66
66
|
};
|
|
67
67
|
var filterXSS = (str) => str.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
68
|
+
/**
|
|
69
|
+
* 拆出 path、query(不含 ?)与 hash(含 #)
|
|
70
|
+
*
|
|
71
|
+
* 不用 URL 对象:它要求入参是合法的绝对地址,且会把已有参数重新编码
|
|
72
|
+
*/
|
|
73
|
+
var splitUrl = (url) => {
|
|
74
|
+
const hashIndex = url.indexOf("#");
|
|
75
|
+
const rest = hashIndex === -1 ? url : url.slice(0, hashIndex);
|
|
76
|
+
const queryIndex = rest.indexOf("?");
|
|
77
|
+
return {
|
|
78
|
+
path: queryIndex === -1 ? rest : rest.slice(0, queryIndex),
|
|
79
|
+
query: queryIndex === -1 ? "" : rest.slice(queryIndex + 1),
|
|
80
|
+
hash: hashIndex === -1 ? "" : url.slice(hashIndex)
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
var joinUrl = (path, query, hash) => `${path}${query ? `?${query}` : ""}${hash}`;
|
|
84
|
+
/** 按参数名逐段比较,不把参数名当正则,也避免 i 标志带来的大小写误匹配 */
|
|
85
|
+
var isParamOf = (name, pair) => pair.split("=")[0] === name;
|
|
68
86
|
var getUrlParam = (param, url) => {
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
let strArr;
|
|
73
|
-
if (matches && matches.length > 0) {
|
|
74
|
-
strArr = matches[matches.length - 1].split("=");
|
|
75
|
-
if (strArr && strArr.length > 1) return filterXSS(strArr[1]);
|
|
76
|
-
return "";
|
|
77
|
-
}
|
|
78
|
-
return "";
|
|
87
|
+
const { query, hash } = splitUrl(url || location.href);
|
|
88
|
+
const value = new URLSearchParams(query).getAll(param).pop() ?? new URLSearchParams(hash.slice(1)).getAll(param).pop();
|
|
89
|
+
return value ? filterXSS(value) : "";
|
|
79
90
|
};
|
|
80
91
|
/**
|
|
81
92
|
* 设置url中指定的参数
|
|
@@ -89,22 +100,14 @@ var getUrlParam = (param, url) => {
|
|
|
89
100
|
* @return {string} [返回处理后的url]
|
|
90
101
|
*/
|
|
91
102
|
var setUrlParam = (name, value, url = globalThis.location.href) => {
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
else
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const first = strArr.charAt(0);
|
|
101
|
-
url = url.replace(strArr, key);
|
|
102
|
-
url = url.replace(key, value ? first + extra : "");
|
|
103
|
-
} else if (value) {
|
|
104
|
-
if (url.indexOf("?") > -1) url += `&${extra}`;
|
|
105
|
-
else url += `?${extra}`;
|
|
106
|
-
}
|
|
107
|
-
return url;
|
|
103
|
+
const { path, query, hash } = splitUrl(url);
|
|
104
|
+
const pairs = query ? query.split("&") : [];
|
|
105
|
+
const index = pairs.findIndex((pair) => isParamOf(name, pair));
|
|
106
|
+
if (index === -1) {
|
|
107
|
+
if (value) pairs.push(`${name}=${encodeURIComponent(value)}`);
|
|
108
|
+
} else if (value) pairs[index] = `${name}=${encodeURIComponent(value)}`;
|
|
109
|
+
else pairs.splice(index, 1);
|
|
110
|
+
return joinUrl(path, pairs.join("&"), hash);
|
|
108
111
|
};
|
|
109
112
|
var getSearchObj = (search = globalThis.location.search ? globalThis.location.search.substring(1) : "") => {
|
|
110
113
|
return search.split("&").reduce((obj, item) => {
|
|
@@ -116,24 +119,10 @@ var getSearchObj = (search = globalThis.location.search ? globalThis.location.se
|
|
|
116
119
|
}, {});
|
|
117
120
|
};
|
|
118
121
|
var delQueStr = (url, ref) => {
|
|
119
|
-
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
let returnurl = "";
|
|
124
|
-
const isHit = Array.isArray(ref) ? function(v) {
|
|
125
|
-
return ~ref.indexOf(v);
|
|
126
|
-
} : function(v) {
|
|
127
|
-
return v === ref;
|
|
128
|
-
};
|
|
129
|
-
if (str.indexOf("&") !== -1) {
|
|
130
|
-
arr = str.split("&");
|
|
131
|
-
for (let i = 0, len = arr.length; i < len; i++) if (!isHit(arr[i].split("=")[0])) returnurl = `${returnurl + arr[i].split("=")[0]}=${arr[i].split("=")[1]}&`;
|
|
132
|
-
return returnurl ? `${url.substr(0, url.indexOf("?"))}?${returnurl.substr(0, returnurl.length - 1)}` : url.substr(0, url.indexOf("?"));
|
|
133
|
-
}
|
|
134
|
-
arr = str.split("=");
|
|
135
|
-
if (isHit(arr[0])) return url.substr(0, url.indexOf("?"));
|
|
136
|
-
return url;
|
|
122
|
+
const { path, query, hash } = splitUrl(url);
|
|
123
|
+
if (!query) return url;
|
|
124
|
+
const names = Array.isArray(ref) ? ref : [ref];
|
|
125
|
+
return joinUrl(path, query.split("&").filter((pair) => !names.includes(pair.split("=")[0])).join("&"), hash);
|
|
137
126
|
};
|
|
138
127
|
var isObject = (obj) => Object.prototype.toString.call(obj) === "[object Object]";
|
|
139
128
|
var isPop = (node) => Boolean(node?.type?.toLowerCase().endsWith("pop"));
|
|
@@ -295,7 +284,7 @@ var replaceChildNodes = (newNodes, data = []) => {
|
|
|
295
284
|
const sortedNodes = newNodes.map((node, order) => ({
|
|
296
285
|
node,
|
|
297
286
|
order,
|
|
298
|
-
depth: locations.get(`${node.id}`)?.depth ??
|
|
287
|
+
depth: locations.get(`${node.id}`)?.depth ?? 1e400
|
|
299
288
|
})).sort((a, b) => a.depth === b.depth ? a.order - b.order : a.depth - b.depth).map(({ node }) => node);
|
|
300
289
|
for (let i = 0, l = sortedNodes.length; i < l; i++) {
|
|
301
290
|
const newNode = sortedNodes[i];
|
|
@@ -416,6 +405,32 @@ var getDefaultValueFromFields = (fields) => {
|
|
|
416
405
|
});
|
|
417
406
|
return data;
|
|
418
407
|
};
|
|
408
|
+
/** 示例元素里的空值要能写进 JSON,undefined 会被丢掉 */
|
|
409
|
+
var MOCK_SCALAR = {
|
|
410
|
+
string: "",
|
|
411
|
+
number: 0,
|
|
412
|
+
boolean: false,
|
|
413
|
+
null: null,
|
|
414
|
+
any: null
|
|
415
|
+
};
|
|
416
|
+
/** 按字段生成 mock。空数组补一条子字段示例,已有内容不动 */
|
|
417
|
+
var getMockDataFromFields = (fields = []) => {
|
|
418
|
+
const fill = (list, data, placeholder) => {
|
|
419
|
+
list?.forEach((field) => {
|
|
420
|
+
const value = data[field.name];
|
|
421
|
+
if (placeholder && value === void 0 && field.type && field.type in MOCK_SCALAR) data[field.name] = MOCK_SCALAR[field.type];
|
|
422
|
+
else if (field.type === "object" && isObject(value)) fill(field.fields, value, placeholder);
|
|
423
|
+
else if (field.type === "array" && Array.isArray(value) && !value.length && field.fields?.length) {
|
|
424
|
+
const item = getDefaultValueFromFields(field.fields);
|
|
425
|
+
fill(field.fields, item, true);
|
|
426
|
+
data[field.name] = [item];
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
};
|
|
430
|
+
const data = getDefaultValueFromFields(fields);
|
|
431
|
+
fill(fields, data, false);
|
|
432
|
+
return data;
|
|
433
|
+
};
|
|
419
434
|
var getKeys = Object.keys;
|
|
420
435
|
var calculatePercentage = (value, percentageStr) => {
|
|
421
436
|
return value * (globalThis.parseFloat(percentageStr) / 100);
|
|
@@ -460,4 +475,4 @@ var isValueIncludeDataSource = (value) => {
|
|
|
460
475
|
return false;
|
|
461
476
|
};
|
|
462
477
|
var removeDataSourceFieldPrefix = (id) => id?.replace("ds-field::", "") || "";
|
|
463
|
-
export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DATA_SOURCE_SET_DATA_METHOD_NAME, 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, isArrayIndex, isDslNode, isNumber, isObject, isPage, isPageFragment, isPageOrFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, removeDataSourceFieldPrefix, replaceChildNode, replaceChildNodes, setDslDomRelateConfig, setIdToEl, setUrlParam, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
|
|
478
|
+
export { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, DATA_SOURCE_SET_DATA_METHOD_NAME, 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, getMockDataFromFields, getNodeInfo, getNodePath, getNodes, getSearchObj, getUrlParam, getValueByKeyPath, guid, injectStyle, isArrayIndex, isDslNode, isNumber, isObject, isPage, isPageFragment, isPageOrFragment, isPercentage, isPop, isSameDomain, isValueIncludeDataSource, removeClassName, removeClassNameByClassName, removeDataSourceFieldPrefix, replaceChildNode, replaceChildNodes, setDslDomRelateConfig, setIdToEl, setUrlParam, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
|
|
@@ -408,7 +408,7 @@
|
|
|
408
408
|
}
|
|
409
409
|
// node_modules/.pnpm/lodash-es@4.18.1/node_modules/lodash-es/_isIndex.js
|
|
410
410
|
/** Used as references for various `Number` constants. */
|
|
411
|
-
var MAX_SAFE_INTEGER$1 =
|
|
411
|
+
var MAX_SAFE_INTEGER$1 = 0x1fffffffffffff;
|
|
412
412
|
/** Used to detect unsigned integer values. */
|
|
413
413
|
var reIsUint = /^(?:0|[1-9]\d*)$/;
|
|
414
414
|
/**
|
|
@@ -522,7 +522,7 @@
|
|
|
522
522
|
}
|
|
523
523
|
// node_modules/.pnpm/lodash-es@4.18.1/node_modules/lodash-es/isLength.js
|
|
524
524
|
/** Used as references for various `Number` constants. */
|
|
525
|
-
var MAX_SAFE_INTEGER =
|
|
525
|
+
var MAX_SAFE_INTEGER = 0x1fffffffffffff;
|
|
526
526
|
/**
|
|
527
527
|
* Checks if `value` is a valid array-like length.
|
|
528
528
|
*
|
|
@@ -2398,17 +2398,28 @@
|
|
|
2398
2398
|
return info;
|
|
2399
2399
|
};
|
|
2400
2400
|
var filterXSS = (str) => str.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
2401
|
+
/**
|
|
2402
|
+
* 拆出 path、query(不含 ?)与 hash(含 #)
|
|
2403
|
+
*
|
|
2404
|
+
* 不用 URL 对象:它要求入参是合法的绝对地址,且会把已有参数重新编码
|
|
2405
|
+
*/
|
|
2406
|
+
var splitUrl = (url) => {
|
|
2407
|
+
const hashIndex = url.indexOf("#");
|
|
2408
|
+
const rest = hashIndex === -1 ? url : url.slice(0, hashIndex);
|
|
2409
|
+
const queryIndex = rest.indexOf("?");
|
|
2410
|
+
return {
|
|
2411
|
+
path: queryIndex === -1 ? rest : rest.slice(0, queryIndex),
|
|
2412
|
+
query: queryIndex === -1 ? "" : rest.slice(queryIndex + 1),
|
|
2413
|
+
hash: hashIndex === -1 ? "" : url.slice(hashIndex)
|
|
2414
|
+
};
|
|
2415
|
+
};
|
|
2416
|
+
var joinUrl = (path, query, hash) => `${path}${query ? `?${query}` : ""}${hash}`;
|
|
2417
|
+
/** 按参数名逐段比较,不把参数名当正则,也避免 i 标志带来的大小写误匹配 */
|
|
2418
|
+
var isParamOf = (name, pair) => pair.split("=")[0] === name;
|
|
2401
2419
|
var getUrlParam = (param, url) => {
|
|
2402
|
-
const
|
|
2403
|
-
const
|
|
2404
|
-
|
|
2405
|
-
let strArr;
|
|
2406
|
-
if (matches && matches.length > 0) {
|
|
2407
|
-
strArr = matches[matches.length - 1].split("=");
|
|
2408
|
-
if (strArr && strArr.length > 1) return filterXSS(strArr[1]);
|
|
2409
|
-
return "";
|
|
2410
|
-
}
|
|
2411
|
-
return "";
|
|
2420
|
+
const { query, hash } = splitUrl(url || location.href);
|
|
2421
|
+
const value = new URLSearchParams(query).getAll(param).pop() ?? new URLSearchParams(hash.slice(1)).getAll(param).pop();
|
|
2422
|
+
return value ? filterXSS(value) : "";
|
|
2412
2423
|
};
|
|
2413
2424
|
/**
|
|
2414
2425
|
* 设置url中指定的参数
|
|
@@ -2422,22 +2433,14 @@
|
|
|
2422
2433
|
* @return {string} [返回处理后的url]
|
|
2423
2434
|
*/
|
|
2424
2435
|
var setUrlParam = (name, value, url = globalThis.location.href) => {
|
|
2425
|
-
const
|
|
2426
|
-
const
|
|
2427
|
-
const
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
else
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
const first = strArr.charAt(0);
|
|
2434
|
-
url = url.replace(strArr, key);
|
|
2435
|
-
url = url.replace(key, value ? first + extra : "");
|
|
2436
|
-
} else if (value) {
|
|
2437
|
-
if (url.indexOf("?") > -1) url += `&${extra}`;
|
|
2438
|
-
else url += `?${extra}`;
|
|
2439
|
-
}
|
|
2440
|
-
return url;
|
|
2436
|
+
const { path, query, hash } = splitUrl(url);
|
|
2437
|
+
const pairs = query ? query.split("&") : [];
|
|
2438
|
+
const index = pairs.findIndex((pair) => isParamOf(name, pair));
|
|
2439
|
+
if (index === -1) {
|
|
2440
|
+
if (value) pairs.push(`${name}=${encodeURIComponent(value)}`);
|
|
2441
|
+
} else if (value) pairs[index] = `${name}=${encodeURIComponent(value)}`;
|
|
2442
|
+
else pairs.splice(index, 1);
|
|
2443
|
+
return joinUrl(path, pairs.join("&"), hash);
|
|
2441
2444
|
};
|
|
2442
2445
|
var getSearchObj = (search = globalThis.location.search ? globalThis.location.search.substring(1) : "") => {
|
|
2443
2446
|
return search.split("&").reduce((obj, item) => {
|
|
@@ -2449,24 +2452,10 @@
|
|
|
2449
2452
|
}, {});
|
|
2450
2453
|
};
|
|
2451
2454
|
var delQueStr = (url, ref) => {
|
|
2452
|
-
|
|
2453
|
-
if (
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
let returnurl = "";
|
|
2457
|
-
const isHit = Array.isArray(ref) ? function(v) {
|
|
2458
|
-
return ~ref.indexOf(v);
|
|
2459
|
-
} : function(v) {
|
|
2460
|
-
return v === ref;
|
|
2461
|
-
};
|
|
2462
|
-
if (str.indexOf("&") !== -1) {
|
|
2463
|
-
arr = str.split("&");
|
|
2464
|
-
for (let i = 0, len = arr.length; i < len; i++) if (!isHit(arr[i].split("=")[0])) returnurl = `${returnurl + arr[i].split("=")[0]}=${arr[i].split("=")[1]}&`;
|
|
2465
|
-
return returnurl ? `${url.substr(0, url.indexOf("?"))}?${returnurl.substr(0, returnurl.length - 1)}` : url.substr(0, url.indexOf("?"));
|
|
2466
|
-
}
|
|
2467
|
-
arr = str.split("=");
|
|
2468
|
-
if (isHit(arr[0])) return url.substr(0, url.indexOf("?"));
|
|
2469
|
-
return url;
|
|
2455
|
+
const { path, query, hash } = splitUrl(url);
|
|
2456
|
+
if (!query) return url;
|
|
2457
|
+
const names = Array.isArray(ref) ? ref : [ref];
|
|
2458
|
+
return joinUrl(path, query.split("&").filter((pair) => !names.includes(pair.split("=")[0])).join("&"), hash);
|
|
2470
2459
|
};
|
|
2471
2460
|
var isObject = (obj) => Object.prototype.toString.call(obj) === "[object Object]";
|
|
2472
2461
|
var isPop = (node) => Boolean(node?.type?.toLowerCase().endsWith("pop"));
|
|
@@ -2628,7 +2617,7 @@
|
|
|
2628
2617
|
const sortedNodes = newNodes.map((node, order) => ({
|
|
2629
2618
|
node,
|
|
2630
2619
|
order,
|
|
2631
|
-
depth: locations.get(`${node.id}`)?.depth ??
|
|
2620
|
+
depth: locations.get(`${node.id}`)?.depth ?? 1e400
|
|
2632
2621
|
})).sort((a, b) => a.depth === b.depth ? a.order - b.order : a.depth - b.depth).map(({ node }) => node);
|
|
2633
2622
|
for (let i = 0, l = sortedNodes.length; i < l; i++) {
|
|
2634
2623
|
const newNode = sortedNodes[i];
|
|
@@ -2749,6 +2738,32 @@
|
|
|
2749
2738
|
});
|
|
2750
2739
|
return data;
|
|
2751
2740
|
};
|
|
2741
|
+
/** 示例元素里的空值要能写进 JSON,undefined 会被丢掉 */
|
|
2742
|
+
var MOCK_SCALAR = {
|
|
2743
|
+
string: "",
|
|
2744
|
+
number: 0,
|
|
2745
|
+
boolean: false,
|
|
2746
|
+
null: null,
|
|
2747
|
+
any: null
|
|
2748
|
+
};
|
|
2749
|
+
/** 按字段生成 mock。空数组补一条子字段示例,已有内容不动 */
|
|
2750
|
+
var getMockDataFromFields = (fields = []) => {
|
|
2751
|
+
const fill = (list, data, placeholder) => {
|
|
2752
|
+
list?.forEach((field) => {
|
|
2753
|
+
const value = data[field.name];
|
|
2754
|
+
if (placeholder && value === void 0 && field.type && field.type in MOCK_SCALAR) data[field.name] = MOCK_SCALAR[field.type];
|
|
2755
|
+
else if (field.type === "object" && isObject(value)) fill(field.fields, value, placeholder);
|
|
2756
|
+
else if (field.type === "array" && Array.isArray(value) && !value.length && field.fields?.length) {
|
|
2757
|
+
const item = getDefaultValueFromFields(field.fields);
|
|
2758
|
+
fill(field.fields, item, true);
|
|
2759
|
+
data[field.name] = [item];
|
|
2760
|
+
}
|
|
2761
|
+
});
|
|
2762
|
+
};
|
|
2763
|
+
const data = getDefaultValueFromFields(fields);
|
|
2764
|
+
fill(fields, data, false);
|
|
2765
|
+
return data;
|
|
2766
|
+
};
|
|
2752
2767
|
var getKeys = Object.keys;
|
|
2753
2768
|
var calculatePercentage = (value, percentageStr) => {
|
|
2754
2769
|
return value * (globalThis.parseFloat(percentageStr) / 100);
|
|
@@ -2823,6 +2838,7 @@
|
|
|
2823
2838
|
exports.getIdFromEl = getIdFromEl;
|
|
2824
2839
|
exports.getKeys = getKeys;
|
|
2825
2840
|
exports.getKeysArray = getKeysArray;
|
|
2841
|
+
exports.getMockDataFromFields = getMockDataFromFields;
|
|
2826
2842
|
exports.getNodeInfo = getNodeInfo;
|
|
2827
2843
|
exports.getNodePath = getNodePath;
|
|
2828
2844
|
exports.getNodes = getNodes;
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.8.
|
|
2
|
+
"version": "1.8.7",
|
|
3
3
|
"name": "@tmagic/utils",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"@types/lodash-es": "^4.17.12"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@tmagic/schema": "1.8.
|
|
37
|
+
"@tmagic/schema": "1.8.7",
|
|
38
38
|
"typescript": "^6.0.3"
|
|
39
39
|
},
|
|
40
40
|
"peerDependenciesMeta": {
|
package/src/index.ts
CHANGED
|
@@ -154,21 +154,35 @@ export const getNodeInfo = (id: Id, root: { id: Id; items?: MNode[] } | null, sk
|
|
|
154
154
|
export const filterXSS = (str: string) =>
|
|
155
155
|
str.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
|
156
156
|
|
|
157
|
+
/**
|
|
158
|
+
* 拆出 path、query(不含 ?)与 hash(含 #)
|
|
159
|
+
*
|
|
160
|
+
* 不用 URL 对象:它要求入参是合法的绝对地址,且会把已有参数重新编码
|
|
161
|
+
*/
|
|
162
|
+
const splitUrl = (url: string) => {
|
|
163
|
+
const hashIndex = url.indexOf('#');
|
|
164
|
+
const rest = hashIndex === -1 ? url : url.slice(0, hashIndex);
|
|
165
|
+
const queryIndex = rest.indexOf('?');
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
path: queryIndex === -1 ? rest : rest.slice(0, queryIndex),
|
|
169
|
+
query: queryIndex === -1 ? '' : rest.slice(queryIndex + 1),
|
|
170
|
+
hash: hashIndex === -1 ? '' : url.slice(hashIndex),
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const joinUrl = (path: string, query: string, hash: string) => `${path}${query ? `?${query}` : ''}${hash}`;
|
|
175
|
+
|
|
176
|
+
/** 按参数名逐段比较,不把参数名当正则,也避免 i 标志带来的大小写误匹配 */
|
|
177
|
+
const isParamOf = (name: string, pair: string) => pair.split('=')[0] === name;
|
|
178
|
+
|
|
157
179
|
export const getUrlParam = (param: string, url?: string) => {
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
strArr = matches[matches.length - 1].split('=');
|
|
165
|
-
if (strArr && strArr.length > 1) {
|
|
166
|
-
// 过滤XSS字符
|
|
167
|
-
return filterXSS(strArr[1]);
|
|
168
|
-
}
|
|
169
|
-
return '';
|
|
170
|
-
}
|
|
171
|
-
return '';
|
|
180
|
+
const { query, hash } = splitUrl(url || location.href);
|
|
181
|
+
// URLSearchParams 不会抛错,也不要求 url 是合法地址
|
|
182
|
+
const value =
|
|
183
|
+
new URLSearchParams(query).getAll(param).pop() ?? new URLSearchParams(hash.slice(1)).getAll(param).pop();
|
|
184
|
+
|
|
185
|
+
return value ? filterXSS(value) : '';
|
|
172
186
|
};
|
|
173
187
|
|
|
174
188
|
/**
|
|
@@ -183,36 +197,24 @@ export const getUrlParam = (param: string, url?: string) => {
|
|
|
183
197
|
* @return {string} [返回处理后的url]
|
|
184
198
|
*/
|
|
185
199
|
export const setUrlParam = (name: string, value: string, url = globalThis.location.href) => {
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
200
|
+
const { path, query, hash } = splitUrl(url);
|
|
201
|
+
const pairs = query ? query.split('&') : [];
|
|
202
|
+
const index = pairs.findIndex((pair) => isParamOf(name, pair));
|
|
203
|
+
|
|
204
|
+
if (index === -1) {
|
|
205
|
+
// 原url中不含有该参数且value不为空时,追加到已有参数之后
|
|
206
|
+
if (value) {
|
|
207
|
+
pairs.push(`${name}=${encodeURIComponent(value)}`);
|
|
208
|
+
}
|
|
209
|
+
} else if (value) {
|
|
210
|
+
// 原位替换,不动其它参数
|
|
211
|
+
pairs[index] = `${name}=${encodeURIComponent(value)}`;
|
|
195
212
|
} else {
|
|
196
|
-
|
|
213
|
+
// value为空时删除该参数
|
|
214
|
+
pairs.splice(index, 1);
|
|
197
215
|
}
|
|
198
216
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
// 当原url中含有要替换的属性:value不为空时,仅对值做替换,为空时,直接把参数删除掉
|
|
202
|
-
if (strArr) {
|
|
203
|
-
const first = strArr.charAt(0);
|
|
204
|
-
url = url.replace(strArr, key);
|
|
205
|
-
url = url.replace(key, value ? first + extra : '');
|
|
206
|
-
} else if (value) {
|
|
207
|
-
// 当原url中不含有要替换的属性且value值不为空时,直接在url后面添加参数字符串
|
|
208
|
-
if (url.indexOf('?') > -1) {
|
|
209
|
-
url += `&${extra}`;
|
|
210
|
-
} else {
|
|
211
|
-
url += `?${extra}`;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
// 其它情况直接返回原url
|
|
215
|
-
return url;
|
|
217
|
+
return joinUrl(path, pairs.join('&'), hash);
|
|
216
218
|
};
|
|
217
219
|
|
|
218
220
|
export const getSearchObj = (
|
|
@@ -225,43 +227,16 @@ export const getSearchObj = (
|
|
|
225
227
|
};
|
|
226
228
|
|
|
227
229
|
export const delQueStr = (url: string, ref: string[] | string) => {
|
|
228
|
-
|
|
229
|
-
if (url.indexOf('?') !== -1) {
|
|
230
|
-
str = url.substring(url.indexOf('?') + 1);
|
|
231
|
-
} else {
|
|
232
|
-
return url;
|
|
233
|
-
}
|
|
234
|
-
let arr = [];
|
|
235
|
-
let returnurl = '';
|
|
236
|
-
|
|
237
|
-
const isHit = Array.isArray(ref)
|
|
238
|
-
? function (v: string) {
|
|
239
|
-
return ~ref.indexOf(v);
|
|
240
|
-
}
|
|
241
|
-
: function (v: string) {
|
|
242
|
-
return v === ref;
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
if (str.indexOf('&') !== -1) {
|
|
246
|
-
arr = str.split('&');
|
|
247
|
-
for (let i = 0, len = arr.length; i < len; i++) {
|
|
248
|
-
if (!isHit(arr[i].split('=')[0])) {
|
|
249
|
-
returnurl = `${returnurl + arr[i].split('=')[0]}=${arr[i].split('=')[1]}&`;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
230
|
+
const { path, query, hash } = splitUrl(url);
|
|
252
231
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
: url.substr(0, url.indexOf('?'));
|
|
232
|
+
if (!query) {
|
|
233
|
+
return url;
|
|
256
234
|
}
|
|
257
235
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
if (isHit(arr[0])) {
|
|
261
|
-
return url.substr(0, url.indexOf('?'));
|
|
262
|
-
}
|
|
236
|
+
const names = Array.isArray(ref) ? ref : [ref];
|
|
237
|
+
const pairs = query.split('&').filter((pair) => !names.includes(pair.split('=')[0]));
|
|
263
238
|
|
|
264
|
-
return
|
|
239
|
+
return joinUrl(path, pairs.join('&'), hash);
|
|
265
240
|
};
|
|
266
241
|
|
|
267
242
|
export const isObject = (obj: any) => Object.prototype.toString.call(obj) === '[object Object]';
|
|
@@ -714,6 +689,38 @@ export const getDefaultValueFromFields = (fields: DataSchema[]) => {
|
|
|
714
689
|
return data;
|
|
715
690
|
};
|
|
716
691
|
|
|
692
|
+
/** 示例元素里的空值要能写进 JSON,undefined 会被丢掉 */
|
|
693
|
+
const MOCK_SCALAR: Record<string, any> = {
|
|
694
|
+
string: '',
|
|
695
|
+
number: 0,
|
|
696
|
+
boolean: false,
|
|
697
|
+
null: null,
|
|
698
|
+
any: null,
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
/** 按字段生成 mock。空数组补一条子字段示例,已有内容不动 */
|
|
702
|
+
export const getMockDataFromFields = (fields: DataSchema[] = []) => {
|
|
703
|
+
const fill = (list: DataSchema[] | undefined, data: Record<string, any>, placeholder: boolean) => {
|
|
704
|
+
list?.forEach((field) => {
|
|
705
|
+
const value = data[field.name];
|
|
706
|
+
|
|
707
|
+
if (placeholder && value === undefined && field.type && field.type in MOCK_SCALAR) {
|
|
708
|
+
data[field.name] = MOCK_SCALAR[field.type];
|
|
709
|
+
} else if (field.type === 'object' && isObject(value)) {
|
|
710
|
+
fill(field.fields, value, placeholder);
|
|
711
|
+
} else if (field.type === 'array' && Array.isArray(value) && !value.length && field.fields?.length) {
|
|
712
|
+
const item = getDefaultValueFromFields(field.fields);
|
|
713
|
+
fill(field.fields, item, true);
|
|
714
|
+
data[field.name] = [item];
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
const data = getDefaultValueFromFields(fields);
|
|
720
|
+
fill(fields, data, false);
|
|
721
|
+
return data;
|
|
722
|
+
};
|
|
723
|
+
|
|
717
724
|
export const getKeys = Object.keys as <T extends object>(obj: T) => Array<keyof T>;
|
|
718
725
|
|
|
719
726
|
export const calculatePercentage = (value: number, percentageStr: string) => {
|
package/types/index.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { DataSchema, DataSourceDeps, Id, MComponent, MNode, MNodeInstance } from "@tmagic/schema";
|
|
2
2
|
import { Component } from "vue";
|
|
3
|
-
import { EventEmitter } from "events";
|
|
4
3
|
import { PascalCasedProperties, Writable } from "type-fest";
|
|
5
4
|
import { CodeBlockContent, CodeBlockDSL, DataSourceSchema, DepData, DepExtendedData, DepTargetType, EventOption, Id as Id$1, MApp, MContainer, MNode as MNode$1, MPage, MPageFragment, Target, TargetDescriptor, TargetNode, TargetOptions } from "@tmagic/core";
|
|
6
5
|
import { ChangeRecord, FormConfig, TableColumnConfig } from "@tmagic/form";
|
|
7
6
|
import StageCore, { CanDropIn, ContainerHighlightType, CustomizeMoveableOptions, GuidesOptions, RenderType, UpdateDragEl } from "@tmagic/stage";
|
|
8
|
-
|
|
7
|
+
import { EventEmitter } from "events";
|
|
9
8
|
type DepsData = Record<string, Record<Id$1, DepData>>;
|
|
10
9
|
interface CollectWorkerResult {
|
|
11
10
|
deps: DepsData;
|
|
@@ -19,8 +18,6 @@ interface CollectWorkerPayload {
|
|
|
19
18
|
depExtendedData: DepExtendedData;
|
|
20
19
|
deep: boolean;
|
|
21
20
|
}
|
|
22
|
-
//#endregion
|
|
23
|
-
//#region temp/packages/editor/src/sdk/services/BaseService.d.ts
|
|
24
21
|
/**
|
|
25
22
|
* 对Class进行扩展
|
|
26
23
|
* 给Class中的每个方法都添加before after两个钩子
|
|
@@ -63,8 +60,6 @@ declare class BaseService extends EventEmitter {
|
|
|
63
60
|
removeAllPlugins(): void;
|
|
64
61
|
private doTask;
|
|
65
62
|
}
|
|
66
|
-
//#endregion
|
|
67
|
-
//#region temp/packages/editor/src/sdk/services/ComponentList.d.ts
|
|
68
63
|
declare class ComponentList extends BaseService {
|
|
69
64
|
private state;
|
|
70
65
|
constructor(ctx: SdkContext);
|
|
@@ -77,8 +72,6 @@ declare class ComponentList extends BaseService {
|
|
|
77
72
|
destroy(): void;
|
|
78
73
|
}
|
|
79
74
|
type ComponentListService = ComponentList;
|
|
80
|
-
//#endregion
|
|
81
|
-
//#region temp/packages/editor/src/sdk/services/DataSource.d.ts
|
|
82
75
|
interface State$1 {
|
|
83
76
|
datasourceTypeList: DatasourceTypeOption[];
|
|
84
77
|
dataSources: DataSourceSchema[];
|
|
@@ -248,8 +241,6 @@ declare class DataSource extends BaseService {
|
|
|
248
241
|
private applyHistoryStep;
|
|
249
242
|
}
|
|
250
243
|
type DataSourceService = DataSource;
|
|
251
|
-
//#endregion
|
|
252
|
-
//#region temp/packages/editor/src/sdk/services/Dep.d.ts
|
|
253
244
|
interface DepEvents {
|
|
254
245
|
'add-target': [target: Target];
|
|
255
246
|
'remove-target': [id: string | number, type: string | DepTargetType];
|
|
@@ -333,8 +324,6 @@ declare class Dep extends BaseService {
|
|
|
333
324
|
private updateCollectingState;
|
|
334
325
|
}
|
|
335
326
|
type DepService = Dep;
|
|
336
|
-
//#endregion
|
|
337
|
-
//#region temp/packages/editor/src/sdk/services/Editor.d.ts
|
|
338
327
|
interface EditorServiceConfig {
|
|
339
328
|
disabledPageFragment?: boolean;
|
|
340
329
|
canDropIn?: CanDropInFunction;
|
|
@@ -516,7 +505,7 @@ declare class Editor extends BaseService {
|
|
|
516
505
|
* @param options 可选配置
|
|
517
506
|
* @param options.doNotPushHistory 是否不写入历史记录(默认 false)
|
|
518
507
|
*/
|
|
519
|
-
moveLayer(offset: number | LayerOffset,
|
|
508
|
+
moveLayer(offset: number | LayerOffset, options?: DslOpOptions): Promise<void>;
|
|
520
509
|
/**
|
|
521
510
|
* 移动一个或多个节点到指定容器中。
|
|
522
511
|
*
|
|
@@ -532,7 +521,7 @@ declare class Editor extends BaseService {
|
|
|
532
521
|
* @param options.doNotPushHistory 是否不写入历史记录(默认 false)
|
|
533
522
|
*/
|
|
534
523
|
moveToContainer(config: MNode$1 | MNode$1[], targetId: Id$1, { doNotSelect, doNotSwitchPage, doNotPushHistory, historyDescription, historySource }?: DslOpOptions): Promise<MNode$1 | MNode$1[]>;
|
|
535
|
-
dragTo(config: MNode$1 | MNode$1[], targetParent: MContainer, targetIndex: number,
|
|
524
|
+
dragTo(config: MNode$1 | MNode$1[], targetParent: MContainer, targetIndex: number, options?: DslOpOptions): Promise<void>;
|
|
536
525
|
/**
|
|
537
526
|
* 下列 *AndGetHistoryId 方法与对应的普通操作(add / remove / update ...)行为完全一致,
|
|
538
527
|
* 返回值在 {@link DslOpWithHistoryIdsResult} 中同时包含原操作结果与本次写入历史栈的 uuid 列表({@link StepValue.uuid}),
|
|
@@ -662,9 +651,11 @@ declare class Editor extends BaseService {
|
|
|
662
651
|
/**
|
|
663
652
|
* 构造一条页面级「set root」历史记录(不携带选区 / modifiedNodeIds 上下文)并落到该页面自己的栈。
|
|
664
653
|
*
|
|
665
|
-
* 连续 set root
|
|
666
|
-
*
|
|
667
|
-
*
|
|
654
|
+
* 连续 set root 替换:若该页栈最新一条已是**同来源、同为整页 update**的 set root 记录
|
|
655
|
+
* ({@link StepValue.rootStep} 且 `source` 相同),则用本次记录**替换**它而非新增,避免源码反复保存时堆积记录。
|
|
656
|
+
* 替换时必须把 `oldSchema` 追溯到最早那一跳,并沿用原 uuid:否则只剩「上一版 → 本版」,
|
|
657
|
+
* 撤销到基线时 DSL 会停在中途版本且无法继续回退,已发出的 historyId 也会失效。
|
|
658
|
+
* 其余情况(来源不同 / 页面增删)照常新增,保留完整变更链条。
|
|
668
659
|
*/
|
|
669
660
|
private pushPageDiffStep;
|
|
670
661
|
private pushOpHistory;
|
|
@@ -676,8 +667,8 @@ declare class Editor extends BaseService {
|
|
|
676
667
|
* 「重新插回节点」(撤销 remove / 重做 add)需按 step 记录的 parentId / index 精确还原,
|
|
677
668
|
* 不走 this.add,由手工 splice + stage.add 完成后补发等价的 add / change 事件(见 emitHistoryInsertEvents)。
|
|
678
669
|
*
|
|
679
|
-
* 注意:这些路径都会发出 add / remove / update / change
|
|
680
|
-
*
|
|
670
|
+
* 注意:这些路径都会发出 add / remove / update / change 事件,且 `change` 事件带 `isHistoryOp: true`,
|
|
671
|
+
* 业务侧可据此跳过仅针对「用户主动编辑」的副作用(加锁 / 埋点 / 自动保存等)。
|
|
681
672
|
*
|
|
682
673
|
* @param step 操作记录
|
|
683
674
|
* @param reverse true = 撤销,false = 重做
|
|
@@ -693,8 +684,6 @@ declare class Editor extends BaseService {
|
|
|
693
684
|
private toRaw;
|
|
694
685
|
}
|
|
695
686
|
type EditorService = Editor;
|
|
696
|
-
//#endregion
|
|
697
|
-
//#region temp/packages/editor/src/sdk/services/Events.d.ts
|
|
698
687
|
declare const canUsePluginMethods$7: {
|
|
699
688
|
async: readonly [];
|
|
700
689
|
sync: readonly ["setEvent", "getEvent", "setMethod", "getMethod"];
|
|
@@ -721,8 +710,6 @@ declare class Events extends BaseService {
|
|
|
721
710
|
usePlugin(options: AsyncHookPlugin<AsyncMethodName$4, Events> & SyncHookPlugin<SyncMethodName$4, Events>): void;
|
|
722
711
|
}
|
|
723
712
|
type EventsService = Events;
|
|
724
|
-
//#endregion
|
|
725
|
-
//#region temp/packages/editor/src/sdk/services/History.d.ts
|
|
726
713
|
declare const canUsePluginMethods$6: {
|
|
727
714
|
sync: readonly ["push", "undo", "redo"];
|
|
728
715
|
};
|
|
@@ -735,12 +722,12 @@ declare class History extends BaseService {
|
|
|
735
722
|
* 注册一个扩展历史类型,使其可与内置 `page` / `codeBlock` / `dataSource` 一样走统一的
|
|
736
723
|
* {@link push} / {@link undo} / {@link redo}(按 id 分栈、独立 undo/redo)。
|
|
737
724
|
*
|
|
725
|
+
* 扩展类型与内置类型一样统一派发 `change` 事件,无需(也不支持)单独指定事件名。
|
|
726
|
+
*
|
|
738
727
|
* @param stepType 自定义历史类型标识(勿与内置类型重名)
|
|
739
|
-
* @param options.event push/undo/redo 后派发的事件名,缺省为 `${stepType}-history-change`
|
|
740
728
|
* @param options.name 历史面板中的展示名称(tab / 分组标题等),缺省回退到 stepType 本身
|
|
741
729
|
*/
|
|
742
730
|
registerStepType(stepType: string, options?: {
|
|
743
|
-
event?: string;
|
|
744
731
|
name?: string;
|
|
745
732
|
}): void;
|
|
746
733
|
/**
|
|
@@ -780,10 +767,16 @@ declare class History extends BaseService {
|
|
|
780
767
|
* 不存在或 id 缺省时返回 undefined。
|
|
781
768
|
*/
|
|
782
769
|
getMarker(stepType: HistoryStepType, id?: Id$1): StepValue | undefined;
|
|
770
|
+
/**
|
|
771
|
+
* 目标栈「回到初始」是否可达:容量裁剪删掉已应用的真实步骤后撤销链断开,返回 false。
|
|
772
|
+
* 与是否存在 {@link getMarker} 基线无关——没有基线的栈同样可能因裁剪回不到加载时的 DSL,
|
|
773
|
+
* 历史面板的各 tab 统一用它判断初始行是否可高亮 / 可点击。
|
|
774
|
+
*/
|
|
775
|
+
isInitialReachable(stepType: HistoryStepType, id?: Id$1): boolean;
|
|
783
776
|
/**
|
|
784
777
|
* 把一条步骤推入指定历史栈。统一入口,所有类型(page / codeBlock / dataSource / 扩展)行为完全一致:
|
|
785
|
-
* 按 `stepType` 选择目标栈类型,按 `id
|
|
786
|
-
*
|
|
778
|
+
* 按 `stepType` 选择目标栈类型,按 `id`(必填)选择具体栈,按需建栈后入栈,并派发历史变更事件
|
|
779
|
+
* (统一为 `change`,回调签名 `(step, stepType, id, isHistoryOp)`)。
|
|
787
780
|
*
|
|
788
781
|
* 跨页 / 跨资源操作(如 `moveToContainer` 把节点搬到其它页)必须显式传入目标 id,
|
|
789
782
|
* 否则无法落到正确的栈。step 可由 `createStackStep` 等构造后传入。
|
|
@@ -799,7 +792,7 @@ declare class History extends BaseService {
|
|
|
799
792
|
/**
|
|
800
793
|
* 撤销指定历史栈的最近一次变更。统一入口,所有类型行为一致:
|
|
801
794
|
* 按 `id`(必填)+ `stepType` 定位栈,不会越过 index 0 的 initial 基线(所有类型同等适用),
|
|
802
|
-
* 仅在确有可撤销 step
|
|
795
|
+
* 仅在确有可撤销 step 时派发历史变更事件(签名同 {@link push})。
|
|
803
796
|
*
|
|
804
797
|
* @param stepType 历史类型
|
|
805
798
|
* @param id 目标栈 id(page 为 pageId,其余类型为对应资源 id),必填
|
|
@@ -857,6 +850,9 @@ declare class History extends BaseService {
|
|
|
857
850
|
*
|
|
858
851
|
* 同一栈内任意时刻最多只有一条记录为 `saved`;从 IndexedDB 恢复时游标会被定位到最近一条已保存记录之后。
|
|
859
852
|
*
|
|
853
|
+
* 标记后把对应栈裁剪到容量上限({@link markStackSaved} → {@link UndoRedo.trim}):入栈与还原都不裁剪,
|
|
854
|
+
* 只有保存后才收敛,避免用户在落库前丢掉可撤销的历史;initial 基线与 set root 产生的记录不会被裁掉。
|
|
855
|
+
*
|
|
860
856
|
* @param stepType 历史类型
|
|
861
857
|
* @param id 目标栈 id;缺省表示标记全部类型 / 全部栈
|
|
862
858
|
*/
|
|
@@ -895,6 +891,10 @@ declare class History extends BaseService {
|
|
|
895
891
|
}): PersistedHistoryState;
|
|
896
892
|
/**
|
|
897
893
|
* 用 {@link serialize} 导出的快照重建全部撤销/重做栈,会整体覆盖当前内存中的历史状态。
|
|
894
|
+
*
|
|
895
|
+
* 单个栈 / 单个类型的快照损坏只丢弃该部分,其余照常还原;
|
|
896
|
+
* 快照里的栈全部还原失败时抛出首个错误,此时内存中的历史状态保持不变
|
|
897
|
+
* (不会把「什么都没恢复」当成一次成功的还原)。
|
|
898
898
|
*/
|
|
899
899
|
restore(snapshot: PersistedHistoryState): PersistedHistoryState;
|
|
900
900
|
/**
|
|
@@ -965,6 +965,12 @@ declare class History extends BaseService {
|
|
|
965
965
|
} | null;
|
|
966
966
|
usePlugin(options: SyncHookPlugin<SyncMethodName$3, History>): void;
|
|
967
967
|
emit<Name extends keyof HistoryEvents, Param extends HistoryEvents[Name]>(eventName: Name, ...args: Param): boolean;
|
|
968
|
+
/**
|
|
969
|
+
* 所有历史类型统一派发 `change`(签名 `(step, stepType, id, isHistoryOp)`);
|
|
970
|
+
* 代码块 / 数据源额外派发文档约定的同构事件(签名 `(id, step, isHistoryOp)`)。
|
|
971
|
+
* `isHistoryOp` 仅在 `undo` / `redo` 时为 true,供业务侧跳过历史回放触发的副作用。
|
|
972
|
+
*/
|
|
973
|
+
private emitStepChange;
|
|
968
974
|
/**
|
|
969
975
|
* 取出指定历史类型的栈桶(`Record<id, UndoRedo>`);不存在时按需创建空桶,
|
|
970
976
|
* 从而支持通过 {@link registerStepType} 或首次 push 动态扩展历史类型。
|
|
@@ -988,8 +994,6 @@ declare class History extends BaseService {
|
|
|
988
994
|
private resolveDbName;
|
|
989
995
|
}
|
|
990
996
|
type HistoryService = History;
|
|
991
|
-
//#endregion
|
|
992
|
-
//#region temp/packages/editor/src/sdk/services/Props.d.ts
|
|
993
997
|
declare const canUsePluginMethods$5: {
|
|
994
998
|
async: readonly ["setPropsConfig", "getPropsConfig", "setPropsValue", "getPropsValue", "fillConfig", "getDefaultPropsValue"];
|
|
995
999
|
sync: readonly ["createId", "setNewItemId"];
|
|
@@ -1086,8 +1090,6 @@ declare class Props extends BaseService {
|
|
|
1086
1090
|
private setRelateId;
|
|
1087
1091
|
}
|
|
1088
1092
|
type PropsService = Props;
|
|
1089
|
-
//#endregion
|
|
1090
|
-
//#region temp/packages/editor/src/sdk/services/WebStorage.d.ts
|
|
1091
1093
|
interface Options {
|
|
1092
1094
|
namespace?: string;
|
|
1093
1095
|
protocol?: Protocol;
|
|
@@ -1149,8 +1151,6 @@ declare class WebStorage extends BaseService {
|
|
|
1149
1151
|
private getValueAndProtocol;
|
|
1150
1152
|
}
|
|
1151
1153
|
type StorageService = WebStorage;
|
|
1152
|
-
//#endregion
|
|
1153
|
-
//#region temp/packages/editor/src/sdk/validate/index.d.ts
|
|
1154
1154
|
/**
|
|
1155
1155
|
* `record` 模式下,校验结果要等到修改落进 DSL 之后再写入 `invalidNodeIds`(修改过程中会先清掉旧记录),
|
|
1156
1156
|
* 因此 guard 返回一个提交函数交给调用方在合适的时机调用;`strict` 抛错,`off` 返回空。
|
|
@@ -1183,8 +1183,6 @@ interface Validator {
|
|
|
1183
1183
|
guardMove(sourceIds: Id$1[], target: MContainer, scene: CanDropInScene): Promise<void>;
|
|
1184
1184
|
guardDataSource(config: DataSourceSchema, isNew: boolean): void;
|
|
1185
1185
|
}
|
|
1186
|
-
//#endregion
|
|
1187
|
-
//#region temp/packages/editor/src/sdk/type.d.ts
|
|
1188
1186
|
/**
|
|
1189
1187
|
* 响应式实现,默认直接使用 Vue 的 `reactive` / `shallowReactive` / `toRaw` / `nextTick`;
|
|
1190
1188
|
* 可通过 `SdkOptions.reactivity` 覆盖。
|
|
@@ -1306,8 +1304,6 @@ interface SdkContext {
|
|
|
1306
1304
|
ready: Promise<void>;
|
|
1307
1305
|
parseDSL: (code: string) => any;
|
|
1308
1306
|
}
|
|
1309
|
-
//#endregion
|
|
1310
|
-
//#region temp/packages/editor/src/sdk/services/CodeBlock.d.ts
|
|
1311
1307
|
declare const canUsePluginMethods$3: {
|
|
1312
1308
|
async: readonly ["setCodeDslById", "setEditStatus", "setCombineIds", "setUndeleteableList", "deleteCodeDslByIds"];
|
|
1313
1309
|
sync: string[];
|
|
@@ -1535,8 +1531,6 @@ declare class CodeBlock extends BaseService {
|
|
|
1535
1531
|
private applyHistoryStep;
|
|
1536
1532
|
}
|
|
1537
1533
|
type CodeBlockService = CodeBlock;
|
|
1538
|
-
//#endregion
|
|
1539
|
-
//#region temp/packages/editor/src/services/keybinding.d.ts
|
|
1540
1534
|
declare class Keybinding extends BaseService {
|
|
1541
1535
|
ctrlKey: string;
|
|
1542
1536
|
private controllers;
|
|
@@ -1573,8 +1567,6 @@ declare class Keybinding extends BaseService {
|
|
|
1573
1567
|
private getKeyconKeys;
|
|
1574
1568
|
}
|
|
1575
1569
|
type KeybindingService = Keybinding;
|
|
1576
|
-
//#endregion
|
|
1577
|
-
//#region temp/packages/editor/src/services/stageOverlay.d.ts
|
|
1578
1570
|
declare const canUsePluginMethods$2: {
|
|
1579
1571
|
async: never[];
|
|
1580
1572
|
sync: readonly ["openOverlay", "closeOverlay", "updateOverlay", "createStage"];
|
|
@@ -1599,8 +1591,6 @@ declare class StageOverlay extends BaseService {
|
|
|
1599
1591
|
private updateSelectStatus;
|
|
1600
1592
|
}
|
|
1601
1593
|
type StageOverlayService = StageOverlay;
|
|
1602
|
-
//#endregion
|
|
1603
|
-
//#region temp/packages/editor/src/services/ui.d.ts
|
|
1604
1594
|
declare const canUsePluginMethods$1: {
|
|
1605
1595
|
async: readonly ["zoom", "calcZoom"];
|
|
1606
1596
|
sync: readonly [];
|
|
@@ -1618,8 +1608,6 @@ declare class Ui extends BaseService {
|
|
|
1618
1608
|
private setStageRect;
|
|
1619
1609
|
}
|
|
1620
1610
|
type UiService = Ui;
|
|
1621
|
-
//#endregion
|
|
1622
|
-
//#region temp/packages/editor/src/utils/undo-redo.d.ts
|
|
1623
1611
|
/**
|
|
1624
1612
|
* UndoRedo 栈的可序列化快照,用于持久化(如写入 IndexedDB)后再还原。
|
|
1625
1613
|
*/
|
|
@@ -1630,6 +1618,8 @@ interface SerializedUndoRedo<T = any> {
|
|
|
1630
1618
|
listCursor: number;
|
|
1631
1619
|
/** 栈容量上限。 */
|
|
1632
1620
|
listMaxSize: number;
|
|
1621
|
+
/** 「回到初始」是否可达:容量裁剪删掉已应用的真实步骤后为 false,旧快照缺省视为 true。 */
|
|
1622
|
+
initialReachable?: boolean;
|
|
1633
1623
|
}
|
|
1634
1624
|
declare class UndoRedo<T = any> {
|
|
1635
1625
|
/**
|
|
@@ -1645,6 +1635,8 @@ declare class UndoRedo<T = any> {
|
|
|
1645
1635
|
private elementList;
|
|
1646
1636
|
private listCursor;
|
|
1647
1637
|
private listMaxSize;
|
|
1638
|
+
/** 容量裁剪是否删掉过已应用的真实步骤(撤销链已断,「回到初始」不可达)。 */
|
|
1639
|
+
private initialReachable;
|
|
1648
1640
|
constructor(listMaxSize?: number);
|
|
1649
1641
|
/**
|
|
1650
1642
|
* 导出当前栈的可序列化快照(深克隆,避免外部改动污染内部状态)。
|
|
@@ -1652,6 +1644,25 @@ declare class UndoRedo<T = any> {
|
|
|
1652
1644
|
*/
|
|
1653
1645
|
serialize(): SerializedUndoRedo<T>;
|
|
1654
1646
|
pushElement(element: T): void;
|
|
1647
|
+
/**
|
|
1648
|
+
* 把栈裁剪到容量上限(`listMaxSize` 条真实步骤,index 0 的 initial 基线额外占一个位置)。
|
|
1649
|
+
*
|
|
1650
|
+
* 只在保存后调用(见 historyService.markSaved):保存前不裁剪,避免用户还没落库就丢掉可撤销的历史。
|
|
1651
|
+
* **永不删除** initial 基线与「整体设置 root」产生的记录(`rootStep`);
|
|
1652
|
+
* 受保护记录本身占满容量时栈保持超限。
|
|
1653
|
+
*
|
|
1654
|
+
* 分两个阶段删除,保证撤销链与重做链各自连续:
|
|
1655
|
+
* 1. 先从最旧的记录开始删**已应用**区(游标之前)的可删记录,撤销链仍然连续;
|
|
1656
|
+
* 2. 已应用区删无可删仍超限时,溢出量落在重做区。重做链必须连续——后一条是按前一条的 DSL 打的补丁,
|
|
1657
|
+
* 只删掉开头几条会让留下的步骤叠在缺失的基线上,因此整段丢弃游标之后的步骤。
|
|
1658
|
+
* (不从重做区末尾往前删:重做区里可能有受保护的 `rootStep`,跳过它去删它前面的步骤同样会留空洞。)
|
|
1659
|
+
*
|
|
1660
|
+
* 「回到初始」是否仍可达,取决于被删的已应用步骤相对最早一条 `rootStep` 的位置:
|
|
1661
|
+
* - 位于其**之后**:撤销链完整——一直撤销会退到这条整页快照,再撤销它即回到加载时的 DSL,仍可达;
|
|
1662
|
+
* - 位于其**之前**(或栈内没有 `rootStep`):撤到底只能停在中途版本,标记为不可达。
|
|
1663
|
+
* 只删掉重做区记录时不影响可达性。
|
|
1664
|
+
*/
|
|
1665
|
+
trim(): void;
|
|
1655
1666
|
canUndo(): boolean;
|
|
1656
1667
|
/** 返回被撤销的操作 */
|
|
1657
1668
|
undo(): T | null;
|
|
@@ -1686,9 +1697,9 @@ declare class UndoRedo<T = any> {
|
|
|
1686
1697
|
getCursor(): number;
|
|
1687
1698
|
/** 栈内总步数。 */
|
|
1688
1699
|
getLength(): number;
|
|
1700
|
+
/** 「回到初始」是否可达:容量裁剪删掉已应用的真实步骤后撤销链断开,返回 false。 */
|
|
1701
|
+
isInitialReachable(): boolean;
|
|
1689
1702
|
}
|
|
1690
|
-
//#endregion
|
|
1691
|
-
//#region temp/packages/editor/src/type.d.ts
|
|
1692
1703
|
interface Services {
|
|
1693
1704
|
editorService: EditorService;
|
|
1694
1705
|
historyService: HistoryService;
|
|
@@ -2088,6 +2099,7 @@ interface BaseStepValue<T = unknown, U extends Record<string, any> = {}> {
|
|
|
2088
2099
|
* 是否为「整体设置 root」(set root)产生的记录(由 {@link Editor.pushRootDiffHistory} 写入)。
|
|
2089
2100
|
* 用于「连续 set root 合并」:当某页栈最新一条已是 root 记录时,下一条 set root 会替换它而非新增,
|
|
2090
2101
|
* 避免源码反复保存 / 外部重设 DSL 时堆积多条 root 记录。
|
|
2102
|
+
* 保存后的容量裁剪(见 historyService.markSaved)也不会删除这类记录。
|
|
2091
2103
|
*/
|
|
2092
2104
|
rootStep?: boolean;
|
|
2093
2105
|
/** 操作人 */
|
|
@@ -2400,12 +2412,18 @@ interface EditorChangeEventHistoryMeta {
|
|
|
2400
2412
|
* 撤销 / 重做路径补发的事件恒为 true(撤销/重做本身不再入栈)。
|
|
2401
2413
|
*/
|
|
2402
2414
|
doNotPushHistory?: boolean;
|
|
2415
|
+
/**
|
|
2416
|
+
* 本次 DSL 变更是否由撤销 / 重做回放产生(缺省为 false)。
|
|
2417
|
+
* 业务侧监听 `change` 做埋点、加锁、自动保存等副作用时,可用它跳过历史回放触发的变更。
|
|
2418
|
+
* 仅由编辑器内部在回放链路设置,不通过 {@link HistoryOpOptions} 等外部入参传入。
|
|
2419
|
+
*/
|
|
2420
|
+
isHistoryOp?: boolean;
|
|
2403
2421
|
}
|
|
2404
2422
|
/**
|
|
2405
2423
|
* {@link EditorEvents.change} 的回调参数:以 `type` 区分操作类型,并携带对应的操作内容。
|
|
2406
2424
|
* `data` 为本次变更的节点列表,每项包含 node 及其所属的 page(可能为 null);
|
|
2407
2425
|
* `move-layer` 额外带层级偏移 `offset`,`drag-to` 额外带目标位置 `targetIndex` / `targetParent`;
|
|
2408
|
-
* 历史相关字段(historySource / doNotPushHistory)见 {@link EditorChangeEventHistoryMeta}。
|
|
2426
|
+
* 历史相关字段(historySource / doNotPushHistory / isHistoryOp)见 {@link EditorChangeEventHistoryMeta}。
|
|
2409
2427
|
*/
|
|
2410
2428
|
type EditorChangeEvent = ({
|
|
2411
2429
|
type: 'add';
|
|
@@ -2427,9 +2445,13 @@ type EditorChangeEvent = ({
|
|
|
2427
2445
|
targetParent: MContainer;
|
|
2428
2446
|
} & EditorChangeEventHistoryMeta);
|
|
2429
2447
|
interface HistoryEvents {
|
|
2430
|
-
change: [state: BaseStepValue | StepValue | CodeBlockStepValue | DataSourceStepValue, stepType: HistoryStepType, id: Id$1
|
|
2431
|
-
|
|
2432
|
-
|
|
2448
|
+
change: [state: BaseStepValue | StepValue | CodeBlockStepValue | DataSourceStepValue, stepType: HistoryStepType, id: Id$1,
|
|
2449
|
+
/** 本次变更是否由 `undo` / `redo` 触发;`push` / `replaceCurrentStep` 为 false */
|
|
2450
|
+
isHistoryOp?: boolean];
|
|
2451
|
+
/** 代码块专属事件;`isHistoryOp` 语义同 {@link HistoryEvents.change} 的第四个参数 */
|
|
2452
|
+
'code-block-history-change': [id: Id$1, state: CodeBlockStepValue, isHistoryOp: boolean];
|
|
2453
|
+
/** 数据源专属事件;`isHistoryOp` 语义同 {@link HistoryEvents.change} 的第四个参数 */
|
|
2454
|
+
'data-source-history-change': [id: Id$1, state: DataSourceStepValue, isHistoryOp: boolean];
|
|
2433
2455
|
'restore-from-indexed-db': [snapshot: PersistedHistoryState | null];
|
|
2434
2456
|
'save-to-indexed-db': [snapshot: PersistedHistoryState];
|
|
2435
2457
|
'mark-saved': [{
|
|
@@ -2465,6 +2487,9 @@ type AsyncMethodName = Writable<(typeof canUsePluginMethods)['async']>;
|
|
|
2465
2487
|
* - doNotPushHistory: 操作完成后是否不要将本次操作压入历史栈(撤销/重做记录),默认 false
|
|
2466
2488
|
* - historyDescription: 入栈时附带的人类可读描述,用于历史面板展示;不影响 undo/redo 行为,缺省时面板会自动生成描述
|
|
2467
2489
|
* - historySource: 操作途径,取值见 {@link HistoryOpSource}(画布 / 树面板 / 组件面板 / 配置面板 / 源码编辑器 / 右键菜单 / 工具栏 / 快捷键 / 回滚 / 接口 等),用于历史面板展示与埋点;不影响 undo/redo 行为
|
|
2490
|
+
*
|
|
2491
|
+
* 注意:`change` 事件上的 `isHistoryOp` 由编辑器在撤销 / 重做回放时内部设置,
|
|
2492
|
+
* 不是本配置的一部分;外部传入不会被读取,避免真实编辑被误标成历史回放。
|
|
2468
2493
|
*/
|
|
2469
2494
|
interface HistoryOpOptions {
|
|
2470
2495
|
doNotPushHistory?: boolean;
|
|
@@ -2493,9 +2518,11 @@ interface DslOpOptions extends HistoryOpOptions {
|
|
|
2493
2518
|
* 指定后不再按当前选中节点与上一次新增节点计算锚点,也不要求有选中节点;越界 / 非数字一律追加到末尾。
|
|
2494
2519
|
* 批量 add 时数字下标是本批节点的起始位置,按父容器分别累计偏移,保持传入顺序;'end' 逐个追加。
|
|
2495
2520
|
* 页面 / 页面片作用于 root.items,同样不再固定追加到末尾。人工添加不传,沿用锚点行为。
|
|
2521
|
+
* - doNotFixPosition: 跳过按画布尺寸修正节点位置(left / top),程序化 add 传入的设计坐标原样保留。
|
|
2496
2522
|
*/
|
|
2497
2523
|
interface AddOpOptions extends DslOpOptions {
|
|
2498
2524
|
insertIndex?: number | 'end';
|
|
2525
|
+
doNotFixPosition?: boolean;
|
|
2499
2526
|
}
|
|
2500
2527
|
/**
|
|
2501
2528
|
* {@link EditorService.update} / {@link EditorService.doUpdate} 的额外数据
|
|
@@ -2521,8 +2548,6 @@ interface UpdateOptions extends HistoryOpOptionsWithChangeRecords {
|
|
|
2521
2548
|
error?: string;
|
|
2522
2549
|
};
|
|
2523
2550
|
}
|
|
2524
|
-
//#endregion
|
|
2525
|
-
//#region temp/packages/utils/src/dom.d.ts
|
|
2526
2551
|
export declare const asyncLoadJs: (url: string, crossOrigin?: string, document?: Document) => any;
|
|
2527
2552
|
export declare const asyncLoadCss: (url: string, document?: Document) => any;
|
|
2528
2553
|
export declare const addClassName: (el: Element, doc: Document, className: string) => void;
|
|
@@ -2544,13 +2569,9 @@ export declare const setDslDomRelateConfig: <K extends keyof typeof dslDomRelate
|
|
|
2544
2569
|
export declare const getIdFromEl: () => (el?: HTMLElement | SVGElement | null) => string | undefined;
|
|
2545
2570
|
export declare const getElById: () => (doc?: Document, id?: string | number) => HTMLElement;
|
|
2546
2571
|
export declare const setIdToEl: () => (el: HTMLElement | SVGElement, id: string | number) => void;
|
|
2547
|
-
//#endregion
|
|
2548
|
-
//#region temp/packages/utils/src/const.d.ts
|
|
2549
2572
|
export declare const DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = "ds-field::";
|
|
2550
2573
|
export declare const DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX = "ds-field-changed";
|
|
2551
2574
|
export declare const DATA_SOURCE_SET_DATA_METHOD_NAME = "setDataFromEvent";
|
|
2552
|
-
//#endregion
|
|
2553
|
-
//#region temp/packages/utils/src/index.d.ts
|
|
2554
2575
|
export declare const getGlobalThis: () => any;
|
|
2555
2576
|
export declare const sleep: (ms: number) => Promise<void>;
|
|
2556
2577
|
export declare const toLine: (name?: string) => string;
|
|
@@ -2634,6 +2655,8 @@ export declare const PAGE_FRAGMENT_CONTAINER_ID_KEY = "tmagic-page-fragment-cont
|
|
|
2634
2655
|
export declare const compiledNode: (compile: (value: any) => any, node: MNode, dataSourceDeps?: DataSourceDeps, sourceId?: Id) => MNode;
|
|
2635
2656
|
export declare const compiledCond: (op: string, fieldValue: any, inputValue: any, range?: number[]) => boolean;
|
|
2636
2657
|
export declare const getDefaultValueFromFields: (fields: DataSchema[]) => Record<string, any>;
|
|
2658
|
+
/** 按字段生成 mock。空数组补一条子字段示例,已有内容不动 */
|
|
2659
|
+
export declare const getMockDataFromFields: (fields?: DataSchema[]) => Record<string, any>;
|
|
2637
2660
|
export declare const getKeys: <T extends object>(obj: T) => Array<keyof T>;
|
|
2638
2661
|
export declare const calculatePercentage: (value: number, percentageStr: string) => number;
|
|
2639
2662
|
export declare const isPercentage: (value: number | string) => boolean;
|
|
@@ -2654,5 +2677,4 @@ export interface NodeItem {
|
|
|
2654
2677
|
export declare const traverseNode: <T extends NodeItem = NodeItem>(node: T, cb: (node: T, parents: T[]) => void, parents?: T[], evalCbAfter?: boolean) => void;
|
|
2655
2678
|
export declare const isValueIncludeDataSource: (value: any) => boolean;
|
|
2656
2679
|
export declare const removeDataSourceFieldPrefix: (id?: string) => string;
|
|
2657
|
-
//#endregion
|
|
2658
2680
|
export {};
|