Skip to content

sidebar items expand/collapse when click the arrow icon before item. #11

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 5 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
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
loadNavbar: true,
mergeNavbar: true,
maxLevel: 4,
subMaxLevel: 2,
subMaxLevel: 6,
name: 'docsify',
search: {
noData: {
Expand Down
35 changes: 34 additions & 1 deletion src/core/event/scroll.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Tweezer from 'tweezer.js';
import { isMobile } from '../util/env';
import * as dom from '../util/dom';
import { removeParams } from '../router/util';
import config from '../config';
import Tweezer from 'tweezer.js';

const nav = {};
let hoverOver = false;
Expand Down Expand Up @@ -38,6 +38,7 @@ function highlight(path) {
const anchors = dom.findAll('.anchor');
const wrap = dom.find(sidebar, '.sidebar-nav');
let active = dom.find(sidebar, 'li.active');
let parents = dom.findAll(sidebar, 'li.parent');
const doc = document.documentElement;
const top = ((doc && doc.scrollTop) || document.body.scrollTop) - coverHeight;
let last;
Expand Down Expand Up @@ -70,6 +71,9 @@ function highlight(path) {
li.classList.add('active');
active = li;

parents.forEach(node => node.classList.remove('parent'));
active = findParents(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')) {
Expand All @@ -85,6 +89,31 @@ function highlight(path) {
}
}

function findParents(active) {
if (!active) {
return active;
}

let top = active;
let node = active.parentNode;

while (node) {
if (node.classList.contains('app-sub-sidebar')) {
node = node.parentNode;
continue;
} else if (node.classList.contains('has-children')) {
node.classList.add('parent');
if (node.classList.contains('collapse')) {
top = node;
}
node = node.parentNode;
continue;
} else {
return top;
}
}
}

function getNavKey(path, id) {
return `${decodeURIComponent(path)}?id=${decodeURIComponent(id)}`;
}
Expand Down Expand Up @@ -149,8 +178,12 @@ export function scrollIntoView(path, id) {
const li = nav[getNavKey(path, id)];
const sidebar = dom.getNode('.sidebar');
const active = dom.find(sidebar, 'li.active');
const parents = dom.findAll(sidebar, 'li.parent');
active && active.classList.remove('active');
parents.forEach(node => node.classList.remove('parent'));

li && li.classList.add('active');
findParents(li);
}

const scrollEl = dom.$.scrollingElement || dom.$.documentElement;
Expand Down
7 changes: 7 additions & 0 deletions src/core/event/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ 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 &&
target.nextSibling.classList.contains('section-link')
) {
dom.toggleClass(target.parentNode, 'collapse');
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import marked from 'marked';
import { isAbsolutePath, getPath, getParentPath } from '../router/util';
import { isFn, merge, cached, isPrimitive } from '../util/core';
import { tree as treeTpl } from './tpl';
Expand All @@ -12,6 +11,7 @@ import { paragraphCompiler } from './compiler/paragraph';
import { taskListCompiler } from './compiler/taskList';
import { taskListItemCompiler } from './compiler/taskListItem';
import { linkCompiler } from './compiler/link';
import marked from 'marked';

const cachedLinks = {};

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

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

Expand Down
7 changes: 6 additions & 1 deletion src/core/render/tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ export function tree(toc, tpl = '<ul class="app-sub-sidebar">{inner}</ul>') {

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

40%, 80%
transform rotate(10deg)

.app-sub-sidebar
li
> span
&:after
content '\00A0'
font-size 12px
line-height 1em
padding 0 6px 0 0
float left

li.has-children
> span
&:after
writing-mode vertical-lr
content '\276F'
font-size 12px
line-height 1em
padding 12px 0 0 0
float left

li.has-children.collapse
> span
&:after
writing-mode horizontal-tb
content '\276F'
font-size 12px
line-height 1em
padding 9px 4px 0 2px
float left
3 changes: 2 additions & 1 deletion src/themes/buble.styl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ $sidebar-width = 16rem
ul li ul
padding 0

li.active
li.active,
li.collapse.parent
a
color #333

Expand Down
3 changes: 2 additions & 1 deletion src/themes/dark.styl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ body
ul li ul
padding 0

ul li.active > a
ul li.active > a,
li.collapse.parent > a
color var(--theme-color, $color-primary)
font-weight 600

Expand Down
10 changes: 2 additions & 8 deletions src/themes/dolphin.styl
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,12 @@ body
ul li ul
padding 0

ul li.active > a
ul li.active > a,
li.collapse.parent > a
border-right 2px solid
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
10 changes: 2 additions & 8 deletions src/themes/vue.styl
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,12 @@ body
ul li ul
padding 0

ul li.active > a
ul li.active > a,
li.collapse.parent > a
border-right 2px solid
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