From 41150fc0cdba2141aba77932d596e7f5e1559c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 9 Apr 2018 17:28:43 +0200 Subject: [PATCH] FIX: J/K keyboard shortcut misbehaving --- .../discourse/lib/keyboard-shortcuts.js.es6 | 47 +++++++------------ 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6 b/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6 index 766a77e0240..6a1ddbdf9f1 100644 --- a/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6 +++ b/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6 @@ -311,9 +311,7 @@ export default { _moveSelection(direction) { const $articles = this._findArticles(); - if (typeof $articles === 'undefined') { - return; - } + if (typeof $articles === 'undefined') return; const $selected = ($articles.filter('.selected').length !== 0) ? $articles.filter('.selected') @@ -322,27 +320,15 @@ export default { let index = $articles.index($selected); if ($selected.length !== 0) { - if (direction === -1 && index === 0) { return; } - if (direction === 1 && index === ($articles.length - 1) ) { return; } + if (direction === -1 && index === 0) return; + if (direction === 1 && index === $articles.length - 1) return; } - // if nothing is selected go to the first post on screen + // when nothing is selected if ($selected.length === 0) { - const scrollTop = $(document).scrollTop(); - - index = 0; - $articles.each(function() { - const top = $(this).position().top; - if (top >= scrollTop) { - return false; - } - index += 1; - }); - - if (index >= $articles.length) { - index = $articles.length - 1; - } - + // select the first post with its top visible + const offset = minimumOffset(); + index = $articles.toArray().findIndex(article => article.getBoundingClientRect().top > offset); direction = 0; } @@ -362,7 +348,11 @@ export default { }, _scrollToPost($article) { - $(window).scrollTop($article.offset().top - minimumOffset()); + if ($article.find("#post_1").length > 0) { + $(window).scrollTop(0); + } else { + $(window).scrollTop($article.offset().top - minimumOffset()); + } }, _scrollList($article) { @@ -390,14 +380,13 @@ export default { _findArticles() { - const $topicList = $('.topic-list'), - $topicArea = $('.posts-wrapper'); + const $topicList = $(".topic-list"); + const $postsWrapper = $(".posts-wrapper"); - if ($topicArea.length > 0) { - return $('.posts-wrapper .topic-post, .topic-list tbody tr'); - } - else if ($topicList.length > 0) { - return $topicList.find('.topic-list-item'); + if ($postsWrapper.length > 0) { + return $(".posts-wrapper .topic-post, .topic-list tbody tr"); + } else if ($topicList.length > 0) { + return $topicList.find(".topic-list-item"); } },