Skip to content

Commit 344987a

Browse files
wzc520pyfmalexzhang1030webfansplz
authored
feat: add route-meta for pages (#560)
Co-authored-by: Alex <[email protected]> Co-authored-by: arlo <[email protected]>
1 parent 1ae7a28 commit 344987a

File tree

5 files changed

+70
-19
lines changed

5 files changed

+70
-19
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script setup lang="ts">
2+
import { VueCodeBlock } from '@vue/devtools-ui'
3+
import type { RouteMeta } from 'vue-router'
4+
5+
defineProps<{
6+
meta: RouteMeta
7+
}>()
8+
9+
defineEmits<{
10+
close: []
11+
}>()
12+
</script>
13+
14+
<template>
15+
<div p-2>
16+
<div class="flex items-center justify-between">
17+
<span class="font-500">Route meta detail</span>
18+
<div class="i-carbon-close cursor-pointer p1 $ui-text" @click="$emit('close')" />
19+
</div>
20+
<VueCodeBlock :code="JSON.stringify(meta, null, 2)" lang="json" lines />
21+
</div>
22+
</template>

packages/client/src/components/pages/RoutesTable.vue

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { VueBadge } from '@vue/devtools-ui'
3-
import type { RouteRecordNormalized } from 'vue-router'
3+
import type { RouteMeta, RouteRecordNormalized } from 'vue-router'
44
import { useDevToolsState } from '@vue/devtools-core'
55
import { openInEditor } from '../../composables/open-in-editor'
66
@@ -12,6 +12,7 @@ const props = defineProps<{
1212
1313
defineEmits<{
1414
(e: 'navigate', path: string): void
15+
(e: 'selectMeta', meta: RouteMeta): void
1516
}>()
1617
1718
const sorted = computed(() => {
@@ -20,6 +21,15 @@ const sorted = computed(() => {
2021
2122
const _vueInspectorDetected = computed(() => vueInspectorDetected.value)
2223
const state = useDevToolsState()
24+
25+
function metaToString(meta: RouteMeta, num: number = 0) {
26+
const metaStr = JSON.stringify(meta, null, num)
27+
return metaStr === '{}' ? '-' : metaStr
28+
}
29+
30+
const metaFieldVisible = computed(() => {
31+
return sorted.value.some(item => Object.keys(item.meta)?.length)
32+
})
2333
</script>
2434

2535
<template>
@@ -34,7 +44,9 @@ const state = useDevToolsState()
3444
<th text-left>
3545
Name
3646
</th>
37-
<th text-left />
47+
<th v-if="metaFieldVisible" text-left>
48+
Route Meta
49+
</th>
3850
</tr>
3951
</thead>
4052
<tbody>
@@ -77,6 +89,9 @@ const state = useDevToolsState()
7789
<td w-0 ws-nowrap pr-1 text-left text-sm font-mono op50>
7890
{{ item.name }}
7991
</td>
92+
<td v-if="metaFieldVisible" w-50 ws-nowrap pr-1 text-left text-sm font-mono op50 hover="text-primary op100">
93+
<span inline-block w-50 cursor-pointer overflow-hidden text-ellipsis :title="metaToString(item.meta, 2)" @click="() => $emit('selectMeta', item.meta)">{{ metaToString(item.meta) }}</span>
94+
</td>
8095
</tr>
8196
</tbody>
8297
</table>

packages/client/src/pages/pages.vue

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import type { RouterInfo } from '@vue/devtools-kit'
33
import { VueInput } from '@vue/devtools-ui'
44
import { DevToolsMessagingEvents, onDevToolsConnected, rpc } from '@vue/devtools-core'
5-
import type { RouteLocationNormalizedLoaded, RouteRecordNormalized } from 'vue-router'
5+
import type { RouteLocationNormalizedLoaded, RouteMeta, RouteRecordNormalized } from 'vue-router'
6+
import { Pane, Splitpanes } from 'splitpanes'
67
78
const routeInput = ref('')
89
const currentRoute = ref<RouteLocationNormalizedLoaded | null>(null)
@@ -16,6 +17,8 @@ const routeInputMatched = computed(() => {
1617
1718
const routes = ref<RouteRecordNormalized[]>([])
1819
20+
const selectedMeta = ref<RouteMeta>()
21+
1922
function init(data: RouterInfo) {
2023
routes.value = data.routes
2124
currentRoute.value = data.currentRoute as RouteLocationNormalizedLoaded
@@ -54,7 +57,7 @@ onUnmounted(() => {
5457

5558
<template>
5659
<div block h-screen of-auto>
57-
<div h-full of-auto>
60+
<div h-full class="grid grid-rows-[auto_1fr]">
5861
<div border="b base" flex="~ col gap1" px4 py3>
5962
<div>
6063
<template v-if="false">
@@ -95,20 +98,28 @@ onUnmounted(() => {
9598
@navigate="navigateToRoute"
9699
/>
97100
</SectionBlock> -->
98-
<SectionBlock
99-
icon="i-carbon-tree-view-alt"
100-
text="All Routes"
101-
:description="`${routes.length} routes registered in your application`"
102-
:padding="false"
103-
>
104-
<RoutesTable
105-
v-if="routes.length"
106-
:pages="routes"
107-
:matched="currentRoute?.matched ?? []"
108-
:matched-pending="routeInputMatched"
109-
@navigate="navigateToRoute"
110-
/>
111-
</SectionBlock>
101+
<Splitpanes class="of-hidden">
102+
<Pane size="70" class="of-auto!">
103+
<SectionBlock
104+
icon="i-carbon-tree-view-alt"
105+
text="All Routes"
106+
:description="`${routes.length} routes registered in your application`"
107+
:padding="false"
108+
>
109+
<RoutesTable
110+
v-if="routes.length"
111+
:pages="routes"
112+
:matched="currentRoute?.matched ?? []"
113+
:matched-pending="routeInputMatched"
114+
@navigate="navigateToRoute"
115+
@select-meta="(meta: RouteMeta) => selectedMeta = meta"
116+
/>
117+
</SectionBlock>
118+
</Pane>
119+
<Pane v-if="!!selectedMeta" size="30" class="of-auto!">
120+
<RouteMetaDetail :meta="selectedMeta" @close="selectedMeta = undefined" />
121+
</Pane>
122+
</Splitpanes>
112123
</div>
113124
</div>
114125
</template>

packages/devtools-kit/src/core/router/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ function getRoutes(router?: Router) {
1414

1515
function filterRoutes(routes: RouteRecordRaw[]) {
1616
return routes.map((item) => {
17-
let { path, name, children } = item
17+
let { path, name, children, meta } = item
1818
if (children?.length)
1919
children = filterRoutes(children)
2020

2121
return {
2222
path,
2323
name,
2424
children,
25+
meta,
2526
}
2627
})
2728
}

packages/playground/basic/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ const routes: RouteRecordRaw[] = [
3838
path: '/hello',
3939
component: () => import('./pages/Hello.vue'),
4040
name: 'hello',
41+
meta: { auth: 'admin', note: 'Hey! Manger' },
4142
},
4243
{
4344
path: '/hey/:id',
4445
component: Hey,
4546
name: 'hey',
47+
meta: { auth: 'user', note: 'Hey!' },
4648
},
4749
{
4850
path: '/vue-query',

0 commit comments

Comments
 (0)