Skip to content

Commit 69ef213

Browse files
committed
rebase to upstream/develop and submit again.
1 parent eb1a3ce commit 69ef213

File tree

7 files changed

+113
-23
lines changed

7 files changed

+113
-23
lines changed

src/core/event/scroll.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function highlight(path) {
3636

3737
const sidebar = dom.getNode('.sidebar');
3838
const anchors = dom.findAll('.anchor');
39-
const wrap = dom.find(sidebar, '.sidebar-nav');
39+
const wrap = dom.find(sidebar, '.app-sub-sidebar');
4040
let active = dom.find(sidebar, 'li.active');
4141
const doc = document.documentElement;
4242
const top = ((doc && doc.scrollTop) || document.body.scrollTop) - coverHeight;
@@ -70,12 +70,14 @@ function highlight(path) {
7070
li.classList.add('active');
7171
active = li;
7272

73+
updateTree(active);
74+
7375
// Scroll into view
7476
// https://github.com/vuejs/vuejs.org/blob/master/themes/vue/source/js/common.js#L282-L297
7577
if (!hoverOver && dom.body.classList.contains('sticky')) {
7678
const height = sidebar.clientHeight;
7779
const curOffset = 0;
78-
const cur = active.offsetTop + active.clientHeight + 40;
80+
const cur = active.offsetTop + active.clientHeight + height / 2;
7981
const isInView =
8082
active.offsetTop >= wrap.scrollTop && cur <= wrap.scrollTop + height;
8183
const notThan = cur - curOffset < height;
@@ -85,6 +87,27 @@ function highlight(path) {
8587
}
8688
}
8789

90+
function updateTree(active) {
91+
let prevParents = dom.findAll('.parent');
92+
let prevWifes = dom.findAll('.wife');
93+
let prevChanges = dom.findAll('.change');
94+
let prevExpands = dom.findAll('.expand');
95+
96+
prevParents.forEach(node => node.classList.remove('parent'));
97+
prevWifes.forEach(node => node.classList.remove('wife'));
98+
prevChanges.forEach(node => node.classList.remove('change'));
99+
prevExpands.forEach(node => node.classList.remove('expand'));
100+
101+
let father = active.parentNode;
102+
while (father && father.className !== 'app-sub-sidebar') {
103+
father.classList.add('parent');
104+
father = father.parentNode;
105+
}
106+
107+
let wife = active.lastChild;
108+
wife.nodeName === 'UL' && wife.classList.add('wife');
109+
}
110+
88111
function getNavKey(path, id) {
89112
return `${decodeURIComponent(path)}?id=${decodeURIComponent(id)}`;
90113
}
@@ -143,14 +166,25 @@ export function scrollIntoView(path, id) {
143166
return;
144167
}
145168
const topMargin = config().topMargin;
169+
170+
let prevSections = dom.findAll('.current');
171+
prevSections.forEach(node => node.classList.remove('current'));
172+
146173
const section = dom.find('#' + id);
147-
section && scrollTo(section, topMargin);
174+
if (section) {
175+
section.classList.add('current');
176+
scrollTo(section, topMargin);
177+
}
148178

149-
const li = nav[getNavKey(path, id)];
150179
const sidebar = dom.getNode('.sidebar');
151180
const active = dom.find(sidebar, 'li.active');
152181
active && active.classList.remove('active');
153-
li && li.classList.add('active');
182+
183+
const li = nav[getNavKey(path, id)];
184+
if (li) {
185+
li.classList.add('active');
186+
updateTree(li);
187+
}
154188
}
155189

156190
const scrollEl = dom.$.scrollingElement || dom.$.documentElement;

src/core/event/sidebar.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ export function collapse(el) {
4242
target.nextSibling.classList.contains('app-sub-sidebar')
4343
) {
4444
dom.toggleClass(target.parentNode, 'collapse');
45+
} else if (
46+
target.nodeName === 'SPAN' &&
47+
target.nextSibling &&
48+
target.nextSibling.classList.contains('section-link')
49+
) {
50+
dom.toggleClass(target, 'change');
51+
52+
let expand = target.nextSibling.nextSibling;
53+
if (expand && expand.nodeName === 'UL') {
54+
dom.toggleClass(expand, 'expand');
55+
}
4556
}
4657
});
4758
}

src/core/render/compiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export class Compiler {
277277
}
278278

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

@@ -309,7 +309,7 @@ export class Compiler {
309309

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

315315
header(text, level) {

src/core/render/tpl.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,27 @@ export function cover() {
8484
* Render tree
8585
* @param {Array} toc Array of TOC section links
8686
* @param {String} tpl TPL list
87+
* @param {Boolean} using specified TPL for all level(true), or root only(false)
8788
* @return {String} Rendered tree
8889
*/
89-
export function tree(toc, tpl = '<ul class="app-sub-sidebar">{inner}</ul>') {
90+
export function tree(toc, tpl = '<ul>{inner}</ul>', all = false) {
9091
if (!toc || !toc.length) {
9192
return '';
9293
}
9394

9495
let innerHTML = '';
9596
toc.forEach(node => {
96-
innerHTML += `<li><a class="section-link" href="${node.slug}">${node.title}</a></li>`;
9797
if (node.children) {
98-
innerHTML += tree(node.children, tpl);
98+
innerHTML += `<li class="has-children"><span></span><a class="section-link" href="${node.slug}">${node.title}</a>`;
99+
if (!all) {
100+
innerHTML += tree(node.children);
101+
} else {
102+
innerHTML += tree(node.children, tpl, true);
103+
}
104+
} else {
105+
innerHTML += `<li><span></span><a class="section-link" href="${node.slug}">${node.title}</a>`;
99106
}
107+
innerHTML += `</li>`;
100108
});
101109
return tpl.replace('{inner}', innerHTML);
102110
}

src/themes/basic/_layout.styl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,52 @@ body.close
470470

471471
40%, 80%
472472
transform rotate(10deg)
473+
474+
475+
.app-sub-sidebar
476+
477+
ul
478+
display none
479+
480+
ul.parent,
481+
ul.wife,
482+
ul.expand
483+
display block
484+
485+
ul.parent.expand,
486+
ul.wife.expand
487+
display none
488+
489+
li span
490+
display block
491+
width 20px
492+
font-family monospace
493+
font-weight bold
494+
font-size 20px
495+
float left
496+
&:after
497+
content '-'
498+
&.change
499+
&:after
500+
content '-'
501+
502+
li.has-children
503+
> span
504+
&:after
505+
content '+'
506+
> span.change
507+
&:after
508+
content '-'
509+
510+
li.parent,
511+
li.has-children.active
512+
> span
513+
&:after
514+
content '-'
515+
> span.change
516+
&:after
517+
content '+'
518+
519+
.current
520+
span
521+
color #c94922

src/themes/dolphin.styl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ body
3939
color var(--theme-color, $color-primary)
4040
font-weight 600
4141

42-
.app-sub-sidebar
43-
li
44-
&::before
45-
content '-'
46-
padding-right 4px
47-
float left
42+
4843

4944
/* markdown content found on pages */
5045
.markdown-section h1, .markdown-section h2, .markdown-section h3, .markdown-section h4, .markdown-section strong

src/themes/vue.styl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ body
3939
color var(--theme-color, $color-primary)
4040
font-weight 600
4141

42-
.app-sub-sidebar
43-
li
44-
&::before
45-
content '-'
46-
padding-right 4px
47-
float left
48-
4942
/* markdown content found on pages */
5043
.markdown-section h1, .markdown-section h2, .markdown-section h3, .markdown-section h4, .markdown-section strong
5144
color #2c3e50

0 commit comments

Comments
 (0)