From 2d9db9ebc9471ad7760329241bbc84d2a8e460d3 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 29 May 2014 15:58:05 +1000 Subject: [PATCH] BUGFIX: j/k navigation was scrolling stuff in insane ways on first press --- .../keyboard_shortcuts_component.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/components/keyboard_shortcuts_component.js b/app/assets/javascripts/discourse/components/keyboard_shortcuts_component.js index 7d9210be0b3..0f89df1d6e8 100644 --- a/app/assets/javascripts/discourse/components/keyboard_shortcuts_component.js +++ b/app/assets/javascripts/discourse/components/keyboard_shortcuts_component.js @@ -133,11 +133,31 @@ Discourse.KeyboardShortcuts = Ember.Object.createWithMixins({ // loop is not allowed if (direction === -1 && index === 0) { return; } + // if nothing is selected go to the first post on screen + if ($selected.length === 0) { + var scrollTop = $('body').scrollTop(); + + index = 0; + $articles.each(function(){ + var top = $(this).position().top; + if(top > scrollTop) { + return false; + } + index += 1; + }); + + if(index >= $articles.length){ + index = $articles.length - 1; + } + } + var $article = $articles.eq(index + direction); if ($article.size() > 0) { $articles.removeClass('selected'); - $article.addClass('selected'); + Em.run.next(function(){ + $article.addClass('selected'); + }); var rgx = new RegExp("post-cloak-(\\d+)").exec($article.parent()[0].id); if (rgx === null || typeof rgx[1] === 'undefined') {