@@ -83,6 +83,8 @@ export function countChanges(changes: ICellDiffInfo[]): number {
83
83
}
84
84
85
85
export function sortCellChanges ( changes : ICellDiffInfo [ ] ) : ICellDiffInfo [ ] {
86
+ const indexes = new Map < ICellDiffInfo , number > ( ) ;
87
+ changes . forEach ( ( c , i ) => indexes . set ( c , i ) ) ;
86
88
return [ ...changes ] . sort ( ( a , b ) => {
87
89
// For unchanged and modified, use modifiedCellIndex
88
90
if ( ( a . type === 'unchanged' || a . type === 'modified' ) &&
@@ -101,10 +103,22 @@ export function sortCellChanges(changes: ICellDiffInfo[]): ICellDiffInfo[] {
101
103
}
102
104
103
105
if ( a . type === 'delete' && b . type === 'insert' ) {
104
- return - 1 ;
106
+ // If the deleted cell comes before the inserted cell, we want the delete to come first
107
+ // As this means the cell was deleted before it was inserted
108
+ // We would like to see the deleted cell first in the list
109
+ // Else in the UI it would look weird to see an inserted cell before a deleted cell,
110
+ // When the users operation was to first delete the cell and then insert a new one
111
+ // I.e. this is merely just a simple way to ensure we have a stable sort.
112
+ return indexes . get ( a ) ! - indexes . get ( b ) ! ;
105
113
}
106
114
if ( a . type === 'insert' && b . type === 'delete' ) {
107
- return 1 ;
115
+ // If the deleted cell comes before the inserted cell, we want the delete to come first
116
+ // As this means the cell was deleted before it was inserted
117
+ // We would like to see the deleted cell first in the list
118
+ // Else in the UI it would look weird to see an inserted cell before a deleted cell,
119
+ // When the users operation was to first delete the cell and then insert a new one
120
+ // I.e. this is merely just a simple way to ensure we have a stable sort.
121
+ return indexes . get ( a ) ! - indexes . get ( b ) ! ;
108
122
}
109
123
110
124
if ( ( a . type === 'delete' && b . type !== 'insert' ) || ( a . type !== 'insert' && b . type === 'delete' ) ) {
0 commit comments