Skip to content

Commit a11237c

Browse files
authored
fix: fix define getter (#304)
1 parent adcdae0 commit a11237c

File tree

14 files changed

+155
-56
lines changed

14 files changed

+155
-56
lines changed

docs/plugins/markdown/revealjs/README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,27 @@ You can enable built-in plugins in reveal.js via `plugins` in plugin options. It
162162

163163
You can also import and call `defineRevealJsConfig` in [client config file][client-config] to customize reveal.js:
164164

165+
The `defineRevealJsConfig` function accepts a ref, getter or plain object as reveal.js options:
166+
165167
```js title=".vuepress/client.js"
166168
import { defineRevealJsConfig } from '@vuepress/plugin-revealjs/client'
167169

168-
defineRevealJsConfig({
169-
// reveal.js options here
170+
// plain object
171+
const options1 = {
172+
// options
173+
}
174+
175+
// or getter
176+
const options2 = () => ({
177+
// options
170178
})
179+
180+
// or ref
181+
const options3 = ref({
182+
// options
183+
})
184+
185+
defineRevealJsConfig(options1or2or3)
171186
```
172187

173188
::: note

docs/plugins/search/docsearch.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,18 @@ export default {
357357
### defineDocSearchConfig
358358

359359
```ts
360-
interface DocSearchClientOptions extends DocSearchProps {
361-
locales?: Record<string, DocSearchProps>
360+
type DocSearchClientLocaleOptions = Partial<DocSearchProps>
361+
362+
interface DocSearchClientOptions extends DocSearchClientLocaleOptions {
363+
locales?: Record<string, DocSearchClientLocaleOptions>
362364
}
363365

364-
const defineDocSearchConfig: (options: DocSearchClientOptions) => void
366+
const defineDocSearchConfig: (
367+
options: MaybeRefOrGetter<DocSearchClientOptions>,
368+
) => void
365369
```
366370
367-
Customize DocSearch options.
371+
Customize DocSearch options, support plain object, ref or getter.
368372
369373
::: warning
370374

docs/plugins/search/slimsearch.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ In particular, [DocSearch](https://docsearch.algolia.com/) is a free search serv
551551

552552
### defineSearchConfig
553553

554-
Customize [search options](https://mister-hope.github.io/slimsearch/interfaces/SearchOptions.html).
554+
Customize [search options](https://mister-hope.github.io/slimsearch/interfaces/SearchOptions.html), accepts plain object, ref or getter functions as parameters.
555555

556556
Since searching is done in a Web Worker, setting function-typed options for `slimsearch` is not supported.
557557

@@ -587,6 +587,10 @@ interface SearchOptions extends SearchLocaleOptions {
587587
/** Setting different options per locale */
588588
locales?: Record<string, SearchLocaleOptions>
589589
}
590+
591+
export const defineSearchConfig: (
592+
options: MaybeRefOrGetter<SearchOptions>,
593+
) => void
590594
```
591595
592596
```ts title=".vuepress/client.ts"

docs/zh/plugins/markdown/revealjs/README.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,27 @@ layout: SlidePage
162162

163163
你也可以在[客户端配置文件][client-config]中导入并调用 `defineRevealJsConfig` 来自定义 reveal.js:
164164

165+
`defineRevealJsConfig` 函数接受一个 ref、getter 或普通对象作为 reveal.js 选项:
166+
165167
```ts title=".vuepress/client.ts"
166168
import { defineRevealJsConfig } from '@vuepress/plugin-revealjs/client'
167169

168-
defineRevealJsConfig({
169-
// 在此设置 reveal.js 选项
170+
// 普通对象
171+
const options1 = {
172+
// 选项
173+
}
174+
175+
// 或 getter
176+
const options2 = () => ({
177+
// 选项
178+
})
179+
180+
// 或 ref
181+
const options3 = ref({
182+
// 选项
170183
})
184+
185+
defineRevealJsConfig(options1or2or3)
171186
```
172187

173188
::: note
@@ -218,3 +233,7 @@ Reveal.js 选项,请参见[reveal.js config](https://revealjs.com/config/),R
218233
@[code css](@vuepress/plugin-revealjs/src/client/styles/vars.css)
219234

220235
[client-config]: https://vuejs.press/zh/guide/configuration.html#%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6
236+
237+
```
238+
239+
```

docs/zh/plugins/search/docsearch.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,18 @@ export default {
356356
### defineDocSearchConfig
357357

358358
```ts
359-
interface DocSearchClientOptions extends DocSearchProps {
360-
locales?: Record<string, DocSearchProps>
359+
type DocSearchClientLocaleOptions = Partial<DocSearchProps>
360+
361+
interface DocSearchClientOptions extends DocSearchClientLocaleOptions {
362+
locales?: Record<string, DocSearchClientLocaleOptions>
361363
}
362364

363-
const defineDocSearchConfig: (options: DocSearchClientOptions) => void
365+
const defineDocSearchConfig: (
366+
options: MaybeRefOrGetter<DocSearchClientOptions>,
367+
) => void
364368
```
365369
366-
自定义 DocSearch 选项。
370+
自定义 DocSearch 选项,支持普通对象,Ref 者 Getter
367371
368372
::: warning
369373

docs/zh/plugins/search/slimsearch.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ terminate()
554554

555555
### defineSearchConfig
556556

557-
自定义 [搜索选项](https://mister-hope.github.io/slimsearch/interfaces/SearchOptions.html)
557+
自定义 [搜索选项](https://mister-hope.github.io/slimsearch/interfaces/SearchOptions.html),接受普通对象,Ref 或 Getter
558558

559559
由于搜索是在 Web Worker 中完成的,因此不支持 `slimsearch` 中需要被设置为函数的选项。
560560

@@ -590,6 +590,10 @@ interface SearchOptions extends SearchLocaleOptions {
590590
/** 基于每个语言来设置选项 */
591591
locales?: Record<string, SearchLocaleOptions>
592592
}
593+
594+
export const defineSearchConfig: (
595+
options: MaybeRefOrGetter<SearchOptions>,
596+
) => void
593597
```
594598
595599
```ts title=".vuepress/client.ts"

plugins/blog/plugin-comment/src/client/helpers/comment.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isFunction } from '@vuepress/helper/client'
22
import type { App, MaybeRefOrGetter, Ref } from 'vue'
3-
import { inject, isRef, ref, watch } from 'vue'
3+
import { computed, inject, isRef, readonly, ref, watch } from 'vue'
44
import type {
55
ArtalkOptions,
66
CommentOptions,
@@ -27,9 +27,10 @@ const defineCommentConfig = <T extends CommentOptions>(
2727
(value) => {
2828
comment.value = { ...commentOptions, ...value }
2929
},
30+
{ immediate: true },
3031
)
3132
} else if (isFunction(options)) {
32-
watch(options, (value) => {
33+
watch(computed(options), (value) => {
3334
comment.value = { ...commentOptions, ...value }
3435
})
3536
} else {
@@ -57,5 +58,5 @@ export const defineWalineConfig = defineCommentConfig<WalineOptions>
5758
export const useWalineOptions = useCommentOptions<WalineOptions>
5859

5960
export const injectCommentConfig = (app: App): void => {
60-
app.provide(commentSymbol, comment)
61+
app.provide(commentSymbol, readonly(comment))
6162
}

plugins/features/plugin-catalog/src/client/helpers/useCatalogInfoGetter.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isString } from '@vuepress/helper/client'
2-
import type { App, Component } from 'vue'
2+
import type { App, Component, InjectionKey } from 'vue'
33
import { inject } from 'vue'
44

55
export interface CatalogInfo {
@@ -30,7 +30,9 @@ export type CatalogInfoGetter = (
3030
let catalogGetter: CatalogInfoGetter = (meta) =>
3131
isString(meta.title) ? { title: meta.title } : null
3232

33-
const catalogInfoSymbol = Symbol(__VUEPRESS_DEV__ ? 'catalog-info-getter' : '')
33+
const catalogInfoSymbol: InjectionKey<CatalogInfoGetter> = Symbol(
34+
__VUEPRESS_DEV__ ? 'catalog-info-getter' : '',
35+
)
3436

3537
export const defineCatalogInfoGetter = (getter: CatalogInfoGetter): void => {
3638
catalogGetter = getter

plugins/features/plugin-photo-swipe/src/client/helpers/photo-swipe.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import { isFunction } from '@vuepress/helper/client'
22
import type { PhotoSwipeOptions as OriginalPhotoSwipeOptions } from 'photoswipe'
3-
import type { App, MaybeRefOrGetter, Ref } from 'vue'
4-
import { inject, isRef, ref, watch } from 'vue'
3+
import type { App, InjectionKey, MaybeRefOrGetter, Ref } from 'vue'
4+
import { computed, inject, isRef, ref, watch } from 'vue'
55

66
export type PhotoSwipeOptions = Omit<
77
OriginalPhotoSwipeOptions,
88
// These are handled internally
99
'dataSource' | 'index'
1010
>
1111

12+
export type PhotoSwipeOptionsRef = Ref<PhotoSwipeOptions>
13+
1214
declare const __VUEPRESS_DEV__: boolean
1315

14-
const photoswipeOptions: Ref<PhotoSwipeOptions> = ref({})
16+
const photoswipeOptions: PhotoSwipeOptionsRef = ref({})
1517

16-
const photoswipeSymbol = Symbol(__VUEPRESS_DEV__ ? 'photoswipe' : '')
18+
const photoswipeSymbol: InjectionKey<PhotoSwipeOptionsRef> = Symbol(
19+
__VUEPRESS_DEV__ ? 'photoswipe' : '',
20+
)
1721

1822
export const definePhotoSwipeConfig = (
1923
options: MaybeRefOrGetter<PhotoSwipeOptions>,
@@ -24,17 +28,18 @@ export const definePhotoSwipeConfig = (
2428
(value) => {
2529
photoswipeOptions.value = value
2630
},
31+
{ immediate: true },
2732
)
2833
} else if (isFunction(options)) {
29-
watch(options, (value) => {
34+
watch(computed(options), (value) => {
3035
photoswipeOptions.value = value
3136
})
3237
} else {
3338
photoswipeOptions.value = options
3439
}
3540
}
3641

37-
export const usePhotoSwipeOptions = (): Ref<PhotoSwipeOptions> =>
42+
export const usePhotoSwipeOptions = (): PhotoSwipeOptionsRef =>
3843
inject(photoswipeSymbol)!
3944

4045
export const injectPhotoSwipeConfig = (app: App): void => {

plugins/features/plugin-watermark/src/client/helper/watermark.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const defineWatermarkConfig = (
6565
{ immediate: true },
6666
)
6767
} else if (isFunction(userConfig)) {
68-
watch(userConfig, (value) => {
68+
watch(computed(userConfig), (value) => {
6969
watermarkOptions.value = value
7070
})
7171
} else {

plugins/markdown/plugin-revealjs/src/client/components/RevealJs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const RevealJs = defineComponent({
7373
backgroundTransition: 'slide',
7474
transition: 'slide',
7575
slideNumber: true,
76-
...revealOptions,
76+
...revealOptions.value,
7777
hash: isSlidePage,
7878
mouseWheel: isSlidePage,
7979
...frontmatter.value.revealJs,
@@ -87,7 +87,7 @@ export const RevealJs = defineComponent({
8787
plugins: [
8888
plugins.map(({ default: plugin }) => plugin),
8989

90-
revealOptions.plugins ?? [],
90+
revealOptions.value.plugins ?? [],
9191
].flat(),
9292
})
9393

plugins/markdown/plugin-revealjs/src/client/helpers/revealJs.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
// eslint-disable-next-line import/no-rename-default
22
import type Reveal from 'reveal.js'
3-
import type { App } from 'vue'
4-
import { inject } from 'vue'
3+
import type { App, InjectionKey, MaybeRefOrGetter, Ref } from 'vue'
4+
import { computed, inject, isRef, ref, watch } from 'vue'
5+
import { isFunction } from 'vuepress/shared'
56

67
declare const __VUEPRESS_DEV__: boolean
78

8-
export type RevealJsOptions = Omit<Reveal.Options, 'embedded'>
9+
export type RevealJsOptions = Partial<Omit<Reveal.Options, 'embedded'>>
910

10-
let revealOptions: Partial<RevealJsOptions> = {}
11+
export type RevealJsOptionsRef = Ref<RevealJsOptions>
1112

12-
const revealJsSymbol = Symbol(__VUEPRESS_DEV__ ? 'revealjs' : '')
13+
const revealOptions: RevealJsOptionsRef = ref({})
14+
15+
const revealJsSymbol: InjectionKey<RevealJsOptionsRef> = Symbol(
16+
__VUEPRESS_DEV__ ? 'revealjs' : '',
17+
)
1318

1419
export const defineRevealJsConfig = (
15-
options: Partial<RevealJsOptions>,
20+
options: MaybeRefOrGetter<RevealJsOptions>,
1621
): void => {
17-
revealOptions = options
22+
if (isRef(options)) {
23+
watch(
24+
() => options.value,
25+
(value) => {
26+
revealOptions.value = value
27+
},
28+
{ immediate: true },
29+
)
30+
} else if (isFunction(options)) {
31+
watch(computed(options), (value) => {
32+
revealOptions.value = value
33+
})
34+
} else {
35+
revealOptions.value = options
36+
}
1837
}
1938

20-
export const useRevealJsConfig = (): Partial<RevealJsOptions> =>
39+
export const useRevealJsConfig = (): RevealJsOptionsRef =>
2140
inject(revealJsSymbol)!
2241

2342
export const injectRevealJsConfig = (app: App): void => {

plugins/search/plugin-docsearch/src/client/helpers/docsearch.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type { DocSearchProps } from '@docsearch/react'
22
import { deepAssign, isFunction } from '@vuepress/helper/client'
33
import type { App, ComputedRef, InjectionKey, MaybeRefOrGetter, Ref } from 'vue'
4-
import { computed, inject, isRef, ref, watch } from 'vue'
4+
import { computed, inject, isRef, readonly, ref, watch } from 'vue'
55
import { useRouteLocale } from 'vuepress/client'
66
import type { DocSearchOptions } from '../../shared/index.js'
77

88
declare const __VUEPRESS_DEV__: boolean
99
declare const __DOCSEARCH_OPTIONS__: DocSearchOptions
1010

11-
const docSearchOptions: Partial<DocSearchProps> = __DOCSEARCH_OPTIONS__
11+
const docSearchDefineOptions: Partial<DocSearchProps> = __DOCSEARCH_OPTIONS__
1212

13-
const docsearch = ref(docSearchOptions as DocSearchProps)
13+
const docsearchOptions = ref(docSearchDefineOptions as DocSearchProps)
1414

1515
const docsearchSymbol: InjectionKey<
1616
Ref<
@@ -33,16 +33,17 @@ export const defineDocSearchConfig = (
3333
watch(
3434
() => options.value,
3535
(value) => {
36-
docsearch.value = deepAssign({}, docSearchOptions, value)
36+
docsearchOptions.value = deepAssign({}, docSearchDefineOptions, value)
3737
},
38+
{ immediate: true },
3839
)
3940
} else if (isFunction(options)) {
40-
watch(options, (value) => {
41-
docsearch.value = deepAssign({}, docSearchOptions, value)
41+
watch(computed(options), (value) => {
42+
docsearchOptions.value = deepAssign({}, docSearchDefineOptions, value)
4243
})
4344
} else {
4445
// @ts-expect-error: Types loop back
45-
docsearch.value = deepAssign({}, docSearchOptions, options)
46+
docsearchOptions.value = deepAssign({}, docSearchDefineOptions, options)
4647
}
4748
}
4849

@@ -57,5 +58,5 @@ export const useDocSearchOptions = (): ComputedRef<DocSearchProps> => {
5758
}
5859

5960
export const injectDocSearchConfig = (app: App): void => {
60-
app.provide(docsearchSymbol, docsearch)
61+
app.provide(docsearchSymbol, readonly(docsearchOptions))
6162
}

0 commit comments

Comments
 (0)