5
5
checkObject ,
6
6
cloneValue ,
7
7
getKey ,
8
- swapElements ,
8
+ swapElements1 ,
9
+ swapElements2 ,
9
10
getDataFunctions
10
11
} from "../../../shared/utils" ;
11
12
import {
@@ -239,6 +240,7 @@ export class Each extends DataComponent {
239
240
nodeNext = document . createComment ( "" ) ;
240
241
parentNode . appendChild ( nodeNext ) ;
241
242
}
243
+ currentComponent . nodes = new Array ( newDataLength ) ;
242
244
if ( this . isIteration ) {
243
245
for ( let i = 0 ; i < newDataLength ; i ++ ) {
244
246
const indexData = data [ i ] ;
@@ -254,7 +256,7 @@ export class Each extends DataComponent {
254
256
importData ,
255
257
newKey
256
258
) ;
257
- currentComponent . nodes . push ( currentNode ) ;
259
+ currentComponent . nodes [ i ] = currentNode ;
258
260
parentNode . insertBefore ( el , nodeNext ) ;
259
261
}
260
262
} else {
@@ -271,7 +273,7 @@ export class Each extends DataComponent {
271
273
importData ,
272
274
newKey
273
275
) ;
274
- currentComponent . nodes . push ( currentNode ) ;
276
+ currentComponent . nodes [ i ] = currentNode ;
275
277
parentNode . insertBefore ( el , nodeNext ) ;
276
278
}
277
279
}
@@ -365,13 +367,18 @@ export class Each extends DataComponent {
365
367
importData ,
366
368
oldData
367
369
) ;
370
+ const elNext = newData [ currentNewLastIndex + 1 ] ;
368
371
const el1 = ( newData [ newFirstIndex ] =
369
372
oldNodes [ currentOldLastIndex ] ) . el as Element ;
370
373
const el2 = ( newData [ currentNewLastIndex ] =
371
374
oldNodes [ oldFirstIndex ++ ] ) . el as Element ;
372
375
el1 [ EACH_INDEX_NAME ] = newFirstIndex ++ ;
373
376
el2 [ EACH_INDEX_NAME ] = currentNewLastIndex ;
374
- swapElements ( el1 , el2 , parentNode ) ;
377
+ if ( elNext ) {
378
+ swapElements1 ( el1 , el2 , elNext . el , parentNode ) ;
379
+ } else {
380
+ swapElements2 ( el1 , el2 , parentNode ) ;
381
+ }
375
382
newLastIndex -- ;
376
383
oldLastIndex -- ;
377
384
continue ;
@@ -421,14 +428,19 @@ export class Each extends DataComponent {
421
428
( nodeNext as ChildNode ) . remove ( ) ;
422
429
}
423
430
} else if ( isRemove ) {
424
- for (
425
- let i = oldFirstIndex ;
426
- oldFirstIndex < oldLastIndex -- ;
427
- i ++
428
- ) {
429
- const currentNode = oldNodes [ i ] ;
430
- const { el } = currentNode ;
431
- ( el as ChildNode ) . remove ( ) ;
431
+ if ( oldLastIndex - oldFirstIndex === 1 ) {
432
+ const currentNode = oldNodes [ oldFirstIndex ] . el ;
433
+ ( currentNode as ChildNode ) . remove ( ) ;
434
+ } else {
435
+ for (
436
+ let i = oldFirstIndex ;
437
+ oldFirstIndex < oldLastIndex -- ;
438
+ i ++
439
+ ) {
440
+ const currentNode = oldNodes [ i ] ;
441
+ const { el } = currentNode ;
442
+ ( el as ChildNode ) . remove ( ) ;
443
+ }
432
444
}
433
445
} else {
434
446
const indexesOldArr = { } ;
@@ -533,13 +545,18 @@ export class Each extends DataComponent {
533
545
importData ,
534
546
oldData
535
547
) ;
548
+ const elNext = newData [ currentNewLastIndex + 1 ] ;
536
549
const el1 = ( newData [ newFirstIndex ] =
537
550
oldNodes [ currentOldLastIndex ] ) . el as Element ;
538
551
const el2 = ( newData [ currentNewLastIndex ] =
539
552
oldNodes [ oldFirstIndex ++ ] ) . el as Element ;
540
553
el1 [ EACH_INDEX_NAME ] = newFirstIndex ++ ;
541
554
el2 [ EACH_INDEX_NAME ] = currentNewLastIndex ;
542
- swapElements ( el1 , el2 , parentNode ) ;
555
+ if ( elNext ) {
556
+ swapElements1 ( el1 , el2 , elNext . el , parentNode ) ;
557
+ } else {
558
+ swapElements2 ( el1 , el2 , parentNode ) ;
559
+ }
543
560
newLastIndex -- ;
544
561
oldLastIndex -- ;
545
562
continue ;
@@ -621,16 +638,24 @@ export class Each extends DataComponent {
621
638
( nodeNext as ChildNode ) . remove ( ) ;
622
639
}
623
640
} else {
624
- for (
625
- let i = oldFirstIndex ;
626
- oldFirstIndex < oldLastIndex -- ;
627
- i ++
628
- ) {
629
- const currentNode = oldNodes [ i ] ;
641
+ if ( oldLastIndex - oldFirstIndex === 1 ) {
642
+ const currentNode = oldNodes [ oldFirstIndex ] ;
630
643
if ( currentNode !== undefined ) {
631
644
const { el } = currentNode ;
632
645
( el as ChildNode ) . remove ( ) ;
633
646
}
647
+ } else {
648
+ for (
649
+ let i = oldFirstIndex ;
650
+ oldFirstIndex < oldLastIndex -- ;
651
+ i ++
652
+ ) {
653
+ const currentNode = oldNodes [ i ] ;
654
+ if ( currentNode !== undefined ) {
655
+ const { el } = currentNode ;
656
+ ( el as ChildNode ) . remove ( ) ;
657
+ }
658
+ }
634
659
}
635
660
}
636
661
}
0 commit comments