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

Lines changed: 17 additions & 2 deletions
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

Lines changed: 8 additions & 4 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 21 additions & 2 deletions
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

Lines changed: 8 additions & 4 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 11 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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 {

0 commit comments

Comments
 (0)