/
pages.vue
141 lines (130 loc) · 3.58 KB
/
pages.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<script setup lang="ts">
import { objectPick } from '@antfu/utils'
import { nextTick } from 'vue'
import type { RouteInfo } from '~~/../src/types'
definePageMeta({
icon: 'carbon-tree-view-alt',
title: 'Pages',
shouldShow: () => !!useClient().value,
order: 1,
})
const router = useClientRouter()
const route = useClientRoute()
const config = useServerConfig()
const serverPages = useServerPages()
const layouts = useLayouts()
const routes = computed((): RouteInfo[] => {
return (router.value?.getRoutes() || [])
.map(i => objectPick(i, ['path', 'name', 'meta', 'props', 'children']))
.map((i) => {
return {
...serverPages.value?.find(j => j.name && j.name === i.name),
...i,
}
})
})
const routeInput = ref('')
until(route).toBeTruthy().then((v) => {
routeInput.value = v.path
})
until(router).toBeTruthy().then((v) => {
v.afterEach(() => {
nextTick(() => {
routeInput.value = route.value.path
})
})
})
async function navigate() {
if (routeInput.value !== route.value.path)
router.value.push(routeInput.value || '/')
}
const routeInputMatched = computed(() => {
if (routeInput.value === route.value.path)
return []
return router.value.resolve(routeInput.value || '/').matched
})
function navigateToRoute(path: string) {
router.value.push(path)
}
</script>
<template>
<div v-if="config?.pages && router" h-full of-auto>
<div border="b base" flex="~ col gap1" px4 py3 navbar-glass>
<div>
<template v-if="route.path !== routeInput">
<span op50>Navigate from </span>
<span font-mono>{{ route.path }}</span>
<span op50> to </span>
</template>
<template v-else>
<span op50>Current route</span>
</template>
</div>
<NTextInput
v-model="routeInput"
font-mono
icon="carbon-direction-right-01 scale-y--100"
:class="route.path === routeInput ? '' : routeInputMatched.length ? 'text-green' : 'text-orange' "
@keydown.enter="navigate"
/>
<div>
<template v-if="route.path !== routeInput">
<span>Press <b font-bold>Enter</b> to navigate</span>
<span v-if="!routeInputMatched.length" text-orange op75> (no match)</span>
</template>
<template v-else>
<span op50>Edit path above to navigate</span>
</template>
</div>
</div>
<NSectionBlock
v-if="routeInputMatched.length"
icon="carbon-tree-view"
text="Matched Routes"
:padding="false"
>
<RoutesTable
:pages="routeInputMatched"
:layouts="layouts || []"
:matched="[]"
:matched-pending="routeInputMatched"
@navigate="navigateToRoute"
/>
</NSectionBlock>
<NSectionBlock
icon="carbon-tree-view-alt"
text="All Routes"
:description="`${routes.length} routes registered in your application`"
padding="pr5"
>
<RoutesTable
:pages="routes"
:layouts="layouts || []"
:matched="route.matched"
:matched-pending="routeInputMatched"
@navigate="navigateToRoute"
/>
</NSectionBlock>
</div>
<LaunchPage
v-else
icon="carbon-tree-view-alt"
title="Nuxt Routing"
description="Create `./pages/index.vue` to enable routing"
:actions="[
{
label: 'Learn more',
src: 'https://nuxt.com/docs/getting-started/routing',
attrs: {
n: 'primary',
},
},
{
label: 'Enable Routing',
handle() {
return rpc.runWizard('enablePages')
},
},
]"
/>
</template>