onchain-utility 0.0.16 → 0.0.17

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.
@@ -1145,6 +1145,46 @@ function levelTransformTree({
1145
1145
  });
1146
1146
  return root.children;
1147
1147
  }
1148
+
1149
+ /** 获取全部子孙节点 */
1150
+ function getTreeDescendants({
1151
+ data,
1152
+ key,
1153
+ signKey = 'id',
1154
+ childrenKey = 'children'
1155
+ }) {
1156
+ const {
1157
+ self
1158
+ } = getTreeRelation({
1159
+ childrenKey,
1160
+ data,
1161
+ lookup(item, index) {
1162
+ return item[signKey] === key;
1163
+ }
1164
+ });
1165
+ return arrayAttributeFlat({
1166
+ childrenKey,
1167
+ data: self?.[childrenKey] || []
1168
+ });
1169
+ }
1170
+
1171
+ /** 获取全部祖先节点 */
1172
+ function getTreeAncestors({
1173
+ data,
1174
+ key,
1175
+ signKey = 'id',
1176
+ childrenKey = 'children'
1177
+ }) {
1178
+ return getTreeRelation({
1179
+ childrenKey,
1180
+ data,
1181
+ lookup(item, index) {
1182
+ return item[signKey] === key;
1183
+ }
1184
+ }).stack;
1185
+ }
1186
+
1187
+ /** 删除树节点 */
1148
1188
  function deleteTreeNodes({
1149
1189
  data,
1150
1190
  selectedKeys,
@@ -1298,6 +1338,51 @@ function getSiblings({
1298
1338
  return keys;
1299
1339
  }
1300
1340
 
1341
+ /**
1342
+ * @description 是否跨层
1343
+ * @date 2022-11-21 20:03:52
1344
+ * @export
1345
+ * @param {string[]} hierarchy 选中任务的层级
1346
+ * @returns {*}
1347
+ */
1348
+ function getSelectedNodeHierarchyStatus({
1349
+ data,
1350
+ selectedKeys,
1351
+ signKey = 'id',
1352
+ childrenKey = 'children'
1353
+ }) {
1354
+ const relations = selectedKeys.map(key => getTreeRelation({
1355
+ childrenKey,
1356
+ data,
1357
+ lookup(item, index) {
1358
+ return item[signKey] === key;
1359
+ }
1360
+ }));
1361
+ // 深度集合
1362
+ const deepSet = new Set(relations.map(rel => rel.stack.length));
1363
+ const isDifferentDepths = deepSet.size > 1;
1364
+ if (isDifferentDepths) {
1365
+ return {
1366
+ isDifferentDepths,
1367
+ isIncludedParent: relations.sort((a, b) => {
1368
+ return b.stack.length - a.stack.length;
1369
+ }).some(item => {
1370
+ const ancestors = getTreeAncestors({
1371
+ childrenKey,
1372
+ data,
1373
+ key: item.self[signKey],
1374
+ signKey
1375
+ }).map(item => item[signKey]);
1376
+ return selectedKeys.some(id => ancestors.includes(id));
1377
+ })
1378
+ };
1379
+ }
1380
+ return {
1381
+ isDifferentDepths,
1382
+ isIncludedParent: false
1383
+ };
1384
+ }
1385
+
1301
1386
  /**
1302
1387
  * @description 合并相邻的选中数据
1303
1388
  * @date 2023-02-15 16:11:16
@@ -1622,7 +1707,12 @@ async function upgrade({
1622
1707
  });
1623
1708
  const insert = (data, index) => {
1624
1709
  data.splice(index, 0, ...selected);
1625
- onUpgraded?.(selected);
1710
+ onUpgraded?.(selected, getExpandedState({
1711
+ childrenKey,
1712
+ originalParent: parent,
1713
+ parent: grandpa,
1714
+ signKey
1715
+ }));
1626
1716
  };
1627
1717
  const insertIndex = getInsertIdx({
1628
1718
  leader: start,
@@ -1684,7 +1774,12 @@ async function downgrade({
1684
1774
  const newChildren = [...(before[childrenKey] || []), ...children];
1685
1775
  if (children.length) {
1686
1776
  before[childrenKey] = [...newChildren];
1687
- onDowngraded?.(children);
1777
+ onDowngraded?.(children, getExpandedState({
1778
+ childrenKey,
1779
+ originalParent: parent,
1780
+ parent: before,
1781
+ signKey
1782
+ }));
1688
1783
  }
1689
1784
  }
1690
1785
  }
@@ -1737,6 +1832,18 @@ function treeSearch({
1737
1832
  }
1738
1833
  }).filter(row => !!row);
1739
1834
  }
1835
+ function getExpandedState({
1836
+ originalParent,
1837
+ parent,
1838
+ signKey = 'id',
1839
+ childrenKey = 'children'
1840
+ }) {
1841
+ const isRemove = !originalParent[childrenKey]?.length;
1842
+ return {
1843
+ add: parent[signKey],
1844
+ remove: isRemove ? originalParent[signKey] : null
1845
+ };
1846
+ }
1740
1847
 
1741
1848
  exports._isMoment = _isMoment;
1742
1849
  exports.arrayAttributeFlat = arrayAttributeFlat;
@@ -1755,7 +1862,10 @@ exports.generatedAcrossHierarchySort = generatedAcrossHierarchySort;
1755
1862
  exports.getBase64Regular = getBase64Regular;
1756
1863
  exports.getHTMLTagString = getHTMLTagString;
1757
1864
  exports.getI18nText = getI18nText;
1865
+ exports.getSelectedNodeHierarchyStatus = getSelectedNodeHierarchyStatus;
1758
1866
  exports.getSiblings = getSiblings;
1867
+ exports.getTreeAncestors = getTreeAncestors;
1868
+ exports.getTreeDescendants = getTreeDescendants;
1759
1869
  exports.getTreeRelation = getTreeRelation;
1760
1870
  exports.hasOwnProperty = hasOwnProperty;
1761
1871
  exports.initTranslateI18nFn = initTranslateI18nFn;
@@ -1143,6 +1143,46 @@ function levelTransformTree({
1143
1143
  });
1144
1144
  return root.children;
1145
1145
  }
1146
+
1147
+ /** 获取全部子孙节点 */
1148
+ function getTreeDescendants({
1149
+ data,
1150
+ key,
1151
+ signKey = 'id',
1152
+ childrenKey = 'children'
1153
+ }) {
1154
+ const {
1155
+ self
1156
+ } = getTreeRelation({
1157
+ childrenKey,
1158
+ data,
1159
+ lookup(item, index) {
1160
+ return item[signKey] === key;
1161
+ }
1162
+ });
1163
+ return arrayAttributeFlat({
1164
+ childrenKey,
1165
+ data: self?.[childrenKey] || []
1166
+ });
1167
+ }
1168
+
1169
+ /** 获取全部祖先节点 */
1170
+ function getTreeAncestors({
1171
+ data,
1172
+ key,
1173
+ signKey = 'id',
1174
+ childrenKey = 'children'
1175
+ }) {
1176
+ return getTreeRelation({
1177
+ childrenKey,
1178
+ data,
1179
+ lookup(item, index) {
1180
+ return item[signKey] === key;
1181
+ }
1182
+ }).stack;
1183
+ }
1184
+
1185
+ /** 删除树节点 */
1146
1186
  function deleteTreeNodes({
1147
1187
  data,
1148
1188
  selectedKeys,
@@ -1296,6 +1336,51 @@ function getSiblings({
1296
1336
  return keys;
1297
1337
  }
1298
1338
 
1339
+ /**
1340
+ * @description 是否跨层
1341
+ * @date 2022-11-21 20:03:52
1342
+ * @export
1343
+ * @param {string[]} hierarchy 选中任务的层级
1344
+ * @returns {*}
1345
+ */
1346
+ function getSelectedNodeHierarchyStatus({
1347
+ data,
1348
+ selectedKeys,
1349
+ signKey = 'id',
1350
+ childrenKey = 'children'
1351
+ }) {
1352
+ const relations = selectedKeys.map(key => getTreeRelation({
1353
+ childrenKey,
1354
+ data,
1355
+ lookup(item, index) {
1356
+ return item[signKey] === key;
1357
+ }
1358
+ }));
1359
+ // 深度集合
1360
+ const deepSet = new Set(relations.map(rel => rel.stack.length));
1361
+ const isDifferentDepths = deepSet.size > 1;
1362
+ if (isDifferentDepths) {
1363
+ return {
1364
+ isDifferentDepths,
1365
+ isIncludedParent: relations.sort((a, b) => {
1366
+ return b.stack.length - a.stack.length;
1367
+ }).some(item => {
1368
+ const ancestors = getTreeAncestors({
1369
+ childrenKey,
1370
+ data,
1371
+ key: item.self[signKey],
1372
+ signKey
1373
+ }).map(item => item[signKey]);
1374
+ return selectedKeys.some(id => ancestors.includes(id));
1375
+ })
1376
+ };
1377
+ }
1378
+ return {
1379
+ isDifferentDepths,
1380
+ isIncludedParent: false
1381
+ };
1382
+ }
1383
+
1299
1384
  /**
1300
1385
  * @description 合并相邻的选中数据
1301
1386
  * @date 2023-02-15 16:11:16
@@ -1620,7 +1705,12 @@ async function upgrade({
1620
1705
  });
1621
1706
  const insert = (data, index) => {
1622
1707
  data.splice(index, 0, ...selected);
1623
- onUpgraded?.(selected);
1708
+ onUpgraded?.(selected, getExpandedState({
1709
+ childrenKey,
1710
+ originalParent: parent,
1711
+ parent: grandpa,
1712
+ signKey
1713
+ }));
1624
1714
  };
1625
1715
  const insertIndex = getInsertIdx({
1626
1716
  leader: start,
@@ -1682,7 +1772,12 @@ async function downgrade({
1682
1772
  const newChildren = [...(before[childrenKey] || []), ...children];
1683
1773
  if (children.length) {
1684
1774
  before[childrenKey] = [...newChildren];
1685
- onDowngraded?.(children);
1775
+ onDowngraded?.(children, getExpandedState({
1776
+ childrenKey,
1777
+ originalParent: parent,
1778
+ parent: before,
1779
+ signKey
1780
+ }));
1686
1781
  }
1687
1782
  }
1688
1783
  }
@@ -1735,5 +1830,17 @@ function treeSearch({
1735
1830
  }
1736
1831
  }).filter(row => !!row);
1737
1832
  }
1833
+ function getExpandedState({
1834
+ originalParent,
1835
+ parent,
1836
+ signKey = 'id',
1837
+ childrenKey = 'children'
1838
+ }) {
1839
+ const isRemove = !originalParent[childrenKey]?.length;
1840
+ return {
1841
+ add: parent[signKey],
1842
+ remove: isRemove ? originalParent[signKey] : null
1843
+ };
1844
+ }
1738
1845
 
1739
- export { _isMoment, arrayAttributeFlat, asyncDfs, base64ToFile, base64ToMimeType, baseTemplate, bfs, createEnumObject, deleteTreeNodes, dfs, downgrade, fromBase64UTF8, generateSecureUUID, generatedAcrossHierarchySort, getBase64Regular, getHTMLTagString, getI18nText, getSiblings, getTreeRelation, hasOwnProperty, initTranslateI18nFn, insert, isArray, isBoolean, isEmptyObject, isFunction, isIntegerKey, isNaN, isNil, isNull, isNumber, isObject, isString, isUndefined, levelTransformTree, makeDestructurable, mergeAdjacent, movUp, move, moveDown, t, toBase64UTF8, toRawType, toTypeString, translateI18n, treeSearch, upgrade, useReactive, useShallowReactive, useStore };
1846
+ export { _isMoment, arrayAttributeFlat, asyncDfs, base64ToFile, base64ToMimeType, baseTemplate, bfs, createEnumObject, deleteTreeNodes, dfs, downgrade, fromBase64UTF8, generateSecureUUID, generatedAcrossHierarchySort, getBase64Regular, getHTMLTagString, getI18nText, getSelectedNodeHierarchyStatus, getSiblings, getTreeAncestors, getTreeDescendants, getTreeRelation, hasOwnProperty, initTranslateI18nFn, insert, isArray, isBoolean, isEmptyObject, isFunction, isIntegerKey, isNaN, isNil, isNull, isNumber, isObject, isString, isUndefined, levelTransformTree, makeDestructurable, mergeAdjacent, movUp, move, moveDown, t, toBase64UTF8, toRawType, toTypeString, translateI18n, treeSearch, upgrade, useReactive, useShallowReactive, useStore };
package/dist/Tree.js CHANGED
@@ -61,6 +61,46 @@ function levelTransformTree({
61
61
  });
62
62
  return root.children;
63
63
  }
64
+
65
+ /** 获取全部子孙节点 */
66
+ function getTreeDescendants({
67
+ data,
68
+ key,
69
+ signKey = 'id',
70
+ childrenKey = 'children'
71
+ }) {
72
+ const {
73
+ self
74
+ } = getTreeRelation({
75
+ childrenKey,
76
+ data,
77
+ lookup(item, index) {
78
+ return item[signKey] === key;
79
+ }
80
+ });
81
+ return arrayAttributeFlat({
82
+ childrenKey,
83
+ data: self?.[childrenKey] || []
84
+ });
85
+ }
86
+
87
+ /** 获取全部祖先节点 */
88
+ function getTreeAncestors({
89
+ data,
90
+ key,
91
+ signKey = 'id',
92
+ childrenKey = 'children'
93
+ }) {
94
+ return getTreeRelation({
95
+ childrenKey,
96
+ data,
97
+ lookup(item, index) {
98
+ return item[signKey] === key;
99
+ }
100
+ }).stack;
101
+ }
102
+
103
+ /** 删除树节点 */
64
104
  function deleteTreeNodes({
65
105
  data,
66
106
  selectedKeys,
@@ -214,6 +254,51 @@ function getSiblings({
214
254
  return keys;
215
255
  }
216
256
 
257
+ /**
258
+ * @description 是否跨层
259
+ * @date 2022-11-21 20:03:52
260
+ * @export
261
+ * @param {string[]} hierarchy 选中任务的层级
262
+ * @returns {*}
263
+ */
264
+ function getSelectedNodeHierarchyStatus({
265
+ data,
266
+ selectedKeys,
267
+ signKey = 'id',
268
+ childrenKey = 'children'
269
+ }) {
270
+ const relations = selectedKeys.map(key => getTreeRelation({
271
+ childrenKey,
272
+ data,
273
+ lookup(item, index) {
274
+ return item[signKey] === key;
275
+ }
276
+ }));
277
+ // 深度集合
278
+ const deepSet = new Set(relations.map(rel => rel.stack.length));
279
+ const isDifferentDepths = deepSet.size > 1;
280
+ if (isDifferentDepths) {
281
+ return {
282
+ isDifferentDepths,
283
+ isIncludedParent: relations.sort((a, b) => {
284
+ return b.stack.length - a.stack.length;
285
+ }).some(item => {
286
+ const ancestors = getTreeAncestors({
287
+ childrenKey,
288
+ data,
289
+ key: item.self[signKey],
290
+ signKey
291
+ }).map(item => item[signKey]);
292
+ return selectedKeys.some(id => ancestors.includes(id));
293
+ })
294
+ };
295
+ }
296
+ return {
297
+ isDifferentDepths,
298
+ isIncludedParent: false
299
+ };
300
+ }
301
+
217
302
  /**
218
303
  * @description 合并相邻的选中数据
219
304
  * @date 2023-02-15 16:11:16
@@ -538,7 +623,12 @@ async function upgrade({
538
623
  });
539
624
  const insert = (data, index) => {
540
625
  data.splice(index, 0, ...selected);
541
- onUpgraded?.(selected);
626
+ onUpgraded?.(selected, getExpandedState({
627
+ childrenKey,
628
+ originalParent: parent,
629
+ parent: grandpa,
630
+ signKey
631
+ }));
542
632
  };
543
633
  const insertIndex = getInsertIdx({
544
634
  leader: start,
@@ -600,7 +690,12 @@ async function downgrade({
600
690
  const newChildren = [...(before[childrenKey] || []), ...children];
601
691
  if (children.length) {
602
692
  before[childrenKey] = [...newChildren];
603
- onDowngraded?.(children);
693
+ onDowngraded?.(children, getExpandedState({
694
+ childrenKey,
695
+ originalParent: parent,
696
+ parent: before,
697
+ signKey
698
+ }));
604
699
  }
605
700
  }
606
701
  }
@@ -653,12 +748,27 @@ function treeSearch({
653
748
  }
654
749
  }).filter(row => !!row);
655
750
  }
751
+ function getExpandedState({
752
+ originalParent,
753
+ parent,
754
+ signKey = 'id',
755
+ childrenKey = 'children'
756
+ }) {
757
+ const isRemove = !originalParent[childrenKey]?.length;
758
+ return {
759
+ add: parent[signKey],
760
+ remove: isRemove ? originalParent[signKey] : null
761
+ };
762
+ }
656
763
 
657
764
  exports.arrayAttributeFlat = arrayAttributeFlat;
658
765
  exports.deleteTreeNodes = deleteTreeNodes;
659
766
  exports.downgrade = downgrade;
660
767
  exports.generatedAcrossHierarchySort = generatedAcrossHierarchySort;
768
+ exports.getSelectedNodeHierarchyStatus = getSelectedNodeHierarchyStatus;
661
769
  exports.getSiblings = getSiblings;
770
+ exports.getTreeAncestors = getTreeAncestors;
771
+ exports.getTreeDescendants = getTreeDescendants;
662
772
  exports.getTreeRelation = getTreeRelation;
663
773
  exports.insert = insert;
664
774
  exports.levelTransformTree = levelTransformTree;
package/dist/Tree.mjs CHANGED
@@ -59,6 +59,46 @@ function levelTransformTree({
59
59
  });
60
60
  return root.children;
61
61
  }
62
+
63
+ /** 获取全部子孙节点 */
64
+ function getTreeDescendants({
65
+ data,
66
+ key,
67
+ signKey = 'id',
68
+ childrenKey = 'children'
69
+ }) {
70
+ const {
71
+ self
72
+ } = getTreeRelation({
73
+ childrenKey,
74
+ data,
75
+ lookup(item, index) {
76
+ return item[signKey] === key;
77
+ }
78
+ });
79
+ return arrayAttributeFlat({
80
+ childrenKey,
81
+ data: self?.[childrenKey] || []
82
+ });
83
+ }
84
+
85
+ /** 获取全部祖先节点 */
86
+ function getTreeAncestors({
87
+ data,
88
+ key,
89
+ signKey = 'id',
90
+ childrenKey = 'children'
91
+ }) {
92
+ return getTreeRelation({
93
+ childrenKey,
94
+ data,
95
+ lookup(item, index) {
96
+ return item[signKey] === key;
97
+ }
98
+ }).stack;
99
+ }
100
+
101
+ /** 删除树节点 */
62
102
  function deleteTreeNodes({
63
103
  data,
64
104
  selectedKeys,
@@ -212,6 +252,51 @@ function getSiblings({
212
252
  return keys;
213
253
  }
214
254
 
255
+ /**
256
+ * @description 是否跨层
257
+ * @date 2022-11-21 20:03:52
258
+ * @export
259
+ * @param {string[]} hierarchy 选中任务的层级
260
+ * @returns {*}
261
+ */
262
+ function getSelectedNodeHierarchyStatus({
263
+ data,
264
+ selectedKeys,
265
+ signKey = 'id',
266
+ childrenKey = 'children'
267
+ }) {
268
+ const relations = selectedKeys.map(key => getTreeRelation({
269
+ childrenKey,
270
+ data,
271
+ lookup(item, index) {
272
+ return item[signKey] === key;
273
+ }
274
+ }));
275
+ // 深度集合
276
+ const deepSet = new Set(relations.map(rel => rel.stack.length));
277
+ const isDifferentDepths = deepSet.size > 1;
278
+ if (isDifferentDepths) {
279
+ return {
280
+ isDifferentDepths,
281
+ isIncludedParent: relations.sort((a, b) => {
282
+ return b.stack.length - a.stack.length;
283
+ }).some(item => {
284
+ const ancestors = getTreeAncestors({
285
+ childrenKey,
286
+ data,
287
+ key: item.self[signKey],
288
+ signKey
289
+ }).map(item => item[signKey]);
290
+ return selectedKeys.some(id => ancestors.includes(id));
291
+ })
292
+ };
293
+ }
294
+ return {
295
+ isDifferentDepths,
296
+ isIncludedParent: false
297
+ };
298
+ }
299
+
215
300
  /**
216
301
  * @description 合并相邻的选中数据
217
302
  * @date 2023-02-15 16:11:16
@@ -536,7 +621,12 @@ async function upgrade({
536
621
  });
537
622
  const insert = (data, index) => {
538
623
  data.splice(index, 0, ...selected);
539
- onUpgraded?.(selected);
624
+ onUpgraded?.(selected, getExpandedState({
625
+ childrenKey,
626
+ originalParent: parent,
627
+ parent: grandpa,
628
+ signKey
629
+ }));
540
630
  };
541
631
  const insertIndex = getInsertIdx({
542
632
  leader: start,
@@ -598,7 +688,12 @@ async function downgrade({
598
688
  const newChildren = [...(before[childrenKey] || []), ...children];
599
689
  if (children.length) {
600
690
  before[childrenKey] = [...newChildren];
601
- onDowngraded?.(children);
691
+ onDowngraded?.(children, getExpandedState({
692
+ childrenKey,
693
+ originalParent: parent,
694
+ parent: before,
695
+ signKey
696
+ }));
602
697
  }
603
698
  }
604
699
  }
@@ -651,5 +746,17 @@ function treeSearch({
651
746
  }
652
747
  }).filter(row => !!row);
653
748
  }
749
+ function getExpandedState({
750
+ originalParent,
751
+ parent,
752
+ signKey = 'id',
753
+ childrenKey = 'children'
754
+ }) {
755
+ const isRemove = !originalParent[childrenKey]?.length;
756
+ return {
757
+ add: parent[signKey],
758
+ remove: isRemove ? originalParent[signKey] : null
759
+ };
760
+ }
654
761
 
655
- export { arrayAttributeFlat, deleteTreeNodes, downgrade, generatedAcrossHierarchySort, getSiblings, getTreeRelation, insert, levelTransformTree, mergeAdjacent, movUp, move, moveDown, treeSearch, upgrade };
762
+ export { arrayAttributeFlat, deleteTreeNodes, downgrade, generatedAcrossHierarchySort, getSelectedNodeHierarchyStatus, getSiblings, getTreeAncestors, getTreeDescendants, getTreeRelation, insert, levelTransformTree, mergeAdjacent, movUp, move, moveDown, treeSearch, upgrade };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "onchain-utility",
3
3
  "description": "This package contains misc utilities for onchain.",
4
4
  "license": "MIT",
5
- "version": "0.0.16",
5
+ "version": "0.0.17",
6
6
  "files": [
7
7
  "dist",
8
8
  "src"
package/src/Tree/index.ts CHANGED
@@ -79,9 +79,44 @@ export function levelTransformTree<T extends Item>({
79
79
  }
80
80
  });
81
81
 
82
- return root.children;
82
+ type _Item = T & {children: _Item[]};
83
+ return root.children as _Item[];
83
84
  }
84
85
 
86
+ /** 获取全部子孙节点 */
87
+ export function getTreeDescendants<T extends Item>({
88
+ data,
89
+ key,
90
+ signKey = 'id',
91
+ childrenKey = 'children',
92
+ }: Params<T> & {key: string}) {
93
+ const {self} = getTreeRelation({
94
+ childrenKey,
95
+ data,
96
+ lookup(item, index) {
97
+ return item[signKey] === key;
98
+ },
99
+ });
100
+ return arrayAttributeFlat({childrenKey, data: self?.[childrenKey] || []});
101
+ }
102
+
103
+ /** 获取全部祖先节点 */
104
+ export function getTreeAncestors<T extends Item>({
105
+ data,
106
+ key,
107
+ signKey = 'id',
108
+ childrenKey = 'children',
109
+ }: Params<T> & {key: string}) {
110
+ return getTreeRelation({
111
+ childrenKey,
112
+ data,
113
+ lookup(item, index) {
114
+ return item[signKey] === key;
115
+ },
116
+ }).stack;
117
+ }
118
+
119
+ /** 删除树节点 */
85
120
  export function deleteTreeNodes<T extends Item>({
86
121
  data,
87
122
  selectedKeys,
@@ -264,6 +299,63 @@ export function getSiblings<T extends Item>({
264
299
  return keys;
265
300
  }
266
301
 
302
+ /**
303
+ * @description 是否跨层
304
+ * @date 2022-11-21 20:03:52
305
+ * @export
306
+ * @param {string[]} hierarchy 选中任务的层级
307
+ * @returns {*}
308
+ */
309
+ export function getSelectedNodeHierarchyStatus<T extends Item>({
310
+ data,
311
+ selectedKeys,
312
+ signKey = 'id',
313
+ childrenKey = 'children',
314
+ }: Params<T> & {
315
+ selectedKeys: string[];
316
+ }): {
317
+ /** 层级深度是否有差别 */
318
+ isDifferentDepths: boolean;
319
+ /** 选中是否包含了祖先节点和其后的节点 */
320
+ isIncludedParent: boolean;
321
+ } {
322
+ const relations = selectedKeys.map((key) =>
323
+ getTreeRelation({
324
+ childrenKey,
325
+ data,
326
+ lookup(item, index) {
327
+ return item[signKey] === key;
328
+ },
329
+ }),
330
+ );
331
+ // 深度集合
332
+ const deepSet = new Set(relations.map((rel) => rel.stack.length));
333
+ const isDifferentDepths = deepSet.size > 1;
334
+ if (isDifferentDepths) {
335
+ return {
336
+ isDifferentDepths,
337
+ isIncludedParent: relations
338
+ .sort((a, b) => {
339
+ return b.stack.length - a.stack.length;
340
+ })
341
+ .some((item) => {
342
+ const ancestors = getTreeAncestors({
343
+ childrenKey,
344
+ data,
345
+ key: item.self[signKey],
346
+ signKey,
347
+ }).map((item) => item[signKey]);
348
+ return selectedKeys.some((id) => ancestors.includes(id));
349
+ }),
350
+ };
351
+ }
352
+
353
+ return {
354
+ isDifferentDepths,
355
+ isIncludedParent: false,
356
+ };
357
+ }
358
+
267
359
  /**
268
360
  * @description 合并相邻的选中数据
269
361
  * @date 2023-02-15 16:11:16
@@ -438,6 +530,16 @@ type MoveParams<T> = Params<T> & {
438
530
  onError?: (params: {selected: T[]; isMoveUp?: boolean}) => boolean | void;
439
531
  };
440
532
 
533
+ interface ParentParams<T> {
534
+ originalParent: T;
535
+ parent: T;
536
+ }
537
+
538
+ interface ExpandedState<T> {
539
+ add: T;
540
+ remove: T | null;
541
+ }
542
+
441
543
  export function move<T extends Item>({
442
544
  data,
443
545
  signKey = 'id',
@@ -509,6 +611,12 @@ export function moveDown<T extends Item>(_: MoveParams<T>) {
509
611
  });
510
612
  }
511
613
 
614
+ interface OnUpgradeParams<T> {
615
+ selected: T[];
616
+ parent: T;
617
+ grandpa?: T;
618
+ }
619
+
512
620
  export async function upgrade<T extends Item>({
513
621
  data,
514
622
  signKey = 'id',
@@ -526,12 +634,11 @@ export async function upgrade<T extends Item>({
526
634
  isSupportMultipleRoot?: boolean;
527
635
  onStopFollow?: (item: T) => boolean;
528
636
  onFilterFollow?: (item: T[]) => T[];
529
- onUpgrade?: (params: {
530
- selected: T[];
531
- parent: T;
532
- grandpa?: T;
533
- }) => Promise<boolean> | boolean;
534
- onUpgraded?: (upgrades: T[]) => void;
637
+ onUpgrade?: (params: OnUpgradeParams<T>) => Promise<boolean> | boolean;
638
+ onUpgraded?: (
639
+ upgrades: T[],
640
+ params: ExpandedState<T[typeof signKey]>,
641
+ ) => void;
535
642
  }) {
536
643
  data = optimizeStructure({childrenKey, data, isSupportMultipleRoot, signKey});
537
644
  const layers = mergeAdjacent({childrenKey, data, selectKeys, signKey});
@@ -606,7 +713,16 @@ export async function upgrade<T extends Item>({
606
713
 
607
714
  const insert = (data: T[], index: number) => {
608
715
  data.splice(index, 0, ...selected);
609
- onUpgraded?.(selected);
716
+ onUpgraded?.(
717
+ selected,
718
+ getExpandedState({
719
+ childrenKey,
720
+ data,
721
+ originalParent: parent,
722
+ parent: grandpa!,
723
+ signKey,
724
+ }),
725
+ );
610
726
  };
611
727
  const insertIndex = getInsertIdx({
612
728
  leader: start,
@@ -619,6 +735,12 @@ export async function upgrade<T extends Item>({
619
735
  }
620
736
  }
621
737
 
738
+ interface OnDowngradedParams<T> {
739
+ selected: T[];
740
+ beforeSibling: T[];
741
+ parent: T | undefined;
742
+ }
743
+
622
744
  export async function downgrade<T extends Item>({
623
745
  data,
624
746
  signKey = 'id',
@@ -628,12 +750,11 @@ export async function downgrade<T extends Item>({
628
750
  childrenKey = 'children',
629
751
  onError,
630
752
  }: MoveParams<T> & {
631
- onDowngrade?: (params: {
632
- selected: T[];
633
- beforeSibling: T[];
634
- parent: T | undefined;
635
- }) => Promise<boolean> | boolean;
636
- onDowngraded?: (downgrades: T[]) => void;
753
+ onDowngrade?: (params: OnDowngradedParams<T>) => Promise<boolean> | boolean;
754
+ onDowngraded?: (
755
+ downgrades: T[],
756
+ params: ExpandedState<T[typeof signKey]>,
757
+ ) => void;
637
758
  }) {
638
759
  data = optimizeStructure({childrenKey, data, signKey});
639
760
  const layers = mergeAdjacent({childrenKey, data, selectKeys, signKey});
@@ -662,7 +783,16 @@ export async function downgrade<T extends Item>({
662
783
  const newChildren = [...(before[childrenKey] || []), ...children];
663
784
  if (children.length) {
664
785
  (before as Item)[childrenKey] = [...newChildren];
665
- onDowngraded?.(children);
786
+ onDowngraded?.(
787
+ children,
788
+ getExpandedState({
789
+ childrenKey,
790
+ data,
791
+ originalParent: parent,
792
+ parent: before,
793
+ signKey,
794
+ }),
795
+ );
666
796
  }
667
797
  }
668
798
  }
@@ -722,3 +852,19 @@ export function treeSearch<T extends Record<string, any>>({
722
852
  })
723
853
  .filter((row): row is T => !!row);
724
854
  }
855
+
856
+ function getExpandedState<T extends Item>({
857
+ originalParent,
858
+ parent,
859
+ signKey = 'id',
860
+ childrenKey = 'children',
861
+ }: ParentParams<T> & Params<T>) {
862
+ const isRemove = !originalParent[childrenKey]?.length;
863
+
864
+ type Key = T[typeof signKey];
865
+
866
+ return {
867
+ add: parent[signKey] as Key,
868
+ remove: isRemove ? (originalParent[signKey] as Key) : null,
869
+ };
870
+ }