FIX: Respect the End key when last post is already rendered (#28524)

Given we are a single-page-app, we hijack the browser's default behavior
for keyboard shortcuts like End because on long topics (20+ posts) we
need to load the last post before really reaching the end of the page.

However, when the last post is already rendered, the End shortcut
currently does nothing, it takes the user to the start of the last post.
If that post is too long, the user will have to scroll down manually.

This change ensures that if the last post of a topic is already rendered
(whether it is in the viewport or not), pressing End will take the user
to the very bottom of the page.

Note for the reviewer: the test added here is for the general case, it
is too hard to test the case where the last post is already rendered,
that isn't covered here.
This commit is contained in:
Penar Musaraj 2024-08-26 14:39:30 -04:00 committed by GitHub
parent 27f08a5c28
commit fe3d82a44a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 4 deletions

View File

@ -38,7 +38,6 @@ import discourseLater from "discourse-common/lib/later";
import { deepMerge } from "discourse-common/lib/object";
import discourseComputed, { bind } from "discourse-common/utils/decorators";
import I18n from "discourse-i18n";
let customPostMessageCallbacks = {};
const RETRIES_ON_RATE_LIMIT = 4;
@ -931,12 +930,17 @@ export default class TopicController extends Controller.extend(
@action
jumpBottom() {
// When a topic only has one lengthy post
const jumpEnd = this.model.highest_post_number === 1 ? true : false;
const highestPostNumber = this.model.highest_post_number;
if (document.getElementById(`post_${highestPostNumber}`)) {
// Do nothing when the last post is already rendered.
// This ensures the browser handles keyboard shortcuts like End.
return;
}
DiscourseURL.routeTo(this.get("model.lastPostUrl"), {
skipIfOnScreen: false,
jumpEnd,
jumpEnd: false,
keepFilter: true,
});
}

View File

@ -86,4 +86,16 @@ describe "Topic page", type: :system do
expect(page).to have_no_css(".topic-admin-menu-content")
end
context "with End keyboard shortcut" do
fab!(:posts) { Fabricate.times(25, :post, topic: topic) }
it "loads last post" do
visit "/t/#{topic.slug}/#{topic.id}/1"
send_keys(:end)
expect(find("#post_#{topic.highest_post_number}")).to be_visible
end
end
end