diff --git a/spec/system/jtech_theme_part1_spec.rb b/spec/system/jtech_theme_part1_spec.rb index 032edb0d..fe514c47 100644 --- a/spec/system/jtech_theme_part1_spec.rb +++ b/spec/system/jtech_theme_part1_spec.rb @@ -365,6 +365,22 @@ end end + # The title's words sit in a span inside the link; a click on them loaded + # the whole page again instead of opening the topic in the app + it "opens a topic from its card's title without reloading the page" do + sign_in(member) + visit("/latest") + expect(page).to have_css(".jt-card") + page.execute_script("window.__jtSamePage = true") + link = find(".jt-card .raw-topic-link", text: topic.title, match: :first) + # core's current TopicLink wraps the words in a span; older ones don't + (link.has_css?("span", wait: 0) ? link.find("span", match: :first) : link).click + expect(page).to have_css("#post_1") + expect(page).to have_current_path(%r{/t/}) + expect(page.evaluate_script("window.__jtSamePage === true")).to eq(true) + expect_no_theme_errors + end + # "Skip to main content", the first Tab on a page, was core's square chip # jammed against the window's top edge, over the logo it "rounds the skip link and keeps it off the window's edge" do diff --git a/themes/jtech/javascripts/discourse/api-initializers/jt-topic-cards.gts b/themes/jtech/javascripts/discourse/api-initializers/jt-topic-cards.gts index 5fea57ce..75024227 100644 --- a/themes/jtech/javascripts/discourse/api-initializers/jt-topic-cards.gts +++ b/themes/jtech/javascripts/discourse/api-initializers/jt-topic-cards.gts @@ -4,6 +4,7 @@ import { settings } from "virtual:theme"; import HeaderTopicCell from "discourse/components/topic-list/header/topic-cell"; import { apiInitializer } from "discourse/lib/api"; import type DAG from "discourse/lib/dag"; +import { wantsNewWindow } from "discourse/lib/intercept-click"; import type Category from "discourse/models/category"; import type Topic from "discourse/models/topic"; import JtTopicCard, { @@ -45,6 +46,8 @@ interface TopicListTransformerArgs { interface TopicListItemClickContext { event: MouseEvent & { target: Element }; + topic: Topic; + navigateToTopic: (topic: Topic, href: string) => void; } // Discovery lists (latest/new/top/category/tag, group + user activity), the @@ -136,6 +139,21 @@ export default apiInitializer((api) => { if (window.getSelection()?.toString()) { return; } + // The title's text sits in a span inside the link. Core opens the topic + // in the app only when the click lands on the link itself, and its + // click interceptor leaves raw links to the row; so a click on the + // words loaded the whole page again, every time. + const own = event.target.closest( + "a.raw-topic-link, a.post-activity, a.badge-posts" + ); + if (own && own !== event.target) { + if (wantsNewWindow(event)) { + return; + } + event.preventDefault(); + context.navigateToTopic(context.topic, own.href); + return; + } if ( (event.target.closest("a, button, input, label") && !event.target.closest(".topic-excerpt")) ||