-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Active sidebar link highlight #272
Changes from 1 commit
bc3336b
30800ac
ffdd8b7
7ca5f5c
c67c64a
58f4b02
1c4c376
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ import Page from './Page.vue' | |
import Sidebar from './Sidebar.vue' | ||
import { pathToComponentName } from '@app/util' | ||
import { resolveSidebarItems } from './util' | ||
import throttle from 'lodash/throttle' | ||
|
||
export default { | ||
components: { Home, Page, Sidebar, Navbar }, | ||
|
@@ -111,6 +112,8 @@ export default { | |
this.$watch('$page', updateMeta) | ||
updateMeta() | ||
|
||
window.addEventListener('scroll', this.onScroll) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You asked a contributor to access There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Emmm... In addtion, what is
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ycmjason Pay attention to your wording. If I don't read your comment, then you will be conveying a very low-level error in a vue official project. Is this reasonable? BTW, please take a good look at Vue before you speak anything, or I'll think you're just making trouble. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ulivz please pay attention to yours as well. You are all contributors. You all can make mistakes and can have different opinions. Point out problems with neutral language, don't bring your ego into this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. come back and see, I really shouldn't bring my feelings into it, and should be more neutral. I'm sorry. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opps, I must have misread previously. Tried to removed this whole thread but turns out only be able to remove my own comment. I was not asking a contributor to access There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will keep these comments, although I want to delete them 😂 My original intention is that I do not want to import any mistake (I really love this project), but my expression is obviously too excited, sorry again. Thanks for your contributions for VuePress. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel really sorry to make you feel that I am offending you all the time. In case you feel the same again, please do tell me and perhaps suggest a way that I should have put it. 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Work hard together to make this project better. Come on! 🎉 |
||
|
||
// configure progress bar | ||
nprogress.configure({ showSpinner: false }) | ||
|
||
|
@@ -129,6 +132,8 @@ export default { | |
|
||
beforeDestroy () { | ||
updateMetaTags(null, this.currentMetaTags) | ||
|
||
window.removeEventListener('scroll', this.onScroll) | ||
}, | ||
|
||
methods: { | ||
|
@@ -152,7 +157,10 @@ export default { | |
this.toggleSidebar(false) | ||
} | ||
} | ||
} | ||
}, | ||
onScroll: throttle(() => { | ||
setActiveHash() | ||
}, 200) | ||
} | ||
} | ||
|
||
|
@@ -173,6 +181,35 @@ function updateMetaTags (meta, current) { | |
}) | ||
} | ||
} | ||
|
||
function setActiveHash () { | ||
const anchors = gatherHeaderAnchors() | ||
|
||
const scrollTop = Math.max(window.pageYOffset, document.documentElement.scrollTop, document.body.scrollTop) | ||
|
||
for (let i = 0; i < anchors.length; i++) { | ||
const anchor = anchors[i] | ||
const nextAnchor = anchors[i + 1] | ||
|
||
const isActive = i === 0 && scrollTop === 0 || | ||
(scrollTop >= anchor.parentElement.offsetTop + 10 && | ||
(typeof nextAnchor === 'undefined' || scrollTop < nextAnchor.parentElement.offsetTop - 10)) | ||
|
||
if (isActive && window.location.hash !== anchor.hash) { | ||
window.location.hash = anchor.hash | ||
return | ||
} | ||
} | ||
} | ||
|
||
function gatherHeaderAnchors () { | ||
const sidebarLinks = Array.from(document.querySelectorAll('.sidebar-group-items a.sidebar-link')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use array spread instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried spread initially, but it seems to have some problems with NodeLists (https://gitlab.com/Rich-Harris/buble/issues/81). I would really prefer to use it here instead though. Do you have a preference for something else, assuming spread wouldn't work? |
||
|
||
const anchors = Array.from(document.querySelectorAll('a.header-anchor')) | ||
.filter(x => sidebarLinks.map(x => x.hash).includes(x.hash)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, |
||
|
||
return anchors | ||
} | ||
</script> | ||
|
||
<style src="prismjs/themes/prism-tomorrow.css"></style> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
lodash.throttle
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it should be
lodash.throttle
.