Skip to content

Commit

Permalink
FEATURE: Add expand all button for TOC
Browse files Browse the repository at this point in the history
This commit adds a button at the top of the TOC
headings list called "Expand all". This will expand
all of the subheadings in the list for easier searching.
Clicking the button again will hide all the subheadings except
the currently active one.
  • Loading branch information
martin-brennan committed Jan 21, 2025
1 parent 05d454d commit 58c0ccc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions common/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ $padding-basis: 0.75em;
transition: opacity 0.3s ease-in-out, max-height 0.3s ease-in-out;
}
&.active,
&.expand-all,
.d-toc-wrapper.overlay & {
ul {
max-height: 500em;
Expand Down
18 changes: 18 additions & 0 deletions javascripts/discourse/components/toc-contents.gjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Component from "@glimmer/component";

Check failure on line 1 in javascripts/discourse/components/toc-contents.gjs

View workflow job for this annotation

GitHub Actions / ci / linting

Run autofix to sort these imports!
import DButton from "discourse/components/d-button";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
Expand All @@ -21,6 +22,7 @@ export default class TocContents extends Component {
@tracked activeHeadingId = null;
@tracked headingPositions = [];
@tracked activeAncestorIds = [];
@tracked expandAll = false;

willDestroy() {
super.willDestroy(...arguments);
Expand All @@ -36,6 +38,10 @@ export default class TocContents extends Component {
return this.mappedTocStructure(this.args.tocStructure);
}

get expandLabel() {
return this.expandAll ? "Reset expand" : "Expand all";
}

@action
setup() {
this.listenForScroll();
Expand Down Expand Up @@ -64,6 +70,11 @@ export default class TocContents extends Component {
);
}

@action
toggleExpandAll() {
this.expandAll = !this.expandAll;
}

@debounce(RESIZE_DEBOUNCE)
calculateHeadingPositions() {
this.updateHeadingPositions();
Expand Down Expand Up @@ -160,6 +171,12 @@ export default class TocContents extends Component {
{{didInsert this.setup}}
{{didUpdate this.updateHeadingPositions @postID}}
>
<div class="d-toc-top-buttons">
<DButton
@action={{this.toggleExpandAll}}
@translatedLabel={{this.expandLabel}}
/>
</div>

{{#each @tocStructure as |heading|}}
<ul class="d-toc-heading">
Expand All @@ -168,6 +185,7 @@ export default class TocContents extends Component {
@activeHeadingId={{this.activeHeadingId}}
@activeAncestorIds={{this.activeAncestorIds}}
@renderTimeline={{@renderTimeline}}
@expandAll={{this.expandAll}}
/>
</ul>
{{/each}}
Expand Down
6 changes: 5 additions & 1 deletion javascripts/discourse/components/toc-heading.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ export default class TocHeading extends Component {
? ` d-toc-${this.args.item.tagName}`
: "";
let activeClass = "";
let expandAllClass = "";
if (this.isActive) {
activeClass = " direct-active active";
} else if (this.isAncestorActive) {
activeClass = " active";
}
return `${baseClass}${typeClass}${activeClass}`;
if (this.args.expandAll) {
expandAllClass = " expand-all";
}
return `${baseClass}${typeClass}${activeClass}${expandAllClass}`;
}

@action
Expand Down

0 comments on commit 58c0ccc

Please sign in to comment.