@@ -67,11 +67,18 @@ export const calculateNewFocusedNodes = (selectionMode: SelectionModeTypes, cont
67
67
const lastSelectedNode = nodesByContextPath [ lastSelectedNodeContextPath ] ;
68
68
if ( lastSelectedNode && lastSelectedNode . parent ) {
69
69
const parentNode = getNodeOrThrow ( nodesByContextPath , lastSelectedNode . parent ) ;
70
- const tempSelection : string [ ] = [ ] ;
70
+ let tempSelection : string [ ] = [ ] ;
71
71
let startSelectionFlag = false ;
72
+ let startIndex = - 1 ;
73
+ let endIndex = - 1 ;
72
74
// if both start and end nodes are within children, then we can do range select
73
- const startAndEndOfSelectionAreOnOneLevel = parentNode . children . some ( child => {
75
+ const startAndEndOfSelectionAreOnOneLevel = parentNode . children . some ( ( child , index ) => {
74
76
if ( child . contextPath === lastSelectedNodeContextPath || child . contextPath === contextPath ) {
77
+ if ( child . contextPath === lastSelectedNodeContextPath ) {
78
+ startIndex = index ;
79
+ } else if ( child . contextPath === contextPath ) {
80
+ endIndex = index ;
81
+ }
75
82
if ( startSelectionFlag ) { // if matches for the second time it means that both start and end of selection were found
76
83
tempSelection . push ( child . contextPath ) ; // also push the last node
77
84
return true ;
@@ -84,6 +91,10 @@ export const calculateNewFocusedNodes = (selectionMode: SelectionModeTypes, cont
84
91
return false ;
85
92
} ) ;
86
93
if ( startAndEndOfSelectionAreOnOneLevel ) {
94
+ // Reverse the selection if the nodes were selected from "bottom" to "top" in the tree, or they would be out of order on insertion
95
+ if ( startIndex > endIndex ) {
96
+ tempSelection = tempSelection . reverse ( ) ;
97
+ }
87
98
const focusedNodesContextPathsSet = new Set ( focusedNodesContextPaths ) ;
88
99
tempSelection . forEach ( contextPath => focusedNodesContextPathsSet . add ( contextPath ) ) ;
89
100
return [ ...focusedNodesContextPathsSet ] as string [ ] ;
0 commit comments