From ee0730254522d6162114968e4d62b93e8b6f7f93 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 9 Sep 2020 17:22:21 +0200 Subject: [PATCH] feat(devtools): add more --- jest.config.js | 7 +++++- src/devtools.ts | 60 ++++++++++++++++++++++++++++++++++++++++++++----- src/router.ts | 2 +- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/jest.config.js b/jest.config.js index 7b070c13..1a2de782 100644 --- a/jest.config.js +++ b/jest.config.js @@ -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: ['/__tests__/**/*.spec.ts?(x)'], watchPathIgnorePatterns: ['/node_modules'], testEnvironment: 'node', diff --git a/src/devtools.ts b/src/devtools.ts index 0e86cdb2..b0bdab99 100644 --- a/src/devtools.ts +++ b/src/devtools.ts @@ -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['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)) +} diff --git a/src/router.ts b/src/router.ts index 0be24c5a..9b64f09b 100644 --- a/src/router.ts +++ b/src/router.ts @@ -1105,7 +1105,7 @@ export function createRouter(options: RouterOptions): Router { unmountApp.call(this, arguments) } - if (__DEV__) { + if (__DEV__ && __BROWSER__) { addDevtools(app, router, matcher) } }, -- 2.47.3