@@ -937,7 +937,7 @@ define(function (require, exports, module) {
937937 // then alphabetically by folder name, then by full path
938938 StringMatch . multiFieldSort ( matches , { matchGoodness : 0 , folderName : 1 , label : 2 } ) ;
939939
940- const topMatches = matches . slice ( 0 , 5 ) ;
940+ const topMatches = matches . slice ( 0 , 15 ) ;
941941 _renderFolderSuggestions ( topMatches , $suggestions , $input ) ;
942942 }
943943
@@ -953,33 +953,36 @@ define(function (require, exports, module) {
953953 function _registerFolderDialogInputHandlers ( $input , $suggestions , $dlg ) {
954954 // keyboard navigation handler for arrow keys
955955 $input . on ( 'keydown' , function ( e ) {
956+ const isArrowDown = e . keyCode === 40 ;
957+ const isArrowUp = e . keyCode === 38 ;
958+ // we only want to handle the arrow up arrow down keys
959+ if ( ! isArrowDown && ! isArrowUp ) { return ; }
960+
961+ e . preventDefault ( ) ;
956962 const $items = $suggestions . find ( '.folder-suggestion-item' ) ;
957- const $selected = $items . filter ( '.selected' ) ;
963+ if ( $items . length === 0 ) { return ; }
958964
959- if ( e . keyCode === 40 ) { // arrow down key
960- e . preventDefault ( ) ;
961- if ( $items . length === 0 ) { return ; }
965+ const $selected = $items . filter ( '.selected' ) ;
962966
963- if ( $selected . length === 0 ) {
964- $items . first ( ) . addClass ( 'selected' ) ;
965- } else {
966- const currentIndex = $items . index ( $selected ) ;
967- $selected . removeClass ( 'selected' ) ;
968- const nextIndex = ( currentIndex + 1 ) % $items . length ;
969- $items . eq ( nextIndex ) . addClass ( 'selected' ) ;
970- }
971- } else if ( e . keyCode === 38 ) { // arrow up key
972- e . preventDefault ( ) ;
973- if ( $items . length === 0 ) { return ; }
967+ // determine which item to select next
968+ let $nextItem ;
969+ if ( $selected . length === 0 ) {
970+ // no selection - select first or last based on direction
971+ $nextItem = isArrowDown ? $items . first ( ) : $items . last ( ) ;
972+ } else {
973+ // move selection
974+ const currentIndex = $items . index ( $selected ) ;
975+ $selected . removeClass ( 'selected' ) ;
976+ const nextIndex = isArrowDown
977+ ? ( currentIndex + 1 ) % $items . length
978+ : ( currentIndex - 1 + $items . length ) % $items . length ;
979+ $nextItem = $items . eq ( nextIndex ) ;
980+ }
974981
975- if ( $selected . length === 0 ) {
976- $items . last ( ) . addClass ( 'selected' ) ;
977- } else {
978- const currentIndex = $items . index ( $selected ) ;
979- $selected . removeClass ( 'selected' ) ;
980- const nextIndex = ( currentIndex - 1 + $items . length ) % $items . length ;
981- $items . eq ( nextIndex ) . addClass ( 'selected' ) ;
982- }
982+ // apply selection and scroll the selected item into view (if not in view)
983+ $nextItem . addClass ( 'selected' ) ;
984+ if ( $nextItem . length > 0 ) {
985+ $nextItem [ 0 ] . scrollIntoView ( { block : "nearest" , behavior : "auto" } ) ;
983986 }
984987 } ) ;
985988
0 commit comments