onchain-utility 0.0.5 → 0.0.7

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.
@@ -1113,7 +1113,8 @@ function loopDescendants({
1113
1113
  /** 数组对象扁平化 */
1114
1114
  function arrayAttributeFlat({
1115
1115
  data,
1116
- childrenKey = 'children'
1116
+ childrenKey = 'children',
1117
+ getChildren
1117
1118
  }, fn) {
1118
1119
  const stack = [...data];
1119
1120
  const array = [];
@@ -1121,7 +1122,7 @@ function arrayAttributeFlat({
1121
1122
  let index = 0;
1122
1123
  while (stack.length) {
1123
1124
  const item = stack.shift();
1124
- const children = item ? item[childrenKey] : [];
1125
+ const children = item ? getChildren ? getChildren(item) : item[childrenKey] : [];
1125
1126
  fn?.(item, index, parentMap.get(item));
1126
1127
  array.push(item);
1127
1128
  index++;
@@ -1355,6 +1356,7 @@ function move({
1355
1356
  isMoveUp,
1356
1357
  selectKeys,
1357
1358
  childrenKey = 'children',
1359
+ onMove,
1358
1360
  onMoved,
1359
1361
  onError
1360
1362
  }) {
@@ -1377,7 +1379,12 @@ function move({
1377
1379
  const [start] = relArr;
1378
1380
  const end = relArr[len - 1];
1379
1381
  const changePosition = isMoveUp ? start.beforeSibling.pop() : end.afterSibling.shift();
1380
- if (changePosition) {
1382
+ // 锚点的父级
1383
+ const anchorParent = isMoveUp ? start.parent : end.parent;
1384
+ if (changePosition && (!onMove || onMove({
1385
+ parent: anchorParent,
1386
+ selected
1387
+ }))) {
1381
1388
  // 锚点的父级
1382
1389
  const anchorParent = isMoveUp ? start.parent : end.parent;
1383
1390
  // 删除选中
@@ -1454,10 +1461,7 @@ async function upgrade({
1454
1461
  const {
1455
1462
  parent
1456
1463
  } = start;
1457
- if (parent && (!onUpgrade || (await onUpgrade({
1458
- parent,
1459
- selected
1460
- })))) {
1464
+ if (parent) {
1461
1465
  const parentRel = _simpleGetRel({
1462
1466
  childrenKey,
1463
1467
  data,
@@ -1468,6 +1472,13 @@ async function upgrade({
1468
1472
  parent: grandpa,
1469
1473
  index: parentIndex
1470
1474
  } = parentRel;
1475
+ if (onUpgrade && !(await onUpgrade({
1476
+ grandpa,
1477
+ parent,
1478
+ selected
1479
+ }))) {
1480
+ continue;
1481
+ }
1471
1482
  // const originalOldPrtCdr = parent.children?.slice(0, Infinity) || [];
1472
1483
  let followChildren;
1473
1484
  /** 无法跟随的节点 */
@@ -1111,7 +1111,8 @@ function loopDescendants({
1111
1111
  /** 数组对象扁平化 */
1112
1112
  function arrayAttributeFlat({
1113
1113
  data,
1114
- childrenKey = 'children'
1114
+ childrenKey = 'children',
1115
+ getChildren
1115
1116
  }, fn) {
1116
1117
  const stack = [...data];
1117
1118
  const array = [];
@@ -1119,7 +1120,7 @@ function arrayAttributeFlat({
1119
1120
  let index = 0;
1120
1121
  while (stack.length) {
1121
1122
  const item = stack.shift();
1122
- const children = item ? item[childrenKey] : [];
1123
+ const children = item ? getChildren ? getChildren(item) : item[childrenKey] : [];
1123
1124
  fn?.(item, index, parentMap.get(item));
1124
1125
  array.push(item);
1125
1126
  index++;
@@ -1353,6 +1354,7 @@ function move({
1353
1354
  isMoveUp,
1354
1355
  selectKeys,
1355
1356
  childrenKey = 'children',
1357
+ onMove,
1356
1358
  onMoved,
1357
1359
  onError
1358
1360
  }) {
@@ -1375,7 +1377,12 @@ function move({
1375
1377
  const [start] = relArr;
1376
1378
  const end = relArr[len - 1];
1377
1379
  const changePosition = isMoveUp ? start.beforeSibling.pop() : end.afterSibling.shift();
1378
- if (changePosition) {
1380
+ // 锚点的父级
1381
+ const anchorParent = isMoveUp ? start.parent : end.parent;
1382
+ if (changePosition && (!onMove || onMove({
1383
+ parent: anchorParent,
1384
+ selected
1385
+ }))) {
1379
1386
  // 锚点的父级
1380
1387
  const anchorParent = isMoveUp ? start.parent : end.parent;
1381
1388
  // 删除选中
@@ -1452,10 +1459,7 @@ async function upgrade({
1452
1459
  const {
1453
1460
  parent
1454
1461
  } = start;
1455
- if (parent && (!onUpgrade || (await onUpgrade({
1456
- parent,
1457
- selected
1458
- })))) {
1462
+ if (parent) {
1459
1463
  const parentRel = _simpleGetRel({
1460
1464
  childrenKey,
1461
1465
  data,
@@ -1466,6 +1470,13 @@ async function upgrade({
1466
1470
  parent: grandpa,
1467
1471
  index: parentIndex
1468
1472
  } = parentRel;
1473
+ if (onUpgrade && !(await onUpgrade({
1474
+ grandpa,
1475
+ parent,
1476
+ selected
1477
+ }))) {
1478
+ continue;
1479
+ }
1469
1480
  // const originalOldPrtCdr = parent.children?.slice(0, Infinity) || [];
1470
1481
  let followChildren;
1471
1482
  /** 无法跟随的节点 */
package/dist/Tree.js CHANGED
@@ -106,7 +106,8 @@ function loopDescendants({
106
106
  /** 数组对象扁平化 */
107
107
  function arrayAttributeFlat({
108
108
  data,
109
- childrenKey = 'children'
109
+ childrenKey = 'children',
110
+ getChildren
110
111
  }, fn) {
111
112
  const stack = [...data];
112
113
  const array = [];
@@ -114,7 +115,7 @@ function arrayAttributeFlat({
114
115
  let index = 0;
115
116
  while (stack.length) {
116
117
  const item = stack.shift();
117
- const children = item ? item[childrenKey] : [];
118
+ const children = item ? getChildren ? getChildren(item) : item[childrenKey] : [];
118
119
  fn?.(item, index, parentMap.get(item));
119
120
  array.push(item);
120
121
  index++;
@@ -348,6 +349,7 @@ function move({
348
349
  isMoveUp,
349
350
  selectKeys,
350
351
  childrenKey = 'children',
352
+ onMove,
351
353
  onMoved,
352
354
  onError
353
355
  }) {
@@ -370,7 +372,12 @@ function move({
370
372
  const [start] = relArr;
371
373
  const end = relArr[len - 1];
372
374
  const changePosition = isMoveUp ? start.beforeSibling.pop() : end.afterSibling.shift();
373
- if (changePosition) {
375
+ // 锚点的父级
376
+ const anchorParent = isMoveUp ? start.parent : end.parent;
377
+ if (changePosition && (!onMove || onMove({
378
+ parent: anchorParent,
379
+ selected
380
+ }))) {
374
381
  // 锚点的父级
375
382
  const anchorParent = isMoveUp ? start.parent : end.parent;
376
383
  // 删除选中
@@ -447,10 +454,7 @@ async function upgrade({
447
454
  const {
448
455
  parent
449
456
  } = start;
450
- if (parent && (!onUpgrade || (await onUpgrade({
451
- parent,
452
- selected
453
- })))) {
457
+ if (parent) {
454
458
  const parentRel = _simpleGetRel({
455
459
  childrenKey,
456
460
  data,
@@ -461,6 +465,13 @@ async function upgrade({
461
465
  parent: grandpa,
462
466
  index: parentIndex
463
467
  } = parentRel;
468
+ if (onUpgrade && !(await onUpgrade({
469
+ grandpa,
470
+ parent,
471
+ selected
472
+ }))) {
473
+ continue;
474
+ }
464
475
  // const originalOldPrtCdr = parent.children?.slice(0, Infinity) || [];
465
476
  let followChildren;
466
477
  /** 无法跟随的节点 */
package/dist/Tree.mjs CHANGED
@@ -104,7 +104,8 @@ function loopDescendants({
104
104
  /** 数组对象扁平化 */
105
105
  function arrayAttributeFlat({
106
106
  data,
107
- childrenKey = 'children'
107
+ childrenKey = 'children',
108
+ getChildren
108
109
  }, fn) {
109
110
  const stack = [...data];
110
111
  const array = [];
@@ -112,7 +113,7 @@ function arrayAttributeFlat({
112
113
  let index = 0;
113
114
  while (stack.length) {
114
115
  const item = stack.shift();
115
- const children = item ? item[childrenKey] : [];
116
+ const children = item ? getChildren ? getChildren(item) : item[childrenKey] : [];
116
117
  fn?.(item, index, parentMap.get(item));
117
118
  array.push(item);
118
119
  index++;
@@ -346,6 +347,7 @@ function move({
346
347
  isMoveUp,
347
348
  selectKeys,
348
349
  childrenKey = 'children',
350
+ onMove,
349
351
  onMoved,
350
352
  onError
351
353
  }) {
@@ -368,7 +370,12 @@ function move({
368
370
  const [start] = relArr;
369
371
  const end = relArr[len - 1];
370
372
  const changePosition = isMoveUp ? start.beforeSibling.pop() : end.afterSibling.shift();
371
- if (changePosition) {
373
+ // 锚点的父级
374
+ const anchorParent = isMoveUp ? start.parent : end.parent;
375
+ if (changePosition && (!onMove || onMove({
376
+ parent: anchorParent,
377
+ selected
378
+ }))) {
372
379
  // 锚点的父级
373
380
  const anchorParent = isMoveUp ? start.parent : end.parent;
374
381
  // 删除选中
@@ -445,10 +452,7 @@ async function upgrade({
445
452
  const {
446
453
  parent
447
454
  } = start;
448
- if (parent && (!onUpgrade || (await onUpgrade({
449
- parent,
450
- selected
451
- })))) {
455
+ if (parent) {
452
456
  const parentRel = _simpleGetRel({
453
457
  childrenKey,
454
458
  data,
@@ -459,6 +463,13 @@ async function upgrade({
459
463
  parent: grandpa,
460
464
  index: parentIndex
461
465
  } = parentRel;
466
+ if (onUpgrade && !(await onUpgrade({
467
+ grandpa,
468
+ parent,
469
+ selected
470
+ }))) {
471
+ continue;
472
+ }
462
473
  // const originalOldPrtCdr = parent.children?.slice(0, Infinity) || [];
463
474
  let followChildren;
464
475
  /** 无法跟随的节点 */
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.5",
5
+ "version": "0.0.7",
6
6
  "files": [
7
7
  "dist",
8
8
  "src"
package/src/Tree/index.ts CHANGED
@@ -137,7 +137,11 @@ function loopDescendants<T extends Item>(
137
137
 
138
138
  /** 数组对象扁平化 */
139
139
  export function arrayAttributeFlat<T extends Item>(
140
- {data, childrenKey = 'children'}: {data: T[]; childrenKey?: string},
140
+ {
141
+ data,
142
+ childrenKey = 'children',
143
+ getChildren,
144
+ }: {data: T[]; childrenKey?: string; getChildren?: (item: T) => T[]},
141
145
  fn?: (item: T, index: number, parent?: T) => void,
142
146
  ): T[] {
143
147
  const stack = [...data];
@@ -146,7 +150,11 @@ export function arrayAttributeFlat<T extends Item>(
146
150
  let index = 0;
147
151
  while (stack.length) {
148
152
  const item = stack.shift()!;
149
- const children = item ? (item as any)[childrenKey] : [];
153
+ const children = item
154
+ ? getChildren
155
+ ? getChildren(item)
156
+ : (item as any)[childrenKey]
157
+ : [];
150
158
  fn?.(item, index, parentMap.get(item));
151
159
  array.push(item);
152
160
  index++;
@@ -406,6 +414,7 @@ function _simpleGetRel<T extends Item>({
406
414
 
407
415
  type MoveParams<T> = Params<T> & {
408
416
  selectKeys: string[];
417
+ onMove?: (params: {selected: T[]; parent: T}) => boolean;
409
418
  onMoved?: (moves: T[]) => void;
410
419
  onError?: (params: {selected: T[]; isMoveUp?: boolean}) => boolean | void;
411
420
  };
@@ -416,6 +425,7 @@ export function move<T extends Item>({
416
425
  isMoveUp,
417
426
  selectKeys,
418
427
  childrenKey = 'children',
428
+ onMove,
419
429
  onMoved,
420
430
  onError,
421
431
  }: MoveParams<T> & {
@@ -435,7 +445,12 @@ export function move<T extends Item>({
435
445
  const changePosition = isMoveUp
436
446
  ? start.beforeSibling.pop()
437
447
  : end.afterSibling.shift();
438
- if (changePosition) {
448
+ // 锚点的父级
449
+ const anchorParent = isMoveUp ? start.parent : end.parent;
450
+ if (
451
+ changePosition &&
452
+ (!onMove || onMove({parent: anchorParent!, selected}))
453
+ ) {
439
454
  // 锚点的父级
440
455
  const anchorParent = isMoveUp ? start.parent : end.parent;
441
456
  // 删除选中
@@ -491,6 +506,7 @@ export async function upgrade<T extends Item>({
491
506
  onUpgrade?: (params: {
492
507
  selected: T[];
493
508
  parent: T;
509
+ grandpa?: T;
494
510
  }) => Promise<boolean> | boolean;
495
511
  onUpgraded?: (upgrades: T[]) => void;
496
512
  }) {
@@ -508,7 +524,7 @@ export async function upgrade<T extends Item>({
508
524
  const [start] = relArr;
509
525
  const end = relArr[len - 1];
510
526
  const {parent} = start;
511
- if (parent && (!onUpgrade || (await onUpgrade({parent, selected})))) {
527
+ if (parent) {
512
528
  const parentRel = _simpleGetRel({
513
529
  childrenKey,
514
530
  data,
@@ -516,6 +532,9 @@ export async function upgrade<T extends Item>({
516
532
  signKey,
517
533
  });
518
534
  const {parent: grandpa, index: parentIndex} = parentRel;
535
+ if (onUpgrade && !(await onUpgrade({grandpa, parent, selected}))) {
536
+ continue;
537
+ }
519
538
  // const originalOldPrtCdr = parent.children?.slice(0, Infinity) || [];
520
539
  let followChildren: T[];
521
540
  /** 无法跟随的节点 */