A platform for community discussion. Free, open, simple.
DEV: Fix post highlighting for keyboard navigation in the Glimmer Post Stream (#33097)
Sérgio Saquetim4fc7e4b1c94e
diff --git a/app/assets/javascripts/discourse/app/components/post-stream.gjs b/app/assets/javascripts/discourse/app/components/post-stream.gjsindex f4e9836c809..a6b06a75f3a 100644--- a/app/assets/javascripts/discourse/app/components/post-stream.gjs+++ b/app/assets/javascripts/discourse/app/components/post-stream.gjs@@ -2,12 +2,13 @@ import Component from "@glimmer/component";import { cached, tracked } from "@glimmer/tracking";import { concat, fn, get, hash } from "@ember/helper";import { action } from "@ember/object";-import { schedule } from "@ember/runloop";+import { next, schedule } from "@ember/runloop";import { service } from "@ember/service";-import { and, not } from "truth-helpers";+import { and, eq, not } from "truth-helpers";import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";import LoadMore from "discourse/components/load-more";import PostFilteredNotice from "discourse/components/post/filtered-notice";+import concatClass from "discourse/helpers/concat-class";import { bind } from "discourse/lib/decorators";import offsetCalculator from "discourse/lib/offset-calculator";import { Placeholder } from "discourse/lib/posts-with-placeholders";@@ -32,12 +33,21 @@ export default class PostStream extends Component {@tracked cloakAbove;@tracked cloakBelow;+ @tracked keyboardSelectedPostNumber;viewportTracker = new PostStreamViewportTracker();+ constructor() {+ super(...arguments);++ this.#setupEventListeners();+ }+willDestroy() {super.willDestroy(...arguments);+ // clear event listeners+ this.#setupEventListeners(false);// clear pending references in the observerthis.viewportTracker.destroy();}@@ -192,6 +202,34 @@ export default class PostStream extends Component {});}+ @action+ updateKeyboardSelectedPostNumber({ selectedArticle: element }) {+ next(() => {+ this.keyboardSelectedPostNumber = parseInt(+ element.dataset.postNumber,+ 10+ );+ });+ }++ #setupEventListeners(addListeners = true) {+ if (!addListeners) {+ this.appEvents.off(+ "keyboard:move-selection",+ this,+ this.updateKeyboardSelectedPostNumber+ );++ return;+ }++ this.appEvents.on(+ "keyboard:move-selection",+ this,+ this.updateKeyboardSelectedPostNumber+ );+ }+<template><ConditionalLoadingSpinner @condition={{@postStream.loadingAbove}} /><div@@ -242,7 +280,13 @@ export default class PostStream extends Component {}}<PostComponentid={{concat "post_" post.post_number}}- class={{if cloakingData.active "post-stream--cloaked"}}+ class={{concatClass+ (if cloakingData.active "post-stream--cloaked")+ (if+ (eq this.keyboardSelectedPostNumber post.post_number)+ "selected"+ )+ }}style={{cloakingData.style}}@cloaked={{cloakingData.active}}@post={{post}}diff --git a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.jsindex 451ff1eb906..59d13fb0a3e 100644--- a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js+++ b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js@@ -14,6 +14,7 @@ import Composer from "discourse/models/composer";import { capabilities } from "discourse/services/capabilities";let disabledBindings = [];+export function disableDefaultKeyboardShortcuts(bindings) {disabledBindings = disabledBindings.concat(bindings);}@@ -23,6 +24,7 @@ export function clearDisabledDefaultKeyboardBindings() {}let extraKeyboardShortcutsHelp = {};+function addExtraKeyboardShortcutHelp(help) {const category = help.category;if (extraKeyboardShortcutsHelp[category]) {@@ -776,6 +778,11 @@ export default {}}+ this.appEvents.trigger("keyboard:move-selection", {+ articles,+ selectedArticle: article,+ });+for (const a of articles) {a.classList.remove("selected");a.removeAttribute("tabindex");@@ -784,11 +791,6 @@ export default {article.setAttribute("tabindex", "0");article.focus();- this.appEvents.trigger("keyboard:move-selection", {- articles,- selectedArticle: article,- });-const articleTop = domUtils.offset(article).top,articleTopPosition = articleTop - headerOffset();if (@@ -850,7 +852,7 @@ export default {let categoriesTopicsList;if (document.querySelector(".posts-wrapper")) {return document.querySelectorAll(- ".posts-wrapper .topic-post, .topic-list tbody tr"+ ".posts-wrapper .topic-post, .posts-wrapper .post-stream--cloaked, .topic-list tbody tr");} else if (document.querySelector(".topic-list")) {return document.querySelectorAll(".topic-list .topic-list-item");diff --git a/app/assets/stylesheets/common/components/keyboard_shortcuts.scss b/app/assets/stylesheets/common/components/keyboard_shortcuts.scssindex 5f82069e82f..5c6142be0a9 100644--- a/app/assets/stylesheets/common/components/keyboard_shortcuts.scss+++ b/app/assets/stylesheets/common/components/keyboard_shortcuts.scss@@ -18,7 +18,8 @@.topic-list tr.selected,.topic-list-item.selected,.featured-topic.selected,-.topic-post.selected,+.topic-post,+.post-stream--cloaked,.latest-topic-list-item.selected,.search-results .fps-result.selected {&:focus-visible {