onchain-utility 0.0.6 → 0.0.8

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.
@@ -1356,6 +1356,7 @@ function move({
1356
1356
  isMoveUp,
1357
1357
  selectKeys,
1358
1358
  childrenKey = 'children',
1359
+ onMove,
1359
1360
  onMoved,
1360
1361
  onError
1361
1362
  }) {
@@ -1378,7 +1379,12 @@ function move({
1378
1379
  const [start] = relArr;
1379
1380
  const end = relArr[len - 1];
1380
1381
  const changePosition = isMoveUp ? start.beforeSibling.pop() : end.afterSibling.shift();
1381
- if (changePosition) {
1382
+ // 锚点的父级
1383
+ const anchorParent = isMoveUp ? start.parent : end.parent;
1384
+ if (changePosition && (!onMove || onMove({
1385
+ parent: anchorParent,
1386
+ selected
1387
+ }))) {
1382
1388
  // 锚点的父级
1383
1389
  const anchorParent = isMoveUp ? start.parent : end.parent;
1384
1390
  // 删除选中
@@ -1455,10 +1461,7 @@ async function upgrade({
1455
1461
  const {
1456
1462
  parent
1457
1463
  } = start;
1458
- if (parent && (!onUpgrade || (await onUpgrade({
1459
- parent,
1460
- selected
1461
- })))) {
1464
+ if (parent) {
1462
1465
  const parentRel = _simpleGetRel({
1463
1466
  childrenKey,
1464
1467
  data,
@@ -1469,6 +1472,13 @@ async function upgrade({
1469
1472
  parent: grandpa,
1470
1473
  index: parentIndex
1471
1474
  } = parentRel;
1475
+ if (onUpgrade && !(await onUpgrade({
1476
+ grandpa,
1477
+ parent,
1478
+ selected
1479
+ }))) {
1480
+ continue;
1481
+ }
1472
1482
  // const originalOldPrtCdr = parent.children?.slice(0, Infinity) || [];
1473
1483
  let followChildren;
1474
1484
  /** 无法跟随的节点 */
@@ -1597,6 +1607,27 @@ function insert({
1597
1607
  }
1598
1608
  }
1599
1609
 
1610
+ /** 树结构搜索 */
1611
+ function treeSearch({
1612
+ data,
1613
+ searchValue,
1614
+ childrenKey = 'children'
1615
+ }) {
1616
+ return data.map(row => {
1617
+ const children = treeSearch({
1618
+ childrenKey,
1619
+ data: row[childrenKey] || [],
1620
+ searchValue
1621
+ });
1622
+ if (searchValue(row) || children.length) {
1623
+ return {
1624
+ ...row,
1625
+ [childrenKey]: children
1626
+ };
1627
+ }
1628
+ }).filter(row => !!row);
1629
+ }
1630
+
1600
1631
  exports._isMoment = _isMoment;
1601
1632
  exports.arrayAttributeFlat = arrayAttributeFlat;
1602
1633
  exports.asyncDfs = asyncDfs;
@@ -1630,6 +1661,7 @@ exports.t = t;
1630
1661
  exports.toRawType = toRawType;
1631
1662
  exports.toTypeString = toTypeString;
1632
1663
  exports.translateI18n = translateI18n;
1664
+ exports.treeSearch = treeSearch;
1633
1665
  exports.upgrade = upgrade;
1634
1666
  exports.useReactive = useReactive;
1635
1667
  exports.useShallowReactive = useShallowReactive;
@@ -1354,6 +1354,7 @@ function move({
1354
1354
  isMoveUp,
1355
1355
  selectKeys,
1356
1356
  childrenKey = 'children',
1357
+ onMove,
1357
1358
  onMoved,
1358
1359
  onError
1359
1360
  }) {
@@ -1376,7 +1377,12 @@ function move({
1376
1377
  const [start] = relArr;
1377
1378
  const end = relArr[len - 1];
1378
1379
  const changePosition = isMoveUp ? start.beforeSibling.pop() : end.afterSibling.shift();
1379
- if (changePosition) {
1380
+ // 锚点的父级
1381
+ const anchorParent = isMoveUp ? start.parent : end.parent;
1382
+ if (changePosition && (!onMove || onMove({
1383
+ parent: anchorParent,
1384
+ selected
1385
+ }))) {
1380
1386
  // 锚点的父级
1381
1387
  const anchorParent = isMoveUp ? start.parent : end.parent;
1382
1388
  // 删除选中
@@ -1453,10 +1459,7 @@ async function upgrade({
1453
1459
  const {
1454
1460
  parent
1455
1461
  } = start;
1456
- if (parent && (!onUpgrade || (await onUpgrade({
1457
- parent,
1458
- selected
1459
- })))) {
1462
+ if (parent) {
1460
1463
  const parentRel = _simpleGetRel({
1461
1464
  childrenKey,
1462
1465
  data,
@@ -1467,6 +1470,13 @@ async function upgrade({
1467
1470
  parent: grandpa,
1468
1471
  index: parentIndex
1469
1472
  } = parentRel;
1473
+ if (onUpgrade && !(await onUpgrade({
1474
+ grandpa,
1475
+ parent,
1476
+ selected
1477
+ }))) {
1478
+ continue;
1479
+ }
1470
1480
  // const originalOldPrtCdr = parent.children?.slice(0, Infinity) || [];
1471
1481
  let followChildren;
1472
1482
  /** 无法跟随的节点 */
@@ -1595,4 +1605,25 @@ function insert({
1595
1605
  }
1596
1606
  }
1597
1607
 
1598
- export { _isMoment, arrayAttributeFlat, asyncDfs, base64ToFile, base64ToMimeType, baseTemplate, bfs, createEnumObject, deleteTreeNodes, dfs, downgrade, generateSecureUUID, generatedAcrossHierarchySort, getBase64Regular, getHTMLTagString, getI18nText, getSiblings, getTreeRelation, hasOwnProperty, initTranslateI18nFn, insert, isEmptyObject, isIntegerKey, levelTransformTree, makeDestructurable, mergeAdjacent, movUp, move, moveDown, t, toRawType, toTypeString, translateI18n, upgrade, useReactive, useShallowReactive, useStore };
1608
+ /** 树结构搜索 */
1609
+ function treeSearch({
1610
+ data,
1611
+ searchValue,
1612
+ childrenKey = 'children'
1613
+ }) {
1614
+ return data.map(row => {
1615
+ const children = treeSearch({
1616
+ childrenKey,
1617
+ data: row[childrenKey] || [],
1618
+ searchValue
1619
+ });
1620
+ if (searchValue(row) || children.length) {
1621
+ return {
1622
+ ...row,
1623
+ [childrenKey]: children
1624
+ };
1625
+ }
1626
+ }).filter(row => !!row);
1627
+ }
1628
+
1629
+ export { _isMoment, arrayAttributeFlat, asyncDfs, base64ToFile, base64ToMimeType, baseTemplate, bfs, createEnumObject, deleteTreeNodes, dfs, downgrade, generateSecureUUID, generatedAcrossHierarchySort, getBase64Regular, getHTMLTagString, getI18nText, getSiblings, getTreeRelation, hasOwnProperty, initTranslateI18nFn, insert, isEmptyObject, isIntegerKey, levelTransformTree, makeDestructurable, mergeAdjacent, movUp, move, moveDown, t, toRawType, toTypeString, translateI18n, treeSearch, upgrade, useReactive, useShallowReactive, useStore };
package/dist/Tree.js CHANGED
@@ -349,6 +349,7 @@ function move({
349
349
  isMoveUp,
350
350
  selectKeys,
351
351
  childrenKey = 'children',
352
+ onMove,
352
353
  onMoved,
353
354
  onError
354
355
  }) {
@@ -371,7 +372,12 @@ function move({
371
372
  const [start] = relArr;
372
373
  const end = relArr[len - 1];
373
374
  const changePosition = isMoveUp ? start.beforeSibling.pop() : end.afterSibling.shift();
374
- if (changePosition) {
375
+ // 锚点的父级
376
+ const anchorParent = isMoveUp ? start.parent : end.parent;
377
+ if (changePosition && (!onMove || onMove({
378
+ parent: anchorParent,
379
+ selected
380
+ }))) {
375
381
  // 锚点的父级
376
382
  const anchorParent = isMoveUp ? start.parent : end.parent;
377
383
  // 删除选中
@@ -448,10 +454,7 @@ async function upgrade({
448
454
  const {
449
455
  parent
450
456
  } = start;
451
- if (parent && (!onUpgrade || (await onUpgrade({
452
- parent,
453
- selected
454
- })))) {
457
+ if (parent) {
455
458
  const parentRel = _simpleGetRel({
456
459
  childrenKey,
457
460
  data,
@@ -462,6 +465,13 @@ async function upgrade({
462
465
  parent: grandpa,
463
466
  index: parentIndex
464
467
  } = parentRel;
468
+ if (onUpgrade && !(await onUpgrade({
469
+ grandpa,
470
+ parent,
471
+ selected
472
+ }))) {
473
+ continue;
474
+ }
465
475
  // const originalOldPrtCdr = parent.children?.slice(0, Infinity) || [];
466
476
  let followChildren;
467
477
  /** 无法跟随的节点 */
@@ -590,6 +600,27 @@ function insert({
590
600
  }
591
601
  }
592
602
 
603
+ /** 树结构搜索 */
604
+ function treeSearch({
605
+ data,
606
+ searchValue,
607
+ childrenKey = 'children'
608
+ }) {
609
+ return data.map(row => {
610
+ const children = treeSearch({
611
+ childrenKey,
612
+ data: row[childrenKey] || [],
613
+ searchValue
614
+ });
615
+ if (searchValue(row) || children.length) {
616
+ return {
617
+ ...row,
618
+ [childrenKey]: children
619
+ };
620
+ }
621
+ }).filter(row => !!row);
622
+ }
623
+
593
624
  exports.arrayAttributeFlat = arrayAttributeFlat;
594
625
  exports.deleteTreeNodes = deleteTreeNodes;
595
626
  exports.downgrade = downgrade;
@@ -602,4 +633,5 @@ exports.mergeAdjacent = mergeAdjacent;
602
633
  exports.movUp = movUp;
603
634
  exports.move = move;
604
635
  exports.moveDown = moveDown;
636
+ exports.treeSearch = treeSearch;
605
637
  exports.upgrade = upgrade;
package/dist/Tree.mjs CHANGED
@@ -347,6 +347,7 @@ function move({
347
347
  isMoveUp,
348
348
  selectKeys,
349
349
  childrenKey = 'children',
350
+ onMove,
350
351
  onMoved,
351
352
  onError
352
353
  }) {
@@ -369,7 +370,12 @@ function move({
369
370
  const [start] = relArr;
370
371
  const end = relArr[len - 1];
371
372
  const changePosition = isMoveUp ? start.beforeSibling.pop() : end.afterSibling.shift();
372
- if (changePosition) {
373
+ // 锚点的父级
374
+ const anchorParent = isMoveUp ? start.parent : end.parent;
375
+ if (changePosition && (!onMove || onMove({
376
+ parent: anchorParent,
377
+ selected
378
+ }))) {
373
379
  // 锚点的父级
374
380
  const anchorParent = isMoveUp ? start.parent : end.parent;
375
381
  // 删除选中
@@ -446,10 +452,7 @@ async function upgrade({
446
452
  const {
447
453
  parent
448
454
  } = start;
449
- if (parent && (!onUpgrade || (await onUpgrade({
450
- parent,
451
- selected
452
- })))) {
455
+ if (parent) {
453
456
  const parentRel = _simpleGetRel({
454
457
  childrenKey,
455
458
  data,
@@ -460,6 +463,13 @@ async function upgrade({
460
463
  parent: grandpa,
461
464
  index: parentIndex
462
465
  } = parentRel;
466
+ if (onUpgrade && !(await onUpgrade({
467
+ grandpa,
468
+ parent,
469
+ selected
470
+ }))) {
471
+ continue;
472
+ }
463
473
  // const originalOldPrtCdr = parent.children?.slice(0, Infinity) || [];
464
474
  let followChildren;
465
475
  /** 无法跟随的节点 */
@@ -588,4 +598,25 @@ function insert({
588
598
  }
589
599
  }
590
600
 
591
- export { arrayAttributeFlat, deleteTreeNodes, downgrade, generatedAcrossHierarchySort, getSiblings, getTreeRelation, insert, levelTransformTree, mergeAdjacent, movUp, move, moveDown, upgrade };
601
+ /** 树结构搜索 */
602
+ function treeSearch({
603
+ data,
604
+ searchValue,
605
+ childrenKey = 'children'
606
+ }) {
607
+ return data.map(row => {
608
+ const children = treeSearch({
609
+ childrenKey,
610
+ data: row[childrenKey] || [],
611
+ searchValue
612
+ });
613
+ if (searchValue(row) || children.length) {
614
+ return {
615
+ ...row,
616
+ [childrenKey]: children
617
+ };
618
+ }
619
+ }).filter(row => !!row);
620
+ }
621
+
622
+ export { arrayAttributeFlat, deleteTreeNodes, downgrade, generatedAcrossHierarchySort, getSiblings, 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.6",
5
+ "version": "0.0.8",
6
6
  "files": [
7
7
  "dist",
8
8
  "src"
package/src/Tree/index.ts CHANGED
@@ -414,6 +414,7 @@ function _simpleGetRel<T extends Item>({
414
414
 
415
415
  type MoveParams<T> = Params<T> & {
416
416
  selectKeys: string[];
417
+ onMove?: (params: {selected: T[]; parent: T}) => boolean;
417
418
  onMoved?: (moves: T[]) => void;
418
419
  onError?: (params: {selected: T[]; isMoveUp?: boolean}) => boolean | void;
419
420
  };
@@ -424,6 +425,7 @@ export function move<T extends Item>({
424
425
  isMoveUp,
425
426
  selectKeys,
426
427
  childrenKey = 'children',
428
+ onMove,
427
429
  onMoved,
428
430
  onError,
429
431
  }: MoveParams<T> & {
@@ -443,7 +445,12 @@ export function move<T extends Item>({
443
445
  const changePosition = isMoveUp
444
446
  ? start.beforeSibling.pop()
445
447
  : end.afterSibling.shift();
446
- if (changePosition) {
448
+ // 锚点的父级
449
+ const anchorParent = isMoveUp ? start.parent : end.parent;
450
+ if (
451
+ changePosition &&
452
+ (!onMove || onMove({parent: anchorParent!, selected}))
453
+ ) {
447
454
  // 锚点的父级
448
455
  const anchorParent = isMoveUp ? start.parent : end.parent;
449
456
  // 删除选中
@@ -499,6 +506,7 @@ export async function upgrade<T extends Item>({
499
506
  onUpgrade?: (params: {
500
507
  selected: T[];
501
508
  parent: T;
509
+ grandpa?: T;
502
510
  }) => Promise<boolean> | boolean;
503
511
  onUpgraded?: (upgrades: T[]) => void;
504
512
  }) {
@@ -516,7 +524,7 @@ export async function upgrade<T extends Item>({
516
524
  const [start] = relArr;
517
525
  const end = relArr[len - 1];
518
526
  const {parent} = start;
519
- if (parent && (!onUpgrade || (await onUpgrade({parent, selected})))) {
527
+ if (parent) {
520
528
  const parentRel = _simpleGetRel({
521
529
  childrenKey,
522
530
  data,
@@ -524,6 +532,9 @@ export async function upgrade<T extends Item>({
524
532
  signKey,
525
533
  });
526
534
  const {parent: grandpa, index: parentIndex} = parentRel;
535
+ if (onUpgrade && !(await onUpgrade({grandpa, parent, selected}))) {
536
+ continue;
537
+ }
527
538
  // const originalOldPrtCdr = parent.children?.slice(0, Infinity) || [];
528
539
  let followChildren: T[];
529
540
  /** 无法跟随的节点 */
@@ -659,3 +670,26 @@ export function insert<T extends Item>({
659
670
  }
660
671
  }
661
672
  }
673
+
674
+ /** 树结构搜索 */
675
+ export function treeSearch<T extends Record<string, any>>({
676
+ data,
677
+ searchValue,
678
+ childrenKey = 'children',
679
+ }: {
680
+ data: T[];
681
+ searchValue: (item: T) => boolean;
682
+ childrenKey?: string;
683
+ }) {
684
+ return data
685
+ .map((row) => {
686
+ const children: T[] = treeSearch({ childrenKey, data: row[childrenKey] || [], searchValue });
687
+ if (searchValue(row) || children.length) {
688
+ return {
689
+ ...row,
690
+ [childrenKey]: children,
691
+ };
692
+ }
693
+ })
694
+ .filter((row): row is T => !!row);
695
+ }