onchain-utility 0.0.12 → 0.0.14
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/OnchainUtility.js +44 -19
- package/dist/OnchainUtility.mjs +44 -19
- package/dist/Tree.js +44 -19
- package/dist/Tree.mjs +44 -19
- package/package.json +1 -1
- package/src/Tree/index.ts +38 -23
package/dist/OnchainUtility.js
CHANGED
|
@@ -1231,12 +1231,7 @@ function getSiblings({
|
|
|
1231
1231
|
return item[signKey] === key;
|
|
1232
1232
|
}
|
|
1233
1233
|
});
|
|
1234
|
-
|
|
1235
|
-
if (data.length === 1) {
|
|
1236
|
-
keys = parent ? parent[childrenKey].map(item => item[signKey]) : [];
|
|
1237
|
-
} else {
|
|
1238
|
-
keys = (parent ? parent[childrenKey] : data).map(item => item[signKey]);
|
|
1239
|
-
}
|
|
1234
|
+
const keys = parent ? parent[childrenKey].map(item => item[signKey]) : [];
|
|
1240
1235
|
return keys;
|
|
1241
1236
|
}
|
|
1242
1237
|
|
|
@@ -1355,6 +1350,21 @@ function generatedAcrossHierarchySort({
|
|
|
1355
1350
|
nodes
|
|
1356
1351
|
};
|
|
1357
1352
|
}
|
|
1353
|
+
function optimizeStructure({
|
|
1354
|
+
data,
|
|
1355
|
+
childrenKey = 'children',
|
|
1356
|
+
signKey = 'id'
|
|
1357
|
+
}) {
|
|
1358
|
+
if (data.length > 1) {
|
|
1359
|
+
return [{
|
|
1360
|
+
[childrenKey]: data,
|
|
1361
|
+
isCustom: true,
|
|
1362
|
+
[signKey]: 'custom-root'
|
|
1363
|
+
}];
|
|
1364
|
+
} else {
|
|
1365
|
+
return data;
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1358
1368
|
function _simpleGetRel({
|
|
1359
1369
|
data,
|
|
1360
1370
|
key,
|
|
@@ -1379,6 +1389,11 @@ function move({
|
|
|
1379
1389
|
onMoved,
|
|
1380
1390
|
onError
|
|
1381
1391
|
}) {
|
|
1392
|
+
data = optimizeStructure({
|
|
1393
|
+
childrenKey,
|
|
1394
|
+
data,
|
|
1395
|
+
signKey
|
|
1396
|
+
});
|
|
1382
1397
|
const layers = mergeAdjacent({
|
|
1383
1398
|
childrenKey,
|
|
1384
1399
|
data,
|
|
@@ -1407,16 +1422,16 @@ function move({
|
|
|
1407
1422
|
// 锚点的父级
|
|
1408
1423
|
const anchorParent = isMoveUp ? start.parent : end.parent;
|
|
1409
1424
|
// 删除选中
|
|
1410
|
-
anchorParent.
|
|
1425
|
+
anchorParent[childrenKey].splice(start.index, len);
|
|
1411
1426
|
// 锚点
|
|
1412
|
-
const anchorIndex = anchorParent?.
|
|
1427
|
+
const anchorIndex = anchorParent?.[childrenKey]?.indexOf(changePosition);
|
|
1413
1428
|
const arr = [[selected], changePosition];
|
|
1414
1429
|
|
|
1415
1430
|
// 向下移动时 需要调换位置
|
|
1416
1431
|
if (!isMoveUp) {
|
|
1417
1432
|
arr.reverse();
|
|
1418
1433
|
}
|
|
1419
|
-
anchorParent.
|
|
1434
|
+
anchorParent[childrenKey].splice(anchorIndex, 1, ...arr.flat(2));
|
|
1420
1435
|
onMoved?.(selected);
|
|
1421
1436
|
} else {
|
|
1422
1437
|
if (onError?.({
|
|
@@ -1453,6 +1468,11 @@ async function upgrade({
|
|
|
1453
1468
|
onUpgraded,
|
|
1454
1469
|
onError
|
|
1455
1470
|
}) {
|
|
1471
|
+
data = optimizeStructure({
|
|
1472
|
+
childrenKey,
|
|
1473
|
+
data,
|
|
1474
|
+
signKey
|
|
1475
|
+
});
|
|
1456
1476
|
const layers = mergeAdjacent({
|
|
1457
1477
|
childrenKey,
|
|
1458
1478
|
data,
|
|
@@ -1484,7 +1504,7 @@ async function upgrade({
|
|
|
1484
1504
|
const parentRel = _simpleGetRel({
|
|
1485
1505
|
childrenKey,
|
|
1486
1506
|
data,
|
|
1487
|
-
key: parent
|
|
1507
|
+
key: parent[signKey],
|
|
1488
1508
|
signKey
|
|
1489
1509
|
});
|
|
1490
1510
|
const {
|
|
@@ -1498,7 +1518,7 @@ async function upgrade({
|
|
|
1498
1518
|
}))) {
|
|
1499
1519
|
continue;
|
|
1500
1520
|
}
|
|
1501
|
-
// const originalOldPrtCdr = parent
|
|
1521
|
+
// const originalOldPrtCdr = parent[childrenKey]?.slice(0, Infinity) || [];
|
|
1502
1522
|
let followChildren;
|
|
1503
1523
|
/** 无法跟随的节点 */
|
|
1504
1524
|
const unableToFollow = [];
|
|
@@ -1510,28 +1530,28 @@ async function upgrade({
|
|
|
1510
1530
|
// 获取需要跟随 self 的节点所在的范围
|
|
1511
1531
|
const startIndex = anchor.index + 1;
|
|
1512
1532
|
/** 跟随节点 */
|
|
1513
|
-
followChildren = parent.
|
|
1533
|
+
followChildren = parent[childrenKey].slice(startIndex);
|
|
1514
1534
|
/** 中断处的索引 */
|
|
1515
1535
|
const breakOffIndex = followChildren.findIndex(i => {
|
|
1516
|
-
const selected = selectKeys.includes(i
|
|
1536
|
+
const selected = selectKeys.includes(i[signKey]);
|
|
1517
1537
|
// 后续被选中的 || 不满足被跟随节点需要的节点类型
|
|
1518
1538
|
return selected || onStopFollow?.(i);
|
|
1519
1539
|
});
|
|
1520
1540
|
/** 跟随数量 */
|
|
1521
|
-
const followCount = breakOffIndex >= 0 ? breakOffIndex : parent.
|
|
1541
|
+
const followCount = breakOffIndex >= 0 ? breakOffIndex : parent[childrenKey].length;
|
|
1522
1542
|
// 删除选中后的兄弟
|
|
1523
|
-
followChildren = parent.
|
|
1543
|
+
followChildren = parent[childrenKey].splice(start.index + len, followCount);
|
|
1524
1544
|
followChildren = onFilterFollow ? onFilterFollow(followChildren) : followChildren;
|
|
1525
1545
|
// 删除选中节点
|
|
1526
1546
|
} else {
|
|
1527
1547
|
followChildren = [];
|
|
1528
1548
|
}
|
|
1529
1549
|
const self = anchor.self;
|
|
1530
|
-
self
|
|
1531
|
-
parent.
|
|
1550
|
+
self[childrenKey] = [...(self[childrenKey] || []), ...followChildren];
|
|
1551
|
+
parent[childrenKey].splice(start.index, len, ...unableToFollow);
|
|
1532
1552
|
const list = crossLayerSort({
|
|
1533
1553
|
leader: start,
|
|
1534
|
-
oldParentId: parent
|
|
1554
|
+
oldParentId: parent[signKey],
|
|
1535
1555
|
selectSize: len
|
|
1536
1556
|
});
|
|
1537
1557
|
const insert = (data, index) => {
|
|
@@ -1543,7 +1563,7 @@ async function upgrade({
|
|
|
1543
1563
|
list,
|
|
1544
1564
|
oldParentIndex: parentIndex
|
|
1545
1565
|
});
|
|
1546
|
-
insert(grandpa
|
|
1566
|
+
insert(grandpa[childrenKey], insertIndex);
|
|
1547
1567
|
}
|
|
1548
1568
|
}
|
|
1549
1569
|
}
|
|
@@ -1557,6 +1577,11 @@ async function downgrade({
|
|
|
1557
1577
|
childrenKey = 'children',
|
|
1558
1578
|
onError
|
|
1559
1579
|
}) {
|
|
1580
|
+
data = optimizeStructure({
|
|
1581
|
+
childrenKey,
|
|
1582
|
+
data,
|
|
1583
|
+
signKey
|
|
1584
|
+
});
|
|
1560
1585
|
const layers = mergeAdjacent({
|
|
1561
1586
|
childrenKey,
|
|
1562
1587
|
data,
|
package/dist/OnchainUtility.mjs
CHANGED
|
@@ -1229,12 +1229,7 @@ function getSiblings({
|
|
|
1229
1229
|
return item[signKey] === key;
|
|
1230
1230
|
}
|
|
1231
1231
|
});
|
|
1232
|
-
|
|
1233
|
-
if (data.length === 1) {
|
|
1234
|
-
keys = parent ? parent[childrenKey].map(item => item[signKey]) : [];
|
|
1235
|
-
} else {
|
|
1236
|
-
keys = (parent ? parent[childrenKey] : data).map(item => item[signKey]);
|
|
1237
|
-
}
|
|
1232
|
+
const keys = parent ? parent[childrenKey].map(item => item[signKey]) : [];
|
|
1238
1233
|
return keys;
|
|
1239
1234
|
}
|
|
1240
1235
|
|
|
@@ -1353,6 +1348,21 @@ function generatedAcrossHierarchySort({
|
|
|
1353
1348
|
nodes
|
|
1354
1349
|
};
|
|
1355
1350
|
}
|
|
1351
|
+
function optimizeStructure({
|
|
1352
|
+
data,
|
|
1353
|
+
childrenKey = 'children',
|
|
1354
|
+
signKey = 'id'
|
|
1355
|
+
}) {
|
|
1356
|
+
if (data.length > 1) {
|
|
1357
|
+
return [{
|
|
1358
|
+
[childrenKey]: data,
|
|
1359
|
+
isCustom: true,
|
|
1360
|
+
[signKey]: 'custom-root'
|
|
1361
|
+
}];
|
|
1362
|
+
} else {
|
|
1363
|
+
return data;
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1356
1366
|
function _simpleGetRel({
|
|
1357
1367
|
data,
|
|
1358
1368
|
key,
|
|
@@ -1377,6 +1387,11 @@ function move({
|
|
|
1377
1387
|
onMoved,
|
|
1378
1388
|
onError
|
|
1379
1389
|
}) {
|
|
1390
|
+
data = optimizeStructure({
|
|
1391
|
+
childrenKey,
|
|
1392
|
+
data,
|
|
1393
|
+
signKey
|
|
1394
|
+
});
|
|
1380
1395
|
const layers = mergeAdjacent({
|
|
1381
1396
|
childrenKey,
|
|
1382
1397
|
data,
|
|
@@ -1405,16 +1420,16 @@ function move({
|
|
|
1405
1420
|
// 锚点的父级
|
|
1406
1421
|
const anchorParent = isMoveUp ? start.parent : end.parent;
|
|
1407
1422
|
// 删除选中
|
|
1408
|
-
anchorParent.
|
|
1423
|
+
anchorParent[childrenKey].splice(start.index, len);
|
|
1409
1424
|
// 锚点
|
|
1410
|
-
const anchorIndex = anchorParent?.
|
|
1425
|
+
const anchorIndex = anchorParent?.[childrenKey]?.indexOf(changePosition);
|
|
1411
1426
|
const arr = [[selected], changePosition];
|
|
1412
1427
|
|
|
1413
1428
|
// 向下移动时 需要调换位置
|
|
1414
1429
|
if (!isMoveUp) {
|
|
1415
1430
|
arr.reverse();
|
|
1416
1431
|
}
|
|
1417
|
-
anchorParent.
|
|
1432
|
+
anchorParent[childrenKey].splice(anchorIndex, 1, ...arr.flat(2));
|
|
1418
1433
|
onMoved?.(selected);
|
|
1419
1434
|
} else {
|
|
1420
1435
|
if (onError?.({
|
|
@@ -1451,6 +1466,11 @@ async function upgrade({
|
|
|
1451
1466
|
onUpgraded,
|
|
1452
1467
|
onError
|
|
1453
1468
|
}) {
|
|
1469
|
+
data = optimizeStructure({
|
|
1470
|
+
childrenKey,
|
|
1471
|
+
data,
|
|
1472
|
+
signKey
|
|
1473
|
+
});
|
|
1454
1474
|
const layers = mergeAdjacent({
|
|
1455
1475
|
childrenKey,
|
|
1456
1476
|
data,
|
|
@@ -1482,7 +1502,7 @@ async function upgrade({
|
|
|
1482
1502
|
const parentRel = _simpleGetRel({
|
|
1483
1503
|
childrenKey,
|
|
1484
1504
|
data,
|
|
1485
|
-
key: parent
|
|
1505
|
+
key: parent[signKey],
|
|
1486
1506
|
signKey
|
|
1487
1507
|
});
|
|
1488
1508
|
const {
|
|
@@ -1496,7 +1516,7 @@ async function upgrade({
|
|
|
1496
1516
|
}))) {
|
|
1497
1517
|
continue;
|
|
1498
1518
|
}
|
|
1499
|
-
// const originalOldPrtCdr = parent
|
|
1519
|
+
// const originalOldPrtCdr = parent[childrenKey]?.slice(0, Infinity) || [];
|
|
1500
1520
|
let followChildren;
|
|
1501
1521
|
/** 无法跟随的节点 */
|
|
1502
1522
|
const unableToFollow = [];
|
|
@@ -1508,28 +1528,28 @@ async function upgrade({
|
|
|
1508
1528
|
// 获取需要跟随 self 的节点所在的范围
|
|
1509
1529
|
const startIndex = anchor.index + 1;
|
|
1510
1530
|
/** 跟随节点 */
|
|
1511
|
-
followChildren = parent.
|
|
1531
|
+
followChildren = parent[childrenKey].slice(startIndex);
|
|
1512
1532
|
/** 中断处的索引 */
|
|
1513
1533
|
const breakOffIndex = followChildren.findIndex(i => {
|
|
1514
|
-
const selected = selectKeys.includes(i
|
|
1534
|
+
const selected = selectKeys.includes(i[signKey]);
|
|
1515
1535
|
// 后续被选中的 || 不满足被跟随节点需要的节点类型
|
|
1516
1536
|
return selected || onStopFollow?.(i);
|
|
1517
1537
|
});
|
|
1518
1538
|
/** 跟随数量 */
|
|
1519
|
-
const followCount = breakOffIndex >= 0 ? breakOffIndex : parent.
|
|
1539
|
+
const followCount = breakOffIndex >= 0 ? breakOffIndex : parent[childrenKey].length;
|
|
1520
1540
|
// 删除选中后的兄弟
|
|
1521
|
-
followChildren = parent.
|
|
1541
|
+
followChildren = parent[childrenKey].splice(start.index + len, followCount);
|
|
1522
1542
|
followChildren = onFilterFollow ? onFilterFollow(followChildren) : followChildren;
|
|
1523
1543
|
// 删除选中节点
|
|
1524
1544
|
} else {
|
|
1525
1545
|
followChildren = [];
|
|
1526
1546
|
}
|
|
1527
1547
|
const self = anchor.self;
|
|
1528
|
-
self
|
|
1529
|
-
parent.
|
|
1548
|
+
self[childrenKey] = [...(self[childrenKey] || []), ...followChildren];
|
|
1549
|
+
parent[childrenKey].splice(start.index, len, ...unableToFollow);
|
|
1530
1550
|
const list = crossLayerSort({
|
|
1531
1551
|
leader: start,
|
|
1532
|
-
oldParentId: parent
|
|
1552
|
+
oldParentId: parent[signKey],
|
|
1533
1553
|
selectSize: len
|
|
1534
1554
|
});
|
|
1535
1555
|
const insert = (data, index) => {
|
|
@@ -1541,7 +1561,7 @@ async function upgrade({
|
|
|
1541
1561
|
list,
|
|
1542
1562
|
oldParentIndex: parentIndex
|
|
1543
1563
|
});
|
|
1544
|
-
insert(grandpa
|
|
1564
|
+
insert(grandpa[childrenKey], insertIndex);
|
|
1545
1565
|
}
|
|
1546
1566
|
}
|
|
1547
1567
|
}
|
|
@@ -1555,6 +1575,11 @@ async function downgrade({
|
|
|
1555
1575
|
childrenKey = 'children',
|
|
1556
1576
|
onError
|
|
1557
1577
|
}) {
|
|
1578
|
+
data = optimizeStructure({
|
|
1579
|
+
childrenKey,
|
|
1580
|
+
data,
|
|
1581
|
+
signKey
|
|
1582
|
+
});
|
|
1558
1583
|
const layers = mergeAdjacent({
|
|
1559
1584
|
childrenKey,
|
|
1560
1585
|
data,
|
package/dist/Tree.js
CHANGED
|
@@ -210,12 +210,7 @@ function getSiblings({
|
|
|
210
210
|
return item[signKey] === key;
|
|
211
211
|
}
|
|
212
212
|
});
|
|
213
|
-
|
|
214
|
-
if (data.length === 1) {
|
|
215
|
-
keys = parent ? parent[childrenKey].map(item => item[signKey]) : [];
|
|
216
|
-
} else {
|
|
217
|
-
keys = (parent ? parent[childrenKey] : data).map(item => item[signKey]);
|
|
218
|
-
}
|
|
213
|
+
const keys = parent ? parent[childrenKey].map(item => item[signKey]) : [];
|
|
219
214
|
return keys;
|
|
220
215
|
}
|
|
221
216
|
|
|
@@ -334,6 +329,21 @@ function generatedAcrossHierarchySort({
|
|
|
334
329
|
nodes
|
|
335
330
|
};
|
|
336
331
|
}
|
|
332
|
+
function optimizeStructure({
|
|
333
|
+
data,
|
|
334
|
+
childrenKey = 'children',
|
|
335
|
+
signKey = 'id'
|
|
336
|
+
}) {
|
|
337
|
+
if (data.length > 1) {
|
|
338
|
+
return [{
|
|
339
|
+
[childrenKey]: data,
|
|
340
|
+
isCustom: true,
|
|
341
|
+
[signKey]: 'custom-root'
|
|
342
|
+
}];
|
|
343
|
+
} else {
|
|
344
|
+
return data;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
337
347
|
function _simpleGetRel({
|
|
338
348
|
data,
|
|
339
349
|
key,
|
|
@@ -358,6 +368,11 @@ function move({
|
|
|
358
368
|
onMoved,
|
|
359
369
|
onError
|
|
360
370
|
}) {
|
|
371
|
+
data = optimizeStructure({
|
|
372
|
+
childrenKey,
|
|
373
|
+
data,
|
|
374
|
+
signKey
|
|
375
|
+
});
|
|
361
376
|
const layers = mergeAdjacent({
|
|
362
377
|
childrenKey,
|
|
363
378
|
data,
|
|
@@ -386,16 +401,16 @@ function move({
|
|
|
386
401
|
// 锚点的父级
|
|
387
402
|
const anchorParent = isMoveUp ? start.parent : end.parent;
|
|
388
403
|
// 删除选中
|
|
389
|
-
anchorParent.
|
|
404
|
+
anchorParent[childrenKey].splice(start.index, len);
|
|
390
405
|
// 锚点
|
|
391
|
-
const anchorIndex = anchorParent?.
|
|
406
|
+
const anchorIndex = anchorParent?.[childrenKey]?.indexOf(changePosition);
|
|
392
407
|
const arr = [[selected], changePosition];
|
|
393
408
|
|
|
394
409
|
// 向下移动时 需要调换位置
|
|
395
410
|
if (!isMoveUp) {
|
|
396
411
|
arr.reverse();
|
|
397
412
|
}
|
|
398
|
-
anchorParent.
|
|
413
|
+
anchorParent[childrenKey].splice(anchorIndex, 1, ...arr.flat(2));
|
|
399
414
|
onMoved?.(selected);
|
|
400
415
|
} else {
|
|
401
416
|
if (onError?.({
|
|
@@ -432,6 +447,11 @@ async function upgrade({
|
|
|
432
447
|
onUpgraded,
|
|
433
448
|
onError
|
|
434
449
|
}) {
|
|
450
|
+
data = optimizeStructure({
|
|
451
|
+
childrenKey,
|
|
452
|
+
data,
|
|
453
|
+
signKey
|
|
454
|
+
});
|
|
435
455
|
const layers = mergeAdjacent({
|
|
436
456
|
childrenKey,
|
|
437
457
|
data,
|
|
@@ -463,7 +483,7 @@ async function upgrade({
|
|
|
463
483
|
const parentRel = _simpleGetRel({
|
|
464
484
|
childrenKey,
|
|
465
485
|
data,
|
|
466
|
-
key: parent
|
|
486
|
+
key: parent[signKey],
|
|
467
487
|
signKey
|
|
468
488
|
});
|
|
469
489
|
const {
|
|
@@ -477,7 +497,7 @@ async function upgrade({
|
|
|
477
497
|
}))) {
|
|
478
498
|
continue;
|
|
479
499
|
}
|
|
480
|
-
// const originalOldPrtCdr = parent
|
|
500
|
+
// const originalOldPrtCdr = parent[childrenKey]?.slice(0, Infinity) || [];
|
|
481
501
|
let followChildren;
|
|
482
502
|
/** 无法跟随的节点 */
|
|
483
503
|
const unableToFollow = [];
|
|
@@ -489,28 +509,28 @@ async function upgrade({
|
|
|
489
509
|
// 获取需要跟随 self 的节点所在的范围
|
|
490
510
|
const startIndex = anchor.index + 1;
|
|
491
511
|
/** 跟随节点 */
|
|
492
|
-
followChildren = parent.
|
|
512
|
+
followChildren = parent[childrenKey].slice(startIndex);
|
|
493
513
|
/** 中断处的索引 */
|
|
494
514
|
const breakOffIndex = followChildren.findIndex(i => {
|
|
495
|
-
const selected = selectKeys.includes(i
|
|
515
|
+
const selected = selectKeys.includes(i[signKey]);
|
|
496
516
|
// 后续被选中的 || 不满足被跟随节点需要的节点类型
|
|
497
517
|
return selected || onStopFollow?.(i);
|
|
498
518
|
});
|
|
499
519
|
/** 跟随数量 */
|
|
500
|
-
const followCount = breakOffIndex >= 0 ? breakOffIndex : parent.
|
|
520
|
+
const followCount = breakOffIndex >= 0 ? breakOffIndex : parent[childrenKey].length;
|
|
501
521
|
// 删除选中后的兄弟
|
|
502
|
-
followChildren = parent.
|
|
522
|
+
followChildren = parent[childrenKey].splice(start.index + len, followCount);
|
|
503
523
|
followChildren = onFilterFollow ? onFilterFollow(followChildren) : followChildren;
|
|
504
524
|
// 删除选中节点
|
|
505
525
|
} else {
|
|
506
526
|
followChildren = [];
|
|
507
527
|
}
|
|
508
528
|
const self = anchor.self;
|
|
509
|
-
self
|
|
510
|
-
parent.
|
|
529
|
+
self[childrenKey] = [...(self[childrenKey] || []), ...followChildren];
|
|
530
|
+
parent[childrenKey].splice(start.index, len, ...unableToFollow);
|
|
511
531
|
const list = crossLayerSort({
|
|
512
532
|
leader: start,
|
|
513
|
-
oldParentId: parent
|
|
533
|
+
oldParentId: parent[signKey],
|
|
514
534
|
selectSize: len
|
|
515
535
|
});
|
|
516
536
|
const insert = (data, index) => {
|
|
@@ -522,7 +542,7 @@ async function upgrade({
|
|
|
522
542
|
list,
|
|
523
543
|
oldParentIndex: parentIndex
|
|
524
544
|
});
|
|
525
|
-
insert(grandpa
|
|
545
|
+
insert(grandpa[childrenKey], insertIndex);
|
|
526
546
|
}
|
|
527
547
|
}
|
|
528
548
|
}
|
|
@@ -536,6 +556,11 @@ async function downgrade({
|
|
|
536
556
|
childrenKey = 'children',
|
|
537
557
|
onError
|
|
538
558
|
}) {
|
|
559
|
+
data = optimizeStructure({
|
|
560
|
+
childrenKey,
|
|
561
|
+
data,
|
|
562
|
+
signKey
|
|
563
|
+
});
|
|
539
564
|
const layers = mergeAdjacent({
|
|
540
565
|
childrenKey,
|
|
541
566
|
data,
|
package/dist/Tree.mjs
CHANGED
|
@@ -208,12 +208,7 @@ function getSiblings({
|
|
|
208
208
|
return item[signKey] === key;
|
|
209
209
|
}
|
|
210
210
|
});
|
|
211
|
-
|
|
212
|
-
if (data.length === 1) {
|
|
213
|
-
keys = parent ? parent[childrenKey].map(item => item[signKey]) : [];
|
|
214
|
-
} else {
|
|
215
|
-
keys = (parent ? parent[childrenKey] : data).map(item => item[signKey]);
|
|
216
|
-
}
|
|
211
|
+
const keys = parent ? parent[childrenKey].map(item => item[signKey]) : [];
|
|
217
212
|
return keys;
|
|
218
213
|
}
|
|
219
214
|
|
|
@@ -332,6 +327,21 @@ function generatedAcrossHierarchySort({
|
|
|
332
327
|
nodes
|
|
333
328
|
};
|
|
334
329
|
}
|
|
330
|
+
function optimizeStructure({
|
|
331
|
+
data,
|
|
332
|
+
childrenKey = 'children',
|
|
333
|
+
signKey = 'id'
|
|
334
|
+
}) {
|
|
335
|
+
if (data.length > 1) {
|
|
336
|
+
return [{
|
|
337
|
+
[childrenKey]: data,
|
|
338
|
+
isCustom: true,
|
|
339
|
+
[signKey]: 'custom-root'
|
|
340
|
+
}];
|
|
341
|
+
} else {
|
|
342
|
+
return data;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
335
345
|
function _simpleGetRel({
|
|
336
346
|
data,
|
|
337
347
|
key,
|
|
@@ -356,6 +366,11 @@ function move({
|
|
|
356
366
|
onMoved,
|
|
357
367
|
onError
|
|
358
368
|
}) {
|
|
369
|
+
data = optimizeStructure({
|
|
370
|
+
childrenKey,
|
|
371
|
+
data,
|
|
372
|
+
signKey
|
|
373
|
+
});
|
|
359
374
|
const layers = mergeAdjacent({
|
|
360
375
|
childrenKey,
|
|
361
376
|
data,
|
|
@@ -384,16 +399,16 @@ function move({
|
|
|
384
399
|
// 锚点的父级
|
|
385
400
|
const anchorParent = isMoveUp ? start.parent : end.parent;
|
|
386
401
|
// 删除选中
|
|
387
|
-
anchorParent.
|
|
402
|
+
anchorParent[childrenKey].splice(start.index, len);
|
|
388
403
|
// 锚点
|
|
389
|
-
const anchorIndex = anchorParent?.
|
|
404
|
+
const anchorIndex = anchorParent?.[childrenKey]?.indexOf(changePosition);
|
|
390
405
|
const arr = [[selected], changePosition];
|
|
391
406
|
|
|
392
407
|
// 向下移动时 需要调换位置
|
|
393
408
|
if (!isMoveUp) {
|
|
394
409
|
arr.reverse();
|
|
395
410
|
}
|
|
396
|
-
anchorParent.
|
|
411
|
+
anchorParent[childrenKey].splice(anchorIndex, 1, ...arr.flat(2));
|
|
397
412
|
onMoved?.(selected);
|
|
398
413
|
} else {
|
|
399
414
|
if (onError?.({
|
|
@@ -430,6 +445,11 @@ async function upgrade({
|
|
|
430
445
|
onUpgraded,
|
|
431
446
|
onError
|
|
432
447
|
}) {
|
|
448
|
+
data = optimizeStructure({
|
|
449
|
+
childrenKey,
|
|
450
|
+
data,
|
|
451
|
+
signKey
|
|
452
|
+
});
|
|
433
453
|
const layers = mergeAdjacent({
|
|
434
454
|
childrenKey,
|
|
435
455
|
data,
|
|
@@ -461,7 +481,7 @@ async function upgrade({
|
|
|
461
481
|
const parentRel = _simpleGetRel({
|
|
462
482
|
childrenKey,
|
|
463
483
|
data,
|
|
464
|
-
key: parent
|
|
484
|
+
key: parent[signKey],
|
|
465
485
|
signKey
|
|
466
486
|
});
|
|
467
487
|
const {
|
|
@@ -475,7 +495,7 @@ async function upgrade({
|
|
|
475
495
|
}))) {
|
|
476
496
|
continue;
|
|
477
497
|
}
|
|
478
|
-
// const originalOldPrtCdr = parent
|
|
498
|
+
// const originalOldPrtCdr = parent[childrenKey]?.slice(0, Infinity) || [];
|
|
479
499
|
let followChildren;
|
|
480
500
|
/** 无法跟随的节点 */
|
|
481
501
|
const unableToFollow = [];
|
|
@@ -487,28 +507,28 @@ async function upgrade({
|
|
|
487
507
|
// 获取需要跟随 self 的节点所在的范围
|
|
488
508
|
const startIndex = anchor.index + 1;
|
|
489
509
|
/** 跟随节点 */
|
|
490
|
-
followChildren = parent.
|
|
510
|
+
followChildren = parent[childrenKey].slice(startIndex);
|
|
491
511
|
/** 中断处的索引 */
|
|
492
512
|
const breakOffIndex = followChildren.findIndex(i => {
|
|
493
|
-
const selected = selectKeys.includes(i
|
|
513
|
+
const selected = selectKeys.includes(i[signKey]);
|
|
494
514
|
// 后续被选中的 || 不满足被跟随节点需要的节点类型
|
|
495
515
|
return selected || onStopFollow?.(i);
|
|
496
516
|
});
|
|
497
517
|
/** 跟随数量 */
|
|
498
|
-
const followCount = breakOffIndex >= 0 ? breakOffIndex : parent.
|
|
518
|
+
const followCount = breakOffIndex >= 0 ? breakOffIndex : parent[childrenKey].length;
|
|
499
519
|
// 删除选中后的兄弟
|
|
500
|
-
followChildren = parent.
|
|
520
|
+
followChildren = parent[childrenKey].splice(start.index + len, followCount);
|
|
501
521
|
followChildren = onFilterFollow ? onFilterFollow(followChildren) : followChildren;
|
|
502
522
|
// 删除选中节点
|
|
503
523
|
} else {
|
|
504
524
|
followChildren = [];
|
|
505
525
|
}
|
|
506
526
|
const self = anchor.self;
|
|
507
|
-
self
|
|
508
|
-
parent.
|
|
527
|
+
self[childrenKey] = [...(self[childrenKey] || []), ...followChildren];
|
|
528
|
+
parent[childrenKey].splice(start.index, len, ...unableToFollow);
|
|
509
529
|
const list = crossLayerSort({
|
|
510
530
|
leader: start,
|
|
511
|
-
oldParentId: parent
|
|
531
|
+
oldParentId: parent[signKey],
|
|
512
532
|
selectSize: len
|
|
513
533
|
});
|
|
514
534
|
const insert = (data, index) => {
|
|
@@ -520,7 +540,7 @@ async function upgrade({
|
|
|
520
540
|
list,
|
|
521
541
|
oldParentIndex: parentIndex
|
|
522
542
|
});
|
|
523
|
-
insert(grandpa
|
|
543
|
+
insert(grandpa[childrenKey], insertIndex);
|
|
524
544
|
}
|
|
525
545
|
}
|
|
526
546
|
}
|
|
@@ -534,6 +554,11 @@ async function downgrade({
|
|
|
534
554
|
childrenKey = 'children',
|
|
535
555
|
onError
|
|
536
556
|
}) {
|
|
557
|
+
data = optimizeStructure({
|
|
558
|
+
childrenKey,
|
|
559
|
+
data,
|
|
560
|
+
signKey
|
|
561
|
+
});
|
|
537
562
|
const layers = mergeAdjacent({
|
|
538
563
|
childrenKey,
|
|
539
564
|
data,
|
package/package.json
CHANGED
package/src/Tree/index.ts
CHANGED
|
@@ -258,16 +258,9 @@ export function getSiblings<T extends Item>({
|
|
|
258
258
|
return item[signKey] === key;
|
|
259
259
|
},
|
|
260
260
|
});
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
? (parent[childrenKey] as T[]).map((item) => item[signKey])
|
|
265
|
-
: [];
|
|
266
|
-
} else {
|
|
267
|
-
keys = (parent ? (parent[childrenKey] as T[]) : data).map(
|
|
268
|
-
(item) => item[signKey],
|
|
269
|
-
);
|
|
270
|
-
}
|
|
261
|
+
const keys: string[] = parent
|
|
262
|
+
? (parent[childrenKey] as T[]).map((item) => item[signKey])
|
|
263
|
+
: [];
|
|
271
264
|
return keys;
|
|
272
265
|
}
|
|
273
266
|
|
|
@@ -402,6 +395,24 @@ export function generatedAcrossHierarchySort<T extends Item>({
|
|
|
402
395
|
};
|
|
403
396
|
}
|
|
404
397
|
|
|
398
|
+
function optimizeStructure<T>({
|
|
399
|
+
data,
|
|
400
|
+
childrenKey = 'children',
|
|
401
|
+
signKey = 'id',
|
|
402
|
+
}: Params<T>): T[] {
|
|
403
|
+
if (data.length > 1) {
|
|
404
|
+
return [
|
|
405
|
+
{
|
|
406
|
+
[childrenKey]: data,
|
|
407
|
+
isCustom: true,
|
|
408
|
+
[signKey]: 'custom-root',
|
|
409
|
+
} as T,
|
|
410
|
+
];
|
|
411
|
+
} else {
|
|
412
|
+
return data;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
405
416
|
function _simpleGetRel<T extends Item>({
|
|
406
417
|
data,
|
|
407
418
|
key,
|
|
@@ -438,6 +449,7 @@ export function move<T extends Item>({
|
|
|
438
449
|
}: MoveParams<T> & {
|
|
439
450
|
isMoveUp?: boolean;
|
|
440
451
|
}) {
|
|
452
|
+
data = optimizeStructure({childrenKey, data, signKey});
|
|
441
453
|
const layers = mergeAdjacent({childrenKey, data, selectKeys, signKey});
|
|
442
454
|
for (const comb of layers) {
|
|
443
455
|
for (const keys of comb) {
|
|
@@ -461,9 +473,10 @@ export function move<T extends Item>({
|
|
|
461
473
|
// 锚点的父级
|
|
462
474
|
const anchorParent = isMoveUp ? start.parent : end.parent;
|
|
463
475
|
// 删除选中
|
|
464
|
-
anchorParent!.
|
|
476
|
+
anchorParent![childrenKey]!.splice(start.index!, len);
|
|
465
477
|
// 锚点
|
|
466
|
-
const anchorIndex =
|
|
478
|
+
const anchorIndex =
|
|
479
|
+
anchorParent?.[childrenKey]?.indexOf(changePosition);
|
|
467
480
|
|
|
468
481
|
const arr = [[selected]!, changePosition];
|
|
469
482
|
|
|
@@ -471,7 +484,7 @@ export function move<T extends Item>({
|
|
|
471
484
|
if (!isMoveUp) {
|
|
472
485
|
arr.reverse();
|
|
473
486
|
}
|
|
474
|
-
anchorParent!.
|
|
487
|
+
anchorParent![childrenKey]!.splice(anchorIndex, 1, ...arr.flat(2));
|
|
475
488
|
onMoved?.(selected);
|
|
476
489
|
} else {
|
|
477
490
|
if (onError?.({isMoveUp: Boolean(isMoveUp), selected}) === false) {
|
|
@@ -517,6 +530,7 @@ export async function upgrade<T extends Item>({
|
|
|
517
530
|
}) => Promise<boolean> | boolean;
|
|
518
531
|
onUpgraded?: (upgrades: T[]) => void;
|
|
519
532
|
}) {
|
|
533
|
+
data = optimizeStructure({childrenKey, data, signKey});
|
|
520
534
|
const layers = mergeAdjacent({childrenKey, data, selectKeys, signKey});
|
|
521
535
|
const {crossLayerSort, getInsertIdx} = generatedAcrossHierarchySort({
|
|
522
536
|
signKey,
|
|
@@ -535,14 +549,14 @@ export async function upgrade<T extends Item>({
|
|
|
535
549
|
const parentRel = _simpleGetRel({
|
|
536
550
|
childrenKey,
|
|
537
551
|
data,
|
|
538
|
-
key: parent
|
|
552
|
+
key: parent![signKey],
|
|
539
553
|
signKey,
|
|
540
554
|
});
|
|
541
555
|
const {parent: grandpa, index: parentIndex} = parentRel;
|
|
542
556
|
if (onUpgrade && !(await onUpgrade({grandpa, parent, selected}))) {
|
|
543
557
|
continue;
|
|
544
558
|
}
|
|
545
|
-
// const originalOldPrtCdr = parent
|
|
559
|
+
// const originalOldPrtCdr = parent[childrenKey]?.slice(0, Infinity) || [];
|
|
546
560
|
let followChildren: T[];
|
|
547
561
|
/** 无法跟随的节点 */
|
|
548
562
|
const unableToFollow: T[] = [];
|
|
@@ -554,18 +568,18 @@ export async function upgrade<T extends Item>({
|
|
|
554
568
|
// 获取需要跟随 self 的节点所在的范围
|
|
555
569
|
const startIndex = anchor.index! + 1;
|
|
556
570
|
/** 跟随节点 */
|
|
557
|
-
followChildren = parent
|
|
571
|
+
followChildren = parent[childrenKey]!.slice(startIndex);
|
|
558
572
|
/** 中断处的索引 */
|
|
559
573
|
const breakOffIndex = followChildren.findIndex((i) => {
|
|
560
|
-
const selected = selectKeys.includes(i
|
|
574
|
+
const selected = selectKeys.includes(i[signKey]);
|
|
561
575
|
// 后续被选中的 || 不满足被跟随节点需要的节点类型
|
|
562
576
|
return selected || onStopFollow?.(i);
|
|
563
577
|
});
|
|
564
578
|
/** 跟随数量 */
|
|
565
579
|
const followCount =
|
|
566
|
-
breakOffIndex >= 0 ? breakOffIndex : parent
|
|
580
|
+
breakOffIndex >= 0 ? breakOffIndex : parent[childrenKey]!.length;
|
|
567
581
|
// 删除选中后的兄弟
|
|
568
|
-
followChildren = parent
|
|
582
|
+
followChildren = parent[childrenKey]!.splice(
|
|
569
583
|
start.index! + len,
|
|
570
584
|
followCount,
|
|
571
585
|
);
|
|
@@ -578,12 +592,12 @@ export async function upgrade<T extends Item>({
|
|
|
578
592
|
followChildren = [];
|
|
579
593
|
}
|
|
580
594
|
const self = anchor.self as Item;
|
|
581
|
-
self
|
|
582
|
-
parent
|
|
595
|
+
self[childrenKey] = [...(self[childrenKey] || []), ...followChildren];
|
|
596
|
+
parent[childrenKey]!.splice(start.index!, len, ...unableToFollow);
|
|
583
597
|
|
|
584
598
|
const list = crossLayerSort({
|
|
585
599
|
leader: start,
|
|
586
|
-
oldParentId: parent
|
|
600
|
+
oldParentId: parent[signKey],
|
|
587
601
|
selectSize: len,
|
|
588
602
|
});
|
|
589
603
|
|
|
@@ -596,7 +610,7 @@ export async function upgrade<T extends Item>({
|
|
|
596
610
|
list,
|
|
597
611
|
oldParentIndex: parentIndex!,
|
|
598
612
|
});
|
|
599
|
-
insert(grandpa
|
|
613
|
+
insert(grandpa![childrenKey]!, insertIndex);
|
|
600
614
|
}
|
|
601
615
|
}
|
|
602
616
|
}
|
|
@@ -618,6 +632,7 @@ export async function downgrade<T extends Item>({
|
|
|
618
632
|
}) => Promise<boolean> | boolean;
|
|
619
633
|
onDowngraded?: (downgrades: T[]) => void;
|
|
620
634
|
}) {
|
|
635
|
+
data = optimizeStructure({childrenKey, data, signKey});
|
|
621
636
|
const layers = mergeAdjacent({childrenKey, data, selectKeys, signKey});
|
|
622
637
|
for (const comb of layers) {
|
|
623
638
|
for (const keys of comb) {
|