Skip to content

Commit

Permalink
✨ Feat: enhance auto toc feature (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed May 17, 2022
1 parent fd1ebab commit bb7763b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
All notable changes to this project will be documented in this file.
<!-- Releases see https://github.com/Lruihao/FixIt/releases -->


## v0.2.15

> Add hugo new features support.
> Snapshot:
- :sparkles: Feat: enhance auto toc feature ([#104](https://github.com/Lruihao/FixIt/issues/104) [#136](https://github.com/Lruihao/FixIt/issues/136))
- :bug: Fix: Giscus comments invalid config `inputPosition`
- :art: Style: add scroll bar for auto toc ([#136](https://github.com/Lruihao/FixIt/issues/136))
- :arrow_up: Chore: update some third-party libraries
Expand Down
10 changes: 10 additions & 0 deletions assets/css/_partial/_single/_toc.scss
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

.toc-title {
margin: 0.5rem 0;
cursor: pointer;

&::before {
content: '|';
Expand All @@ -72,6 +73,15 @@
color: $global-border-color-dark;
}
}

i.toc-icon {
@include transition(transform 0.2s ease);
}
}

&.toc-hidden i.toc-icon {
@include transform(rotate(-90deg));
@include transition(transform 0.2s ease);
}

.toc-content {
Expand Down
2 changes: 1 addition & 1 deletion assets/js/theme.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/theme.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion layouts/posts/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{{- /* Auto TOC */ -}}
{{- $tocEmpty := eq .TableOfContents `<nav id="TableOfContents"></nav>` -}}
{{- if (ne $toc.enable false) | and (ne $tocEmpty true) -}}
<h2 class="toc-title">{{ T "contents" }}</h2>
<h2 class="toc-title">{{ T "contents" }} <i class="toc-icon fa-solid fa-angle-down fa-fw"></i></h2>
<div class="toc-content{{ if eq $toc.auto false }} always-active{{ end }}" id="toc-content-auto"></div>
{{- end -}}
</aside>
Expand Down
21 changes: 17 additions & 4 deletions src/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,12 @@ class Theme {
});
}
}

initToc() {
/**
* init table of contents
* @param {Boolean} [onInit=false]
* @returns
*/
initToc(onInit = false) {
const $tocCore = document.getElementById('TableOfContents');
if ($tocCore === null) {
return;
Expand All @@ -507,7 +511,7 @@ class Theme {
$tocCore.parentElement.removeChild($tocCore);
$tocContentStatic.appendChild($tocCore);
}
if (this._tocOnScroll) this.scrollEventSet.delete(this._tocOnScroll);
this._tocOnScroll && this.scrollEventSet.delete(this._tocOnScroll);
} else {
const $tocContentAuto = document.getElementById('toc-content-auto');
if ($tocCore.parentElement !== $tocContentAuto) {
Expand Down Expand Up @@ -556,6 +560,15 @@ class Theme {
});
this._tocOnScroll();
this.scrollEventSet.add(this._tocOnScroll);
// only addEventListener once
onInit && document.querySelector('#toc-auto>.toc-title').addEventListener('click', () => {
const animation = ['animate__faster']
const tocHidden = $toc.classList.contains('toc-hidden');
animation.push(tocHidden ? 'animate__fadeIn' : 'animate__fadeOut');
$tocContentAuto.classList.remove(tocHidden ? 'animate__fadeOut' : 'animate__fadeIn');
this.util.animateCSS($tocContentAuto, animation, true);
$toc.classList.toggle('toc-hidden');
}, false)
}
}

Expand Down Expand Up @@ -984,7 +997,7 @@ class Theme {

window.setTimeout(() => {
this.initComment();
this.initToc();
this.initToc(true);

this.onScroll();
this.onResize();
Expand Down

0 comments on commit bb7763b

Please sign in to comment.