From 5253cc24187bba0dd134882ccd47a9cee1f408b4 Mon Sep 17 00:00:00 2001
From: DCloud_LXH <283700113@qq.com>
Date: Tue, 12 Apr 2022 16:09:33 +0800
Subject: [PATCH] feat: add toc
---
docs/.vuepress/config.js | 1 +
.../.vuepress/markdown/createMarkdownArray.js | 8 +-
docs/.vuepress/theme/components/Navbar.vue | 4 +
docs/.vuepress/theme/components/Sticker.vue | 64 +++++++
docs/.vuepress/theme/components/Toc.vue | 166 ++++++++++++++++++
docs/.vuepress/theme/layouts/Layout.vue | 6 +-
docs/.vuepress/theme/styles/index.styl | 5 +
docs/.vuepress/theme/styles/palette.styl | 3 +-
docs/.vuepress/theme/util/index.js | 21 +++
9 files changed, 271 insertions(+), 7 deletions(-)
create mode 100644 docs/.vuepress/theme/components/Sticker.vue
create mode 100644 docs/.vuepress/theme/components/Toc.vue
diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js
index b40a3a54d..1132d850b 100644
--- a/docs/.vuepress/config.js
+++ b/docs/.vuepress/config.js
@@ -36,6 +36,7 @@ const config = {
docsDir: 'docs',
editLinks: true,
editLinkText: '帮助我们改善此页面!',
+ lastUpdated: '上次更新',
// smoothScroll: true,
algolia: {
apiKey: '2fdcc4e76c8e260671ad70065e60b2e7',
diff --git a/docs/.vuepress/markdown/createMarkdownArray.js b/docs/.vuepress/markdown/createMarkdownArray.js
index f92dcd3fc..efc565cfd 100644
--- a/docs/.vuepress/markdown/createMarkdownArray.js
+++ b/docs/.vuepress/markdown/createMarkdownArray.js
@@ -45,15 +45,13 @@ function createMarkdownArray(contents = [], childrenName = 'children') {
}
}
- function removeParent(childs = []) {
+ // 移除最后一项 parent 节点,防止循环引用报错
+ (function removeParent(childs = []) {
childs.forEach(child => {
if (child.parent) delete child.parent
if (child[childrenName]) removeParent(child[childrenName])
})
- }
-
- // 移除最后一项 parent 节点,防止循环引用报错
- removeParent(markdownArray[markdownArray.length - 1][childrenName])
+ })(markdownArray[markdownArray.length - 1][childrenName])
return markdownArray
}
diff --git a/docs/.vuepress/theme/components/Navbar.vue b/docs/.vuepress/theme/components/Navbar.vue
index d7e8ae840..2f512f510 100644
--- a/docs/.vuepress/theme/components/Navbar.vue
+++ b/docs/.vuepress/theme/components/Navbar.vue
@@ -121,6 +121,7 @@ export default {
this.mainNavBar = document.querySelector('.main-navbar')
this.subNavBar = document.querySelector('.sub-navbar')
this.pageContainer = document.querySelector('.page')
+ this.vuepressToc = document.querySelector('.vuepress-toc')
this.navbarHeight = this.navbar.clientHeight
this.subNavBarHeight = this.subNavBar.clientHeight
this.mainNavBarHeight = this.mainNavBar.clientHeight
@@ -134,6 +135,7 @@ export default {
window.removeEventListener('scroll', this.onWindowScroll)
this.fixedNavbar = false
this.sideBar && this.sideBar.removeAttribute('style')
+ this.vuepressToc && this.vuepressToc.removeAttribute('style')
this.navbar && this.navbar.removeAttribute('style')
this.pageContainer && this.pageContainer.removeAttribute('style')
},
@@ -144,6 +146,7 @@ export default {
let sideTop = this.navbarHeight - scrollTop
sideTop <= this.subNavBarHeight && (sideTop = this.subNavBarHeight)
this.sideBar && (this.sideBar.style.top = `${sideTop + 1}px`)
+ this.vuepressToc && (this.vuepressToc.style.top = `${sideTop + 1}px`)
}
if (scrollTop >= this.mainNavBarHeight) {
@@ -182,6 +185,7 @@ export default {
this.navbarHeight = this.navbar.clientHeight
this.subNavBarHeight = this.subNavBar.clientHeight
this.sideBar.style.top = `${this.navbarHeight + 1}px`
+ this.vuepressToc.style.top = `${this.navbarHeight + 1}px`
})
}
}
diff --git a/docs/.vuepress/theme/components/Sticker.vue b/docs/.vuepress/theme/components/Sticker.vue
new file mode 100644
index 000000000..e1ac95104
--- /dev/null
+++ b/docs/.vuepress/theme/components/Sticker.vue
@@ -0,0 +1,64 @@
+
+
+
+
+
+
+
+
+
diff --git a/docs/.vuepress/theme/components/Toc.vue b/docs/.vuepress/theme/components/Toc.vue
new file mode 100644
index 000000000..b8e6f28ff
--- /dev/null
+++ b/docs/.vuepress/theme/components/Toc.vue
@@ -0,0 +1,166 @@
+
+
+ ON THIS PAGE
+
+
+
+
+
+
+
diff --git a/docs/.vuepress/theme/layouts/Layout.vue b/docs/.vuepress/theme/layouts/Layout.vue
index b2e594975..687a07186 100644
--- a/docs/.vuepress/theme/layouts/Layout.vue
+++ b/docs/.vuepress/theme/layouts/Layout.vue
@@ -43,6 +43,8 @@
+
+
@@ -53,6 +55,7 @@ import Page from '@theme/components/Page.vue'
import Sidebar from '@theme/components/Sidebar.vue'
import Footer from '@theme/components/Footer.vue';
import SiderBarBottom from '../components/SiderBarBottom.vue';
+import Toc from '../components/Toc';
import { resolveSidebarItems, forbidScroll, BaiduStat } from '../util'
import navProvider from '../mixin/navProvider';
@@ -65,7 +68,8 @@ export default {
Sidebar,
Navbar,
Footer,
- SiderBarBottom
+ SiderBarBottom,
+ Toc
},
data () {
return {
diff --git a/docs/.vuepress/theme/styles/index.styl b/docs/.vuepress/theme/styles/index.styl
index c5e283149..73f99c6f5 100644
--- a/docs/.vuepress/theme/styles/index.styl
+++ b/docs/.vuepress/theme/styles/index.styl
@@ -81,6 +81,11 @@ body.forbid_scroll
main.page
padding-bottom 0px
+ padding-right 0px
+
+ @media (min-width: 1300px)
+ &
+ padding-right $vuepress-toc-width
{$contentClass}:not(.custom)
> *:first-child
diff --git a/docs/.vuepress/theme/styles/palette.styl b/docs/.vuepress/theme/styles/palette.styl
index 82b9623db..eb2970ff5 100644
--- a/docs/.vuepress/theme/styles/palette.styl
+++ b/docs/.vuepress/theme/styles/palette.styl
@@ -4,4 +4,5 @@ $contentWidth = 1200px
$navbarHeight = 9rem
$navbar-sub-navbar-height = 5rem
$navbar-background-color = #f7f7f7
-$search-container-color = #f5f6f7
\ No newline at end of file
+$search-container-color = #f5f6f7
+$vuepress-toc-width = 220px
\ No newline at end of file
diff --git a/docs/.vuepress/theme/util/index.js b/docs/.vuepress/theme/util/index.js
index 50948fc9e..f73dbdc99 100644
--- a/docs/.vuepress/theme/util/index.js
+++ b/docs/.vuepress/theme/util/index.js
@@ -300,4 +300,25 @@ export function debounce(fn, delay) {
clearTimeout(timeout)
}
return newFn
+}
+
+/*
+ * find parent vm by ref
+ * @param {String} ref
+ * @param {Vue} vm
+ * @param {any} def default value
+ * @returns {Element}
+ */
+export function findContainerInVm(ref, vm, def) {
+ if (!ref) return def
+ let container
+ let parent = vm
+ while ((parent = parent.$parent) && !container) {
+ container = parent.$refs[ref]
+ }
+ // Ensure it's html element (ref could be component)
+ if (container && container.$el) {
+ container = container.$el
+ }
+ return container || def
}
\ No newline at end of file
--
GitLab