2022-03-14 10:47:24 +00:00
|
|
|
import { KeepAlive, h } from 'vue'
|
2023-05-09 17:08:07 +00:00
|
|
|
import type { RouteLocationMatched, RouteLocationNormalizedLoaded, RouterView } from '#vue-router'
|
2022-02-07 11:32:04 +00:00
|
|
|
|
|
|
|
type InstanceOf<T> = T extends new (...args: any[]) => infer R ? R : never
|
2022-10-26 11:16:56 +00:00
|
|
|
type RouterViewSlot = Exclude<InstanceOf<typeof RouterView>['$slots']['default'], undefined>
|
|
|
|
export type RouterViewSlotProps = Parameters<RouterViewSlot>[0]
|
2022-02-07 11:32:04 +00:00
|
|
|
|
|
|
|
const interpolatePath = (route: RouteLocationNormalizedLoaded, match: RouteLocationMatched) => {
|
|
|
|
return match.path
|
2022-02-07 23:42:39 +00:00
|
|
|
.replace(/(:\w+)\([^)]+\)/g, '$1')
|
|
|
|
.replace(/(:\w+)[?+*]/g, '$1')
|
2022-02-07 11:32:04 +00:00
|
|
|
.replace(/:\w+/g, r => route.params[r.slice(1)]?.toString() || '')
|
|
|
|
}
|
|
|
|
|
2022-11-21 13:03:22 +00:00
|
|
|
export const generateRouteKey = (routeProps: RouterViewSlotProps, override?: string | ((route: RouteLocationNormalizedLoaded) => string)) => {
|
2022-08-12 17:47:58 +00:00
|
|
|
const matchedRoute = routeProps.route.matched.find(m => m.components?.default === routeProps.Component.type)
|
|
|
|
const source = override ?? matchedRoute?.meta.key ?? (matchedRoute && interpolatePath(routeProps.route, matchedRoute))
|
2022-02-07 11:32:04 +00:00
|
|
|
return typeof source === 'function' ? source(routeProps.route) : source
|
|
|
|
}
|
2022-01-31 18:58:19 +00:00
|
|
|
|
|
|
|
export const wrapInKeepAlive = (props: any, children: any) => {
|
|
|
|
return { default: () => process.client && props ? h(KeepAlive, props === true ? {} : props, children) : children }
|
|
|
|
}
|