From 9e5e5d4078f9056dea9d15d3cb65a04edcd33c2d Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 16 Feb 2024 08:50:29 +1100 Subject: [PATCH] FEATURE: shift+j and shift+k will scroll entire posts (#25684) * FEATURE: shift+j and shift+k will scroll entire posts When scrolling through topics with very long posts we would like to use `shift+j` and `shift+k` to quickly move between posts. This allows users to bypass the scroll within post behavior when zooming through topics with keyboard shortcuts This overloads the behavior of shift+k and j which can be used to scroll through sections (new/latest/etc...) * remove useless tests These tests are testing nothing, no point carrying them around --- .../discourse/app/lib/keyboard-shortcuts.js | 20 ++++++++++++++----- .../tests/unit/lib/keyboard-shortcuts-test.js | 14 ------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js index a4ace99fe16..22dc109d1f9 100644 --- a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js +++ b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js @@ -403,11 +403,11 @@ export default { }, selectDown() { - this._moveSelection(1); + this._moveSelection({ direction: 1, scrollWithinPosts: true }); }, selectUp() { - this._moveSelection(-1); + this._moveSelection({ direction: -1, scrollWithinPosts: true }); }, goBack() { @@ -640,7 +640,7 @@ export default { } }, - _moveSelection(direction) { + _moveSelection({ direction, scrollWithinPosts }) { // Pressing a move key (J/K) very quick (i.e. keeping J or K pressed) will // move fast by disabling smooth page scrolling. const now = +new Date(); @@ -691,7 +691,7 @@ export default { let article = selected; // Try doing a page scroll in the context of current post. - if (!fast && direction !== 0 && article) { + if (!fast && direction !== 0 && article && scrollWithinPosts) { // The beginning of first article is the beginning of the page. const beginArticle = article.classList.contains("topic-post") && @@ -761,7 +761,12 @@ export default { const articleTop = domUtils.offset(article).top, articleTopPosition = articleTop - headerOffset(); - if (!fast && direction < 0 && article.offsetHeight > window.innerHeight) { + if ( + scrollWithinPosts && + !fast && + direction < 0 && + article.offsetHeight > window.innerHeight + ) { // Scrolling to the last "page" of the previous post if post has multiple // "pages" (if its height does not fit in the screen). return this._scrollTo( @@ -834,6 +839,11 @@ export default { if (index >= 0 && index < sections.length) { sections[index].querySelector("a")?.click(); } + + if (sections.length === 0) { + // use for in post navigation + this._moveSelection({ direction, scrollWithinPosts: false }); + } }, _stopCallback() { diff --git a/app/assets/javascripts/discourse/tests/unit/lib/keyboard-shortcuts-test.js b/app/assets/javascripts/discourse/tests/unit/lib/keyboard-shortcuts-test.js index 6b9d1870673..2546d61edb2 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/keyboard-shortcuts-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/keyboard-shortcuts-test.js @@ -11,20 +11,6 @@ module("Unit | Utility | keyboard-shortcuts", function (hooks) { sinon.stub(DiscourseURL, "routeTo"); }); - test("selectDown calls _moveSelection with 1", function (assert) { - let stub = sinon.stub(KeyboardShortcuts, "_moveSelection"); - - KeyboardShortcuts.selectDown(); - assert.ok(stub.calledWith(1), "_moveSelection is called with 1"); - }); - - test("selectUp calls _moveSelection with -1", function (assert) { - let stub = sinon.stub(KeyboardShortcuts, "_moveSelection"); - - KeyboardShortcuts.selectUp(); - assert.ok(stub.calledWith(-1), "_moveSelection is called with -1"); - }); - test("goBack calls history.back", function (assert) { let called = false; sinon.stub(history, "back").callsFake(function () {