diff --git a/src/wp-includes/js/media-views.js b/src/wp-includes/js/media-views.js index 808c8d0759..c1ad5a99cb 100644 --- a/src/wp-includes/js/media-views.js +++ b/src/wp-includes/js/media-views.js @@ -5253,7 +5253,6 @@ this.collection.on( 'reset', this.render, this ); this.listenTo( this.controller, 'library:selection:add', this.attachmentFocus ); - this.listenTo( this.controller, 'attachment:keydown:arrow', this.arrowEvent ); // Throttle the scroll handler and bind this. this.scroll = _.chain( this.scroll ).bind( this ).throttle( this.options.refreshSensitivity ).value(); @@ -5281,10 +5280,14 @@ arrowEvent: function( event ) { var attachments = this.$el.children( 'li' ), - perRow = Math.round( this.$el.width() / attachments.first().outerWidth() ), + perRow = this.$el.data( 'columns' ), index = attachments.filter( ':focus' ).index(), row = ( index + 1 ) <= perRow ? 1 : Math.ceil( ( index + 1 ) / perRow ); + if ( index === -1 ) { + return; + } + // Left arrow if ( 37 === event.keyCode ) { if ( 0 === index ) { @@ -6001,6 +6004,9 @@ AttachmentView: this.options.AttachmentView }); + // Add keydown listener to the instance of the Attachments view + this.attachments.listenTo( this.controller, 'attachment:keydown:arrow', this.attachments.arrowEvent ); + this.views.add( this.attachments );