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

refactor: optimize toc logic #426

Merged
merged 4 commits into from
Dec 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 2 additions & 4 deletions assets/css/_partial/_single/_toc.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@
#toc-auto {
display: block;
position: absolute;
width: $MAX_LENGTH;
max-width: 0;
padding: 0 .8rem;
border-left: 4px solid $global-border-color;
@include overflow-wrap(break-word);
box-sizing: border-box;
top: 10rem;
left: 0;
visibility: hidden;
left: 80%;
width: 20%;
HEIGE-PCloud marked this conversation as resolved.
Show resolved Hide resolved

[header-desktop=normal] & {
top: 5rem;
Expand Down
20 changes: 0 additions & 20 deletions assets/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,29 +551,9 @@ function initToc () {
const isTocStatic = window.matchMedia && window.matchMedia('only screen and (max-width: 1000px)').matches

if (document.getElementById('toc-static').getAttribute('kept') || isTocStatic) {
const tocContentStatic = document.getElementById('toc-content-static')
if (tocCore.parentElement !== tocContentStatic) {
tocCore.parentElement.removeChild(tocCore)
tocContentStatic.appendChild(tocCore)
}
if (window._tocOnScroll) window.scrollEventSet.delete(window._tocOnScroll)
} else {
const tocContentAuto = document.getElementById('toc-content-auto')
if (tocCore.parentElement !== tocContentAuto) {
tocCore.parentElement.removeChild(tocCore)
tocContentAuto.appendChild(tocCore)
}
// The toc element
const toc = document.getElementById('toc-auto')
// The page element
const page = document.getElementsByClassName('page')[0]
// The rect of the page
const rect = page.getBoundingClientRect()
// The toc is 20px to the right of the page
toc.style.left = `${rect.left + rect.width + 20}px`
// The toc occupy all the right of the window
toc.style.maxWidth = `${window.innerWidth - rect.right - 20}px`
toc.style.visibility = 'visible'
const tocLinkElements = tocCore.querySelectorAll('a:first-child')
const tocLiElements = tocCore.getElementsByTagName('li')
const headerLinkElements = document.getElementsByClassName('headerLink')
Expand Down
11 changes: 7 additions & 4 deletions layouts/posts/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

{{- define "content" -}}
{{- $params := .Scratch.Get "params" -}}

{{- $toc := $params.toc -}}
{{- if eq $toc true -}}
{{- $toc = .Site.Params.page.toc | default dict -}}
Expand All @@ -12,10 +11,12 @@


{{- /* Auto TOC */ -}}
{{- if ne $toc.enable false -}}
{{- if $toc.enable | and (eq $toc.keepStatic false) -}}
<div class="toc" id="toc-auto">
<h2 class="toc-title">{{ T "contents" }}</h2>
<div class="toc-content{{ if eq $toc.auto false }} always-active{{ end }}" id="toc-content-auto"></div>
<div class="toc-content{{ if eq $toc.auto false }} always-active{{ end }}" id="toc-content-auto">
{{- dict "Content" .TableOfContents "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
</div>
</div>
{{- end -}}

Expand Down Expand Up @@ -183,14 +184,16 @@ <h2 class="single-subtitle">{{ . }}</h2>
{{- end -}}

{{- /* Static TOC */ -}}
{{- if ne $toc.enable false -}}
{{- if $toc.enable -}}
<div class="details toc" id="toc-static" kept="{{ if $toc.keepStatic }}true{{ end }}">
<div class="details-summary toc-title">
<span>{{ T "contents" }}</span>
<span><i class="details-icon fas fa-angle-right"></i></span>
</div>
<div class="details-content toc-content" id="toc-content-static">
{{- if $toc.keepStatic -}}
{{- dict "Content" .TableOfContents "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
{{- end -}}
</div>
</div>
{{- end -}}
Expand Down