@tmagic/utils 1.8.2 → 1.8.4-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/index.js +92 -8
- package/dist/tmagic-utils.umd.cjs +97 -12
- package/package.json +2 -2
- package/src/index.ts +144 -7
- package/types/index.d.ts +787 -495
package/dist/es/index.js
CHANGED
|
@@ -188,20 +188,47 @@ var setValueByKeyPath = (keys, value, data = {}) => {
|
|
|
188
188
|
}
|
|
189
189
|
return set(data, keys, value);
|
|
190
190
|
};
|
|
191
|
+
/**
|
|
192
|
+
* 按 id 获取节点(先序遍历顺序),找到的 id 会从传入的 ids 中移除。
|
|
193
|
+
* 内部使用 Map 查找,并在所有 id 都找到后提前结束遍历
|
|
194
|
+
*/
|
|
191
195
|
var getNodes = (ids, data = []) => {
|
|
192
196
|
const nodes = [];
|
|
193
|
-
|
|
197
|
+
if (!ids.length) return nodes;
|
|
198
|
+
const remain = /* @__PURE__ */ new Map();
|
|
199
|
+
for (const id of ids) {
|
|
200
|
+
const key = `${id}`;
|
|
201
|
+
remain.set(key, (remain.get(key) || 0) + 1);
|
|
202
|
+
}
|
|
203
|
+
let remainCount = ids.length;
|
|
204
|
+
const used = /* @__PURE__ */ new Map();
|
|
205
|
+
const get = function(data) {
|
|
194
206
|
if (!Array.isArray(data)) return;
|
|
195
207
|
for (const item of data) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
208
|
+
if (!remainCount) return;
|
|
209
|
+
const key = `${item.id}`;
|
|
210
|
+
const count = remain.get(key);
|
|
211
|
+
if (count) {
|
|
212
|
+
remain.set(key, count - 1);
|
|
213
|
+
used.set(key, (used.get(key) || 0) + 1);
|
|
214
|
+
remainCount -= 1;
|
|
199
215
|
nodes.push(item);
|
|
200
216
|
}
|
|
201
|
-
if (item.items) get(
|
|
217
|
+
if (item.items) get(item.items);
|
|
202
218
|
}
|
|
203
219
|
};
|
|
204
|
-
get(
|
|
220
|
+
get(data);
|
|
221
|
+
if (nodes.length) {
|
|
222
|
+
const rest = [];
|
|
223
|
+
for (const id of ids) {
|
|
224
|
+
const key = `${id}`;
|
|
225
|
+
const count = used.get(key);
|
|
226
|
+
if (count) used.set(key, count - 1);
|
|
227
|
+
else rest.push(id);
|
|
228
|
+
}
|
|
229
|
+
ids.length = 0;
|
|
230
|
+
for (const id of rest) ids.push(id);
|
|
231
|
+
}
|
|
205
232
|
return nodes;
|
|
206
233
|
};
|
|
207
234
|
var getDepKeys = (dataSourceDeps = {}, nodeId) => Array.from(Object.values(dataSourceDeps).reduce((prev, cur) => {
|
|
@@ -236,13 +263,70 @@ var replaceChildNode = (newNode, data, parentId) => {
|
|
|
236
263
|
const index = parent.items?.findIndex((child) => child.id === node.id);
|
|
237
264
|
parent.items.splice(index, 1, newNode);
|
|
238
265
|
};
|
|
266
|
+
/**
|
|
267
|
+
* 批量将新节点更新到data中对应id的节点位置,效果等同于依次调用 replaceChildNode,
|
|
268
|
+
* 但只需遍历一次树来定位所有节点,避免每个节点都做一次全树搜索(O(K*N) -> O(N+K))
|
|
269
|
+
* @param newNodes 新节点列表
|
|
270
|
+
* @param data 需要修改的数据
|
|
271
|
+
*/
|
|
272
|
+
var replaceChildNodes = (newNodes, data = []) => {
|
|
273
|
+
if (!newNodes.length) return;
|
|
274
|
+
const locate = (nodes) => {
|
|
275
|
+
const ids = new Set(nodes.map((node) => `${node.id}`));
|
|
276
|
+
const locations = /* @__PURE__ */ new Map();
|
|
277
|
+
const walk = (list, depth) => {
|
|
278
|
+
for (let i = 0, l = list.length; i < l; i++) {
|
|
279
|
+
if (locations.size === ids.size) return;
|
|
280
|
+
const item = list[i];
|
|
281
|
+
const key = `${item.id}`;
|
|
282
|
+
if (ids.has(key) && !locations.has(key)) locations.set(key, {
|
|
283
|
+
container: depth === 0 ? void 0 : list,
|
|
284
|
+
index: i,
|
|
285
|
+
node: item,
|
|
286
|
+
depth
|
|
287
|
+
});
|
|
288
|
+
if (Array.isArray(item.items)) walk(item.items, depth + 1);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
if (Array.isArray(data)) walk(data, 0);
|
|
292
|
+
return locations;
|
|
293
|
+
};
|
|
294
|
+
let locations = locate(newNodes);
|
|
295
|
+
const sortedNodes = newNodes.map((node, order) => ({
|
|
296
|
+
node,
|
|
297
|
+
order,
|
|
298
|
+
depth: locations.get(`${node.id}`)?.depth ?? Infinity
|
|
299
|
+
})).sort((a, b) => a.depth === b.depth ? a.order - b.order : a.depth - b.depth).map(({ node }) => node);
|
|
300
|
+
for (let i = 0, l = sortedNodes.length; i < l; i++) {
|
|
301
|
+
const newNode = sortedNodes[i];
|
|
302
|
+
const location = locations.get(`${newNode.id}`);
|
|
303
|
+
if (!location) {
|
|
304
|
+
console.warn(`未找到目标节点(${newNode.id})`);
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const { container, node } = location;
|
|
308
|
+
if (!container) {
|
|
309
|
+
console.warn(`未找到父节点(${newNode.id})`);
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
let { index } = location;
|
|
313
|
+
if (container[index]?.id !== node.id) index = container.findIndex((child) => child.id === node.id);
|
|
314
|
+
if (index === -1) {
|
|
315
|
+
console.warn(`未找到目标节点(${newNode.id})`);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
const oldItems = node.items;
|
|
319
|
+
container.splice(index, 1, newNode);
|
|
320
|
+
if (newNode.items !== oldItems && i < l - 1) locations = locate(sortedNodes.slice(i + 1));
|
|
321
|
+
}
|
|
322
|
+
};
|
|
239
323
|
var DSL_NODE_KEY_COPY_PREFIX = "__tmagic__";
|
|
240
324
|
var IS_DSL_NODE_KEY = "__tmagic__dslNode";
|
|
241
325
|
var PAGE_FRAGMENT_CONTAINER_ID_KEY = "tmagic-page-fragment-container-id";
|
|
242
326
|
var compiledNode = (compile, node, dataSourceDeps = {}, sourceId) => {
|
|
243
327
|
let keys = [];
|
|
244
328
|
if (!sourceId) keys = getDepKeys(dataSourceDeps, node.id);
|
|
245
|
-
else keys = dataSourceDeps[sourceId]?.[node.id]
|
|
329
|
+
else keys = dataSourceDeps[sourceId]?.[node.id]?.keys || [];
|
|
246
330
|
keys.forEach((key) => {
|
|
247
331
|
const keys = getKeysArray(key);
|
|
248
332
|
const cacheKey = keys.map((key, index) => {
|
|
@@ -377,4 +461,4 @@ var isValueIncludeDataSource = (value) => {
|
|
|
377
461
|
};
|
|
378
462
|
var removeDataSourceFieldPrefix = (id) => id?.replace("ds-field::", "") || "";
|
|
379
463
|
//#endregion
|
|
380
|
-
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, setDslDomRelateConfig, setIdToEl, setUrlParam, setValueByKeyPath, sleep, toHump, toLine, traverseNode };
|
|
464
|
+
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 };
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
//#endregion
|
|
15
15
|
//#region node_modules/.pnpm/lodash-es@4.18.1/node_modules/lodash-es/_Symbol.js
|
|
16
16
|
/** Built-in value references. */
|
|
17
|
-
var Symbol = root.Symbol;
|
|
17
|
+
var Symbol$1 = root.Symbol;
|
|
18
18
|
//#endregion
|
|
19
19
|
//#region node_modules/.pnpm/lodash-es@4.18.1/node_modules/lodash-es/_getRawTag.js
|
|
20
20
|
/** Used for built-in method references. */
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
*/
|
|
29
29
|
var nativeObjectToString$1 = objectProto$3.toString;
|
|
30
30
|
/** Built-in value references. */
|
|
31
|
-
var symToStringTag$1 = Symbol ? Symbol.toStringTag : void 0;
|
|
31
|
+
var symToStringTag$1 = Symbol$1 ? Symbol$1.toStringTag : void 0;
|
|
32
32
|
/**
|
|
33
33
|
* A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values.
|
|
34
34
|
*
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
var nullTag = "[object Null]";
|
|
74
74
|
var undefinedTag = "[object Undefined]";
|
|
75
75
|
/** Built-in value references. */
|
|
76
|
-
var symToStringTag = Symbol ? Symbol.toStringTag : void 0;
|
|
76
|
+
var symToStringTag = Symbol$1 ? Symbol$1.toStringTag : void 0;
|
|
77
77
|
/**
|
|
78
78
|
* The base implementation of `getTag` without fallbacks for buggy environments.
|
|
79
79
|
*
|
|
@@ -185,7 +185,7 @@
|
|
|
185
185
|
/** Used as references for various `Number` constants. */
|
|
186
186
|
var INFINITY$1 = 1 / 0;
|
|
187
187
|
/** Used to convert symbols to primitives and strings. */
|
|
188
|
-
var symbolProto$1 = Symbol ? Symbol.prototype : void 0;
|
|
188
|
+
var symbolProto$1 = Symbol$1 ? Symbol$1.prototype : void 0;
|
|
189
189
|
var symbolToString = symbolProto$1 ? symbolProto$1.toString : void 0;
|
|
190
190
|
/**
|
|
191
191
|
* The base implementation of `_.toString` which doesn't convert nullish
|
|
@@ -1993,7 +1993,7 @@
|
|
|
1993
1993
|
//#endregion
|
|
1994
1994
|
//#region node_modules/.pnpm/lodash-es@4.18.1/node_modules/lodash-es/_cloneSymbol.js
|
|
1995
1995
|
/** Used to convert symbols to primitives and strings. */
|
|
1996
|
-
var symbolProto = Symbol ? Symbol.prototype : void 0;
|
|
1996
|
+
var symbolProto = Symbol$1 ? Symbol$1.prototype : void 0;
|
|
1997
1997
|
var symbolValueOf = symbolProto ? symbolProto.valueOf : void 0;
|
|
1998
1998
|
/**
|
|
1999
1999
|
* Creates a clone of the `symbol` object.
|
|
@@ -2645,20 +2645,47 @@
|
|
|
2645
2645
|
}
|
|
2646
2646
|
return set(data, keys, value);
|
|
2647
2647
|
};
|
|
2648
|
+
/**
|
|
2649
|
+
* 按 id 获取节点(先序遍历顺序),找到的 id 会从传入的 ids 中移除。
|
|
2650
|
+
* 内部使用 Map 查找,并在所有 id 都找到后提前结束遍历
|
|
2651
|
+
*/
|
|
2648
2652
|
var getNodes = (ids, data = []) => {
|
|
2649
2653
|
const nodes = [];
|
|
2650
|
-
|
|
2654
|
+
if (!ids.length) return nodes;
|
|
2655
|
+
const remain = /* @__PURE__ */ new Map();
|
|
2656
|
+
for (const id of ids) {
|
|
2657
|
+
const key = `${id}`;
|
|
2658
|
+
remain.set(key, (remain.get(key) || 0) + 1);
|
|
2659
|
+
}
|
|
2660
|
+
let remainCount = ids.length;
|
|
2661
|
+
const used = /* @__PURE__ */ new Map();
|
|
2662
|
+
const get = function(data) {
|
|
2651
2663
|
if (!Array.isArray(data)) return;
|
|
2652
2664
|
for (const item of data) {
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2665
|
+
if (!remainCount) return;
|
|
2666
|
+
const key = `${item.id}`;
|
|
2667
|
+
const count = remain.get(key);
|
|
2668
|
+
if (count) {
|
|
2669
|
+
remain.set(key, count - 1);
|
|
2670
|
+
used.set(key, (used.get(key) || 0) + 1);
|
|
2671
|
+
remainCount -= 1;
|
|
2656
2672
|
nodes.push(item);
|
|
2657
2673
|
}
|
|
2658
|
-
if (item.items) get(
|
|
2674
|
+
if (item.items) get(item.items);
|
|
2659
2675
|
}
|
|
2660
2676
|
};
|
|
2661
|
-
get(
|
|
2677
|
+
get(data);
|
|
2678
|
+
if (nodes.length) {
|
|
2679
|
+
const rest = [];
|
|
2680
|
+
for (const id of ids) {
|
|
2681
|
+
const key = `${id}`;
|
|
2682
|
+
const count = used.get(key);
|
|
2683
|
+
if (count) used.set(key, count - 1);
|
|
2684
|
+
else rest.push(id);
|
|
2685
|
+
}
|
|
2686
|
+
ids.length = 0;
|
|
2687
|
+
for (const id of rest) ids.push(id);
|
|
2688
|
+
}
|
|
2662
2689
|
return nodes;
|
|
2663
2690
|
};
|
|
2664
2691
|
var getDepKeys = (dataSourceDeps = {}, nodeId) => Array.from(Object.values(dataSourceDeps).reduce((prev, cur) => {
|
|
@@ -2693,13 +2720,70 @@
|
|
|
2693
2720
|
const index = parent.items?.findIndex((child) => child.id === node.id);
|
|
2694
2721
|
parent.items.splice(index, 1, newNode);
|
|
2695
2722
|
};
|
|
2723
|
+
/**
|
|
2724
|
+
* 批量将新节点更新到data中对应id的节点位置,效果等同于依次调用 replaceChildNode,
|
|
2725
|
+
* 但只需遍历一次树来定位所有节点,避免每个节点都做一次全树搜索(O(K*N) -> O(N+K))
|
|
2726
|
+
* @param newNodes 新节点列表
|
|
2727
|
+
* @param data 需要修改的数据
|
|
2728
|
+
*/
|
|
2729
|
+
var replaceChildNodes = (newNodes, data = []) => {
|
|
2730
|
+
if (!newNodes.length) return;
|
|
2731
|
+
const locate = (nodes) => {
|
|
2732
|
+
const ids = new Set(nodes.map((node) => `${node.id}`));
|
|
2733
|
+
const locations = /* @__PURE__ */ new Map();
|
|
2734
|
+
const walk = (list, depth) => {
|
|
2735
|
+
for (let i = 0, l = list.length; i < l; i++) {
|
|
2736
|
+
if (locations.size === ids.size) return;
|
|
2737
|
+
const item = list[i];
|
|
2738
|
+
const key = `${item.id}`;
|
|
2739
|
+
if (ids.has(key) && !locations.has(key)) locations.set(key, {
|
|
2740
|
+
container: depth === 0 ? void 0 : list,
|
|
2741
|
+
index: i,
|
|
2742
|
+
node: item,
|
|
2743
|
+
depth
|
|
2744
|
+
});
|
|
2745
|
+
if (Array.isArray(item.items)) walk(item.items, depth + 1);
|
|
2746
|
+
}
|
|
2747
|
+
};
|
|
2748
|
+
if (Array.isArray(data)) walk(data, 0);
|
|
2749
|
+
return locations;
|
|
2750
|
+
};
|
|
2751
|
+
let locations = locate(newNodes);
|
|
2752
|
+
const sortedNodes = newNodes.map((node, order) => ({
|
|
2753
|
+
node,
|
|
2754
|
+
order,
|
|
2755
|
+
depth: locations.get(`${node.id}`)?.depth ?? Infinity
|
|
2756
|
+
})).sort((a, b) => a.depth === b.depth ? a.order - b.order : a.depth - b.depth).map(({ node }) => node);
|
|
2757
|
+
for (let i = 0, l = sortedNodes.length; i < l; i++) {
|
|
2758
|
+
const newNode = sortedNodes[i];
|
|
2759
|
+
const location = locations.get(`${newNode.id}`);
|
|
2760
|
+
if (!location) {
|
|
2761
|
+
console.warn(`未找到目标节点(${newNode.id})`);
|
|
2762
|
+
continue;
|
|
2763
|
+
}
|
|
2764
|
+
const { container, node } = location;
|
|
2765
|
+
if (!container) {
|
|
2766
|
+
console.warn(`未找到父节点(${newNode.id})`);
|
|
2767
|
+
continue;
|
|
2768
|
+
}
|
|
2769
|
+
let { index } = location;
|
|
2770
|
+
if (container[index]?.id !== node.id) index = container.findIndex((child) => child.id === node.id);
|
|
2771
|
+
if (index === -1) {
|
|
2772
|
+
console.warn(`未找到目标节点(${newNode.id})`);
|
|
2773
|
+
continue;
|
|
2774
|
+
}
|
|
2775
|
+
const oldItems = node.items;
|
|
2776
|
+
container.splice(index, 1, newNode);
|
|
2777
|
+
if (newNode.items !== oldItems && i < l - 1) locations = locate(sortedNodes.slice(i + 1));
|
|
2778
|
+
}
|
|
2779
|
+
};
|
|
2696
2780
|
var DSL_NODE_KEY_COPY_PREFIX = "__tmagic__";
|
|
2697
2781
|
var IS_DSL_NODE_KEY = "__tmagic__dslNode";
|
|
2698
2782
|
var PAGE_FRAGMENT_CONTAINER_ID_KEY = "tmagic-page-fragment-container-id";
|
|
2699
2783
|
var compiledNode = (compile, node, dataSourceDeps = {}, sourceId) => {
|
|
2700
2784
|
let keys = [];
|
|
2701
2785
|
if (!sourceId) keys = getDepKeys(dataSourceDeps, node.id);
|
|
2702
|
-
else keys = dataSourceDeps[sourceId]?.[node.id]
|
|
2786
|
+
else keys = dataSourceDeps[sourceId]?.[node.id]?.keys || [];
|
|
2703
2787
|
keys.forEach((key) => {
|
|
2704
2788
|
const keys = getKeysArray(key);
|
|
2705
2789
|
const cacheKey = keys.map((key, index) => {
|
|
@@ -2887,6 +2971,7 @@
|
|
|
2887
2971
|
exports.removeClassNameByClassName = removeClassNameByClassName;
|
|
2888
2972
|
exports.removeDataSourceFieldPrefix = removeDataSourceFieldPrefix;
|
|
2889
2973
|
exports.replaceChildNode = replaceChildNode;
|
|
2974
|
+
exports.replaceChildNodes = replaceChildNodes;
|
|
2890
2975
|
exports.setDslDomRelateConfig = setDslDomRelateConfig;
|
|
2891
2976
|
exports.setIdToEl = setIdToEl;
|
|
2892
2977
|
exports.setUrlParam = setUrlParam;
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.8.
|
|
2
|
+
"version": "1.8.4-beta.1",
|
|
3
3
|
"name": "@tmagic/utils",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"typescript": "^6.0.3",
|
|
38
|
-
"@tmagic/schema": "1.8.
|
|
38
|
+
"@tmagic/schema": "1.8.4-beta.1"
|
|
39
39
|
},
|
|
40
40
|
"peerDependenciesMeta": {
|
|
41
41
|
"typescript": {
|
package/src/index.ts
CHANGED
|
@@ -356,29 +356,71 @@ export const setValueByKeyPath = (keys: string | number, value: any, data: Recor
|
|
|
356
356
|
return objectSet(data, keys, value);
|
|
357
357
|
};
|
|
358
358
|
|
|
359
|
+
/**
|
|
360
|
+
* 按 id 获取节点(先序遍历顺序),找到的 id 会从传入的 ids 中移除。
|
|
361
|
+
* 内部使用 Map 查找,并在所有 id 都找到后提前结束遍历
|
|
362
|
+
*/
|
|
359
363
|
export const getNodes = (ids: Id[], data: MNode[] = []): MNode[] => {
|
|
360
364
|
const nodes: MNode[] = [];
|
|
361
365
|
|
|
362
|
-
|
|
366
|
+
if (!ids.length) {
|
|
367
|
+
return nodes;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// id -> 待查找的次数
|
|
371
|
+
const remain = new Map<string, number>();
|
|
372
|
+
for (const id of ids) {
|
|
373
|
+
const key = `${id}`;
|
|
374
|
+
remain.set(key, (remain.get(key) || 0) + 1);
|
|
375
|
+
}
|
|
376
|
+
let remainCount = ids.length;
|
|
377
|
+
const used = new Map<string, number>();
|
|
378
|
+
|
|
379
|
+
const get = function (data: MNode[]) {
|
|
363
380
|
if (!Array.isArray(data)) {
|
|
364
381
|
return;
|
|
365
382
|
}
|
|
366
383
|
|
|
367
384
|
for (const item of data) {
|
|
368
|
-
|
|
385
|
+
if (!remainCount) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const key = `${item.id}`;
|
|
390
|
+
const count = remain.get(key);
|
|
369
391
|
|
|
370
|
-
if (
|
|
371
|
-
|
|
392
|
+
if (count) {
|
|
393
|
+
remain.set(key, count - 1);
|
|
394
|
+
used.set(key, (used.get(key) || 0) + 1);
|
|
395
|
+
remainCount -= 1;
|
|
372
396
|
nodes.push(item);
|
|
373
397
|
}
|
|
374
398
|
|
|
375
399
|
if (item.items) {
|
|
376
|
-
get(
|
|
400
|
+
get(item.items);
|
|
377
401
|
}
|
|
378
402
|
}
|
|
379
403
|
};
|
|
380
404
|
|
|
381
|
-
get(
|
|
405
|
+
get(data);
|
|
406
|
+
|
|
407
|
+
if (nodes.length) {
|
|
408
|
+
// 保持原有行为:将已找到的 id 从 ids 中移除
|
|
409
|
+
const rest: Id[] = [];
|
|
410
|
+
for (const id of ids) {
|
|
411
|
+
const key = `${id}`;
|
|
412
|
+
const count = used.get(key);
|
|
413
|
+
if (count) {
|
|
414
|
+
used.set(key, count - 1);
|
|
415
|
+
} else {
|
|
416
|
+
rest.push(id);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
ids.length = 0;
|
|
420
|
+
for (const id of rest) {
|
|
421
|
+
ids.push(id);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
382
424
|
|
|
383
425
|
return nodes;
|
|
384
426
|
};
|
|
@@ -430,6 +472,101 @@ export const replaceChildNode = (newNode: MNode, data?: MNode[], parentId?: Id):
|
|
|
430
472
|
parent.items.splice(index, 1, newNode);
|
|
431
473
|
};
|
|
432
474
|
|
|
475
|
+
/**
|
|
476
|
+
* 批量将新节点更新到data中对应id的节点位置,效果等同于依次调用 replaceChildNode,
|
|
477
|
+
* 但只需遍历一次树来定位所有节点,避免每个节点都做一次全树搜索(O(K*N) -> O(N+K))
|
|
478
|
+
* @param newNodes 新节点列表
|
|
479
|
+
* @param data 需要修改的数据
|
|
480
|
+
*/
|
|
481
|
+
export const replaceChildNodes = (newNodes: MNode[], data: MNode[] = []): void => {
|
|
482
|
+
if (!newNodes.length) {
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
interface Location {
|
|
487
|
+
/** 节点所在的数组,undefined 表示节点在 data 顶层(没有父节点) */
|
|
488
|
+
container?: MNode[];
|
|
489
|
+
index: number;
|
|
490
|
+
node: MNode;
|
|
491
|
+
/** 节点在树中的层级,data 顶层为 0 */
|
|
492
|
+
depth: number;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const locate = (nodes: MNode[]) => {
|
|
496
|
+
const ids = new Set(nodes.map((node) => `${node.id}`));
|
|
497
|
+
const locations = new Map<string, Location>();
|
|
498
|
+
|
|
499
|
+
const walk = (list: MNode[], depth: number) => {
|
|
500
|
+
for (let i = 0, l = list.length; i < l; i++) {
|
|
501
|
+
if (locations.size === ids.size) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const item = list[i];
|
|
506
|
+
const key = `${item.id}`;
|
|
507
|
+
|
|
508
|
+
if (ids.has(key) && !locations.has(key)) {
|
|
509
|
+
locations.set(key, { container: depth === 0 ? undefined : list, index: i, node: item, depth });
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (Array.isArray(item.items)) {
|
|
513
|
+
walk(item.items, depth + 1);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
if (Array.isArray(data)) {
|
|
519
|
+
walk(data, 0);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
return locations;
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
let locations = locate(newNodes);
|
|
526
|
+
|
|
527
|
+
// 层级浅的先替换(父节点先于子节点),避免先替换了子节点后,父节点的新数据中仍是旧的子节点而把子节点的更新覆盖掉;
|
|
528
|
+
// 同一层级保持传入顺序,未找到的节点排在最后
|
|
529
|
+
const sortedNodes = newNodes
|
|
530
|
+
.map((node, order) => ({ node, order, depth: locations.get(`${node.id}`)?.depth ?? Infinity }))
|
|
531
|
+
.sort((a, b) => (a.depth === b.depth ? a.order - b.order : a.depth - b.depth))
|
|
532
|
+
.map(({ node }) => node);
|
|
533
|
+
|
|
534
|
+
for (let i = 0, l = sortedNodes.length; i < l; i++) {
|
|
535
|
+
const newNode = sortedNodes[i];
|
|
536
|
+
const location = locations.get(`${newNode.id}`);
|
|
537
|
+
|
|
538
|
+
if (!location) {
|
|
539
|
+
console.warn(`未找到目标节点(${newNode.id})`);
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const { container, node } = location;
|
|
544
|
+
|
|
545
|
+
if (!container) {
|
|
546
|
+
console.warn(`未找到父节点(${newNode.id})`);
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
let { index } = location;
|
|
551
|
+
if (container[index]?.id !== node.id) {
|
|
552
|
+
index = container.findIndex((child: MNode) => child.id === node.id);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
if (index === -1) {
|
|
556
|
+
console.warn(`未找到目标节点(${newNode.id})`);
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const oldItems = node.items;
|
|
561
|
+
container.splice(index, 1, newNode);
|
|
562
|
+
|
|
563
|
+
// 新节点的 items 不是原来的数组时,后续节点的位置信息可能失效,需要重新定位
|
|
564
|
+
if (newNode.items !== oldItems && i < l - 1) {
|
|
565
|
+
locations = locate(sortedNodes.slice(i + 1));
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
|
|
433
570
|
export const DSL_NODE_KEY_COPY_PREFIX = '__tmagic__';
|
|
434
571
|
export const IS_DSL_NODE_KEY = '__tmagic__dslNode';
|
|
435
572
|
export const PAGE_FRAGMENT_CONTAINER_ID_KEY = 'tmagic-page-fragment-container-id';
|
|
@@ -445,7 +582,7 @@ export const compiledNode = (
|
|
|
445
582
|
keys = getDepKeys(dataSourceDeps, node.id);
|
|
446
583
|
} else {
|
|
447
584
|
const dep = dataSourceDeps[sourceId];
|
|
448
|
-
keys = dep?.[node.id]
|
|
585
|
+
keys = dep?.[node.id]?.keys || [];
|
|
449
586
|
}
|
|
450
587
|
|
|
451
588
|
keys.forEach((key) => {
|