mirror of
https://github.com/fmhy/edit.git
synced 2026-02-18 09:11:33 +11:00
improve
This commit is contained in:
parent
4772ae05ae
commit
c0aee824cd
1 changed files with 24 additions and 19 deletions
|
|
@ -1,25 +1,30 @@
|
||||||
import nprogress, { type NProgress } from 'nprogress' // Import NProgress library and its type definition
|
import { NProgress, type Configuration as NProgressConfiguration } from 'nprogress'
|
||||||
import type { EnhanceAppContext } from 'vitepress' // Import EnhanceAppContext type definition from VitePress
|
import type { EnhanceAppContext } from 'vitepress'
|
||||||
|
|
||||||
export function loadProgress(router: EnhanceAppContext['router']): NProgress { // Export a function called loadProgress that takes in a router object as an argument
|
let nProgressInstance: NProgress | null = null
|
||||||
if (typeof window === 'undefined') return // Return early if the window object is not defined
|
|
||||||
|
|
||||||
// Initialize NProgress with some custom configurations
|
export function loadProgress(router: EnhanceAppContext['router']): NProgress {
|
||||||
setTimeout(() => {
|
if (typeof window === 'undefined') {
|
||||||
nprogress.configure({ showSpinner: false })
|
return {
|
||||||
|
start: () => {},
|
||||||
|
done: () => {},
|
||||||
|
configure: () => {},
|
||||||
|
} as NProgress
|
||||||
|
}
|
||||||
|
|
||||||
// Set up event listeners for the router's onBeforeRouteChange and onAfterRouteChanged events
|
if (!nProgressInstance) {
|
||||||
const cacheBeforeRouteChange = router.onBeforeRouteChange
|
nProgressInstance = new NProgress((config: NProgressConfiguration) => {
|
||||||
const cacheAfterRouteChange = router.onAfterRouteChanged
|
config.showSpinner = false
|
||||||
router.onBeforeRouteChange = (to) => {
|
})
|
||||||
nprogress.start() // Start the NProgress bar when the user navigates to a new page
|
}
|
||||||
cacheBeforeRouteChange?.(to) // Call the original onBeforeRouteChange event handler
|
|
||||||
}
|
router.onBeforeRouteUpdate(() => {
|
||||||
router.onAfterRouteChanged = (to) => {
|
nProgressInstance.start()
|
||||||
nprogress.done() // Stop the NProgress bar after the route has changed
|
|
||||||
cacheAfterRouteChange?.(to) // Call the original onAfterRouteChanged event handler
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return nprogress // Return the NProgress object
|
router.onAfterRouteUpdated(() => {
|
||||||
|
nProgressInstance.done()
|
||||||
|
})
|
||||||
|
|
||||||
|
return nProgressInstance
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue