Skip to content

Commit b0c9e11

Browse files
committedMay 24, 2018
refactor: load 'active header links' on demand
1 parent 4c09627 commit b0c9e11

File tree

5 files changed

+68
-44
lines changed

5 files changed

+68
-44
lines changed
 

Diff for: ‎lib/app/root-mixins/activeHeaderLinks.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import store from '@app/store'
2+
import throttle from 'lodash.throttle'
3+
4+
export default {
5+
mounted () {
6+
window.addEventListener('scroll', this.onScroll)
7+
},
8+
methods: {
9+
onScroll: throttle(function () {
10+
this.setActiveHash()
11+
}, 300),
12+
setActiveHash () {
13+
const sidebarLinks = [].slice.call(document.querySelectorAll('.sidebar-link'))
14+
const anchors = [].slice.call(document.querySelectorAll('.header-anchor'))
15+
.filter(anchor => sidebarLinks.some(sidebarLink => sidebarLink.hash === anchor.hash))
16+
17+
const scrollTop = Math.max(
18+
window.pageYOffset,
19+
document.documentElement.scrollTop,
20+
document.body.scrollTop
21+
)
22+
23+
for (let i = 0; i < anchors.length; i++) {
24+
const anchor = anchors[i]
25+
const nextAnchor = anchors[i + 1]
26+
27+
const isActive = i === 0 && scrollTop === 0 ||
28+
(scrollTop >= anchor.parentElement.offsetTop + 10 &&
29+
(!nextAnchor || scrollTop < nextAnchor.parentElement.offsetTop - 10))
30+
31+
if (isActive && decodeURIComponent(this.$route.hash) !== decodeURIComponent(anchor.hash)) {
32+
store.disableScrollBehavior = true
33+
this.$router.replace(decodeURIComponent(anchor.hash), () => {
34+
// execute after scrollBehavior handler.
35+
this.$nextTick(() => {
36+
store.disableScrollBehavior = false
37+
})
38+
})
39+
return
40+
}
41+
}
42+
}
43+
},
44+
beforeDestroy () {
45+
window.removeEventListener('scroll', this.onScroll)
46+
}
47+
}

Diff for: ‎lib/app/root-mixins/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
import updateMeta from './updateMeta'
2-
export default [updateMeta]
2+
import activeHeaderLinks from '@activeHeaderLinks'
3+
4+
console.log(activeHeaderLinks)
5+
export default [
6+
updateMeta, // required
7+
activeHeaderLinks // optional
8+
]

Diff for: ‎lib/default-theme/Layout.vue

-42
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ import Navbar from './Navbar.vue'
2828
import Page from './Page.vue'
2929
import Sidebar from './Sidebar.vue'
3030
import { pathToComponentName } from '@app/util'
31-
import store from '@app/store'
3231
import { resolveSidebarItems } from './util'
33-
import throttle from 'lodash.throttle'
3432
3533
export default {
3634
components: { Home, Page, Sidebar, Navbar },
@@ -88,8 +86,6 @@ export default {
8886
},
8987
9088
mounted () {
91-
window.addEventListener('scroll', this.onScroll)
92-
9389
// configure progress bar
9490
nprogress.configure({ showSpinner: false })
9591
@@ -106,10 +102,6 @@ export default {
106102
})
107103
},
108104
109-
beforeDestroy () {
110-
window.removeEventListener('scroll', this.onScroll)
111-
},
112-
113105
methods: {
114106
toggleSidebar (to) {
115107
this.isSidebarOpen = typeof to === 'boolean' ? to : !this.isSidebarOpen
@@ -131,40 +123,6 @@ export default {
131123
this.toggleSidebar(false)
132124
}
133125
}
134-
},
135-
onScroll: throttle(function () {
136-
this.setActiveHash()
137-
}, 300),
138-
setActiveHash () {
139-
if (this.$site.themeConfig.disableActiveHash) {
140-
return
141-
}
142-
143-
const sidebarLinks = [].slice.call(document.querySelectorAll('.sidebar-link'))
144-
const anchors = [].slice.call(document.querySelectorAll('.header-anchor'))
145-
.filter(anchor => sidebarLinks.some(sidebarLink => sidebarLink.hash === anchor.hash))
146-
147-
const scrollTop = Math.max(window.pageYOffset, document.documentElement.scrollTop, document.body.scrollTop)
148-
149-
for (let i = 0; i < anchors.length; i++) {
150-
const anchor = anchors[i]
151-
const nextAnchor = anchors[i + 1]
152-
153-
const isActive = i === 0 && scrollTop === 0 ||
154-
(scrollTop >= anchor.parentElement.offsetTop + 10 &&
155-
(!nextAnchor || scrollTop < nextAnchor.parentElement.offsetTop - 10))
156-
157-
if (isActive && decodeURIComponent(this.$route.hash) !== decodeURIComponent(anchor.hash)) {
158-
store.disableScrollBehavior = true
159-
this.$router.replace(decodeURIComponent(anchor.hash), () => {
160-
// execute after scrollBehavior handler.
161-
this.$nextTick(() => {
162-
store.disableScrollBehavior = false
163-
})
164-
})
165-
return
166-
}
167-
}
168126
}
169127
}
170128
}

Diff for: ‎lib/prepare.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = async function prepare (sourceDir) {
4343
await writeTemp('routes.js', [
4444
componentCode,
4545
routesCode
46-
].join('\n\n'))
46+
].join('\n'))
4747

4848
// 3. generate siteData
4949
const dataCode = `export const siteData = ${JSON.stringify(options.siteData, null, 2)}`

Diff for: ‎lib/webpack/createBaseConfig.js

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path')
22

33
module.exports = function createBaseConfig ({
44
siteConfig,
5+
siteData,
56
sourceDir,
67
outDir,
78
publicPath,
@@ -56,6 +57,18 @@ module.exports = function createBaseConfig ({
5657
.add(path.resolve(__dirname, '../../../'))
5758
.add('node_modules')
5859

60+
// Load extra root mixins on demand.
61+
const { activeHeaderLinks = true } = siteData.themeConfig
62+
const rootMixinsLoadingConfig = { activeHeaderLinks }
63+
for (const mixinName in rootMixinsLoadingConfig) {
64+
const enabled = rootMixinsLoadingConfig[mixinName]
65+
config.resolve
66+
.alias.set(`@${mixinName}`, enabled
67+
? path.resolve(__dirname, `../app/root-mixins/${mixinName}.js`)
68+
: path.resolve(__dirname, './noopModule.js')
69+
)
70+
}
71+
5972
config.resolveLoader
6073
.set('symlinks', true)
6174
.modules

0 commit comments

Comments
 (0)
Please sign in to comment.