@tmagic/utils 1.8.3 → 1.8.4
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/const.js +1 -2
- package/dist/es/dom.js +1 -2
- package/dist/es/index.js +93 -10
- package/dist/tmagic-utils.umd.cjs +222 -262
- package/package.json +3 -3
- package/src/index.ts +144 -7
- package/types/index.d.ts +48 -4
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.8.
|
|
2
|
+
"version": "1.8.4",
|
|
3
3
|
"name": "@tmagic/utils",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"@types/lodash-es": "^4.17.12"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"
|
|
38
|
-
"
|
|
37
|
+
"@tmagic/schema": "1.8.4",
|
|
38
|
+
"typescript": "^6.0.3"
|
|
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) => {
|
package/types/index.d.ts
CHANGED
|
@@ -432,7 +432,7 @@ declare class Editor extends BaseService {
|
|
|
432
432
|
/** 设置结构合法性校验用到的配置,只覆盖传入的字段 */
|
|
433
433
|
setConfig(config: Partial<EditorServiceConfig>): void;
|
|
434
434
|
getConfig(): Readonly<EditorServiceConfig>;
|
|
435
|
-
doAdd(node: MNode$1, parent: MContainer,
|
|
435
|
+
doAdd(node: MNode$1, parent: MContainer, options?: AddOpOptions): Promise<MNode$1>;
|
|
436
436
|
/**
|
|
437
437
|
* 向指点容器添加组件节点
|
|
438
438
|
* @param addConfig 将要添加的组件节点配置
|
|
@@ -440,7 +440,7 @@ declare class Editor extends BaseService {
|
|
|
440
440
|
* @param options 可选配置,见 {@link DslOpOptions}
|
|
441
441
|
* @returns 添加后的节点
|
|
442
442
|
*/
|
|
443
|
-
add(addNode: AddMNode | MNode$1[], parent?: MContainer | null, options?:
|
|
443
|
+
add(addNode: AddMNode | MNode$1[], parent?: MContainer | null, options?: AddOpOptions): Promise<MNode$1 | MNode$1[]>;
|
|
444
444
|
doRemove(node: MNode$1, { doNotSelect, doNotSwitchPage }?: DslOpOptions): Promise<void>;
|
|
445
445
|
/**
|
|
446
446
|
* 删除组件
|
|
@@ -541,7 +541,7 @@ declare class Editor extends BaseService {
|
|
|
541
541
|
* 当本次操作未写入历史(doNotPushHistory 为 true、或操作无实际变更 / 提前返回)时 historyIds 为 `[]`。
|
|
542
542
|
*/
|
|
543
543
|
/** 等价于 {@link add},并额外返回本次写入历史记录的 uuid 列表(未入栈时 historyIds 为 `[]`)。 */
|
|
544
|
-
addAndGetHistoryId(addNode: AddMNode | MNode$1[], parent?: MContainer | null, options?:
|
|
544
|
+
addAndGetHistoryId(addNode: AddMNode | MNode$1[], parent?: MContainer | null, options?: AddOpOptions): Promise<DslOpWithHistoryIdsResult<MNode$1 | MNode$1[]>>;
|
|
545
545
|
/** 等价于 {@link remove},并额外返回本次写入历史记录的 uuid 列表(未入栈时 historyIds 为 `[]`)。 */
|
|
546
546
|
removeAndGetHistoryId(nodeOrNodeList: MNode$1 | MNode$1[], options?: DslOpOptions): Promise<DslOpWithHistoryIdsResult<void>>;
|
|
547
547
|
/** 等价于 {@link update},并额外返回本次写入历史记录的 uuid 列表(未入栈时 historyIds 为 `[]`)。 */
|
|
@@ -1751,8 +1751,20 @@ interface StageOptions {
|
|
|
1751
1751
|
*/
|
|
1752
1752
|
disabledFlashTip?: boolean;
|
|
1753
1753
|
zoom?: number;
|
|
1754
|
+
/** 画布 iframe 的 sandbox 属性值,不传 / false 表示不设置,见 @tmagic/stage 的 StageRenderConfig.iframeSandbox */
|
|
1755
|
+
iframeSandbox?: string | false;
|
|
1754
1756
|
/** 画布双击前的钩子函数,返回 false 则阻止默认的双击行为 */
|
|
1755
1757
|
beforeDblclick?: (event: MouseEvent) => Promise<boolean | void> | boolean | void;
|
|
1758
|
+
/**
|
|
1759
|
+
* 点击组件列表新增组件前的钩子,详见 EditorProps.beforeAppendComponent。
|
|
1760
|
+
* 仅点击新增时调用;返回 false 取消新增,支持异步。
|
|
1761
|
+
*/
|
|
1762
|
+
beforeAppendComponent?: BeforeAppendComponent;
|
|
1763
|
+
/**
|
|
1764
|
+
* 组件列表拖入画布、写入节点前的钩子,详见 EditorProps.beforeDropComponent。
|
|
1765
|
+
* 仅 dragType 为组件列表时调用;返回 false 取消本次拖入,支持异步。
|
|
1766
|
+
*/
|
|
1767
|
+
beforeDropComponent?: BeforeDropComponent;
|
|
1756
1768
|
}
|
|
1757
1769
|
/**
|
|
1758
1770
|
* 节点校验错误信息,按来源(属性表单 / 样式表单)分别保存错误文案。
|
|
@@ -1939,6 +1951,16 @@ interface ComponentItem {
|
|
|
1939
1951
|
[key: string]: any;
|
|
1940
1952
|
};
|
|
1941
1953
|
}
|
|
1954
|
+
/**
|
|
1955
|
+
* 点击组件列表新增组件前的钩子。
|
|
1956
|
+
* 入参为当前组件列表项,允许异步;返回 false 时取消本次新增。
|
|
1957
|
+
*/
|
|
1958
|
+
type BeforeAppendComponent = (item: ComponentItem) => boolean | void | Promise<boolean | void>;
|
|
1959
|
+
/**
|
|
1960
|
+
* 组件列表拖入画布、写入节点前的钩子。
|
|
1961
|
+
* 入参为即将新增的节点和父节点,允许异步;返回 false 时取消本次拖入。
|
|
1962
|
+
*/
|
|
1963
|
+
type BeforeDropComponent = (dragData: AddMNode, parent: MContainer) => boolean | void | Promise<boolean | void>;
|
|
1942
1964
|
interface ComponentGroup {
|
|
1943
1965
|
/** 显示文案 */
|
|
1944
1966
|
title: string;
|
|
@@ -2465,6 +2487,16 @@ interface DslOpOptions extends HistoryOpOptions {
|
|
|
2465
2487
|
doNotSelect?: boolean;
|
|
2466
2488
|
doNotSwitchPage?: boolean;
|
|
2467
2489
|
}
|
|
2490
|
+
/**
|
|
2491
|
+
* {@link EditorService.add} / {@link EditorService.doAdd} 的额外配置
|
|
2492
|
+
* - insertIndex: 插入下标,落在 [0, 父容器 items.length];'end' 表示固定追加到末尾。
|
|
2493
|
+
* 指定后不再按当前选中节点与上一次新增节点计算锚点,也不要求有选中节点;越界 / 非数字一律追加到末尾。
|
|
2494
|
+
* 批量 add 时数字下标是本批节点的起始位置,按父容器分别累计偏移,保持传入顺序;'end' 逐个追加。
|
|
2495
|
+
* 页面 / 页面片作用于 root.items,同样不再固定追加到末尾。人工添加不传,沿用锚点行为。
|
|
2496
|
+
*/
|
|
2497
|
+
interface AddOpOptions extends DslOpOptions {
|
|
2498
|
+
insertIndex?: number | 'end';
|
|
2499
|
+
}
|
|
2468
2500
|
/**
|
|
2469
2501
|
* {@link EditorService.update} / {@link EditorService.doUpdate} 的额外数据
|
|
2470
2502
|
* - changeRecords: 单节点 form 端变更记录(多节点场景下被忽略,使用 changeRecordList)
|
|
@@ -2575,6 +2607,10 @@ export declare const getKeysArray: (keys: string | number) => string[];
|
|
|
2575
2607
|
export declare const isArrayIndex: (key: string | number) => boolean;
|
|
2576
2608
|
export declare const getValueByKeyPath: (keys?: number | string | string[], data?: Record<string | number, any>) => any;
|
|
2577
2609
|
export declare const setValueByKeyPath: (keys: string | number, value: any, data?: Record<string | number, any>) => any;
|
|
2610
|
+
/**
|
|
2611
|
+
* 按 id 获取节点(先序遍历顺序),找到的 id 会从传入的 ids 中移除。
|
|
2612
|
+
* 内部使用 Map 查找,并在所有 id 都找到后提前结束遍历
|
|
2613
|
+
*/
|
|
2578
2614
|
export declare const getNodes: (ids: Id[], data?: MNode[]) => MNode[];
|
|
2579
2615
|
export declare const getDepKeys: (dataSourceDeps: DataSourceDeps | undefined, nodeId: Id) => Id[];
|
|
2580
2616
|
export declare const getDepNodeIds: (dataSourceDeps?: DataSourceDeps) => string[];
|
|
@@ -2585,6 +2621,13 @@ export declare const getDepNodeIds: (dataSourceDeps?: DataSourceDeps) => string[
|
|
|
2585
2621
|
* @param parentId 父节点 id
|
|
2586
2622
|
*/
|
|
2587
2623
|
export declare const replaceChildNode: (newNode: MNode, data?: MNode[], parentId?: Id) => void;
|
|
2624
|
+
/**
|
|
2625
|
+
* 批量将新节点更新到data中对应id的节点位置,效果等同于依次调用 replaceChildNode,
|
|
2626
|
+
* 但只需遍历一次树来定位所有节点,避免每个节点都做一次全树搜索(O(K*N) -> O(N+K))
|
|
2627
|
+
* @param newNodes 新节点列表
|
|
2628
|
+
* @param data 需要修改的数据
|
|
2629
|
+
*/
|
|
2630
|
+
export declare const replaceChildNodes: (newNodes: MNode[], data?: MNode[]) => void;
|
|
2588
2631
|
export declare const DSL_NODE_KEY_COPY_PREFIX = "__tmagic__";
|
|
2589
2632
|
export declare const IS_DSL_NODE_KEY = "__tmagic__dslNode";
|
|
2590
2633
|
export declare const PAGE_FRAGMENT_CONTAINER_ID_KEY = "tmagic-page-fragment-container-id";
|
|
@@ -2611,4 +2654,5 @@ export interface NodeItem {
|
|
|
2611
2654
|
export declare const traverseNode: <T extends NodeItem = NodeItem>(node: T, cb: (node: T, parents: T[]) => void, parents?: T[], evalCbAfter?: boolean) => void;
|
|
2612
2655
|
export declare const isValueIncludeDataSource: (value: any) => boolean;
|
|
2613
2656
|
export declare const removeDataSourceFieldPrefix: (id?: string) => string;
|
|
2614
|
-
//#endregion
|
|
2657
|
+
//#endregion
|
|
2658
|
+
export {};
|