2022-01-31 18:58:19 +00:00
|
|
|
import { Component, KeepAlive, h } from 'vue'
|
2022-02-07 11:32:04 +00:00
|
|
|
import { RouterView, RouteLocationMatched, RouteLocationNormalizedLoaded } from 'vue-router'
|
|
|
|
|
|
|
|
type InstanceOf<T> = T extends new (...args: any[]) => infer R ? R : never
|
|
|
|
export type RouterViewSlotProps = Parameters<InstanceOf<typeof RouterView>['$slots']['default']>[0]
|
|
|
|
|
|
|
|
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() || '')
|
|
|
|
}
|
|
|
|
|
|
|
|
export const generateRouteKey = (override: string | ((route: RouteLocationNormalizedLoaded) => string), routeProps: RouterViewSlotProps) => {
|
|
|
|
const matchedRoute = routeProps.route.matched.find(m => m.components.default === routeProps.Component.type)
|
|
|
|
const source = override ?? matchedRoute?.meta.key ?? interpolatePath(routeProps.route, matchedRoute)
|
|
|
|
return typeof source === 'function' ? source(routeProps.route) : source
|
|
|
|
}
|
2022-01-31 18:58:19 +00:00
|
|
|
|
|
|
|
const Fragment = {
|
|
|
|
setup (_props, { slots }) {
|
|
|
|
return () => slots.default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const wrapIf = (component: Component, props: any, slots: any) => {
|
|
|
|
return { default: () => props ? h(component, props === true ? {} : props, slots) : h(Fragment, {}, slots) }
|
|
|
|
}
|
|
|
|
|
|
|
|
export const wrapInKeepAlive = (props: any, children: any) => {
|
|
|
|
return { default: () => process.client && props ? h(KeepAlive, props === true ? {} : props, children) : children }
|
|
|
|
}
|