@@ -50,6 +50,7 @@ class DoubleLinkedList {
5050 public length = 0 ;
5151 public head : DoubleLinkedListNode | null = null ;
5252 public tail : DoubleLinkedListNode | null = null ;
53+ public reordered = false ;
5354
5455 public get ( position : number ) {
5556 if ( position >= this . length ) {
@@ -129,6 +130,34 @@ class DoubleLinkedList {
129130 }
130131 this . length -- ;
131132 }
133+
134+ public needsReorder ( _node : DoubleLinkedListNode ) {
135+ if ( ! this . reordered &&
136+ _node . value . previousSibling &&
137+ isNodeInLinkedList ( _node . value . previousSibling ) &&
138+ _node . previous &&
139+ _node . previous . value !== _node . value . previousSibling ) {
140+ return true ;
141+ }
142+ return false ;
143+ }
144+
145+ public reorder ( ) {
146+ if ( this . reordered ) return false ;
147+ let current = this . tail ;
148+ const head = this . head ;
149+ while ( current ) {
150+ const prev = current . previous ;
151+ this . removeNode ( current . value ) ;
152+ this . addNode ( current . value ) ;
153+ if ( current === head ) {
154+ break ;
155+ }
156+ current = prev ;
157+ }
158+ this . reordered = true ;
159+ return true ;
160+ }
132161}
133162
134163const moveKey = ( id : number , parentId : number ) => `${ id } @${ parentId } ` ;
@@ -391,9 +420,13 @@ export default class MutationBuffer {
391420 const parentId = this . mirror . getId ( _node . value . parentNode ) ;
392421 const nextId = getNextId ( _node . value ) ;
393422
394- if ( nextId === - 1 ) continue ;
395- // nextId !== -1 && parentId !== -1
396- else if ( parentId !== - 1 ) {
423+ if ( nextId === - 1 ) {
424+ if ( addList . needsReorder ( _node ) && addList . reorder ( ) ) {
425+ tailNode = addList . tail ;
426+ }
427+ continue ;
428+ } else if ( parentId !== - 1 ) {
429+ // nextId !== -1 && parentId !== -1
397430 node = _node ;
398431 break ;
399432 }
0 commit comments