Skip to content

Commit 8657f64

Browse files
authored
DEV: Update heading positions when navigating directly between topics (#92)
1 parent f2b78d4 commit 8657f64

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

javascripts/discourse/components/toc-contents.gjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Component from "@glimmer/component";
22
import { tracked } from "@glimmer/tracking";
33
import { action } from "@ember/object";
44
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
5+
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
56
import { service } from "@ember/service";
67
import { headerOffset } from "discourse/lib/offset-calculator";
78
import { debounce } from "discourse-common/utils/decorators";
@@ -154,7 +155,11 @@ export default class TocContents extends Component {
154155
{{#unless @renderTimeline}}
155156
<TocMiniButtons @renderTimeline={{@renderTimeline}} @postID={{@postID}} />
156157
{{/unless}}
157-
<div id="d-toc" {{didInsert this.setup}}>
158+
<div
159+
id="d-toc"
160+
{{didInsert this.setup}}
161+
{{didUpdate this.updateHeadingPositions @postID}}
162+
>
158163

159164
{{#each @tocStructure as |heading|}}
160165
<ul class="d-toc-heading">

spec/system/discotoc_timeline_user_spec.rb

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
:post,
1515
raw:
1616
"<div data-theme-toc='true'></div>\n\n# Heading 1\nContent for the first heading\n## Heading 2\nContent for the second heading\n### Heading 3\nContent for the third heading\n# Heading 4\nContent for the fourth heading",
17-
topic: topic_1,
17+
topic: topic_1
1818
)
1919
end
2020

@@ -23,7 +23,7 @@
2323
:post,
2424
raw:
2525
"\n# Heading 1\nContent for the first heading\n## Heading 2\nContent for the second heading\n### Heading 3\nContent for the third heading\n# Heading 4\nContent for the fourth heading",
26-
topic: topic_2,
26+
topic: topic_2
2727
)
2828
end
2929

@@ -32,7 +32,7 @@
3232
:post,
3333
raw:
3434
"intentionally \n long \n content \n so \n there's \n plenty \n to be \n scrolled \n past \n which \n will \n force \n the \n timeline \n to \n hide \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll ",
35-
topic: topic_1,
35+
topic: topic_1
3636
)
3737
end
3838

@@ -41,7 +41,7 @@
4141
:post,
4242
raw:
4343
"<div data-theme-toc='true'></div>\n\n# Heading For Reply 1\nContent for the first heading\n## Heading For Reply 2\nContent for the second heading\n### Heading For Reply 3\nContent for the third heading\n# Heading For Reply 4\nContent for the fourth heading",
44-
topic: topic_1,
44+
topic: topic_1
4545
)
4646
end
4747

@@ -50,7 +50,7 @@
5050
:post,
5151
raw:
5252
"intentionally \n long \n content \n so \n there's \n plenty \n to be \n scrolled \n past \n which \n will \n force \n the \n timeline \n to \n hide \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll \n scroll ",
53-
topic: topic_1,
53+
topic: topic_1
5454
)
5555
end
5656

@@ -94,6 +94,30 @@
9494
expect(page).to have_no_css(".d-toc-item.d-toc-h1")
9595
end
9696

97+
it "table of contents updates the highlighted section after navigating directly to other topic" do
98+
source_topic = Fabricate(:topic, category: category, tags: [tag])
99+
100+
Fabricate(
101+
:post,
102+
topic: source_topic,
103+
raw:
104+
"<div data-theme-toc='true'></div>\n\n# Heading 1 on the source topic\nContent for the first heading\n## Heading 2\nContent for the second heading\n### Heading 3\nContent for the third heading\n# Heading 4\nContent for the fourth heading\n<a href=c>Other topic</a>"
105+
)
106+
visit("/t/#{source_topic.id}")
107+
108+
expect(page).to have_css(
109+
".d-toc-item.d-toc-h1.active a[data-d-toc='toc-h1-heading-1-on-the-source-topic']"
110+
)
111+
find("a[href='/t/#{topic_1.slug}/#{topic_1.id}'").click
112+
113+
expect(page).to have_css(
114+
".d-toc-item.d-toc-h1.active a[data-d-toc='toc-h1-heading-1']"
115+
)
116+
expect(page).to have_no_css(
117+
"a[data-d-toc='toc-h1-heading-1-on-the-source-topic']"
118+
)
119+
end
120+
97121
it "timeline will appear without markup if auto_TOC_categories is set to the topic's category" do
98122
theme.update_setting(:auto_TOC_categories, "#{category.id}")
99123
theme.save!

0 commit comments

Comments
 (0)