Skip to content

add manual/auto expand/collapse function to sub-sidebar. #1145

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

Closed
wants to merge 2 commits into from
Closed
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
44 changes: 39 additions & 5 deletions src/core/event/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function highlight(path) {

const sidebar = dom.getNode('.sidebar');
const anchors = dom.findAll('.anchor');
const wrap = dom.find(sidebar, '.sidebar-nav');
const wrap = dom.find(sidebar, '.app-sub-sidebar');
let active = dom.find(sidebar, 'li.active');
const doc = document.documentElement;
const top = ((doc && doc.scrollTop) || document.body.scrollTop) - coverHeight;
Expand Down Expand Up @@ -70,12 +70,14 @@ function highlight(path) {
li.classList.add('active');
active = li;

updateTree(active);

// Scroll into view
// https://github.com/vuejs/vuejs.org/blob/master/themes/vue/source/js/common.js#L282-L297
if (!hoverOver && dom.body.classList.contains('sticky')) {
const height = sidebar.clientHeight;
const curOffset = 0;
const cur = active.offsetTop + active.clientHeight + 40;
const cur = active.offsetTop + active.clientHeight + height / 2;
const isInView =
active.offsetTop >= wrap.scrollTop && cur <= wrap.scrollTop + height;
const notThan = cur - curOffset < height;
Expand All @@ -85,6 +87,27 @@ function highlight(path) {
}
}

function updateTree(active) {
let prevParents = dom.findAll('.parent');
let prevWifes = dom.findAll('.wife');
let prevChanges = dom.findAll('.change');
let prevExpands = dom.findAll('.expand');

prevParents.forEach(node => node.classList.remove('parent'));
prevWifes.forEach(node => node.classList.remove('wife'));
prevChanges.forEach(node => node.classList.remove('change'));
prevExpands.forEach(node => node.classList.remove('expand'));

let father = active.parentNode;
while (father && father.className !== 'app-sub-sidebar') {
father.classList.add('parent');
father = father.parentNode;
}

let wife = active.lastChild;
wife.nodeName === 'UL' && wife.classList.add('wife');
}

function getNavKey(path, id) {
return `${decodeURIComponent(path)}?id=${decodeURIComponent(id)}`;
}
Expand Down Expand Up @@ -143,14 +166,25 @@ export function scrollIntoView(path, id) {
return;
}
const topMargin = config().topMargin;

let prevSections = dom.findAll('.current');
prevSections.forEach(node => node.classList.remove('current'));

const section = dom.find('#' + id);
section && scrollTo(section, topMargin);
if (section) {
section.classList.add('current');
scrollTo(section, topMargin);
}

const li = nav[getNavKey(path, id)];
const sidebar = dom.getNode('.sidebar');
const active = dom.find(sidebar, 'li.active');
active && active.classList.remove('active');
li && li.classList.add('active');

const li = nav[getNavKey(path, id)];
if (li) {
li.classList.add('active');
updateTree(li);
}
}

const scrollEl = dom.$.scrollingElement || dom.$.documentElement;
Expand Down
11 changes: 11 additions & 0 deletions src/core/event/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ export function collapse(el) {
target.nextSibling.classList.contains('app-sub-sidebar')
) {
dom.toggleClass(target.parentNode, 'collapse');
} else if (
target.nodeName === 'SPAN' &&
target.nextSibling &&
target.nextSibling.classList.contains('section-link')
) {
dom.toggleClass(target, 'change');

let expand = target.nextSibling.nextSibling;
if (expand && expand.nodeName === 'UL') {
dom.toggleClass(expand, 'expand');
}
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class Compiler {
}

const tree = this.cacheTree[currentPath] || genTree(toc, level);
html = treeTpl(tree, '<ul>{inner}</ul>');
html = treeTpl(tree, '<ul class="app-sub-sidebar">{inner}</ul>');
this.cacheTree[currentPath] = tree;
}

Expand Down Expand Up @@ -309,7 +309,7 @@ export class Compiler {

cacheTree[currentPath] = tree;
this.toc = [];
return treeTpl(tree);
return treeTpl(tree, '<ul class="app-sub-sidebar">{inner}</ul>');
}

header(text, level) {
Expand Down
14 changes: 11 additions & 3 deletions src/core/render/tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,27 @@ export function cover() {
* Render tree
* @param {Array} toc Array of TOC section links
* @param {String} tpl TPL list
* @param {Boolean} using specified TPL for all level(true), or root only(false)
* @return {String} Rendered tree
*/
export function tree(toc, tpl = '<ul class="app-sub-sidebar">{inner}</ul>') {
export function tree(toc, tpl = '<ul>{inner}</ul>', all = false) {
if (!toc || !toc.length) {
return '';
}

let innerHTML = '';
toc.forEach(node => {
innerHTML += `<li><a class="section-link" href="${node.slug}">${node.title}</a></li>`;
if (node.children) {
innerHTML += tree(node.children, tpl);
innerHTML += `<li class="has-children"><span></span><a class="section-link" href="${node.slug}">${node.title}</a>`;
if (!all) {
innerHTML += tree(node.children);
} else {
innerHTML += tree(node.children, tpl, true);
}
} else {
innerHTML += `<li><span></span><a class="section-link" href="${node.slug}">${node.title}</a>`;
}
innerHTML += `</li>`;
});
return tpl.replace('{inner}', innerHTML);
}
Expand Down
49 changes: 49 additions & 0 deletions src/themes/basic/_layout.styl
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,52 @@ body.close

40%, 80%
transform rotate(10deg)


.app-sub-sidebar

ul
display none

ul.parent,
ul.wife,
ul.expand
display block

ul.parent.expand,
ul.wife.expand
display none

li span
display block
width 20px
font-family monospace
font-weight bold
font-size 20px
float left
&:after
content '\00A0'
&.change
&:after
content '\00A0'

li.has-children
> span
&:after
content '\203A'
> span.change
&:after
content '-'

li.parent,
li.has-children.active
> span
&:after
content '-'
> span.change
&:after
content '\203A'

.current
span
color #c94922
7 changes: 1 addition & 6 deletions src/themes/dolphin.styl
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ body
color var(--theme-color, $color-primary)
font-weight 600

.app-sub-sidebar
li
&::before
content '-'
padding-right 4px
float left


/* markdown content found on pages */
.markdown-section h1, .markdown-section h2, .markdown-section h3, .markdown-section h4, .markdown-section strong
Expand Down
7 changes: 0 additions & 7 deletions src/themes/vue.styl
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ body
color var(--theme-color, $color-primary)
font-weight 600

.app-sub-sidebar
li
&::before
content '-'
padding-right 4px
float left

/* markdown content found on pages */
.markdown-section h1, .markdown-section h2, .markdown-section h3, .markdown-section h4, .markdown-section strong
color #2c3e50
Expand Down