feat(admin): 增加启动与导航反馈并保护工作标签状态

This commit is contained in:
2026-12-01 20:01:40 +08:00
parent d8a16b0778
commit f02d47468b
22 changed files with 869 additions and 141 deletions
+52
View File
@@ -0,0 +1,52 @@
import { isNavigationFailure, NavigationFailureType, type RouteLocationNormalized, type Router } from 'vue-router'
/**
* Observe the navigation, never decide it. Keep one lightweight bar across redirects
* and overlapping guards; a late failure for an older route doesn't own the bar.
*/
export function installNavigationFeedback(router: Router, doc: Document = document,
notify?: (failedPath?: string) => void) {
let owner: RouteLocationNormalized | undefined
let timer: ReturnType<typeof setTimeout> | undefined
let bar: HTMLElement | undefined
function stop() {
clearTimeout(timer)
timer = undefined
owner = undefined
bar?.remove()
bar = undefined
}
function owns(to: RouteLocationNormalized) {
return owner === to || (to.redirectedFrom && to.redirectedFrom === (owner?.redirectedFrom ?? owner))
}
router.beforeEach(to => {
if (doc.getElementById('admin-boot')) return
notify?.()
owner = to
if (timer !== undefined || bar) return
timer = setTimeout(() => {
timer = undefined
bar = doc.createElement('div')
bar.id = 'admin-route-progress'
bar.setAttribute('role', 'progressbar')
bar.setAttribute('aria-label', '正在打开页面')
doc.body.appendChild(bar)
}, 180)
})
router.afterEach((to, _from, failure) => {
// Duplicates bypass beforeEach, including redirects back to the current route.
if (owns(to) || isNavigationFailure(failure, NavigationFailureType.duplicated)) stop()
})
router.onError((error, to) => {
if (owns(to)) {
stop()
// Page assets failed after the app started. Don't reload over an
// unsaved form or mistake a background request for a route error.
notify?.(to.fullPath)
}
console.error('路由导航失败:', error)
})
}
+5
View File
@@ -0,0 +1,5 @@
import { shallowRef } from 'vue'
// Ephemeral UI state. Navigation targets are issued only by the existing router.
export const failedRoute = shallowRef<string>()
export function reportRouteLoad(path?: string) { failedRoute.value = path }