55 checkObject ,
66 cloneValue ,
77 getKey ,
8- swapElements ,
8+ swapElements1 ,
9+ swapElements2 ,
910 getDataFunctions
1011} from "../../../shared/utils" ;
1112import {
@@ -239,6 +240,7 @@ export class Each extends DataComponent {
239240 nodeNext = document . createComment ( "" ) ;
240241 parentNode . appendChild ( nodeNext ) ;
241242 }
243+ currentComponent . nodes = new Array ( newDataLength ) ;
242244 if ( this . isIteration ) {
243245 for ( let i = 0 ; i < newDataLength ; i ++ ) {
244246 const indexData = data [ i ] ;
@@ -254,7 +256,7 @@ export class Each extends DataComponent {
254256 importData ,
255257 newKey
256258 ) ;
257- currentComponent . nodes . push ( currentNode ) ;
259+ currentComponent . nodes [ i ] = currentNode ;
258260 parentNode . insertBefore ( el , nodeNext ) ;
259261 }
260262 } else {
@@ -271,7 +273,7 @@ export class Each extends DataComponent {
271273 importData ,
272274 newKey
273275 ) ;
274- currentComponent . nodes . push ( currentNode ) ;
276+ currentComponent . nodes [ i ] = currentNode ;
275277 parentNode . insertBefore ( el , nodeNext ) ;
276278 }
277279 }
@@ -365,13 +367,18 @@ export class Each extends DataComponent {
365367 importData ,
366368 oldData
367369 ) ;
370+ const elNext = newData [ currentNewLastIndex + 1 ] ;
368371 const el1 = ( newData [ newFirstIndex ] =
369372 oldNodes [ currentOldLastIndex ] ) . el as Element ;
370373 const el2 = ( newData [ currentNewLastIndex ] =
371374 oldNodes [ oldFirstIndex ++ ] ) . el as Element ;
372375 el1 [ EACH_INDEX_NAME ] = newFirstIndex ++ ;
373376 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+ }
375382 newLastIndex -- ;
376383 oldLastIndex -- ;
377384 continue ;
@@ -421,14 +428,19 @@ export class Each extends DataComponent {
421428 ( nodeNext as ChildNode ) . remove ( ) ;
422429 }
423430 } 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+ }
432444 }
433445 } else {
434446 const indexesOldArr = { } ;
@@ -533,13 +545,18 @@ export class Each extends DataComponent {
533545 importData ,
534546 oldData
535547 ) ;
548+ const elNext = newData [ currentNewLastIndex + 1 ] ;
536549 const el1 = ( newData [ newFirstIndex ] =
537550 oldNodes [ currentOldLastIndex ] ) . el as Element ;
538551 const el2 = ( newData [ currentNewLastIndex ] =
539552 oldNodes [ oldFirstIndex ++ ] ) . el as Element ;
540553 el1 [ EACH_INDEX_NAME ] = newFirstIndex ++ ;
541554 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+ }
543560 newLastIndex -- ;
544561 oldLastIndex -- ;
545562 continue ;
@@ -621,16 +638,24 @@ export class Each extends DataComponent {
621638 ( nodeNext as ChildNode ) . remove ( ) ;
622639 }
623640 } 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 ] ;
630643 if ( currentNode !== undefined ) {
631644 const { el } = currentNode ;
632645 ( el as ChildNode ) . remove ( ) ;
633646 }
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+ }
634659 }
635660 }
636661 }
0 commit comments