From 90fcdad3cdfac9fccfbd3c06ae5c0dc54833a522 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Wed, 10 Jul 2019 17:22:09 +0300 Subject: [PATCH] UX: Discard selected post if it is not in viewport. (#7869) This way, users can combine keyboard shortcuts with mouse scrolling. --- .../discourse/lib/keyboard-shortcuts.js.es6 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6 b/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6 index 11e7ca7604c..5e0a3996ea3 100644 --- a/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6 +++ b/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6 @@ -439,9 +439,22 @@ export default { $selected = $articles.filter("[data-islastviewedtopic=true]"); } + // Discard selection if it is not in viewport, so users can combine + // keyboard shortcuts with mouse scrolling. + if ($selected.length !== 0) { + const offset = minimumOffset(); + const beginScreen = $(window).scrollTop() - offset; + const endScreen = beginScreen + window.innerHeight + offset; + const beginArticle = $selected.offset().top; + const endArticle = $selected.offset().top + $selected.height(); + if (beginScreen > endArticle || beginArticle > endScreen) { + $selected = null; + } + } + // If still nothing is selected, select the first post that is // visible and cancel move operation. - if ($selected.length === 0) { + if (!$selected || $selected.length === 0) { const offset = minimumOffset(); $selected = $articles .toArray()