Skip to content

Commit

Permalink
feat(devtools): display router view path
Browse files Browse the repository at this point in the history
Close #1119
  • Loading branch information
posva committed Sep 29, 2021
1 parent a56fce2 commit 3ce3834
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/RouterView.ts
Expand Up @@ -24,7 +24,7 @@ import {
viewDepthKey,
routerViewLocationKey,
} from './injectionSymbols'
import { assign } from './utils'
import { assign, isBrowser } from './utils'
import { warn } from './warning'
import { isSameRouteRecord } from './location'

Expand All @@ -34,6 +34,11 @@ export interface RouterViewProps {
route?: RouteLocationNormalized
}

export interface RouterViewDevtoolsContext
extends Pick<RouteLocationMatched, 'path' | 'name' | 'meta'> {
depth: number
}

export const RouterViewImpl = /*#__PURE__*/ defineComponent({
name: 'RouterView',
// #674 we manually inherit them
Expand Down Expand Up @@ -141,6 +146,29 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
})
)

if (
(__DEV__ || __FEATURE_PROD_DEVTOOLS__) &&
isBrowser &&
component.ref
) {
// TODO: can display if it's an alias, its props
const info: RouterViewDevtoolsContext = {
depth,
name: matchedRoute.name,
path: matchedRoute.path,
meta: matchedRoute.meta,
}

const internalInstances = Array.isArray(component.ref)
? component.ref.map(r => r.i)
: [component.ref.i]

internalInstances.forEach(instance => {
// @ts-expect-error
instance.__vrv_devtools = info
})
}

return (
// pass the vnode to the slot as a prop.
// h and <component :is="..."> both accept vnodes
Expand Down
14 changes: 13 additions & 1 deletion src/devtools.ts
Expand Up @@ -15,6 +15,7 @@ import { RouteRecordMatcher } from './matcher/pathMatcher'
import { PathParser } from './matcher/pathParserRanker'
import { Router } from './router'
import { UseLinkDevtoolsContext } from './RouterLink'
import { RouterViewDevtoolsContext } from './RouterView'
import { RouteLocationNormalized } from './types'
import { assign } from './utils'

Expand Down Expand Up @@ -86,8 +87,19 @@ export function addDevtools(app: App, router: Router, matcher: RouterMatcher) {
}
})

// mark router-link as active
// mark router-link as active and display tags on router views
api.on.visitComponentTree(({ treeNode: node, componentInstance }) => {
if (componentInstance.__vrv_devtools) {
const info =
componentInstance.__vrv_devtools as RouterViewDevtoolsContext

node.tags.push({
label: (info.name ? `${info.name.toString()}: ` : '') + info.path,
textColor: 0,
tooltip: 'This component is rendered by &lt;router-view&gt;',
backgroundColor: PINK_500,
})
}
// if multiple useLink are used
if (Array.isArray(componentInstance.__vrl_devtools)) {
componentInstance.__devtoolsApi = api
Expand Down

0 comments on commit 3ce3834

Please sign in to comment.