@@ -161,16 +161,70 @@ class SortablePane extends Component {
161161 return this . isHorizontal ( ) ? width : height ;
162162 }
163163
164- getItemCountByPosition ( position ) {
164+ /**
165+ * Find the position sum of halfway points of panes surrounding a given pane
166+ *
167+ * |-------------|
168+ * | | ---> 'previous' halfway
169+ * |-------------|
170+ * <--- margin
171+ * |-------------|
172+ * | currentPane |
173+ * |-------------|
174+ * <--- margin
175+ * |-------------|
176+ * | |
177+ * | | ---> 'next' halfway
178+ * | |
179+ * |-------------|
180+ *
181+ *
182+ * @param {number } currentPane - Index of rerference pane
183+ * @param {number[] } sizes - Array of pane sizes
184+ * @param {number } margin - The margin between panes
185+ * @return {object } - Object containing 'prevoius' and 'next'
186+ * pane halfway points
187+ */
188+ getSurroundingHalfSizes ( currentPane , sizes , margin ) {
189+ const nextPane = currentPane + 1 ;
190+ const prevPane = currentPane - 1 ;
191+
192+ return sizes . reduce ( ( sums , val , index ) => {
193+ const newSums = { } ;
194+ if ( index < prevPane ) {
195+ newSums . previous = sums . previous + val + margin ;
196+ } else if ( index === prevPane ) {
197+ newSums . previous = sums . previous + val / 2 ;
198+ } else {
199+ newSums . previous = sums . previous ;
200+ }
201+
202+ if ( index < nextPane ) {
203+ newSums . next = sums . next + val + margin ;
204+ } else if ( index === nextPane ) {
205+ newSums . next = sums . next + val / 2 ;
206+ } else {
207+ newSums . next = sums . next ;
208+ }
209+ return newSums ;
210+ } , { previous : 0 , next : 0 } ) ;
211+ }
212+
213+ /**
214+ * Determine where a particular pane should be ordered
215+ *
216+ * @param {number } position - Top of the current pane
217+ * @param {number } paneIndex - Index of the pane
218+ * @return {number } - New index of the pane based on position
219+ */
220+ getItemCountByPosition ( position , paneIndex ) {
165221 const size = this . getPaneSizeList ( ) ;
166222 const { margin } = this . props ;
167- let sum = 0 ;
168- if ( position < 0 ) return 0 ;
169- for ( let i = 0 ; i < size . length ; i ++ ) {
170- sum += size [ i ] + margin ;
171- if ( sum >= position ) return i + 1 ;
172- }
173- return size . length ;
223+ const halfsizes = this . getSurroundingHalfSizes ( paneIndex , size , margin ) ;
224+
225+ if ( position + size [ paneIndex ] > halfsizes . next ) return paneIndex + 1 ;
226+ if ( position < halfsizes . previous ) return paneIndex - 1 ;
227+ return paneIndex ;
174228 }
175229
176230 setSize ( ) {
@@ -263,7 +317,8 @@ class SortablePane extends Component {
263317 const mouse = this . isHorizontal ( ) ? pageX - delta : pageY - delta ;
264318 const { length } = this . props . children ;
265319 const order = this . getPanePropsArrayOf ( 'order' ) ;
266- const row = clamp ( Math . round ( this . getItemCountByPosition ( mouse ) ) , 0 , length - 1 ) ;
320+ const newPosition = this . getItemCountByPosition ( mouse , order . indexOf ( lastPressed ) ) ;
321+ const row = clamp ( Math . round ( newPosition ) , 0 , length - 1 ) ;
267322 const newPanes = reinsert ( panes , order . indexOf ( lastPressed ) , row ) ;
268323 this . setState ( { mouse, panes : newPanes } ) ;
269324 if ( ! isEqual ( panes , newPanes ) ) onOrderChange ( panes , newPanes ) ;
0 commit comments