Skip to content

Commit 6673237

Browse files
committed
feat(client)!: shorten data keys
1 parent 6252869 commit 6673237

File tree

18 files changed

+242
-180
lines changed

18 files changed

+242
-180
lines changed

e2e/docs/.vuepress/theme/client/layouts/Layout.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<script setup lang="ts">
2-
import { Content, useSiteData } from 'vuepress/client'
3-
4-
const siteData = useSiteData()
2+
import { Content } from 'vuepress/client'
53
</script>
64

75
<template>
86
<div class="e2e-theme">
97
<nav class="e2e-theme-nav">
108
<div>Languages</div>
119
<ul>
12-
<li v-for="[key, value] in Object.entries(siteData.locales)" :key="key">
10+
<li v-for="[key, value] in Object.entries($site.locales)" :key="key">
1311
<RouterLink :to="key">{{ value.lang }}</RouterLink>
1412
</li>
1513
</ul>

packages/client/src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { clientConfigs } from '@internal/clientConfigs'
22
import { createApp, createSSRApp, h } from 'vue'
3-
import { usePageLayout } from './composables/index.js'
3+
import { useData } from './composables/index.js'
44
import { siteData } from './internal/siteData.js'
55
import { createVueRouter } from './router/createVueRouter.js'
66
import { setupGlobalComponents } from './setupGlobalComponents.js'
@@ -35,7 +35,7 @@ export const createVueApp: CreateVueAppFunction = async () => {
3535
)
3636

3737
// get page layout
38-
const pageLayout = usePageLayout()
38+
const { pageLayout } = useData()
3939

4040
// render layout and root components
4141
return () => [h(pageLayout.value), clientRootComponents]

packages/client/src/components/AutoLink.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isLinkWithProtocol } from '@vuepress/shared'
22
import type { PropType, SlotsType, VNode } from 'vue'
33
import { computed, defineComponent, h, toRef } from 'vue'
44
import { useRoute } from 'vue-router'
5-
import { useSiteData } from '../composables/index.js'
5+
import { useSite } from '../composables/index.js'
66
import { RouteLink } from './RouteLink.js'
77

88
export interface AutoLinkConfig {
@@ -67,7 +67,7 @@ export const AutoLink = defineComponent({
6767
setup(props, { slots }) {
6868
const config = toRef(props, 'config')
6969
const route = useRoute()
70-
const siteData = useSiteData()
70+
const site = useSite()
7171

7272
// If the link has non-http protocol
7373
const withProtocol = computed(() => isLinkWithProtocol(config.value.link))
@@ -102,7 +102,7 @@ export const AutoLink = defineComponent({
102102
// Should not be active in `exact` mode
103103
if (config.value.exact) return false
104104

105-
const localePaths = Object.keys(siteData.value.locales)
105+
const localePaths = Object.keys(site.value.locales)
106106

107107
return localePaths.length
108108
? // Check all the locales

packages/client/src/components/Content.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { computed, defineAsyncComponent, defineComponent, h, watch } from 'vue'
2-
import { usePageComponent, usePageFrontmatter } from '../composables/index.js'
2+
import { useData } from '../composables/index.js'
33
import { contentUpdatedCallbacks } from '../internal/contentUpdatedCallbacks'
44
import { resolveRoute } from '../router/index.js'
55
import type { ContentUpdatedReason } from '../types/index.js'
@@ -28,7 +28,7 @@ export const Content = defineComponent({
2828
},
2929

3030
setup(props) {
31-
const pageComponent = usePageComponent()
31+
const { frontmatter, pageComponent } = useData()
3232
const ContentComponent = computed(() => {
3333
if (!props.path) return pageComponent.value
3434
const route = resolveRoute(props.path)
@@ -37,7 +37,6 @@ export const Content = defineComponent({
3737
)
3838
})
3939

40-
const frontmatter = usePageFrontmatter()
4140
watch(
4241
frontmatter,
4342
() => {

packages/client/src/composables/clientData.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/client/src/composables/clientDataUtils.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { InjectionKey } from 'vue'
2+
import { inject } from 'vue'
3+
import type { Data } from '../types/index.js'
4+
5+
/**
6+
* Injection key for vuepress data
7+
*/
8+
export const dataSymbol: InjectionKey<Data> = Symbol(
9+
__VUEPRESS_DEV__ ? 'data' : '',
10+
)
11+
12+
/**
13+
* Returns VuePress data
14+
*/
15+
export const useData = <
16+
Frontmatter extends Record<string, unknown> = Record<string, unknown>,
17+
Page extends Record<string, unknown> = Record<string, unknown>,
18+
>(): Data<Frontmatter, Page> => {
19+
const data = inject<Data<Frontmatter, Page>>(dataSymbol)
20+
if (!data) {
21+
throw new Error('useData() is called without provider.')
22+
}
23+
return data
24+
}
25+
26+
// FIXME: remove this in stable
27+
28+
/** @deprecated using `useData` instead */
29+
export const useClientData = useData
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import type {
2+
LayoutsRef,
3+
PageComponentRef,
4+
PageDataRef,
5+
PageFrontmatterRef,
6+
PageHeadRef,
7+
PageLangRef,
8+
PageLayoutRef,
9+
RedirectsRef,
10+
RouteLocaleRef,
11+
RoutePathRef,
12+
RoutesRef,
13+
SiteDataRef,
14+
SiteLocaleDataRef,
15+
} from '../types/index.js'
16+
import { useData } from './data.js'
17+
18+
// page related
19+
20+
/**
21+
* Get page data
22+
*/
23+
export const usePage = <
24+
T extends Record<string, unknown> = Record<string, unknown>,
25+
>(): PageDataRef<T> => useData<Record<string, unknown>, T>().page
26+
/**
27+
* Get page frontmatter
28+
*/
29+
export const useFrontmatter = <
30+
T extends Record<string, unknown> = Record<string, unknown>,
31+
>(): PageFrontmatterRef<T> => useData<T>().frontmatter
32+
/**
33+
* Get page head
34+
*/
35+
export const useHead = (): PageHeadRef => useData().head
36+
/**
37+
* Get page lang
38+
*/
39+
export const useLang = (): PageLangRef => useData().lang
40+
41+
// route related
42+
43+
/**
44+
* Get routes map
45+
*/
46+
export const useRoutes = (): RoutesRef => useData().routes
47+
/**
48+
* Get redirect records
49+
*/
50+
export const useRedirects = (): RedirectsRef => useData().redirects
51+
52+
/**
53+
* Get route locale
54+
*/
55+
export const useRouteLocale = (): RouteLocaleRef => useData().routeLocale
56+
/**
57+
* Get route path
58+
*/
59+
export const useRoutePath = (): RoutePathRef => useData().routePath
60+
61+
// site related
62+
/**
63+
* Get site data
64+
*/
65+
export const useSite = (): SiteDataRef => useData().site
66+
/**
67+
* Get site locale data
68+
*/
69+
export const useSiteLocale = (): SiteLocaleDataRef => useData().siteLocale
70+
71+
// internal composables before `useData` was introduced, which are never expected to be called outside of vuePress/client
72+
// these are marked deprecated and kept for backward compatibility for now
73+
// FIXME: remove these in stable
74+
75+
/** @deprecated use useData().pageComponent instead */
76+
export const usePageComponent = (): PageComponentRef => useData().pageComponent
77+
/** @deprecated use useData().pageLayout instead */
78+
export const usePageLayout = (): PageLayoutRef => useData().pageLayout
79+
/** @deprecated use useData().layouts instead */
80+
export const useLayouts = (): LayoutsRef => useData().layouts
81+
82+
// original long composable names
83+
// these are kept for backward compatibility, and might not need to implicitly marked as deprecated
84+
85+
/** Note: suggest using `usePage` instead */
86+
export const usePageData = <
87+
T extends Record<string, unknown> = Record<string, unknown>,
88+
>(): PageDataRef<T> => useData<Record<string, unknown>, T>().page
89+
/** Note: suggest using `useFrontmatter` instead */
90+
export const usePageFrontmatter = <
91+
T extends Record<string, unknown> = Record<string, unknown>,
92+
>(): PageFrontmatterRef<T> => useData<T>().frontmatter
93+
/** Note: suggest using `useHead` instead */
94+
export const usePageHead = (): PageHeadRef => useData().head
95+
/** Note: suggest using `useLang` instead */
96+
export const usePageLang = (): PageLangRef => useData().lang
97+
/** Note: suggest using `useSite` instead */
98+
export const useSiteData = (): SiteDataRef => useData().site
99+
/** Note: suggest using `useSiteLocale` instead */
100+
export const useSiteLocaleData = (): SiteLocaleDataRef => useData().siteLocale
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from './clientData.js'
2-
export * from './clientDataUtils.js'
1+
export * from './data.js'
2+
export * from './dataUtils.js'
33
export * from './onContentUpdated.js'
44
export * from './updateHead.js'

packages/client/src/devtools/constants.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const INSPECTOR_NODE_INTERNAL = {
1717
const INSPECTOR_NODE_SITE = {
1818
id: 'SITE',
1919
label: 'Site',
20-
keys: ['siteData', 'siteLocaleData'],
20+
keys: ['site', 'siteLocale'],
2121
} as const satisfies InspectorNodeConfig
2222

2323
const INSPECTOR_NODE_ROUTE = {
@@ -30,11 +30,11 @@ const INSPECTOR_NODE_PAGE = {
3030
id: 'PAGE',
3131
label: 'Page',
3232
keys: [
33-
'pageData',
34-
'pageFrontmatter',
35-
'pageLang',
36-
'pageHead',
37-
'pageHeadTitle',
33+
'page',
34+
'frontmatter',
35+
'lang',
36+
'head',
37+
'headTitle',
3838
'pageLayout',
3939
'pageComponent',
4040
],

packages/client/src/devtools/setupDevtools.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { setupDevtoolsPlugin } from '@vue/devtools-api'
22
import type { App } from 'vue'
33
import { watch } from 'vue'
4-
import type { ClientData } from '../types/index.js'
4+
import type { Data } from '../types/index.js'
55
import * as DEVTOOLS from './constants.js'
6-
import type {
7-
ClientDataKey,
8-
ClientDataValue,
9-
InspectorNodeConfig,
10-
} from './types.js'
6+
import type { DataKey, DataValue, InspectorNodeConfig } from './types.js'
117

12-
export const setupDevtools = (app: App, clientData: ClientData): void => {
8+
export const setupDevtools = (app: App, clientData: Data): void => {
139
setupDevtoolsPlugin(
1410
{
1511
// fix recursive reference
@@ -23,11 +19,11 @@ export const setupDevtools = (app: App, clientData: ClientData): void => {
2319
},
2420
(api) => {
2521
const clientDataEntries = Object.entries(clientData) as [
26-
ClientDataKey,
27-
ClientDataValue,
22+
DataKey,
23+
DataValue,
2824
][]
29-
const clientDataKeys = Object.keys(clientData) as ClientDataKey[]
30-
const clientDataValues = Object.values(clientData) as ClientDataValue[]
25+
const clientDataKeys = Object.keys(clientData) as DataKey[]
26+
const clientDataValues = Object.values(clientData) as DataValue[]
3127

3228
// setup component state
3329
api.on.inspectComponent((payload) => {
@@ -55,7 +51,7 @@ export const setupDevtools = (app: App, clientData: ClientData): void => {
5551
(node) => ({
5652
id: node.id,
5753
label: node.label,
58-
children: node.keys.map((key: ClientDataKey) => ({
54+
children: node.keys.map((key: DataKey) => ({
5955
id: key,
6056
label: key,
6157
})),
@@ -81,12 +77,12 @@ export const setupDevtools = (app: App, clientData: ClientData): void => {
8177
}
8278

8379
// root nodes children state
84-
if (clientDataKeys.includes(payload.nodeId as ClientDataKey)) {
80+
if (clientDataKeys.includes(payload.nodeId as DataKey)) {
8581
payload.state = {
8682
[DEVTOOLS.INSPECTOR_STATE_SECTION_NAME]: [
8783
{
8884
key: payload.nodeId,
89-
value: clientData[payload.nodeId as ClientDataKey].value,
85+
value: clientData[payload.nodeId as DataKey].value,
9086
},
9187
],
9288
}

packages/client/src/devtools/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { CustomInspectorNode } from '@vue/devtools-kit'
2-
import type { ClientData } from '../types/index.js'
2+
import type { Data } from '../types/index.js'
33

4-
export type ClientDataKey = keyof ClientData
5-
export type ClientDataValue = ClientData[ClientDataKey]
4+
export type DataKey = keyof Data
5+
export type DataValue = Data[DataKey]
66

77
export interface InspectorNodeConfig
88
extends Pick<CustomInspectorNode, 'id' | 'label'> {
9-
keys: ClientDataKey[]
9+
keys: DataKey[]
1010
}

0 commit comments

Comments
 (0)