Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add expand all button for TOC #105

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -4,6 +4,7 @@ import { action } from "@ember/object";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
import { service } from "@ember/service";
import DButton from "discourse/components/d-button";
import { headerOffset } from "discourse/lib/offset-calculator";
import { debounce } from "discourse-common/utils/decorators";
import TocHeading from "../components/toc-heading";
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
Loading