]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(devtools): add more
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 9 Sep 2020 15:22:21 +0000 (17:22 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Wed, 4 Nov 2020 21:19:57 +0000 (22:19 +0100)
jest.config.js
src/devtools.ts
src/router.ts

index 7b070c133b22714264e91e04eed5177b6795c441..1a2de782d7a44c5413dea602c6160bfca54a4c75 100644 (file)
@@ -7,7 +7,12 @@ module.exports = {
   coverageDirectory: 'coverage',
   coverageReporters: ['html', 'lcov', 'text'],
   collectCoverageFrom: ['src/**/*.ts'],
-  coveragePathIgnorePatterns: ['/node_modules/', 'src/index.ts', 'src/entries'],
+  coveragePathIgnorePatterns: [
+    '/node_modules/',
+    'src/index.ts',
+    'src/entries',
+    'src/devtools.ts',
+  ],
   testMatch: ['<rootDir>/__tests__/**/*.spec.ts?(x)'],
   watchPathIgnorePatterns: ['<rootDir>/node_modules'],
   testEnvironment: 'node',
index 0e86cdb201bea90c57f036b1102d123f81b0f21b..b0bdab99b107ed8e24c4af412b4e25e9025a382c 100644 (file)
@@ -100,7 +100,6 @@ export function addDevtools(app: App, router: Router, matcher: RouterMatcher) {
         })
       })
 
-      console.log('adding devtools to timeline')
       router.beforeEach((to, from) => {
         const data: TimelineEvent<any, any>['data'] = {
           guard: formatDisplay('beforEach'),
@@ -111,7 +110,6 @@ export function addDevtools(app: App, router: Router, matcher: RouterMatcher) {
           to: formatRouteLocation(to, 'Target location'),
         }
 
-        console.log('adding to timeline')
         api.addTimelineEvent({
           layerId: navigationsLayerId,
           event: {
@@ -161,18 +159,30 @@ export function addDevtools(app: App, router: Router, matcher: RouterMatcher) {
         })
       })
 
-      const routerInspectorId = 'hahaha router-inspector'
+      const routerInspectorId = 'router-inspector'
 
       api.addInspector({
         id: routerInspectorId,
         label: 'Routes',
         icon: 'book',
-        treeFilterPlaceholder: 'Filter routes',
+        treeFilterPlaceholder: 'Search routes',
       })
 
       api.on.getInspectorTree(payload => {
         if (payload.app === app && payload.inspectorId === routerInspectorId) {
-          const routes = matcher.getRoutes().filter(route => !route.parent)
+          const routes = matcher.getRoutes().filter(
+            route =>
+              !route.parent &&
+              (!payload.filter ||
+                // save isActive state
+                isRouteMatching(route, payload.filter))
+          )
+          // reset match state if no filter is provided
+          if (!payload.filter) {
+            routes.forEach(route => {
+              ;(route as any).__vd_match = false
+            })
+          }
           payload.rootNodes = routes.map(formatRouteRecordForInspector)
         }
       })
@@ -248,7 +258,7 @@ function formatRouteRecordMatcherForStateInspector(
     fields.push({
       editable: false,
       key: 'aliases',
-      value: route.alias,
+      value: route.alias.map(alias => alias.record.path),
     })
 
   fields.push({
@@ -291,6 +301,14 @@ function formatRouteRecordForInspector(
     })
   }
 
+  if ((route as any).__vd_match) {
+    tags.push({
+      label: 'matches',
+      textColor: 0,
+      backgroundColor: 0xf4f4f4,
+    })
+  }
+
   if (record.redirect) {
     tags.push({
       label:
@@ -309,3 +327,33 @@ function formatRouteRecordForInspector(
     children: route.children.map(formatRouteRecordForInspector),
   }
 }
+
+const EXTRACT_REGEXP_RE = /^\/(.*)\/([a-z]*)$/
+
+function isRouteMatching(route: RouteRecordMatcher, filter: string): boolean {
+  const found = String(route.re).match(EXTRACT_REGEXP_RE)
+  // reset the matching state
+  ;(route as any).__vd_match = false
+  if (!found || found.length < 3) return false
+
+  // use a regexp without $ at the end to match nested routes better
+  const nonEndingRE = new RegExp(found[1].replace(/\$$/, ''), found[2])
+  if (nonEndingRE.test(filter)) {
+    // mark children as matches
+    route.children.some(child => isRouteMatching(child, filter))
+    // exception case: `/`
+    if (route.record.path !== '/' || filter === '/') {
+      ;(route as any).__vd_match = route.re.test(filter)
+      return true
+    }
+    // hide the / route
+    return false
+  }
+
+  // also allow partial matching on the path
+  if (route.record.path.startsWith(filter)) return true
+  if (route.record.name && String(route.record.name).includes(filter))
+    return true
+
+  return route.children.some(child => isRouteMatching(child, filter))
+}
index 0be24c5aba535d18143cc2e788a8d7930b2c3a5f..9b64f09b2115a244a65f52725ddd243b834362b4 100644 (file)
@@ -1105,7 +1105,7 @@ export function createRouter(options: RouterOptions): Router {
         unmountApp.call(this, arguments)
       }
 
-      if (__DEV__) {
+      if (__DEV__ && __BROWSER__) {
         addDevtools(app, router, matcher)
       }
     },